Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / xmldb / actions / edit_xml_file / edit_xml_file.class.php
blob734c6725dcdaacb43d45f6791fd2e0ef2a504b41
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) 2001-3001 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 edit one loaded XML file
29 class edit_xml_file 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 'change' => 'xmldb',
42 'edit' => 'xmldb',
43 'up' => 'xmldb',
44 'down' => 'xmldb',
45 'delete' => 'xmldb',
46 'vieworiginal' => 'xmldb',
47 'viewedited' => 'xmldb',
48 'tables' => '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'
57 ));
60 /**
61 * Invoke method, every class will have its own
62 * returns true/false on completion, setting both
63 * errormsg and output as necessary
65 function invoke() {
66 parent::invoke();
68 $result = true;
70 /// Set own core attributes
71 //$this->does_generate = ACTION_NONE;
72 $this->does_generate = ACTION_GENERATE_HTML;
74 /// These are always here
75 global $CFG, $XMLDB;
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];
86 if ($dbdir) {
87 /// Only if the directory exists and it has been loaded
88 if (!$dbdir->path_exists || !$dbdir->xml_loaded) {
89 return false;
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));
99 /// Get it
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">';
104 $o .= '<div>';
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>&nbsp;</td><td><input type="submit" value="' .$this->str['change'] . '" /></td></tr>';
115 $o.= ' </table>';
116 $o.= '</div></form>';
117 /// Calculate the buttons
118 $b = ' <p class="centerpara buttons">';
119 /// The view original XML button
120 $b .= '&nbsp;<a href="index.php?action=view_structure_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original">[' . $this->str['vieworiginal'] . ']</a>';
121 /// The view edited XML button
122 if ($structure->hasChanged()) {
123 $b .= '&nbsp;<a href="index.php?action=view_structure_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited">[' . $this->str['viewedited'] . ']</a>';
124 } else {
125 $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
127 /// The new table button
128 $b .= '&nbsp;<a href="index.php?action=new_table&amp;postaction=edit_table&amp;table=changeme&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newtable'] . ']</a>';
129 /// The new from MySQL button
130 if ($CFG->dbfamily == 'mysql') {
131 $b .= '&nbsp;<a href="index.php?action=new_table_from_mysql&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newtablefrommysql'] . ']</a>';
132 } else {
133 $b .= '&nbsp;[' . $this->str['newtablefrommysql'] . ']';
135 /// The new statement button
136 $b .= '&nbsp;<a href="index.php?action=new_statement&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newstatement'] . ']</a>';
137 /// The back to main menu button
138 $b .= '&nbsp;<a href="index.php?action=main_view#lastused">[' . $this->str['backtomainview'] . ']</a>';
139 $b .= '</p>';
140 $b .= ' <p class="centerpara buttons">';
141 /// The view sql code button
142 $b .= '<a href="index.php?action=view_structure_sql&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' .$this->str['viewsqlcode'] . ']</a>';
143 /// The view php code button
144 $b .= '&nbsp;<a href="index.php?action=view_structure_php&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['viewphpcode'] . ']</a>';
145 $b .= '</p>';
146 $o .= $b;
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();
160 if ($tables) {
161 $o .= '<h3 class="main">' . $this->str['tables'] . '</h3>';
162 $o .= '<table id="listtables" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
163 $row = 0;
164 foreach ($tables as $table) {
165 /// Calculate buttons
166 $b = '</td><td class="button cell">';
167 /// The edit button
168 $b .= '<a href="index.php?action=edit_table&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
169 $b .= '</td><td class="button cell">';
170 /// The up button
171 if ($table->getPrevious()) {
172 $b .= '<a href="index.php?action=move_updown_table&amp;direction=up&amp;table=' . $table->getName() . '&amp;postaction=edit_xml_file' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>';
173 } else {
174 $b .= '[' . $this->str['up'] . ']';
176 $b .= '</td><td class="button cell">';
177 /// The down button
178 if ($table->getNext()) {
179 $b .= '<a href="index.php?action=move_updown_table&amp;direction=down&amp;table=' . $table->getName() . '&amp;postaction=edit_xml_file' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>';
180 } else {
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&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
189 } else {
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 .= '&nbsp;<a href="index.php?action=view_reserved_words"><span class="error">' . $this->str['reserved'] . '</span></a>';
196 $b .= '</td>';
197 /// Print table row
198 $o .= '<tr class="r' . $row . '"><td class="table cell"><a href="index.php?action=view_table_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $table->getName() . '&amp;select=edited">' . $table->getName() . '</a>' . $b . '</tr>';
199 $row = ($row + 1) % 2;
201 $o .= '</table>';
203 ///Add the statements list
204 $statements =& $structure->getStatements();
205 if ($statements) {
206 $o .= '<h3 class="main">' . $this->str['statements'] . '</h3>';
207 $o .= '<table id="liststatements" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
208 $row = 0;
209 foreach ($statements as $statement) {
210 /// Calculate buttons
211 $b = '</td><td class="button cell">';
212 /// The edit button
213 $b .= '<a href="index.php?action=edit_statement&amp;statement=' . $statement->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
214 $b .= '</td><td class="button cell">';
215 /// The up button
216 if ($statement->getPrevious()) {
217 $b .= '<a href="index.php?action=move_updown_statement&amp;direction=up&amp;statement=' . $statement->getName() . '&amp;postaction=edit_xml_file' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>';
218 } else {
219 $b .= '[' . $this->str['up'] . ']';
221 $b .= '</td><td class="button cell">';
222 /// The down button
223 if ($statement->getNext()) {
224 $b .= '<a href="index.php?action=move_updown_statement&amp;direction=down&amp;statement=' . $statement->getName() . '&amp;postaction=edit_xml_file' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>';
225 } else {
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&amp;statement=' . $statement->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
231 $b .= '</td>';
232 /// Print statement row
233 $o .= '<tr class="r' . $row . '"><td class="statement cell"><a href="index.php?action=view_statement_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;statement=' . $statement->getName() . '&amp;select=edited">' . $statement->getName() . '</a>' . $b . '</tr>';
234 $row = ($row + 1) % 2;
236 $o .= '</table>';
238 ///Add the back to main
241 $this->output = $o;
245 /// Launch postaction if exists (leave this unmodified)
246 if ($this->getPostAction() && $result) {
247 return $this->launch($this->getPostAction());
250 return $result;