Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/edit_index_save/edit_index_save.class.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00033 class edit_index_save extends XMLDBAction {
00034 
00038     function init() {
00039         parent::init();
00040 
00041         // Set own custom attributes
00042 
00043         // Get needed strings
00044         $this->loadStrings(array(
00045             'indexnameempty' => 'tool_xmldb',
00046             'incorrectindexname' => 'tool_xmldb',
00047             'duplicateindexname' => 'tool_xmldb',
00048             'nofieldsspecified' => 'tool_xmldb',
00049             'duplicatefieldsused' => 'tool_xmldb',
00050             'fieldsnotintable' => 'tool_xmldb',
00051             'fieldsusedinkey' => 'tool_xmldb',
00052             'fieldsusedinindex' => 'tool_xmldb',
00053             'back' => 'tool_xmldb',
00054             'administration' => ''
00055         ));
00056     }
00057 
00063     function invoke() {
00064         parent::invoke();
00065 
00066         $result = true;
00067 
00068         // Set own core attributes
00069         //$this->does_generate = ACTION_NONE;
00070         $this->does_generate = ACTION_GENERATE_HTML;
00071 
00072         // These are always here
00073         global $CFG, $XMLDB;
00074 
00075         // Do the job, setting result as needed
00076 
00077         if (!data_submitted()) { // Basic prevention
00078             print_error('wrongcall', 'error');
00079         }
00080 
00081         // Get parameters
00082         $dirpath = required_param('dir', PARAM_PATH);
00083         $dirpath = $CFG->dirroot . $dirpath;
00084 
00085         $tableparam = strtolower(required_param('table', PARAM_PATH));
00086         $indexparam = strtolower(required_param('index', PARAM_PATH));
00087         $name = trim(strtolower(optional_param('name', $indexparam, PARAM_PATH)));
00088 
00089         $comment = required_param('comment', PARAM_CLEAN);
00090         $comment = trim($comment);
00091 
00092         $unique = required_param('unique', PARAM_INT);
00093         $fields = required_param('fields', PARAM_CLEAN);
00094         $fields = str_replace(' ', '', trim(strtolower($fields)));
00095 
00096         $editeddir =& $XMLDB->editeddirs[$dirpath];
00097         $structure =& $editeddir->xml_file->getStructure();
00098         $table =& $structure->getTable($tableparam);
00099         $index =& $table->getIndex($indexparam);
00100         $oldhash = $index->getHash();
00101 
00102         $errors = array(); // To store all the errors found
00103 
00104         // Perform some checks
00105         // Check empty name
00106         if (empty($name)) {
00107             $errors[] = $this->str['indexnameempty'];
00108         }
00109         // Check incorrect name
00110         if ($name == 'changeme') {
00111             $errors[] = $this->str['incorrectindexname'];
00112         }
00113         // Check duplicate name
00114         if ($indexparam != $name && $table->getIndex($name)) {
00115             $errors[] = $this->str['duplicateindexname'];
00116         }
00117         $fieldsarr = explode(',', $fields);
00118         // Check the fields isn't empty
00119         if (empty($fieldsarr[0])) {
00120             $errors[] = $this->str['nofieldsspecified'];
00121         } else {
00122             // Check that there aren't duplicate column names
00123             $uniquearr = array_unique($fieldsarr);
00124             if (count($fieldsarr) != count($uniquearr)) {
00125                 $errors[] = $this->str['duplicatefieldsused'];
00126             }
00127             // Check that all the fields in belong to the table
00128             foreach ($fieldsarr as $field) {
00129                 if (!$table->getField($field)) {
00130                     $errors[] = $this->str['fieldsnotintable'];
00131                     break;
00132                 }
00133             }
00134             // Check that there isn't any key using exactly the same fields
00135             $tablekeys = $table->getKeys();
00136             if ($tablekeys) {
00137                 foreach ($tablekeys as $tablekey) {
00138                     $keyfieldsarr = $tablekey->getFields();
00139                     // Compare both arrays, looking for differences
00140                     $diferences = array_merge(array_diff($fieldsarr, $keyfieldsarr), array_diff($keyfieldsarr, $fieldsarr));
00141                     if (empty($diferences)) {
00142                         $errors[] = $this->str['fieldsusedinkey'];
00143                         break;
00144                     }
00145                 }
00146             }
00147             // Check that there isn't any index using exactly the same fields
00148             $tableindexes = $table->getIndexes();
00149             if ($tableindexes) {
00150                 foreach ($tableindexes as $tableindex) {
00151                     // Skip checking against itself
00152                     if ($indexparam == $tableindex->getName()) {
00153                         continue;
00154                     }
00155                     $indexfieldsarr = $tableindex->getFields();
00156                     // Compare both arrays, looking for differences
00157                     $diferences = array_merge(array_diff($fieldsarr, $indexfieldsarr), array_diff($indexfieldsarr, $fieldsarr));
00158                     if (empty($diferences)) {
00159                         $errors[] = $this->str['fieldsusedinindex'];
00160                         break;
00161                     }
00162                 }
00163             }
00164         }
00165 
00166         if (!empty($errors)) {
00167             $tempindex = new xmldb_index($name);
00168             $tempindex->setUnique($unique);
00169             $tempindex->setFields($fieldsarr);
00170             // Prepare the output
00171             $o = '<p>' .implode(', ', $errors) . '</p>
00172                   <p>' . $tempindex->readableInfo() . '</p>';
00173             $o.= '<a href="index.php?action=edit_index&amp;index=' .$index->getName() . '&amp;table=' . $table->getName() .
00174                  '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
00175             $this->output = $o;
00176         }
00177 
00178         // Continue if we aren't under errors
00179         if (empty($errors)) {
00180             // If there is one name change, do it, changing the prev and next
00181             // attributes of the adjacent fields
00182             if ($indexparam != $name) {
00183                 $index->setName($name);
00184                 if ($index->getPrevious()) {
00185                     $prev =& $table->getIndex($index->getPrevious());
00186                     $prev->setNext($name);
00187                     $prev->setChanged(true);
00188                 }
00189                 if ($index->getNext()) {
00190                     $next =& $table->getIndex($index->getNext());
00191                     $next->setPrevious($name);
00192                     $next->setChanged(true);
00193                 }
00194             }
00195 
00196             // Set comment
00197             $index->setComment($comment);
00198 
00199             // Set the rest of fields
00200             $index->setUnique($unique);
00201             $index->setFields($fieldsarr);
00202 
00203             // If the hash has changed from the old one, change the version
00204             // and mark the structure as changed
00205             $index->calculateHash(true);
00206             if ($oldhash != $index->getHash()) {
00207                 $index->setChanged(true);
00208                 $table->setChanged(true);
00209                 // Recalculate the structure hash
00210                 $structure->calculateHash(true);
00211                 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
00212                 // Mark as changed
00213                 $structure->setChanged(true);
00214             }
00215 
00216             // Launch postaction if exists (leave this here!)
00217             if ($this->getPostAction() && $result) {
00218                 return $this->launch($this->getPostAction());
00219             }
00220         }
00221 
00222         // Return ok if arrived here
00223         return $result;
00224     }
00225 }
00226 
 All Data Structures Namespaces Files Functions Variables Enumerations