3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
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. //
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: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will edit one loaded XML file
29 class edit_xml_file
extends XMLDBAction
{
32 * Init method, every subclass will have its own
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
46 'vieworiginal' => 'xmldb',
47 'viewedited' => 'xmldb',
49 'statements' => 'xmldb',
50 'newtable' => 'xmldb',
51 'newtablefrommysql' => 'xmldb',
52 'newstatement' => 'xmldb',
53 'viewsqlcode' => 'xmldb',
54 'viewphpcode' => 'xmldb',
55 'reserved' => 'xmldb',
56 'backtomainview' => 'xmldb'
61 * Invoke method, every class will have its own
62 * returns true/false on completion, setting both
63 * errormsg and output as necessary
70 /// Set own core attributes
71 //$this->does_generate = ACTION_NONE;
72 $this->does_generate
= ACTION_GENERATE_HTML
;
74 /// These are always here
77 /// Do the job, setting $result as needed
79 /// Get the dir containing the file
80 $dirpath = required_param('dir', PARAM_PATH
);
81 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
83 /// Get the correct dir
84 if (!empty($XMLDB->dbdirs
)) {
85 $dbdir =& $XMLDB->dbdirs
[$dirpath];
87 /// Only if the directory exists and it has been loaded
88 if (!$dbdir->path_exists ||
!$dbdir->xml_loaded
) {
91 /// Check if the in-memory object exists and create it
92 if (empty($XMLDB->editeddirs
)) {
93 $XMLDB->editeddirs
= array();
95 /// Check if the dir exists and copy it from dbdirs
96 if (!isset($XMLDB->editeddirs
[$dirpath])) {
97 $XMLDB->editeddirs
[$dirpath] = unserialize(serialize($dbdir));
100 $editeddir =& $XMLDB->editeddirs
[$dirpath];
101 $structure =& $editeddir->xml_file
->getStructure();
102 /// Add the main form
103 $o = '<form id="form" action="index.php" method="post">';
105 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot
, '', $dirpath) . '" />';
106 $o.= ' <input type="hidden" name ="action" value="edit_xml_file_save" />';
107 $o.= ' <input type="hidden" name ="postaction" value="edit_xml_file" />';
108 $o.= ' <input type="hidden" name ="path" value="' . s($structure->getPath()) .'" />';
109 $o.= ' <input type="hidden" name ="version" value="' . s($structure->getVersion()) .'" />';
110 $o.= ' <table id="formelements" class="boxaligncenter">';
111 $o.= ' <tr valign="top"><td>Path:</td><td>' . s($structure->getPath()) . '</td></tr>';
112 $o.= ' <tr valign="top"><td>Version:</td><td>' . s($structure->getVersion()) . '</td></tr>';
113 $o.= ' <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td><textarea name="comment" rows="3" cols="80" id="comment">' . $structure->getComment() . '</textarea></td></tr>';
114 $o.= ' <tr><td> </td><td><input type="submit" value="' .$this->str
['change'] . '" /></td></tr>';
116 $o.= '</div></form>';
117 /// Calculate the buttons
118 $b = ' <p class="centerpara buttons">';
119 /// The view original XML button
120 $b .= ' <a href="index.php?action=view_structure_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&select=original">[' . $this->str
['vieworiginal'] . ']</a>';
121 /// The view edited XML button
122 if ($structure->hasChanged()) {
123 $b .= ' <a href="index.php?action=view_structure_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&select=edited">[' . $this->str
['viewedited'] . ']</a>';
125 $b .= ' [' . $this->str
['viewedited'] . ']';
127 /// The new table button
128 $b .= ' <a href="index.php?action=new_table&postaction=edit_table&table=changeme&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['newtable'] . ']</a>';
129 /// The new from MySQL button
130 if ($CFG->dbfamily
== 'mysql') {
131 $b .= ' <a href="index.php?action=new_table_from_mysql&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['newtablefrommysql'] . ']</a>';
133 $b .= ' [' . $this->str
['newtablefrommysql'] . ']';
135 /// The new statement button
136 $b .= ' <a href="index.php?action=new_statement&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['newstatement'] . ']</a>';
137 /// The back to main menu button
138 $b .= ' <a href="index.php?action=main_view#lastused">[' . $this->str
['backtomainview'] . ']</a>';
140 $b .= ' <p class="centerpara buttons">';
141 /// The view sql code button
142 $b .= '<a href="index.php?action=view_structure_sql&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' .$this->str
['viewsqlcode'] . ']</a>';
143 /// The view php code button
144 $b .= ' <a href="index.php?action=view_structure_php&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['viewphpcode'] . ']</a>';
147 /// Join all the reserved words into one big array
148 /// Calculate list of available SQL generators
149 $plugins = get_list_of_plugins('lib/xmldb/classes/generators');
150 $reserved_words = array();
151 foreach($plugins as $plugin) {
152 $classname = 'XMLDB' . $plugin;
153 $generator = new $classname();
154 $reserved_words = array_merge($reserved_words, $generator->getReservedWords());
156 sort($reserved_words);
157 $reserved_words = array_unique($reserved_words);
158 /// Add the tables list
159 $tables =& $structure->getTables();
161 $o .= '<h3 class="main">' . $this->str
['tables'] . '</h3>';
162 $o .= '<table id="listtables" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
164 foreach ($tables as $table) {
165 /// Calculate buttons
166 $b = '</td><td class="button cell">';
168 $b .= '<a href="index.php?action=edit_table&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['edit'] . ']</a>';
169 $b .= '</td><td class="button cell">';
171 if ($table->getPrevious()) {
172 $b .= '<a href="index.php?action=move_updown_table&direction=up&table=' . $table->getName() . '&postaction=edit_xml_file' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['up'] . ']</a>';
174 $b .= '[' . $this->str
['up'] . ']';
176 $b .= '</td><td class="button cell">';
178 if ($table->getNext()) {
179 $b .= '<a href="index.php?action=move_updown_table&direction=down&table=' . $table->getName() . '&postaction=edit_xml_file' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['down'] . ']</a>';
181 $b .= '[' . $this->str
['down'] . ']';
183 $b .= '</td><td class="button cell">';
184 /// The delete button (if we have more than one and it isn't used)
185 if (count($tables) > 1 &&
186 !$structure->getTableUses($table->getName())) {
187 ///!$structure->getTableUses($table->getName())) {
188 $b .= '<a href="index.php?action=delete_table&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['delete'] . ']</a>';
190 $b .= '[' . $this->str
['delete'] . ']';
192 /// Detect if the table name is a reserved word
193 if (in_array($table->getName(), $reserved_words)) {
194 $b .= ' <a href="index.php?action=view_reserved_words"><span class="error">' . $this->str
['reserved'] . '</span></a>';
198 $o .= '<tr class="r' . $row . '"><td class="table cell"><a href="index.php?action=view_table_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&table=' . $table->getName() . '&select=edited">' . $table->getName() . '</a>' . $b . '</tr>';
199 $row = ($row +
1) %
2;
203 ///Add the statements list
204 $statements =& $structure->getStatements();
206 $o .= '<h3 class="main">' . $this->str
['statements'] . '</h3>';
207 $o .= '<table id="liststatements" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
209 foreach ($statements as $statement) {
210 /// Calculate buttons
211 $b = '</td><td class="button cell">';
213 $b .= '<a href="index.php?action=edit_statement&statement=' . $statement->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['edit'] . ']</a>';
214 $b .= '</td><td class="button cell">';
216 if ($statement->getPrevious()) {
217 $b .= '<a href="index.php?action=move_updown_statement&direction=up&statement=' . $statement->getName() . '&postaction=edit_xml_file' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['up'] . ']</a>';
219 $b .= '[' . $this->str
['up'] . ']';
221 $b .= '</td><td class="button cell">';
223 if ($statement->getNext()) {
224 $b .= '<a href="index.php?action=move_updown_statement&direction=down&statement=' . $statement->getName() . '&postaction=edit_xml_file' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['down'] . ']</a>';
226 $b .= '[' . $this->str
['down'] . ']';
228 $b .= '</td><td class="button cell">';
229 /// The delete button
230 $b .= '<a href="index.php?action=delete_statement&statement=' . $statement->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['delete'] . ']</a>';
232 /// Print statement row
233 $o .= '<tr class="r' . $row . '"><td class="statement cell"><a href="index.php?action=view_statement_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&statement=' . $statement->getName() . '&select=edited">' . $statement->getName() . '</a>' . $b . '</tr>';
234 $row = ($row +
1) %
2;
238 ///Add the back to main
245 /// Launch postaction if exists (leave this unmodified)
246 if ($this->getPostAction() && $result) {
247 return $this->launch($this->getPostAction());