Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / xmldb / actions / edit_field_save / edit_field_save.class.php
blob91f7196e41a30659cde498d8803f9385761069e8
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 field
29 class edit_field_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 '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' => ''
56 ));
59 /**
60 * Invoke method, every class will have its own
61 * returns true/false on completion, setting both
62 * errormsg and output as necessary
64 function invoke() {
65 parent::invoke();
67 $result = true;
69 /// Set own core attributes
70 $this->does_generate = ACTION_NONE;
71 //$this->does_generate = ACTION_GENERATE_HTML;
73 /// These are always here
74 global $CFG, $XMLDB;
76 /// Do the job, setting result as needed
78 if (!data_submitted('nomatch')) { ///Basic prevention
79 error('Wrong action call');
82 /// Get parameters
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
114 if ($sequence) {
115 $unsigned = true;
116 $notnull = true;
117 $enum = false;
118 $default = NULL;
120 if ($type != XMLDB_TYPE_NUMBER && $type != XMLDB_TYPE_FLOAT) {
121 $decimals = NULL;
123 if ($type != XMLDB_TYPE_CHAR && $type != XMLDB_TYPE_TEXT) {
124 $enum = false;
126 if ($type == XMLDB_TYPE_BINARY) {
127 $default = NULL;
129 if (!$enum) {
130 $enumvalues = NULL;
132 if ($default === '') {
133 $default = NULL;
136 /// Perform some checks
137 /// Check empty name
138 if (empty($name)) {
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'];
149 /// Integer checks
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) &&
156 !empty($default) &&
157 intval($default)==floatval($default)))) {
158 $errors[] = $this->str['defaultincorrect'];
161 /// Number checks
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) &&
168 !empty($decimals) &&
169 intval($decimals)==floatval($decimals) &&
170 $decimals >= 0 &&
171 $decimals < $length))) {
172 $errors[] = $this->str['numberincorrectdecimals'];
174 if (!(empty($default) || (is_numeric($default) &&
175 !empty($default)))) {
176 $errors[] = $this->str['defaultincorrect'];
179 /// Float checks
180 if ($type == XMLDB_TYPE_FLOAT) {
181 if (!(empty($length) || (is_numeric($length) &&
182 !empty($length) &&
183 intval($length)==floatval($length) &&
184 $length > 0 &&
185 $length <= 20))) {
186 $errors[] = $this->str['floatincorrectlength'];
188 if (!(empty($decimals) || (is_numeric($decimals) &&
189 !empty($decimals) &&
190 intval($decimals)==floatval($decimals) &&
191 $decimals >= 0 &&
192 $decimals < $length))) {
193 $errors[] = $this->str['floatincorrectdecimals'];
195 if (!(empty($default) || (is_numeric($default) &&
196 !empty($default)))) {
197 $errors[] = $this->str['defaultincorrect'];
200 /// Char checks
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'];
213 /// Text checks
214 if ($type == XMLDB_TYPE_TEXT) {
215 if ($length != 'small' &&
216 $length != 'medium' &&
217 $length != 'big') {
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'];
227 /// Binary checks
228 if ($type == XMLDB_TYPE_BINARY) {
229 if ($length != 'small' &&
230 $length != 'medium' &&
231 $length != 'big') {
232 $errors[] = $this->str['binaryincorrectlength'];
235 /// Enum checks
236 if ($enum) {
237 $enumerr = false;
238 $enumarr = explode(',',$enumvalues);
239 $maxlength = 0;
240 if ($enumarr) {
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) {
248 $maxlength = $le;
250 /// Skip if under error
251 if ($enumerr) {
252 continue;
254 /// Look for quoted strings
255 if (substr($enumelement, 0, 1) != "'" ||
256 substr($enumelement, -1, 1) != "'") {
257 $enumerr = true;
260 } else {
261 $enumerr = true;
263 if ($enumerr) {
264 $errors[] = $this->str['enumvaluesincorrect'];
265 } else {
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
285 $site = get_site();
286 print_header("$site->shortname: XMLDB",
287 "$site->fullname",
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&amp;field=' .$field->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
292 die; /// re-die :-P
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);
313 /// Set comment
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));
336 /// Mark as changed
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
347 return $result;