Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / xmldb / actions / edit_sentence_save / edit_sentence_save.class.php
blob426b6d72bc862a2722325b1f68a093d7e80cc5e8
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will save the changes performed to one sentence
29 class edit_sentence_save extends XMLDBAction {
31 /**
32 * Init method, every subclass will have its own
34 function init() {
35 parent::init();
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
41 'cannotuseidfield' => 'xmldb',
42 'missingfieldsinsentence' => 'xmldb',
43 'missingvaluesinsentence' => 'xmldb',
44 'wrongnumberoffieldsorvalues' => 'xmldb',
45 'administration' => ''
46 ));
49 /**
50 * Invoke method, every class will have its own
51 * returns true/false on completion, setting both
52 * errormsg and output as necessary
54 function invoke() {
55 parent::invoke();
57 $result = true;
59 /// Set own core attributes
60 $this->does_generate = ACTION_NONE;
61 //$this->does_generate = ACTION_GENERATE_HTML;
63 /// These are always here
64 global $CFG, $XMLDB;
66 /// Do the job, setting result as needed
68 /// Get parameters
69 $dirpath = required_param('dir', PARAM_PATH);
70 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
72 $statementparam = strtolower(required_param('statement', PARAM_CLEAN));
73 $sentenceparam = strtolower(required_param('sentence', PARAM_ALPHANUM));
75 $fields = required_param('fields', PARAM_CLEAN);
76 $fields = trim(stripslashes_safe($fields));
77 $values = required_param('values', PARAM_CLEAN);
78 $values = trim(stripslashes_safe($values));
80 $editeddir =& $XMLDB->editeddirs[$dirpath];
81 $structure =& $editeddir->xml_file->getStructure();
82 $statement =& $structure->getStatement($statementparam);
83 $sentences =& $statement->getSentences();
85 $oldsentence = $sentences[$sentenceparam];
87 if (!$statement) {
88 $this->errormsg = 'Wrong statement specified: ' . $statementparam;
89 return false;
92 /// For now, only insert sentences are allowed
93 if ($statement->getType() != XMLDB_STATEMENT_INSERT) {
94 $this->errormsg = 'Wrong Statement Type. Only INSERT allowed';
95 return false;
98 $errors = array(); /// To store all the errors found
100 /// Build the whole sentence
101 $sentence = '(' . $fields . ') VALUES (' . $values . ')';
103 /// Perform some checks
104 $fields = $statement->getFieldsFromInsertSentence($sentence);
105 $values = $statement->getValuesFromInsertSentence($sentence);
107 if (in_array('id', $fields)) {
108 $errors[] = $this->str['cannotuseidfield'];
110 if ($result && count($fields) == 0) {
111 $errors[] = $this->str['missingfieldsinsentence'];
113 if ($result && count($values) == 0) {
114 $errors[] = $this->str['missingvaluesinsentence'];
116 if ($result && count($fields) != count($values)) {
117 $errors[] = $this->str['wrongnumberoffieldsorvalues'];
120 if (!empty($errors)) {
121 /// Prepare the output
122 $site = get_site();
123 print_header("$site->shortname: XMLDB",
124 "$site->fullname",
125 "<a href=\"../index.php\">" . $this->str['administration'] . "</a> -> <a href=\"index.php\">XMLDB</a>");
126 notice ('<p>' .implode(', ', $errors) . '</p>
127 <p>' . s($sentence),
128 'index.php?action=edit_sentence&amp;sentence=' .$sentenceparam . '&amp;statement=' . urlencode($statementparam) . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
129 die; /// re-die :-P
132 /// Continue if we aren't under errors
133 if (empty($errors)) {
134 $sentences[$sentenceparam] = $sentence;
136 /// If the sentence has changed from the old one, change the version
137 /// and mark the statement and structure as changed
138 if ($oldsentence != $sentence) {
139 $statement->setChanged(true);
140 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
141 /// Mark as changed
142 $structure->setChanged(true);
145 /// Launch postaction if exists (leave this here!)
146 if ($this->getPostAction() && $result) {
147 return $this->launch($this->getPostAction());
151 /// Return ok if arrived here
152 return $result;