2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * This class will show all the actions available under the XMLDB editor interface
26 * From here, files can be created, edited, saved and deleted, plus some
27 * extra utilities like displaying docs, xml info and performing various consistency tests
30 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class main_view
extends XMLDBAction
{
36 * Init method, every subclass will have its own
41 // Set own custom attributes
42 $this->sesskey_protected
= false; // This action doesn't need sesskey protection
45 $this->loadStrings(array(
46 'load' => 'tool_xmldb',
47 'create' => 'tool_xmldb',
48 'edit' => 'tool_xmldb',
49 'save' => 'tool_xmldb',
50 'revert' => 'tool_xmldb',
51 'unload' => 'tool_xmldb',
52 'delete' => 'tool_xmldb',
53 'reservedwords' => 'tool_xmldb',
54 'gotolastused' => 'tool_xmldb',
55 'checkindexes' => 'tool_xmldb',
56 'checkdefaults' => 'tool_xmldb',
57 'checkforeignkeys' => 'tool_xmldb',
58 'checkbigints' => 'tool_xmldb',
59 'checkoraclesemantics' => 'tool_xmldb',
60 'reconcilefiles' => 'tool_xmldb',
61 'doc' => 'tool_xmldb',
62 'filemodifiedoutfromeditor' => 'tool_xmldb',
63 'viewxml' => 'tool_xmldb',
64 'pendingchangescannotbesavedreload' => 'tool_xmldb'
69 * Invoke method, every class will have its own
70 * returns true/false on completion, setting both
71 * errormsg and output as necessary
78 // Set own core attributes
79 $this->does_generate
= ACTION_GENERATE_HTML
;
81 // These are always here
82 global $CFG, $XMLDB, $SESSION, $DB;
86 if (isset($SESSION->lastused
)) {
87 if ($lastused = $SESSION->lastused
) {
89 $o .= '<p class="centerpara"><a href="#lastused">' . $this->str
['gotolastused'] . '</a></p>';
95 // Calculate the buttons
96 $b = '<p class="centerpara buttons">';
97 // The reserved_words button
98 $b .= ' <a href="index.php?action=view_reserved_words">[' . $this->str
['reservedwords'] . ']</a>';
100 $b .= ' <a href="index.php?action=generate_all_documentation">[' . $this->str
['doc'] . ']</a>';
101 // The reconcile XMLDB files button.
102 $b .= ' <a href="index.php?action=reconcile_files">[' . $this->str
['reconcilefiles'] . ']</a>';
103 // The check indexes button
104 $b .= ' <a href="index.php?action=check_indexes&sesskey=' . sesskey() . '">[' . $this->str
['checkindexes'] . ']</a>';
105 // The check defaults button
106 $b .= ' <a href="index.php?action=check_defaults&sesskey=' . sesskey() . '">[' . $this->str
['checkdefaults'] . ']</a>';
107 // The check bigints button (only for MySQL and PostgreSQL) MDL-11038a
108 if ($DB->get_dbfamily() == 'mysql' ||
$DB->get_dbfamily() == 'postgres') {
109 $b .= ' <a href="index.php?action=check_bigints&sesskey=' . sesskey() . '">[' . $this->str
['checkbigints'] . ']</a>';
111 // The check semantics button (only for Oracle) MDL-29416
112 if ($DB->get_dbfamily() == 'oracle') {
113 $b .= ' <a href="index.php?action=check_oracle_semantics&sesskey=' . sesskey() . '">[' . $this->str
['checkoraclesemantics'] . ']</a>';
115 $b .= ' <a href="index.php?action=check_foreign_keys&sesskey=' . sesskey() . '">[' . $this->str
['checkforeignkeys'] . ']</a>';
117 // Send buttons to output
122 // Get the list of DB directories
123 $result = $this->launch('get_db_directories');
124 // Display list of DB directories if everything is ok
125 if ($result && !empty($XMLDB->dbdirs
)) {
126 $o .= '<table id="listdirectories" border="0" cellpadding="5" cellspacing="1"' .
127 ' class="table-striped table-sm admintable generaltable">';
129 foreach ($XMLDB->dbdirs
as $key => $dbdir) {
130 // Detect if this is the lastused dir
132 if (str_replace($CFG->dirroot
, '', $key) == $lastused) {
135 $elementtext = str_replace($CFG->dirroot
. '/', '', $key);
136 // Calculate the dbdir has_changed field if needed
137 if (!isset($dbdir->has_changed
) && isset($dbdir->xml_loaded
)) {
138 $dbdir->xml_changed
= false;
139 if (isset($XMLDB->editeddirs
[$key])) {
140 $editeddir = $XMLDB->editeddirs
[$key];
141 if (isset($editeddir->xml_file
)) {
142 $structure = $editeddir->xml_file
->getStructure();
143 if ($structure->hasChanged()) {
144 $dbdir->xml_changed
= true;
145 $editeddir->xml_changed
= true;
150 // The file name (link to edit if the file is loaded)
151 if ($dbdir->path_exists
&&
152 file_exists($key . '/install.xml') &&
153 is_readable($key . '/install.xml') &&
155 !empty($dbdir->xml_loaded
)) {
156 $f = '<a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '">' . $elementtext . '</a>';
160 // Calculate the buttons
161 $b = ' <td class="button cell">';
163 if ($dbdir->path_exists
&&
164 !file_exists($key . '/install.xml') &&
165 is_writeable($key)) {
166 $b .= '<a href="index.php?action=create_xml_file&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '&time=' . time() . '&postaction=main_view#lastused">[' . $this->str
['create'] . ']</a>';
168 $b .= '[' . $this->str
['create'] . ']';
170 $b .= '</td><td class="button cell">';
172 if ($dbdir->path_exists
&&
173 file_exists($key . '/install.xml') &&
174 is_readable($key . '/install.xml') &&
175 empty($dbdir->xml_loaded
)) {
176 $b .= '<a href="index.php?action=load_xml_file&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '&time=' . time() . '&postaction=main_view#lastused">[' . $this->str
['load'] . ']</a>';
178 $b .= '[' . $this->str
['load'] . ']';
180 $b .= '</td><td class="button cell">';
182 if ($dbdir->path_exists
&&
183 file_exists($key . '/install.xml') &&
184 is_readable($key . '/install.xml') &&
186 !empty($dbdir->xml_loaded
)) {
187 $b .= '<a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '">[' . $this->str
['edit'] . ']</a>';
189 $b .= '[' . $this->str
['edit'] . ']';
191 $b .= '</td><td class="button cell">';
193 if ($dbdir->path_exists
&&
194 file_exists($key . '/install.xml') &&
195 is_writeable($key . '/install.xml') &&
196 is_writeable($key) &&
197 !empty($dbdir->xml_loaded
) &&
198 !empty($dbdir->xml_changed
)) {
199 $b .= '<a href="index.php?action=save_xml_file&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '&time=' . time() . '&postaction=main_view#lastused">[' . $this->str
['save'] . ']</a>';
200 // Check if the file has been manually edited while being modified in the editor
201 if ($dbdir->filemtime
!= filemtime($key . '/install.xml')) {
202 // File manually modified. Add to action error, will be displayed inline.
203 $this->errormsg
= $this->str
['filemodifiedoutfromeditor'];
206 $b .= '[' . $this->str
['save'] . ']';
208 $b .= '</td><td class="button cell">';
209 // The document button
210 if ($dbdir->path_exists
&&
211 file_exists($key . '/install.xml') &&
212 is_readable($key . '/install.xml') &&
214 $b .= '<a href="index.php?action=generate_documentation&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '">[' . $this->str
['doc'] . ']</a>';
216 $b .= '[' . $this->str
['doc'] . ']';
218 $b .= '</td><td class="button cell">';
219 // The view xml button
220 if ($dbdir->path_exists
&&
221 file_exists($key . '/install.xml') &&
222 is_readable($key . '/install.xml')) {
223 $b .= '<a href="index.php?action=view_xml&file=' . urlencode(str_replace($CFG->dirroot
, '', $key) . '/install.xml') . '">[' . $this->str
['viewxml'] . ']</a>';
225 $b .= '[' . $this->str
['viewxml'] . ']';
227 $b .= '</td><td class="button cell">';
229 if ($dbdir->path_exists
&&
230 file_exists($key . '/install.xml') &&
231 is_readable($key . '/install.xml') &&
232 is_writeable($key) &&
233 !empty($dbdir->xml_loaded
) &&
234 !empty($dbdir->xml_changed
)) {
235 $b .= '<a href="index.php?action=revert_changes&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '">[' . $this->str
['revert'] . ']</a>';
237 $b .= '[' . $this->str
['revert'] . ']';
239 $b .= '</td><td class="button cell">';
241 if ($dbdir->path_exists
&&
242 file_exists($key . '/install.xml') &&
243 is_readable($key . '/install.xml') &&
244 !empty($dbdir->xml_loaded
) &&
245 empty($dbdir->xml_changed
)) {
246 $b .= '<a href="index.php?action=unload_xml_file&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '&time=' . time() . '&postaction=main_view#lastused">[' . $this->str
['unload'] . ']</a>';
248 $b .= '[' . $this->str
['unload'] . ']';
250 $b .= '</td><td class="button cell">';
252 if ($dbdir->path_exists
&&
253 file_exists($key . '/install.xml') &&
254 is_readable($key . '/install.xml') &&
255 is_writeable($key) &&
256 empty($dbdir->xml_loaded
)) {
257 $b .= '<a href="index.php?action=delete_xml_file&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $key)) . '">[' . $this->str
['delete'] . ']</a>';
259 $b .= '[' . $this->str
['delete'] . ']';
262 // include the higlight
264 $o .= '<tr class="highlight"><td class="directory cell"><a name="lastused" />' . $f . '</td>' . $b . '</tr>';
266 $o .= '<tr class="r' . $row . '"><td class="directory cell">' . $f . '</td>' . $b . '</tr>';
268 $row = ($row +
1) %
2;
269 // show errors if they exist
270 if (isset($dbdir->xml_file
)) {
271 if ($structure = $dbdir->xml_file
->getStructure()) {
272 $errors = !empty($this->errormsg
) ?
array($this->errormsg
) : array();
273 $structureerrors = $structure->getAllErrors();
274 if ($structureerrors) {
275 $errors = array_merge($errors, $structureerrors);
277 if (!empty($errors)) {
279 $o .= '<tr class="highlight"><td class="error cell" colspan="10">' . implode (', ', $errors) . '</td></tr>';
281 $o .= '<tr class="r' . $row . '"><td class="error cell" colspan="10">' . implode (', ', $errors) . '</td></tr>';
286 // If there are changes pending to be saved, but the file cannot be written... inform here
287 if ($dbdir->path_exists
&&
288 file_exists($key . '/install.xml') &&
289 !empty($dbdir->xml_loaded
) &&
290 !empty($dbdir->xml_changed
) &&
291 (!is_writeable($key . '/install.xml') ||
!is_writeable($key))) {
294 $o .= '<tr class="highlight"><td class="error cell" colspan="10">';
296 $o .= '<tr class="r' . $row . '"><td class="error cell" colspan="10">';
298 $o .= $this->str
['pendingchangescannotbesavedreload'];
308 // Finally, return result