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