[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/xmldb/actions/edit_field_save/ -> edit_field_save.class.php (source)

   1  <?php // $Id: edit_field_save.class.php,v 1.5 2007/10/10 05:25:26 nicolasconnault Exp $
   2  
   3  ///////////////////////////////////////////////////////////////////////////
   4  //                                                                       //
   5  // NOTICE OF COPYRIGHT                                                   //
   6  //                                                                       //
   7  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   8  //          http://moodle.com                                            //
   9  //                                                                       //
  10  // Copyright (C) 1999 onwards 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  ///////////////////////////////////////////////////////////////////////////
  26  
  27  /// This class will save the changes performed to one field
  28  
  29  class edit_field_save extends XMLDBAction {
  30  
  31      /**
  32       * Init method, every subclass will have its own
  33       */
  34      function init() {
  35          parent::init();
  36  
  37      /// Set own custom attributes
  38  
  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          ));
  57      }
  58  
  59      /**
  60       * Invoke method, every class will have its own
  61       * returns true/false on completion, setting both
  62       * errormsg and output as necessary
  63       */
  64      function invoke() {
  65          parent::invoke();
  66  
  67          $result = true;
  68  
  69      /// Set own core attributes
  70          $this->does_generate = ACTION_NONE;
  71          //$this->does_generate = ACTION_GENERATE_HTML;
  72  
  73      /// These are always here
  74          global $CFG, $XMLDB;
  75  
  76      /// Do the job, setting result as needed
  77  
  78          if (!data_submitted('nomatch')) { ///Basic prevention
  79              error('Wrong action call');
  80          }
  81  
  82      /// Get parameters
  83          $dirpath = required_param('dir', PARAM_PATH);
  84          $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  85  
  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);
  89  
  90          $comment = required_param('comment', PARAM_CLEAN);
  91          $comment = trim(stripslashes_safe($comment));
  92  
  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));
 104  
 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();
 110  
 111          $errors = array();    /// To store all the errors found
 112  
 113      /// Perform some automatic asumptions
 114          if ($sequence) {
 115              $unsigned = true;
 116              $notnull  = true;
 117              $enum     = false;
 118              $default  = NULL;
 119          }
 120          if ($type != XMLDB_TYPE_NUMBER && $type != XMLDB_TYPE_FLOAT) {
 121              $decimals = NULL;
 122          }
 123          if ($type != XMLDB_TYPE_CHAR && $type != XMLDB_TYPE_TEXT) {
 124              $enum = false;
 125          }
 126          if ($type == XMLDB_TYPE_BINARY) {
 127              $default = NULL;
 128          }
 129          if (!$enum) {
 130              $enumvalues = NULL;
 131          }
 132          if ($default === '') {
 133              $default = NULL;
 134          }
 135  
 136      /// Perform some checks
 137      /// Check empty name
 138          if (empty($name)) {
 139              $errors[] = $this->str['fieldnameempty'];
 140          }
 141      /// Check incorrect name
 142          if ($name == 'changeme') {
 143              $errors[] = $this->str['incorrectfieldname'];
 144          }
 145      /// Check duplicate name
 146          if ($fieldparam != $name && $table->getField($name)) {
 147              $errors[] = $this->str['duplicatefieldname'];
 148          }
 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'];
 154              }
 155              if (!(empty($default) || (is_numeric($default) &&
 156                                         !empty($default) &&
 157                                         intval($default)==floatval($default)))) {
 158                  $errors[] = $this->str['defaultincorrect'];
 159              }
 160          }
 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'];
 166              }
 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'];
 173              }
 174              if (!(empty($default) || (is_numeric($default) &&
 175                                         !empty($default)))) {
 176                  $errors[] = $this->str['defaultincorrect'];
 177              }
 178          }
 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'];
 187              }
 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'];
 194              }
 195              if (!(empty($default) || (is_numeric($default) &&
 196                                         !empty($default)))) {
 197                  $errors[] = $this->str['defaultincorrect'];
 198              }
 199          }
 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'];
 205              }
 206              if ($default !== NULL && $default !== '') {
 207                  if (substr($default, 0, 1) == "'" ||
 208                      substr($default, -1, 1) == "'") {
 209                      $errors[] = $this->str['defaultincorrect'];
 210                  }
 211              }
 212          }
 213      /// Text checks
 214          if ($type == XMLDB_TYPE_TEXT) {
 215              if ($length != 'small' &&
 216                  $length != 'medium' &&
 217                  $length != 'big') {
 218                  $errors[] = $this->str['textincorrectlength'];
 219              }
 220              if ($default !== NULL && $default !== '') {
 221                  if (substr($default, 0, 1) == "'" ||
 222                      substr($default, -1, 1) == "'") {
 223                      $errors[] = $this->str['defaultincorrect'];
 224                  }
 225              }
 226          }
 227      /// Binary checks
 228          if ($type == XMLDB_TYPE_BINARY) {
 229              if ($length != 'small' &&
 230                  $length != 'medium' &&
 231                  $length != 'big') {
 232                  $errors[] = $this->str['binaryincorrectlength'];
 233              }
 234          }
 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;
 249                      }
 250                  /// Skip if under error
 251                      if ($enumerr) {
 252                          continue;
 253                      }
 254                  /// Look for quoted strings
 255                      if (substr($enumelement, 0, 1) != "'" ||
 256                          substr($enumelement, -1, 1) != "'") {
 257                          $enumerr = true;
 258                      }
 259                  }
 260              } else {
 261                  $enumerr = true;
 262              }
 263              if ($enumerr) {
 264                  $errors[] = $this->str['enumvaluesincorrect'];
 265              } else {
 266                  $enumvalues = $enumarr;
 267              }
 268              if ($length < $maxlength) {
 269                  $errors[] = $this->str['wronglengthforenum'];
 270              }
 271          }
 272  
 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              $navlinks = array();
 287              $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc');
 288              $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc');
 289              $navigation = build_navigation($navlinks);
 290              print_header("$site->shortname: XMLDB", "$site->fullname", $navigation);
 291              notice ('<p>' .implode(', ', $errors) . '</p>
 292                       <p>' . $tempfield->readableInfo(),
 293                      'index.php?action=edit_field&amp;field=' .$field->getName() . '&amp;table=' . $table->getName()
 294                      . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
 295              die; /// re-die :-P
 296          }
 297  
 298      /// Continue if we aren't under errors
 299          if (empty($errors)) {
 300          /// If there is one name change, do it, changing the prev and next
 301          /// atributes of the adjacent fields
 302              if ($fieldparam != $name) {
 303                  $field->setName($name);
 304                  if ($field->getPrevious()) {
 305                      $prev =& $table->getField($field->getPrevious());
 306                      $prev->setNext($name);
 307                      $prev->setChanged(true);
 308                  }
 309                  if ($field->getNext()) {
 310                      $next =& $table->getField($field->getNext());
 311                      $next->setPrevious($name);
 312                      $next->setChanged(true);
 313                  }
 314              }
 315  
 316          /// Set comment
 317              $field->setComment($comment);
 318  
 319          /// Set the rest of fields
 320              $field->setType($type);
 321              $field->setLength($length);
 322              $field->setDecimals($decimals);
 323              $field->setUnsigned($unsigned);
 324              $field->setNotNull($notnull);
 325              $field->setSequence($sequence);
 326              $field->setEnum($enum);
 327              $field->setEnumValues($enumvalues);
 328              $field->setDefault($default);
 329  
 330          /// If the hash has changed from the old one, change the version
 331          /// and mark the structure as changed
 332              $field->calculateHash(true);
 333              if ($oldhash != $field->getHash()) {
 334                  $field->setChanged(true);
 335                  $table->setChanged(true);
 336              /// Recalculate the structure hash
 337                  $structure->calculateHash(true);
 338                  $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
 339              /// Mark as changed
 340                  $structure->setChanged(true);
 341              }
 342  
 343          /// Launch postaction if exists (leave this here!)
 344              if ($this->getPostAction() && $result) {
 345                  return $this->launch($this->getPostAction());
 346              }
 347          }
 348  
 349      /// Return ok if arrived here
 350          return $result;
 351      }
 352  }
 353  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7