Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / main_view / main_view.class.php
bloba9cc67e097c86c8c98580106b715636ce7a2e01a
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
27 /// This class will show all the actions available under the XMLDB interface
29 class main_view extends XMLDBAction {
31 /**
32 * Init method, every subclass will have its own
34 function init() {
35 parent::init();
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
41 'load' => 'xmldb',
42 'create' => 'xmldb',
43 'edit' => 'xmldb',
44 'save' => 'xmldb',
45 'revert' => 'xmldb',
46 'unload' => 'xmldb',
47 'delete' => 'xmldb',
48 'reservedwords' => 'xmldb',
49 'test' => 'xmldb',
50 'gotolastused' => 'xmldb',
51 'checkindexes' => 'xmldb',
52 'checkbigints' => 'xmldb'
53 ));
56 /**
57 * Invoke method, every class will have its own
58 * returns true/false on completion, setting both
59 * errormsg and output as necessary
61 function invoke() {
62 parent::invoke();
64 $result = true;
66 /// Set own core attributes
67 $this->does_generate = ACTION_GENERATE_HTML;
69 /// These are always here
70 global $CFG, $XMLDB, $SESSION;
72 /// Get lastused
73 $o = '';
74 if (isset($SESSION->lastused)) {
75 if ($lastused = $SESSION->lastused) {
76 /// Print link
77 $o .= '<p class="centerpara"><a href="#lastused">' . $this->str['gotolastused'] . '</a></p>';
79 } else {
80 $lastused = NULL;
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 test button
88 $b .= '&nbsp;<a href="index.php?action=test">[' . $this->str['test'] . ']</a>';
89 /// The check indexes button
90 $b .= '&nbsp;<a href="index.php?action=check_indexes">[' . $this->str['checkindexes'] . ']</a>';
91 /// The check bigints button (only for MySQL and PostgreSQL) MDL-11038a
92 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
93 $b .= '&nbsp;<a href="index.php?action=check_bigints">[' . $this->str['checkbigints'] . ']</a>';
95 $b .= '</p>';
96 /// Send buttons to output
97 $o .= $b;
99 /// Do the job
101 /// Get the list of DB directories
102 $result = $this->launch('get_db_directories');
103 /// Display list of DB directories if everything is ok
104 if ($result && !empty($XMLDB->dbdirs)) {
105 $o .= '<table id="listdirectories" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
106 $row = 0;
107 foreach ($XMLDB->dbdirs as $key => $dbdir) {
108 /// Detect if this is the lastused dir
109 $hithis = false;
110 if (str_replace($CFG->dirroot, '', $key) == $lastused) {
111 $hithis = true;
113 $elementtext = str_replace($CFG->dirroot . '/', '', $key);
114 /// Calculate the dbdir has_changed field if needed
115 if (!isset($dbdir->has_changed) && isset($dbdir->xml_loaded)) {
116 $dbdir->xml_changed = false;
117 if (isset($XMLDB->editeddirs[$key])) {
118 $editeddir =& $XMLDB->editeddirs[$key];
119 if (isset($editeddir->xml_file)) {
120 $structure =& $editeddir->xml_file->getStructure();
121 if ($structure->hasChanged()) {
122 $dbdir->xml_changed = true;
123 $editeddir->xml_changed = true;
128 /// Calculate the buttons
129 $b = ' <td class="button cell">';
130 /// The create button
131 if ($dbdir->path_exists &&
132 !file_exists($key . '/install.xml') &&
133 is_writeable($key)) {
134 $b .= '<a href="index.php?action=create_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '&amp;time=' . time() . '&amp;postaction=main_view#lastused">[' . $this->str['create'] . ']</a>';
135 } else {
136 $b .= '[' . $this->str['create'] . ']';
138 $b .= '</td><td class="button cell">';
139 /// The load button
140 if ($dbdir->path_exists &&
141 file_exists($key . '/install.xml') &&
142 is_readable($key . '/install.xml') &&
143 empty($dbdir->xml_loaded)) {
144 $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>';
145 } else {
146 $b .= '[' . $this->str['load'] . ']';
148 $b .= '</td><td class="button cell">';
149 /// The edit button
150 if ($dbdir->path_exists &&
151 file_exists($key . '/install.xml') &&
152 is_readable($key . '/install.xml') &&
153 is_readable($key) &&
154 !empty($dbdir->xml_loaded)) {
155 $b .= '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['edit'] . ']</a>';
156 } else {
157 $b .= '[' . $this->str['edit'] . ']';
159 $b .= '</td><td class="button cell">';
160 /// The save button
161 if ($dbdir->path_exists &&
162 file_exists($key . '/install.xml') &&
163 is_writeable($key . '/install.xml') &&
164 is_writeable($key) &&
165 !empty($dbdir->xml_loaded) &&
166 !empty($dbdir->xml_changed)) {
167 $b .= '<a href="index.php?action=save_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '&amp;time=' . time() . '&amp;postaction=main_view#lastused">[' . $this->str['save'] . ']</a>';
168 /// Check if the file has been manually edited while being modified in the editor
169 if ($dbdir->filemtime != filemtime($key . '/install.xml')) {
170 /// File manually modified. Add to errors.
171 if ($structure =& $dbdir->xml_file->getStructure()) {
172 $structure->errormsg = 'Warning: File locally modified while using the XMLDB Editor. Saving will overwrite local changes';
175 } else {
176 $b .= '[' . $this->str['save'] . ']';
178 $b .= '</td><td class="button cell">';
179 /// The revert button
180 if ($dbdir->path_exists &&
181 file_exists($key . '/install.xml') &&
182 is_readable($key . '/install.xml') &&
183 is_writeable($key) &&
184 !empty($dbdir->xml_loaded) &&
185 !empty($dbdir->xml_changed)) {
186 $b .= '<a href="index.php?action=revert_changes&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['revert'] . ']</a>';
187 } else {
188 $b .= '[' . $this->str['revert'] . ']';
190 $b .= '</td><td class="button cell">';
191 /// The unload button
192 if ($dbdir->path_exists &&
193 file_exists($key . '/install.xml') &&
194 is_readable($key . '/install.xml') &&
195 !empty($dbdir->xml_loaded) &&
196 empty($dbdir->xml_changed)) {
197 $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>';
198 } else {
199 $b .= '[' . $this->str['unload'] . ']';
201 $b .= '</td><td class="button cell">';
202 /// The delete button
203 if ($dbdir->path_exists &&
204 file_exists($key . '/install.xml') &&
205 is_readable($key . '/install.xml') &&
206 is_writeable($key) &&
207 empty($dbdir->xml_loaded)) {
208 $b .= '<a href="index.php?action=delete_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $key)) . '">[' . $this->str['delete'] . ']</a>';
209 } else {
210 $b .= '[' . $this->str['delete'] . ']';
212 $b .= '</td>';
213 /// if the file, exist, XML is viewable
214 if ($dbdir->path_exists &&
215 file_exists($key . '/install.xml') &&
216 is_readable($key . '/install.xml')) {
217 $elementtext = '<a href="index.php?action=view_xml&amp;file=' . urlencode(str_replace($CFG->dirroot, '', $key) . '/install.xml') . '">' . $elementtext . '</a></td>';
218 } else {
219 $elementtext = $elementtext . '</td>';
221 /// include the higlight
222 if ($hithis) {
223 $o .= '<tr class="highlight"><td class="directory cell"><a name="lastused" />' . $elementtext . $b . '</tr>';
224 } else {
225 $o .= '<tr class="r' . $row . '"><td class="directory cell">' . $elementtext . $b . '</tr>';
227 $row = ($row + 1) % 2;
228 /// show errors if they exist
229 if (isset($dbdir->xml_file)) {
230 if ($structure =& $dbdir->xml_file->getStructure()) {
231 if ($errors = $structure->getAllErrors()) {
232 if ($hithis) {
233 $o .= '<tr class="highlight"><td class="error cell" colspan="8">' . implode (', ', $errors) . '</td></tr>';
234 } else {
235 $o .= '<tr class="r' . $row . '"><td class="error cell" colspan="8">' . implode (', ', $errors) . '</td></tr>';
241 $o .= '</table>';
243 /// Set the output
244 $this->output = $o;
247 /// Finally, return result
248 return $result;