[ Index ]

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

title

Body

[close]

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

   1  <?php // $Id: edit_index_save.class.php,v 1.5 2007/10/10 05:25:20 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 index
  28  
  29  class edit_index_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              'indexnameempty' => 'xmldb',
  42              'incorrectindexname' => 'xmldb',
  43              'duplicateindexname' => 'xmldb',
  44              'nofieldsspecified' => 'xmldb',
  45              'duplicatefieldsused' => 'xmldb',
  46              'fieldsnotintable' => 'xmldb',
  47              'fieldsusedinkey' => 'xmldb',
  48              'fieldsusedinindex' => 'xmldb',
  49              'administration' => ''
  50          ));
  51      }
  52  
  53      /**
  54       * Invoke method, every class will have its own
  55       * returns true/false on completion, setting both
  56       * errormsg and output as necessary
  57       */
  58      function invoke() {
  59          parent::invoke();
  60  
  61          $result = true;
  62  
  63      /// Set own core attributes
  64          $this->does_generate = ACTION_NONE;
  65          //$this->does_generate = ACTION_GENERATE_HTML;
  66  
  67      /// These are always here
  68          global $CFG, $XMLDB;
  69  
  70      /// Do the job, setting result as needed
  71  
  72          if (!data_submitted('nomatch')) { ///Basic prevention
  73              error('Wrong action call');
  74          }
  75  
  76      /// Get parameters
  77          $dirpath = required_param('dir', PARAM_PATH);
  78          $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  79  
  80          $tableparam = strtolower(required_param('table', PARAM_PATH));
  81          $indexparam = strtolower(required_param('index', PARAM_PATH));
  82          $name = trim(strtolower(optional_param('name', $indexparam, PARAM_PATH)));
  83  
  84          $comment = required_param('comment', PARAM_CLEAN);
  85          $comment = trim(stripslashes_safe($comment));
  86  
  87          $unique = required_param('unique', PARAM_INT);
  88          $fields = required_param('fields', PARAM_CLEAN);
  89          $fields = str_replace(' ', '', trim(strtolower(stripslashes_safe($fields))));
  90  
  91          $editeddir =& $XMLDB->editeddirs[$dirpath];
  92          $structure =& $editeddir->xml_file->getStructure();
  93          $table =& $structure->getTable($tableparam);
  94          $index =& $table->getIndex($indexparam);
  95          $oldhash = $index->getHash();
  96  
  97          $errors = array();    /// To store all the errors found
  98  
  99      /// Perform some checks
 100      /// Check empty name
 101          if (empty($name)) {
 102              $errors[] = $this->str['indexnameempty'];
 103          }
 104      /// Check incorrect name
 105          if ($name == 'changeme') {
 106              $errors[] = $this->str['incorrectindexname'];
 107          }
 108      /// Check duplicate name
 109          if ($indexparam != $name && $table->getIndex($name)) {
 110              $errors[] = $this->str['duplicateindexname'];
 111          }
 112          $fieldsarr = explode(',', $fields);
 113      /// Check the fields isn't empty
 114          if (empty($fieldsarr[0])) {
 115              $errors[] = $this->str['nofieldsspecified'];
 116          } else {
 117          /// Check that there aren't duplicate column names
 118              $uniquearr = array_unique($fieldsarr);
 119              if (count($fieldsarr) != count($uniquearr)) {
 120                  $errors[] = $this->str['duplicatefieldsused'];
 121              }
 122          /// Check that all the fields in belong to the table
 123              foreach ($fieldsarr as $field) {
 124                  if (!$table->getField($field)) {
 125                      $errors[] = $this->str['fieldsnotintable'];
 126                      break;
 127                  }
 128              }
 129          /// Check that there isn't any key using exactly the same fields
 130              $tablekeys = $table->getKeys();
 131              if ($tablekeys) {
 132                  foreach ($tablekeys as $tablekey) {
 133                      $keyfieldsarr = $tablekey->getFields();
 134                  /// Compare both arrays, looking for diferences
 135                      $diferences = array_merge(array_diff($fieldsarr, $keyfieldsarr), array_diff($keyfieldsarr, $fieldsarr));
 136                      if (empty($diferences)) {
 137                          $errors[] = $this->str['fieldsusedinkey'];
 138                          break;
 139                      }
 140                  }
 141              }
 142          /// Check that there isn't any index using exactlt the same fields
 143              $tableindexes = $table->getIndexes();
 144              if ($tableindexes) {
 145                  foreach ($tableindexes as $tableindex) {
 146                  /// Skip checking against itself
 147                      if ($indexparam == $tableindex->getName()) {
 148                          continue;
 149                      }
 150                      $indexfieldsarr = $tableindex->getFields();
 151                  /// Compare both arrays, looking for diferences
 152                      $diferences = array_merge(array_diff($fieldsarr, $indexfieldsarr), array_diff($indexfieldsarr, $fieldsarr));
 153                      if (empty($diferences)) {
 154                          $errors[] = $this->str['fieldsusedinindex'];
 155                          break;
 156                      }
 157                  }
 158              }
 159          }
 160  
 161          if (!empty($errors)) {
 162              $tempindex = new XMLDBIndex($name);
 163              $tempindex->setUnique($unique);
 164              $tempindex->setFields($fieldsarr);
 165          /// Prepare the output
 166              $site = get_site();
 167              $navlinks = array();
 168              $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc');
 169              $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc');
 170              $navigation = build_navigation($navlinks);
 171              print_header("$site->shortname: XMLDB", "$site->fullname", $navigation);
 172              notice ('<p>' .implode(', ', $errors) . '</p>
 173                       <p>' . $tempindex->readableInfo(),
 174                      'index.php?action=edit_index&amp;index=' .$index->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
 175              die; /// re-die :-P
 176          }
 177  
 178      /// Continue if we aren't under errors
 179          if (empty($errors)) {
 180          /// If there is one name change, do it, changing the prev and next
 181          /// atributes of the adjacent fields
 182              if ($indexparam != $name) {
 183                  $index->setName($name);
 184                  if ($index->getPrevious()) {
 185                      $prev =& $table->getIndex($index->getPrevious());
 186                      $prev->setNext($name);
 187                      $prev->setChanged(true);
 188                  }
 189                  if ($index->getNext()) {
 190                      $next =& $table->getIndex($index->getNext());
 191                      $next->setPrevious($name);
 192                      $next->setChanged(true);
 193                  }
 194              }
 195  
 196          /// Set comment
 197              $index->setComment($comment);
 198  
 199          /// Set the rest of fields
 200              $index->setUnique($unique);
 201              $index->setFields($fieldsarr);
 202  
 203          /// If the hash has changed from the old one, change the version
 204          /// and mark the structure as changed
 205              $index->calculateHash(true);
 206              if ($oldhash != $index->getHash()) {
 207                  $index->setChanged(true);
 208                  $table->setChanged(true);
 209              /// Recalculate the structure hash
 210                  $structure->calculateHash(true);
 211                  $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
 212              /// Mark as changed
 213                  $structure->setChanged(true);
 214              }
 215  
 216          /// Launch postaction if exists (leave this here!)
 217              if ($this->getPostAction() && $result) {
 218                  return $this->launch($this->getPostAction());
 219              }
 220          }
 221  
 222      /// Return ok if arrived here
 223          return $result;
 224      }
 225  }
 226  ?>


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