b0c7d558 |
1 | <?php // $Id$ |
2 | |
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 | /////////////////////////////////////////////////////////////////////////// |
26 | |
27 | /// This class will show the PHP needed to perform the desired action |
28 | /// with the specified table. |
29 | |
30 | class view_structure_php extends XMLDBAction { |
31 | |
32 | /** |
33 | * Init method, every subclass will have its own |
34 | */ |
35 | function init() { |
36 | parent::init(); |
37 | |
38 | /// Set own custom attributes |
39 | |
40 | /// Get needed strings |
41 | $this->loadStrings(array( |
42 | 'selectaction' => 'xmldb', |
43 | 'selecttable' => 'xmldb', |
44 | 'view' => 'xmldb', |
45 | 'back' => 'xmldb' |
46 | )); |
47 | } |
48 | |
49 | /** |
50 | * Invoke method, every class will have its own |
51 | * returns true/false on completion, setting both |
52 | * errormsg and output as necessary |
53 | */ |
54 | function invoke() { |
55 | parent::invoke(); |
56 | |
57 | $result = true; |
58 | |
59 | /// Set own core attributes |
60 | $this->does_generate = ACTION_GENERATE_HTML; |
61 | |
62 | /// These are always here |
63 | global $CFG, $XMLDB; |
64 | |
65 | /// Do the job, setting result as needed |
66 | /// Get the dir containing the file |
67 | $dirpath = required_param('dir', PARAM_PATH); |
68 | $dirpath = $CFG->dirroot . stripslashes_safe($dirpath); |
69 | |
70 | /// Get the correct dirs |
71 | if (!empty($XMLDB->dbdirs)) { |
72 | $dbdir =& $XMLDB->dbdirs[$dirpath]; |
73 | } else { |
74 | return false; |
75 | } |
76 | if (!empty($XMLDB->editeddirs)) { |
77 | $editeddir =& $XMLDB->editeddirs[$dirpath]; |
78 | $structure =& $editeddir->xml_file->getStructure(); |
79 | } |
80 | /// ADD YOUR CODE HERE |
81 | |
82 | $tables =& $structure->getTables(); |
83 | $table = reset($tables); |
84 | $defaulttable = null; |
85 | if ($table) { |
86 | $defaulttable = $table->getName(); |
87 | } |
88 | |
89 | /// Get parameters |
90 | $commandparam = optional_param('command', 'create_table', PARAM_PATH); |
91 | $tableparam = optional_param('table', $defaulttable, PARAM_PATH); |
92 | |
93 | /// The back to edit xml button |
94 | $b = ' <p align="center" class="buttons">'; |
95 | $b .= '<a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; |
96 | $b .= '</p>'; |
97 | $o = $b; |
98 | |
99 | /// Calculate the popup of commands |
100 | $commands = array('create_table', |
101 | 'drop_table', |
102 | 'rename_table'); |
103 | foreach ($commands as $command) { |
104 | $popcommands[$command] = str_replace('_', ' ', $command); |
105 | } |
106 | /// Calculate the popup of tables |
107 | foreach ($tables as $table) { |
108 | $poptables[$table->getName()] = $table->getName(); |
109 | } |
110 | /// Now build the form |
111 | $o.= '<form id="form" action="index.php" method="post">'; |
112 | $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />'; |
113 | $o.= ' <input type="hidden" name ="action" value="view_structure_php" />'; |
114 | $o.= ' <table id="formelements" align="center" cellpadding="5">'; |
115 | $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . ' <label for="table" accesskey="t">' . $this->str['selecttable'] . ' </label>' .choose_from_menu($poptables, 'table', $tableparam, '', '', 0, true) . '</td></tr>'; |
116 | $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>'; |
117 | $o.= ' </table>'; |
118 | $o.= '</form>'; |
119 | $o.= ' <table id="phpcode" align="center" cellpadding="5">'; |
120 | $o.= ' <tr><td><textarea cols="80" rows="32">'; |
121 | /// Based on current params, call the needed function |
122 | switch ($commandparam) { |
123 | case 'create_table': |
124 | $o.= s($this->create_table_php($structure, $tableparam)); |
125 | break; |
126 | case 'drop_table': |
127 | $o.= s($this->drop_table_php($structure, $tableparam)); |
128 | break; |
129 | case 'rename_table': |
130 | $o.= s($this->rename_table_php($structure, $tableparam)); |
131 | break; |
132 | } |
133 | $o.= '</textarea></td></tr>'; |
134 | $o.= ' </table>'; |
135 | |
136 | $this->output = $o; |
137 | |
138 | /// Launch postaction if exists (leave this here!) |
139 | if ($this->getPostAction() && $result) { |
140 | return $this->launch($this->getPostAction()); |
141 | } |
142 | |
143 | /// Return ok if arrived here |
144 | return $result; |
145 | } |
146 | |
147 | /** |
148 | * This function will generate all the PHP code needed to |
149 | * create one table using XMLDB objects and functions |
150 | * |
151 | * @param XMLDBStructure structure object containing all the info |
152 | * @param string table table code to be created |
153 | * @return string PHP code to be used to create the table |
154 | */ |
155 | function create_table_php($structure, $table) { |
156 | |
157 | $result = ''; |
158 | /// Validate if we can do it |
3ab430fb |
159 | if (!$table = $structure->getTable($table)) { |
160 | return false; |
161 | } |
162 | if ($table->getAllErrors()) { |
163 | return false; |
164 | } |
b0c7d558 |
165 | |
166 | /// Add the standard PHP header |
167 | $result .= XMLDB_PHP_HEADER; |
168 | |
169 | /// Add contents |
3ab430fb |
170 | $result .= XMLDB_LINEFEED; |
cb78437d |
171 | $result .= ' /// Define table ' . $table->getName() . ' to be created' . XMLDB_LINEFEED; |
3ab430fb |
172 | $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED; |
173 | $result .= XMLDB_LINEFEED; |
174 | $result .= ' /// Adding fields to table ' . $table->getName() . XMLDB_LINEFEED; |
175 | /// Iterate over each field |
176 | foreach ($table->getFields() as $field) { |
177 | /// The field header, with name |
178 | $result .= ' $table->addFieldInfo(' . "'" . $field->getName() . "', "; |
179 | /// The field PHP specs |
180 | $result .= $field->getPHP(false); |
181 | /// The end of the line |
182 | $result .= ');' . XMLDB_LINEFEED; |
183 | } |
184 | /// Iterate over each key |
185 | if ($keys = $table->getKeys()) { |
186 | $result .= XMLDB_LINEFEED; |
187 | $result .= ' /// Adding keys to table ' . $table->getName() . XMLDB_LINEFEED; |
188 | foreach ($keys as $key) { |
189 | /// The key header, with name |
190 | $result .= ' $table->addKeyInfo(' . "'" . $key->getName() . "', "; |
191 | /// The key PHP specs |
192 | $result .= $key->getPHP(); |
193 | /// The end of the line |
194 | $result .= ');' . XMLDB_LINEFEED; |
195 | } |
196 | } |
197 | /// Iterate over each index |
198 | if ($indexes = $table->getIndexes()) { |
199 | $result .= XMLDB_LINEFEED; |
200 | $result .= ' /// Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED; |
201 | foreach ($indexes as $index) { |
202 | /// The index header, with name |
203 | $result .= ' $table->addIndexInfo(' . "'" . $index->getName() . "', "; |
204 | /// The index PHP specs |
205 | $result .= $index->getPHP(); |
206 | /// The end of the line |
207 | $result .= ');' . XMLDB_LINEFEED; |
208 | } |
209 | } |
210 | |
211 | /// Launch the proper DDL |
212 | $result .= XMLDB_LINEFEED; |
213 | $result .= ' /// Launch create table for ' . $table->getName() . XMLDB_LINEFEED; |
214 | $result .= ' $status = $status && create_table($table);' . XMLDB_LINEFEED; |
b0c7d558 |
215 | |
216 | /// Add standard PHP footer |
217 | $result .= XMLDB_PHP_FOOTER; |
218 | |
219 | return $result; |
220 | } |
221 | |
222 | /** |
223 | * This function will generate all the PHP code needed to |
224 | * drop one table using XMLDB objects and functions |
225 | * |
226 | * @param XMLDBStructure structure object containing all the info |
227 | * @param string table table code to be dropped |
228 | * @return string PHP code to be used to drop the table |
229 | */ |
230 | function drop_table_php($structure, $table) { |
231 | |
232 | $result = ''; |
233 | /// Validate if we can do it |
3ab430fb |
234 | if (!$table = $structure->getTable($table)) { |
235 | return false; |
236 | } |
237 | if ($table->getAllErrors()) { |
238 | return false; |
239 | } |
b0c7d558 |
240 | |
241 | /// Add the standard PHP header |
242 | $result .= XMLDB_PHP_HEADER; |
243 | |
244 | /// Add contents |
386c23c9 |
245 | $result .= XMLDB_LINEFEED; |
cb78437d |
246 | $result .= ' /// Define table ' . $table->getName() . ' to be dropped' . XMLDB_LINEFEED; |
386c23c9 |
247 | $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED; |
248 | |
249 | /// Launch the proper DDL |
250 | $result .= XMLDB_LINEFEED; |
251 | $result .= ' /// Launch drop table for ' . $table->getName() . XMLDB_LINEFEED; |
252 | $result .= ' $status = $status && drop_table($table);' . XMLDB_LINEFEED; |
b0c7d558 |
253 | |
254 | /// Add standard PHP footer |
255 | $result .= XMLDB_PHP_FOOTER; |
256 | |
257 | return $result; |
258 | } |
259 | |
260 | /** |
261 | * This function will generate all the PHP code needed to |
262 | * rename one table using XMLDB objects and functions |
263 | * |
264 | * @param XMLDBStructure structure object containing all the info |
265 | * @param string table table code to be renamed |
266 | * @return string PHP code to be used to rename the table |
267 | */ |
268 | function rename_table_php($structure, $table) { |
269 | |
270 | $result = ''; |
271 | /// Validate if we can do it |
3ab430fb |
272 | if (!$table = $structure->getTable($table)) { |
273 | return false; |
274 | } |
275 | if ($table->getAllErrors()) { |
276 | return false; |
277 | } |
b0c7d558 |
278 | |
279 | /// Add the standard PHP header |
280 | $result .= XMLDB_PHP_HEADER; |
281 | |
282 | /// Add contents |
386c23c9 |
283 | $result .= XMLDB_LINEFEED; |
cb78437d |
284 | $result .= ' /// Define table ' . $table->getName() . ' to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED; |
386c23c9 |
285 | $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED; |
286 | |
287 | /// Launch the proper DDL |
288 | $result .= XMLDB_LINEFEED; |
289 | $result .= ' /// Launch rename table for ' . $table->getName() . XMLDB_LINEFEED; |
290 | $result .= ' $status = $status && rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED; |
b0c7d558 |
291 | |
292 | /// Add standard PHP footer |
293 | $result .= XMLDB_PHP_FOOTER; |
294 | |
295 | return $result; |
296 | } |
297 | } |
298 | ?> |