3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
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. //
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: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will save the changes performed to one field
29 class edit_field_save
extends XMLDBAction
{
32 * Init method, every subclass will have its own
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
41 'fieldnameempty' => 'xmldb',
42 'incorrectfieldname' => 'xmldb',
43 'duplicatefieldname' => 'xmldb',
44 'integerincorrectlength' => 'xmldb',
45 'numberincorrectlength' => 'xmldb',
46 'floatincorrectlength' => 'xmldb',
47 'charincorrectlength' => 'xmldb',
48 'textincorrectlength' => 'xmldb',
49 'binaryincorrectlength' => 'xmldb',
50 'numberincorrectdecimals' => 'xmldb',
51 'floatincorrectdecimals' => 'xmldb',
52 'enumvaluesincorrect' => 'xmldb',
53 'wronglengthforenum' => 'xmldb',
54 'defaultincorrect' => 'xmldb',
55 'administration' => ''
60 * Invoke method, every class will have its own
61 * returns true/false on completion, setting both
62 * errormsg and output as necessary
69 /// Set own core attributes
70 $this->does_generate
= ACTION_NONE
;
71 //$this->does_generate = ACTION_GENERATE_HTML;
73 /// These are always here
76 /// Do the job, setting result as needed
78 if (!data_submitted('nomatch')) { ///Basic prevention
79 error('Wrong action call');
83 $dirpath = required_param('dir', PARAM_PATH
);
84 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
86 $tableparam = strtolower(required_param('table', PARAM_PATH
));
87 $fieldparam = strtolower(required_param('field', PARAM_PATH
));
88 $name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH
))),0,30);
90 $comment = required_param('comment', PARAM_CLEAN
);
91 $comment = trim(stripslashes_safe($comment));
93 $type = required_param('type', PARAM_INT
);
94 $length = strtolower(optional_param('length', NULL, PARAM_ALPHANUM
));
95 $decimals = optional_param('decimals', NULL, PARAM_INT
);
96 $unsigned = optional_param('unsigned', false, PARAM_BOOL
);
97 $notnull = optional_param('notnull', false, PARAM_BOOL
);
98 $sequence = optional_param('sequence', false, PARAM_BOOL
);
99 $enum = optional_param('enum', false, PARAM_BOOL
);
100 $enumvalues = optional_param('enumvalues', 0, PARAM_CLEAN
);
101 $enumvalues = trim(stripslashes_safe($enumvalues));
102 $default = optional_param('default', NULL, PARAM_PATH
);
103 $default = trim(stripslashes_safe($default));
105 $editeddir =& $XMLDB->editeddirs
[$dirpath];
106 $structure =& $editeddir->xml_file
->getStructure();
107 $table =& $structure->getTable($tableparam);
108 $field =& $table->getField($fieldparam);
109 $oldhash = $field->getHash();
111 $errors = array(); /// To store all the errors found
113 /// Perform some automatic asumptions
120 if ($type != XMLDB_TYPE_NUMBER
&& $type != XMLDB_TYPE_FLOAT
) {
123 if ($type != XMLDB_TYPE_CHAR
&& $type != XMLDB_TYPE_TEXT
) {
126 if ($type == XMLDB_TYPE_BINARY
) {
132 if ($default === '') {
136 /// Perform some checks
139 $errors[] = $this->str
['fieldnameempty'];
141 /// Check incorrect name
142 if ($name == 'changeme') {
143 $errors[] = $this->str
['incorrectfieldname'];
145 /// Check duplicate name
146 if ($fieldparam != $name && $table->getField($name)) {
147 $errors[] = $this->str
['duplicatefieldname'];
150 if ($type == XMLDB_TYPE_INTEGER
) {
151 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) &&
152 $length > 0 && $length <= 20)) {
153 $errors[] = $this->str
['integerincorrectlength'];
155 if (!(empty($default) ||
(is_numeric($default) &&
157 intval($default)==floatval($default)))) {
158 $errors[] = $this->str
['defaultincorrect'];
162 if ($type == XMLDB_TYPE_NUMBER
) {
163 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) &&
164 $length > 0 && $length <= 20)) {
165 $errors[] = $this->str
['numberincorrectlength'];
167 if (!(empty($decimals) ||
(is_numeric($decimals) &&
169 intval($decimals)==floatval($decimals) &&
171 $decimals < $length))) {
172 $errors[] = $this->str
['numberincorrectdecimals'];
174 if (!(empty($default) ||
(is_numeric($default) &&
175 !empty($default)))) {
176 $errors[] = $this->str
['defaultincorrect'];
180 if ($type == XMLDB_TYPE_FLOAT
) {
181 if (!(empty($length) ||
(is_numeric($length) &&
183 intval($length)==floatval($length) &&
186 $errors[] = $this->str
['floatincorrectlength'];
188 if (!(empty($decimals) ||
(is_numeric($decimals) &&
190 intval($decimals)==floatval($decimals) &&
192 $decimals < $length))) {
193 $errors[] = $this->str
['floatincorrectdecimals'];
195 if (!(empty($default) ||
(is_numeric($default) &&
196 !empty($default)))) {
197 $errors[] = $this->str
['defaultincorrect'];
201 if ($type == XMLDB_TYPE_CHAR
) {
202 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) &&
203 $length > 0 && $length <= 255)) {
204 $errors[] = $this->str
['charincorrectlength'];
206 if ($default !== NULL && $default !== '') {
207 if (substr($default, 0, 1) == "'" ||
208 substr($default, -1, 1) == "'") {
209 $errors[] = $this->str
['defaultincorrect'];
214 if ($type == XMLDB_TYPE_TEXT
) {
215 if ($length != 'small' &&
216 $length != 'medium' &&
218 $errors[] = $this->str
['textincorrectlength'];
220 if ($default !== NULL && $default !== '') {
221 if (substr($default, 0, 1) == "'" ||
222 substr($default, -1, 1) == "'") {
223 $errors[] = $this->str
['defaultincorrect'];
228 if ($type == XMLDB_TYPE_BINARY
) {
229 if ($length != 'small' &&
230 $length != 'medium' &&
232 $errors[] = $this->str
['binaryincorrectlength'];
238 $enumarr = explode(',',$enumvalues);
241 foreach ($enumarr as $key => $enumelement) {
242 /// Clear some spaces
243 $enumarr[$key] = trim($enumelement);
244 $enumelement = trim($enumelement);
245 /// Calculate needed length
246 $le = strlen(str_replace("'", '', $enumelement));
247 if ($le > $maxlength) {
250 /// Skip if under error
254 /// Look for quoted strings
255 if (substr($enumelement, 0, 1) != "'" ||
256 substr($enumelement, -1, 1) != "'") {
264 $errors[] = $this->str
['enumvaluesincorrect'];
266 $enumvalues = $enumarr;
268 if ($length < $maxlength) {
269 $errors[] = $this->str
['wronglengthforenum'];
273 if (!empty($errors)) {
274 $tempfield = new XMLDBField($name);
275 $tempfield->setType($type);
276 $tempfield->setLength($length);
277 $tempfield->setDecimals($decimals);
278 $tempfield->setUnsigned($unsigned);
279 $tempfield->setNotNull($notnull);
280 $tempfield->setSequence($sequence);
281 $tempfield->setEnum($enum);
282 $tempfield->setEnumValues($enumvalues);
283 $tempfield->setDefault($default);
284 /// Prepare the output
286 print_header("$site->shortname: XMLDB",
288 "<a href=\"../index.php\">" . $this->str
['administration'] . "</a> -> <a href=\"index.php\">XMLDB</a>");
289 notice ('<p>' .implode(', ', $errors) . '</p>
290 <p>' . $tempfield->readableInfo(),
291 'index.php?action=edit_field&field=' .$field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)));
295 /// Continue if we aren't under errors
296 if (empty($errors)) {
297 /// If there is one name change, do it, changing the prev and next
298 /// atributes of the adjacent fields
299 if ($fieldparam != $name) {
300 $field->setName($name);
301 if ($field->getPrevious()) {
302 $prev =& $table->getField($field->getPrevious());
303 $prev->setNext($name);
304 $prev->setChanged(true);
306 if ($field->getNext()) {
307 $next =& $table->getField($field->getNext());
308 $next->setPrevious($name);
309 $next->setChanged(true);
314 $field->setComment($comment);
316 /// Set the rest of fields
317 $field->setType($type);
318 $field->setLength($length);
319 $field->setDecimals($decimals);
320 $field->setUnsigned($unsigned);
321 $field->setNotNull($notnull);
322 $field->setSequence($sequence);
323 $field->setEnum($enum);
324 $field->setEnumValues($enumvalues);
325 $field->setDefault($default);
327 /// If the hash has changed from the old one, change the version
328 /// and mark the structure as changed
329 $field->calculateHash(true);
330 if ($oldhash != $field->getHash()) {
331 $field->setChanged(true);
332 $table->setChanged(true);
333 /// Recalculate the structure hash
334 $structure->calculateHash(true);
335 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
337 $structure->setChanged(true);
340 /// Launch postaction if exists (leave this here!)
341 if ($this->getPostAction() && $result) {
342 return $this->launch($this->getPostAction());
346 /// Return ok if arrived here