[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * @package tool_xmldb 19 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 21 */ 22 23 /** 24 * This class verifies all the data introduced when editing a field for correctness, 25 * performing changes / displaying errors depending of the results. 26 * 27 * @package tool_xmldb 28 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class edit_field_save extends XMLDBAction { 32 33 /** 34 * Init method, every subclass will have its own 35 */ 36 function init() { 37 parent::init(); 38 39 // Set own custom attributes 40 41 // Get needed strings 42 $this->loadStrings(array( 43 'fieldnameempty' => 'tool_xmldb', 44 'incorrectfieldname' => 'tool_xmldb', 45 'duplicatefieldname' => 'tool_xmldb', 46 'integerincorrectlength' => 'tool_xmldb', 47 'numberincorrectlength' => 'tool_xmldb', 48 'floatincorrectlength' => 'tool_xmldb', 49 'charincorrectlength' => 'tool_xmldb', 50 'numberincorrectdecimals' => 'tool_xmldb', 51 'floatincorrectdecimals' => 'tool_xmldb', 52 'defaultincorrect' => 'tool_xmldb', 53 'back' => 'tool_xmldb', 54 'administration' => '' 55 )); 56 } 57 58 /** 59 * Invoke method, every class will have its own 60 * returns true/false on completion, setting both 61 * errormsg and output as necessary 62 */ 63 function invoke() { 64 parent::invoke(); 65 66 $result = true; 67 68 // Set own core attributes 69 //$this->does_generate = ACTION_NONE; 70 $this->does_generate = ACTION_GENERATE_HTML; 71 72 // These are always here 73 global $CFG, $XMLDB; 74 75 // Do the job, setting result as needed 76 77 if (!data_submitted()) { // Basic prevention 78 print_error('wrongcall', 'error'); 79 } 80 81 // Get parameters 82 $dirpath = required_param('dir', PARAM_PATH); 83 $dirpath = $CFG->dirroot . $dirpath; 84 85 $tableparam = strtolower(required_param('table', PARAM_PATH)); 86 $fieldparam = strtolower(required_param('field', PARAM_PATH)); 87 $name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH))),0,xmldb_field::NAME_MAX_LENGTH); 88 89 $comment = required_param('comment', PARAM_CLEAN); 90 $comment = trim($comment); 91 92 $type = required_param('type', PARAM_INT); 93 $length = strtolower(optional_param('length', NULL, PARAM_ALPHANUM)); 94 $decimals = optional_param('decimals', NULL, PARAM_INT); 95 $notnull = optional_param('notnull', false, PARAM_BOOL); 96 $sequence = optional_param('sequence', false, PARAM_BOOL); 97 $default = optional_param('default', NULL, PARAM_PATH); 98 $default = trim($default); 99 100 $editeddir = $XMLDB->editeddirs[$dirpath]; 101 $structure = $editeddir->xml_file->getStructure(); 102 $table = $structure->getTable($tableparam); 103 $field = $table->getField($fieldparam); 104 $oldhash = $field->getHash(); 105 106 $errors = array(); // To store all the errors found 107 108 // Perform some automatic assumptions 109 if ($sequence) { 110 $notnull = true; 111 $default = NULL; 112 } 113 if ($type != XMLDB_TYPE_NUMBER && $type != XMLDB_TYPE_FLOAT) { 114 $decimals = NULL; 115 } 116 if ($type == XMLDB_TYPE_BINARY) { 117 $default = NULL; 118 } 119 if ($default === '') { 120 $default = NULL; 121 } 122 123 // Perform some checks 124 // Check empty name 125 if (empty($name)) { 126 $errors[] = $this->str['fieldnameempty']; 127 } 128 // Check incorrect name 129 if ($name == 'changeme') { 130 $errors[] = $this->str['incorrectfieldname']; 131 } 132 // Check duplicate name 133 if ($fieldparam != $name && $table->getField($name)) { 134 $errors[] = $this->str['duplicatefieldname']; 135 } 136 // Integer checks 137 if ($type == XMLDB_TYPE_INTEGER) { 138 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) && 139 $length > 0 && $length <= xmldb_field::INTEGER_MAX_LENGTH)) { 140 $errors[] = $this->str['integerincorrectlength']; 141 } 142 if (!(empty($default) || (is_numeric($default) && 143 !empty($default) && 144 intval($default)==floatval($default)))) { 145 $errors[] = $this->str['defaultincorrect']; 146 } 147 } 148 // Number checks 149 if ($type == XMLDB_TYPE_NUMBER) { 150 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) && 151 $length > 0 && $length <= xmldb_field::NUMBER_MAX_LENGTH)) { 152 $errors[] = $this->str['numberincorrectlength']; 153 } 154 if (!(empty($decimals) || (is_numeric($decimals) && 155 !empty($decimals) && 156 intval($decimals)==floatval($decimals) && 157 $decimals >= 0 && 158 $decimals < $length))) { 159 $errors[] = $this->str['numberincorrectdecimals']; 160 } 161 if (!(empty($default) || (is_numeric($default) && 162 !empty($default)))) { 163 $errors[] = $this->str['defaultincorrect']; 164 } 165 } 166 // Float checks 167 if ($type == XMLDB_TYPE_FLOAT) { 168 if (!(empty($length) || (is_numeric($length) && 169 !empty($length) && 170 intval($length)==floatval($length) && 171 $length > 0 && 172 $length <= xmldb_field::FLOAT_MAX_LENGTH))) { 173 $errors[] = $this->str['floatincorrectlength']; 174 } 175 if (!(empty($decimals) || (is_numeric($decimals) && 176 !empty($decimals) && 177 intval($decimals)==floatval($decimals) && 178 $decimals >= 0 && 179 $decimals < $length))) { 180 $errors[] = $this->str['floatincorrectdecimals']; 181 } 182 if (!(empty($default) || (is_numeric($default) && 183 !empty($default)))) { 184 $errors[] = $this->str['defaultincorrect']; 185 } 186 } 187 // Char checks 188 if ($type == XMLDB_TYPE_CHAR) { 189 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) && 190 $length > 0 && $length <= xmldb_field::CHAR_MAX_LENGTH)) { 191 $errors[] = $this->str['charincorrectlength']; 192 } 193 if ($default !== NULL && $default !== '') { 194 if (substr($default, 0, 1) == "'" || 195 substr($default, -1, 1) == "'") { 196 $errors[] = $this->str['defaultincorrect']; 197 } 198 } 199 } 200 // No text checks 201 // No binary checks 202 203 if (!empty($errors)) { 204 $tempfield = new xmldb_field($name); 205 $tempfield->setType($type); 206 $tempfield->setLength($length); 207 $tempfield->setDecimals($decimals); 208 $tempfield->setNotNull($notnull); 209 $tempfield->setSequence($sequence); 210 $tempfield->setDefault($default); 211 // Prepare the output 212 $o = '<p>' .implode(', ', $errors) . '</p> 213 <p>' . $name . ': ' . $tempfield->readableInfo() . '</p>'; 214 $o.= '<a href="index.php?action=edit_field&field=' . $field->getName() . '&table=' . $table->getName() . 215 '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; 216 $this->output = $o; 217 } 218 219 // Continue if we aren't under errors 220 if (empty($errors)) { 221 // If there is one name change, do it, changing the prev and next 222 // atributes of the adjacent fields 223 if ($fieldparam != $name) { 224 $field->setName($name); 225 if ($field->getPrevious()) { 226 $prev = $table->getField($field->getPrevious()); 227 $prev->setNext($name); 228 $prev->setChanged(true); 229 } 230 if ($field->getNext()) { 231 $next = $table->getField($field->getNext()); 232 $next->setPrevious($name); 233 $next->setChanged(true); 234 } 235 } 236 237 // Set comment 238 $field->setComment($comment); 239 240 // Set the rest of fields 241 $field->setType($type); 242 $field->setLength($length); 243 $field->setDecimals($decimals); 244 $field->setNotNull($notnull); 245 $field->setSequence($sequence); 246 $field->setDefault($default); 247 248 // If the hash has changed from the old one, change the version 249 // and mark the structure as changed 250 $field->calculateHash(true); 251 if ($oldhash != $field->getHash()) { 252 $field->setChanged(true); 253 $table->setChanged(true); 254 // Recalculate the structure hash 255 $structure->calculateHash(true); 256 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); 257 // Mark as changed 258 $structure->setChanged(true); 259 } 260 261 // Launch postaction if exists (leave this here!) 262 if ($this->getPostAction() && $result) { 263 return $this->launch($this->getPostAction()); 264 } 265 } 266 267 // Return ok if arrived here 268 return $result; 269 } 270 } 271
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |