PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/xmldb/actions/edit_index_save/edit_index_save.class.php

https://github.com/nadavkav/MoodleTAO
PHP | 226 lines | 137 code | 26 blank | 63 comment | 28 complexity | 582bbecb5602da7f2d96bbece2f301e6 MD5 | raw file
  1. <?php // $Id$
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
  11. // //
  12. // This program is free software; you can redistribute it and/or modify //
  13. // it under the terms of the GNU General Public License as published by //
  14. // the Free Software Foundation; either version 2 of the License, or //
  15. // (at your option) any later version. //
  16. // //
  17. // This program is distributed in the hope that it will be useful, //
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  20. // GNU General Public License for more details: //
  21. // //
  22. // http://www.gnu.org/copyleft/gpl.html //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////
  25. /// This class will save the changes performed to one index
  26. class edit_index_save extends XMLDBAction {
  27. /**
  28. * Init method, every subclass will have its own
  29. */
  30. function init() {
  31. parent::init();
  32. /// Set own custom attributes
  33. /// Get needed strings
  34. $this->loadStrings(array(
  35. 'indexnameempty' => 'xmldb',
  36. 'incorrectindexname' => 'xmldb',
  37. 'duplicateindexname' => 'xmldb',
  38. 'nofieldsspecified' => 'xmldb',
  39. 'duplicatefieldsused' => 'xmldb',
  40. 'fieldsnotintable' => 'xmldb',
  41. 'fieldsusedinkey' => 'xmldb',
  42. 'fieldsusedinindex' => 'xmldb',
  43. 'administration' => ''
  44. ));
  45. }
  46. /**
  47. * Invoke method, every class will have its own
  48. * returns true/false on completion, setting both
  49. * errormsg and output as necessary
  50. */
  51. function invoke() {
  52. parent::invoke();
  53. $result = true;
  54. /// Set own core attributes
  55. $this->does_generate = ACTION_NONE;
  56. //$this->does_generate = ACTION_GENERATE_HTML;
  57. /// These are always here
  58. global $CFG, $XMLDB;
  59. /// Do the job, setting result as needed
  60. if (!data_submitted('nomatch')) { ///Basic prevention
  61. error('Wrong action call');
  62. }
  63. /// Get parameters
  64. $dirpath = required_param('dir', PARAM_PATH);
  65. $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  66. $tableparam = strtolower(required_param('table', PARAM_PATH));
  67. $indexparam = strtolower(required_param('index', PARAM_PATH));
  68. $name = trim(strtolower(optional_param('name', $indexparam, PARAM_PATH)));
  69. $comment = required_param('comment', PARAM_CLEAN);
  70. $comment = trim(stripslashes_safe($comment));
  71. $unique = required_param('unique', PARAM_INT);
  72. $fields = required_param('fields', PARAM_CLEAN);
  73. $fields = str_replace(' ', '', trim(strtolower(stripslashes_safe($fields))));
  74. $editeddir =& $XMLDB->editeddirs[$dirpath];
  75. $structure =& $editeddir->xml_file->getStructure();
  76. $table =& $structure->getTable($tableparam);
  77. $index =& $table->getIndex($indexparam);
  78. $oldhash = $index->getHash();
  79. $errors = array(); /// To store all the errors found
  80. /// Perform some checks
  81. /// Check empty name
  82. if (empty($name)) {
  83. $errors[] = $this->str['indexnameempty'];
  84. }
  85. /// Check incorrect name
  86. if ($name == 'changeme') {
  87. $errors[] = $this->str['incorrectindexname'];
  88. }
  89. /// Check duplicate name
  90. if ($indexparam != $name && $table->getIndex($name)) {
  91. $errors[] = $this->str['duplicateindexname'];
  92. }
  93. $fieldsarr = explode(',', $fields);
  94. /// Check the fields isn't empty
  95. if (empty($fieldsarr[0])) {
  96. $errors[] = $this->str['nofieldsspecified'];
  97. } else {
  98. /// Check that there aren't duplicate column names
  99. $uniquearr = array_unique($fieldsarr);
  100. if (count($fieldsarr) != count($uniquearr)) {
  101. $errors[] = $this->str['duplicatefieldsused'];
  102. }
  103. /// Check that all the fields in belong to the table
  104. foreach ($fieldsarr as $field) {
  105. if (!$table->getField($field)) {
  106. $errors[] = $this->str['fieldsnotintable'];
  107. break;
  108. }
  109. }
  110. /// Check that there isn't any key using exactly the same fields
  111. $tablekeys = $table->getKeys();
  112. if ($tablekeys) {
  113. foreach ($tablekeys as $tablekey) {
  114. $keyfieldsarr = $tablekey->getFields();
  115. /// Compare both arrays, looking for diferences
  116. $diferences = array_merge(array_diff($fieldsarr, $keyfieldsarr), array_diff($keyfieldsarr, $fieldsarr));
  117. if (empty($diferences)) {
  118. $errors[] = $this->str['fieldsusedinkey'];
  119. break;
  120. }
  121. }
  122. }
  123. /// Check that there isn't any index using exactlt the same fields
  124. $tableindexes = $table->getIndexes();
  125. if ($tableindexes) {
  126. foreach ($tableindexes as $tableindex) {
  127. /// Skip checking against itself
  128. if ($indexparam == $tableindex->getName()) {
  129. continue;
  130. }
  131. $indexfieldsarr = $tableindex->getFields();
  132. /// Compare both arrays, looking for diferences
  133. $diferences = array_merge(array_diff($fieldsarr, $indexfieldsarr), array_diff($indexfieldsarr, $fieldsarr));
  134. if (empty($diferences)) {
  135. $errors[] = $this->str['fieldsusedinindex'];
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. if (!empty($errors)) {
  142. $tempindex = new XMLDBIndex($name);
  143. $tempindex->setUnique($unique);
  144. $tempindex->setFields($fieldsarr);
  145. /// Prepare the output
  146. $site = get_site();
  147. $navlinks = array();
  148. $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc');
  149. $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc');
  150. $navigation = build_navigation($navlinks);
  151. print_header("$site->shortname: XMLDB", "$site->fullname", $navigation);
  152. notice ('<p>' .implode(', ', $errors) . '</p>
  153. <p>' . $tempindex->readableInfo(),
  154. 'index.php?action=edit_index&amp;index=' .$index->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
  155. die; /// re-die :-P
  156. }
  157. /// Continue if we aren't under errors
  158. if (empty($errors)) {
  159. /// If there is one name change, do it, changing the prev and next
  160. /// atributes of the adjacent fields
  161. if ($indexparam != $name) {
  162. $index->setName($name);
  163. if ($index->getPrevious()) {
  164. $prev =& $table->getIndex($index->getPrevious());
  165. $prev->setNext($name);
  166. $prev->setChanged(true);
  167. }
  168. if ($index->getNext()) {
  169. $next =& $table->getIndex($index->getNext());
  170. $next->setPrevious($name);
  171. $next->setChanged(true);
  172. }
  173. }
  174. /// Set comment
  175. $index->setComment($comment);
  176. /// Set the rest of fields
  177. $index->setUnique($unique);
  178. $index->setFields($fieldsarr);
  179. /// If the hash has changed from the old one, change the version
  180. /// and mark the structure as changed
  181. $index->calculateHash(true);
  182. if ($oldhash != $index->getHash()) {
  183. $index->setChanged(true);
  184. $table->setChanged(true);
  185. /// Recalculate the structure hash
  186. $structure->calculateHash(true);
  187. $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
  188. /// Mark as changed
  189. $structure->setChanged(true);
  190. }
  191. /// Launch postaction if exists (leave this here!)
  192. if ($this->getPostAction() && $result) {
  193. return $this->launch($this->getPostAction());
  194. }
  195. }
  196. /// Return ok if arrived here
  197. return $result;
  198. }
  199. }
  200. ?>