PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/xmldb/actions/main_view/main_view.class.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 308 lines | 217 code | 17 blank | 74 comment | 52 complexity | 1c8a9c2d46c83f44d99ec1ba12d7336d MD5 | raw file
  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. * @package tool_xmldb
  18. * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. /**
  22. * This class will show all the actions available under the XMLDB editor interface
  23. *
  24. * From here, files can be created, edited, saved and deleted, plus some
  25. * extra utilities like displaying docs, xml info and performing various consistency tests
  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 main_view extends XMLDBAction {
  32. /**
  33. * Init method, every subclass will have its own
  34. */
  35. function init() {
  36. parent::init();
  37. // Set own custom attributes
  38. $this->sesskey_protected = false; // This action doesn't need sesskey protection
  39. // Get needed strings
  40. $this->loadStrings(array(
  41. 'load' => 'tool_xmldb',
  42. 'create' => 'tool_xmldb',
  43. 'edit' => 'tool_xmldb',
  44. 'save' => 'tool_xmldb',
  45. 'revert' => 'tool_xmldb',
  46. 'unload' => 'tool_xmldb',
  47. 'delete' => 'tool_xmldb',
  48. 'reservedwords' => 'tool_xmldb',
  49. 'gotolastused' => 'tool_xmldb',
  50. 'checkindexes' => 'tool_xmldb',
  51. 'checkdefaults' => 'tool_xmldb',
  52. 'checkforeignkeys' => 'tool_xmldb',
  53. 'checkbigints' => 'tool_xmldb',
  54. 'checkoraclesemantics' => 'tool_xmldb',
  55. 'doc' => 'tool_xmldb',
  56. 'filemodifiedoutfromeditor' => 'tool_xmldb',
  57. 'viewxml' => 'tool_xmldb',
  58. 'pendingchangescannotbesavedreload' => 'tool_xmldb'
  59. ));
  60. }
  61. /**
  62. * Invoke method, every class will have its own
  63. * returns true/false on completion, setting both
  64. * errormsg and output as necessary
  65. */
  66. function invoke() {
  67. parent::invoke();
  68. $result = true;
  69. // Set own core attributes
  70. $this->does_generate = ACTION_GENERATE_HTML;
  71. // These are always here
  72. global $CFG, $XMLDB, $SESSION, $DB;
  73. // Get lastused
  74. $o = '';
  75. if (isset($SESSION->lastused)) {
  76. if ($lastused = $SESSION->lastused) {
  77. // Print link
  78. $o .= '<p class="centerpara"><a href="#lastused">' . $this->str['gotolastused'] . '</a></p>';
  79. }
  80. } else {
  81. $lastused = NULL;
  82. }
  83. // Calculate the buttons
  84. $b = '<p class="centerpara buttons">';
  85. // The reserved_words button
  86. $b .= '&nbsp;<a href="index.php?action=view_reserved_words">[' . $this->str['reservedwords'] . ']</a>';
  87. // The docs button
  88. $b .= '&nbsp;<a href="index.php?action=generate_all_documentation">[' . $this->str['doc'] . ']</a>';
  89. // The check indexes button
  90. $b .= '&nbsp;<a href="index.php?action=check_indexes&amp;sesskey=' . sesskey() . '">[' . $this->str['checkindexes'] . ']</a>';
  91. // The check defaults button
  92. $b .= '&nbsp;<a href="index.php?action=check_defaults&amp;sesskey=' . sesskey() . '">[' . $this->str['checkdefaults'] . ']</a>';
  93. // The check bigints button (only for MySQL and PostgreSQL) MDL-11038a
  94. if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') {
  95. $b .= '&nbsp;<a href="index.php?action=check_bigints&amp;sesskey=' . sesskey() . '">[' . $this->str['checkbigints'] . ']</a>';
  96. }
  97. // The check semantics button (only for Oracle) MDL-29416
  98. if ($DB->get_dbfamily() == 'oracle') {
  99. $b .= '&nbsp;<a href="index.php?action=check_oracle_semantics&amp;sesskey=' . sesskey() . '">[' . $this->str['checkoraclesemantics'] . ']</a>';
  100. }
  101. $b .= '&nbsp;<a href="index.php?action=check_foreign_keys&amp;sesskey=' . sesskey() . '">[' . $this->str['checkforeignkeys'] . ']</a>';
  102. $b .= '</p>';
  103. // Send buttons to output
  104. $o .= $b;
  105. // Do the job
  106. // Get the list of DB directories
  107. $result = $this->launch('get_db_directories');
  108. // Display list of DB directories if everything is ok
  109. if ($result && !empty($XMLDB->dbdirs)) {
  110. $o .= '<table id="listdirectories" border="0" cellpadding="5" cellspacing="1" class="admintable generaltable">';
  111. $row = 0;
  112. foreach ($XMLDB->dbdirs as $key => $dbdir) {
  113. // Detect if this is the lastused dir
  114. $hithis = false;
  115. if (str_replace($CFG->dirroot, '', $key) == $lastused) {
  116. $hithis = true;
  117. }
  118. $elementtext = str_replace($CFG->dirroot . '/', '', $key);
  119. // Calculate the dbdir has_changed field if needed
  120. if (!isset($dbdir->has_changed) && isset($dbdir->xml_loaded)) {
  121. $dbdir->xml_changed = false;
  122. if (isset($XMLDB->editeddirs[$key])) {
  123. $editeddir = $XMLDB->editeddirs[$key];
  124. if (isset($editeddir->xml_file)) {
  125. $structure = $editeddir->xml_file->getStructure();
  126. if ($structure->hasChanged()) {
  127. $dbdir->xml_changed = true;
  128. $editeddir->xml_changed = true;
  129. }
  130. }
  131. }
  132. }
  133. // The file name (link to edit if the file is loaded)
  134. if ($dbdir->path_exists &&
  135. file_exists($key . '/install.xml') &&
  136. is_readable($key . '/install.xml') &&
  137. is_readable($key) &&
  138. !empty($dbdir->xml_loaded)) {
  139. $f = '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">' . $elementtext . '</a>';
  140. } else {
  141. $f = $elementtext;
  142. }
  143. // Calculate the buttons
  144. $b = ' <td class="button cell">';
  145. // The create button
  146. if ($dbdir->path_exists &&
  147. !file_exists($key . '/install.xml') &&
  148. is_writeable($key)) {
  149. $b .= '<a href="index.php?action=create_xml_file&amp;sesskey=' . sesskey() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '&amp;time=' . time() . '&amp;postaction=main_view#lastused">[' . $this->str['create'] . ']</a>';
  150. } else {
  151. $b .= '[' . $this->str['create'] . ']';
  152. }
  153. $b .= '</td><td class="button cell">';
  154. // The load button
  155. if ($dbdir->path_exists &&
  156. file_exists($key . '/install.xml') &&
  157. is_readable($key . '/install.xml') &&
  158. empty($dbdir->xml_loaded)) {
  159. $b .= '<a href="index.php?action=load_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '&amp;time=' . time() . '&amp;postaction=main_view#lastused">[' . $this->str['load'] . ']</a>';
  160. } else {
  161. $b .= '[' . $this->str['load'] . ']';
  162. }
  163. $b .= '</td><td class="button cell">';
  164. // The edit button
  165. if ($dbdir->path_exists &&
  166. file_exists($key . '/install.xml') &&
  167. is_readable($key . '/install.xml') &&
  168. is_readable($key) &&
  169. !empty($dbdir->xml_loaded)) {
  170. $b .= '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['edit'] . ']</a>';
  171. } else {
  172. $b .= '[' . $this->str['edit'] . ']';
  173. }
  174. $b .= '</td><td class="button cell">';
  175. // The save button
  176. if ($dbdir->path_exists &&
  177. file_exists($key . '/install.xml') &&
  178. is_writeable($key . '/install.xml') &&
  179. is_writeable($key) &&
  180. !empty($dbdir->xml_loaded) &&
  181. !empty($dbdir->xml_changed)) {
  182. $b .= '<a href="index.php?action=save_xml_file&amp;sesskey=' . sesskey() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '&amp;time=' . time() . '&amp;postaction=main_view#lastused">[' . $this->str['save'] . ']</a>';
  183. // Check if the file has been manually edited while being modified in the editor
  184. if ($dbdir->filemtime != filemtime($key . '/install.xml')) {
  185. // File manually modified. Add to action error, will be displayed inline.
  186. $this->errormsg = $this->str['filemodifiedoutfromeditor'];
  187. }
  188. } else {
  189. $b .= '[' . $this->str['save'] . ']';
  190. }
  191. $b .= '</td><td class="button cell">';
  192. // The document button
  193. if ($dbdir->path_exists &&
  194. file_exists($key . '/install.xml') &&
  195. is_readable($key . '/install.xml') &&
  196. is_readable($key)) {
  197. $b .= '<a href="index.php?action=generate_documentation&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['doc'] . ']</a>';
  198. } else {
  199. $b .= '[' . $this->str['doc'] . ']';
  200. }
  201. $b .= '</td><td class="button cell">';
  202. // The view xml button
  203. if ($dbdir->path_exists &&
  204. file_exists($key . '/install.xml') &&
  205. is_readable($key . '/install.xml')) {
  206. $b .= '<a href="index.php?action=view_xml&amp;file=' . urlencode(str_replace($CFG->dirroot, '', $key) . '/install.xml') . '">[' . $this->str['viewxml'] . ']</a>';
  207. } else {
  208. $b .= '[' . $this->str['viewxml'] . ']';
  209. }
  210. $b .= '</td><td class="button cell">';
  211. // The revert button
  212. if ($dbdir->path_exists &&
  213. file_exists($key . '/install.xml') &&
  214. is_readable($key . '/install.xml') &&
  215. is_writeable($key) &&
  216. !empty($dbdir->xml_loaded) &&
  217. !empty($dbdir->xml_changed)) {
  218. $b .= '<a href="index.php?action=revert_changes&amp;sesskey=' . sesskey() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['revert'] . ']</a>';
  219. } else {
  220. $b .= '[' . $this->str['revert'] . ']';
  221. }
  222. $b .= '</td><td class="button cell">';
  223. // The unload button
  224. if ($dbdir->path_exists &&
  225. file_exists($key . '/install.xml') &&
  226. is_readable($key . '/install.xml') &&
  227. !empty($dbdir->xml_loaded) &&
  228. empty($dbdir->xml_changed)) {
  229. $b .= '<a href="index.php?action=unload_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '&amp;time=' . time() . '&amp;postaction=main_view#lastused">[' . $this->str['unload'] . ']</a>';
  230. } else {
  231. $b .= '[' . $this->str['unload'] . ']';
  232. }
  233. $b .= '</td><td class="button cell">';
  234. // The delete button
  235. if ($dbdir->path_exists &&
  236. file_exists($key . '/install.xml') &&
  237. is_readable($key . '/install.xml') &&
  238. is_writeable($key) &&
  239. empty($dbdir->xml_loaded)) {
  240. $b .= '<a href="index.php?action=delete_xml_file&amp;sesskey=' . sesskey() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['delete'] . ']</a>';
  241. } else {
  242. $b .= '[' . $this->str['delete'] . ']';
  243. }
  244. $b .= '</td>';
  245. // include the higlight
  246. if ($hithis) {
  247. $o .= '<tr class="highlight"><td class="directory cell"><a name="lastused" />' . $f . '</td>' . $b . '</tr>';
  248. } else {
  249. $o .= '<tr class="r' . $row . '"><td class="directory cell">' . $f . '</td>' . $b . '</tr>';
  250. }
  251. $row = ($row + 1) % 2;
  252. // show errors if they exist
  253. if (isset($dbdir->xml_file)) {
  254. if ($structure = $dbdir->xml_file->getStructure()) {
  255. $errors = !empty($this->errormsg) ? array($this->errormsg) : array();
  256. $structureerrors = $structure->getAllErrors();
  257. if ($structureerrors) {
  258. $errors = array_merge($errors, $structureerrors);
  259. }
  260. if (!empty($errors)) {
  261. if ($hithis) {
  262. $o .= '<tr class="highlight"><td class="error cell" colspan="10">' . implode (', ', $errors) . '</td></tr>';
  263. } else {
  264. $o .= '<tr class="r' . $row . '"><td class="error cell" colspan="10">' . implode (', ', $errors) . '</td></tr>';
  265. }
  266. }
  267. }
  268. }
  269. // If there are changes pending to be saved, but the file cannot be written... inform here
  270. if ($dbdir->path_exists &&
  271. file_exists($key . '/install.xml') &&
  272. !empty($dbdir->xml_loaded) &&
  273. !empty($dbdir->xml_changed) &&
  274. (!is_writeable($key . '/install.xml') || !is_writeable($key))) {
  275. if ($hithis) {
  276. $o .= '<tr class="highlight"><td class="error cell" colspan="10">';
  277. } else {
  278. $o .= '<tr class="r' . $row . '"><td class="error cell" colspan="10">';
  279. }
  280. $o .= $this->str['pendingchangescannotbesavedreload'];
  281. $o .= '</td></tr>';
  282. }
  283. }
  284. $o .= '</table>';
  285. // Set the output
  286. $this->output = $o;
  287. }
  288. // Finally, return result
  289. return $result;
  290. }
  291. }