Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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