Ops! I forgot to commit this 2 days ago! Now a lot of
[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
eef868d1 32 /**
c5a6ae17 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);
eef868d1 85
c5a6ae17 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);
3be91b01 98 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
99 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///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
eef868d1 111 $commands = array('add_field',
c5a6ae17 112 'drop_field',
1934b522 113 'rename_field (not imp!)',
114 'change_field_type (not imp!)',
115 'change_field_precision',
116 'change_field_unsigned',
117 'change_field_notnull',
118 'change_field_sequence (not imp!)',
119 'change_field_enum (not imp!)',
120 'change_field_default');
c5a6ae17 121 foreach ($commands as $command) {
122 $popcommands[$command] = str_replace('_', ' ', $command);
123 }
eafc2b70 124 /// Calculate the popup of fields/keys/indexes
125 $optionspacer = '&nbsp;&nbsp;&nbsp;';
126 if ($fields) {
3be91b01 127 $popfields['fieldshead'] = 'Fields';
eafc2b70 128 foreach ($fields as $field) {
3be91b01 129 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 130 }
131 }
132 if ($keys) {
3be91b01 133 $popfields['keyshead'] = 'Keys';
eafc2b70 134 foreach ($keys as $key) {
3be91b01 135 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 136 }
137 }
138 if ($indexes) {
3be91b01 139 $popfields['indexeshead'] = 'Indexes';
eafc2b70 140 foreach ($indexes as $index) {
3be91b01 141 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 142 }
c5a6ae17 143 }
eafc2b70 144
c5a6ae17 145 /// Now build the form
146 $o.= '<form id="form" action="index.php" method="post">';
147 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 148 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 149 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
150 $o.= ' <table id="formelements" align="center" cellpadding="5">';
3be91b01 151 $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($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
c5a6ae17 152 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
153 $o.= ' </table>';
154 $o.= '</form>';
eafc2b70 155
c5a6ae17 156 $o.= ' <table id="phpcode" align="center" cellpadding="5">';
157 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 158 /// Check we have selected some field/key/index from the popup
159 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
160 $o.= s($this->str['selectonefieldkeyindex']);
161 } else {
162 /// Based on current params, call the needed function
163 switch ($commandparam) {
3be91b01 164 case 'add_field':
165 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
eafc2b70 166 break;
3be91b01 167 case 'drop_field':
168 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
eafc2b70 169 break;
3be91b01 170 case 'rename_field':
171 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
eafc2b70 172 break;
1934b522 173 case 'change_field_precision':
174 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
175 break;
176 case 'change_field_unsigned':
177 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
178 break;
179 case 'change_field_notnull':
180 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
181 break;
182 case 'change_field_default':
183 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
184 break;
eafc2b70 185 }
c5a6ae17 186 }
187 $o.= '</textarea></td></tr>';
188 $o.= ' </table>';
189
190 $this->output = $o;
191
192 /// Launch postaction if exists (leave this here!)
eef868d1 193 if ($this->getPostAction() && $result) {
c5a6ae17 194 return $this->launch($this->getPostAction());
195 }
196
197 /// Return ok if arrived here
198 return $result;
199 }
200
201 /**
eef868d1 202 * This function will generate all the PHP code needed to
3be91b01 203 * create one field using XMLDB objects and functions
eef868d1 204 *
c5a6ae17 205 * @param XMLDBStructure structure object containing all the info
3be91b01 206 * @param string table table name
207 * @param string field field name to be created
208 * @return string PHP code to be used to create the field
c5a6ae17 209 */
3be91b01 210 function add_field_php($structure, $table, $field) {
c5a6ae17 211
212 $result = '';
213 /// Validate if we can do it
214 if (!$table = $structure->getTable($table)) {
215 return false;
216 }
3be91b01 217 if (!$field = $table->getField($field)) {
218 return false;
219 }
c5a6ae17 220 if ($table->getAllErrors()) {
221 return false;
222 }
223
224 /// Add the standard PHP header
225 $result .= XMLDB_PHP_HEADER;
226
227 /// Add contents
228 $result .= XMLDB_LINEFEED;
3be91b01 229 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 230 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 231 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
232 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 233
234 /// Launch the proper DDL
235 $result .= XMLDB_LINEFEED;
3be91b01 236 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
237 $result .= ' $status = $status && add_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 238
239 /// Add standard PHP footer
240 $result .= XMLDB_PHP_FOOTER;
241
242 return $result;
243 }
244
245 /**
eef868d1 246 * This function will generate all the PHP code needed to
3be91b01 247 * drop one field using XMLDB objects and functions
eef868d1 248 *
c5a6ae17 249 * @param XMLDBStructure structure object containing all the info
3be91b01 250 * @param string table table name
251 * @param string field field name to be dropped
252 * @return string PHP code to be used to drop the field
c5a6ae17 253 */
3be91b01 254 function drop_field_php($structure, $table, $field) {
c5a6ae17 255
256 $result = '';
257 /// Validate if we can do it
258 if (!$table = $structure->getTable($table)) {
259 return false;
260 }
3be91b01 261 if (!$field = $table->getField($field)) {
262 return false;
263 }
c5a6ae17 264 if ($table->getAllErrors()) {
265 return false;
266 }
267
268 /// Add the standard PHP header
269 $result .= XMLDB_PHP_HEADER;
270
271 /// Add contents
272 $result .= XMLDB_LINEFEED;
3be91b01 273 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 274 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 275 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 276
277 /// Launch the proper DDL
278 $result .= XMLDB_LINEFEED;
3be91b01 279 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
280 $result .= ' $status = $status && drop_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 281
282 /// Add standard PHP footer
283 $result .= XMLDB_PHP_FOOTER;
284
285 return $result;
286 }
287
288 /**
eef868d1 289 * This function will generate all the PHP code needed to
3be91b01 290 * rename one field using XMLDB objects and functions
eef868d1 291 *
c5a6ae17 292 * @param XMLDBStructure structure object containing all the info
3be91b01 293 * @param string table table name
294 * @param string field field name to be renamed
295 * @return string PHP code to be used to drop the field
c5a6ae17 296 */
3be91b01 297 function rename_field_php($structure, $table, $field) {
c5a6ae17 298
299 $result = '';
300 /// Validate if we can do it
301 if (!$table = $structure->getTable($table)) {
302 return false;
303 }
3be91b01 304 if (!$field = $table->getField($field)) {
305 return false;
306 }
c5a6ae17 307 if ($table->getAllErrors()) {
308 return false;
309 }
310
311 /// Add the standard PHP header
312 $result .= XMLDB_PHP_HEADER;
313
314 /// Add contents
315 $result .= XMLDB_LINEFEED;
3be91b01 316 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
c5a6ae17 317 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 318 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 319
320 /// Launch the proper DDL
321 $result .= XMLDB_LINEFEED;
3be91b01 322 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
323 $result .= ' $status = $status && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 324
325 /// Add standard PHP footer
326 $result .= XMLDB_PHP_FOOTER;
327
328 return $result;
329 }
3be91b01 330
1934b522 331 /**
332 * This function will generate all the PHP code needed to
333 * change the precision of one field using XMLDB objects and functions
334 *
335 * @param XMLDBStructure structure object containing all the info
336 * @param string table table name
337 * @param string field field name to change precision
338 */
339 function change_field_precision_php($structure, $table, $field) {
340
341 $result = '';
342 /// Validate if we can do it
343 if (!$table = $structure->getTable($table)) {
344 return false;
345 }
346 if (!$field = $table->getField($field)) {
347 return false;
348 }
349 if ($table->getAllErrors()) {
350 return false;
351 }
352
353 /// Calculate the precision tip text
354 $precision = '(' . $field->getLength();
355 if ($field->getDecimals()) {
356 $precision .= ', ' . $field->getDecimals();
357 }
358 $precision .= ')';
359
360 /// Add the standard PHP header
361 $result .= XMLDB_PHP_HEADER;
362
363 /// Add contents
364 $result .= XMLDB_LINEFEED;
365 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
366 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
367 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
368 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
369
370 /// Launch the proper DDL
371 $result .= XMLDB_LINEFEED;
372 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
373 $result .= ' $status = $status && change_field_precision($table, $field);' . XMLDB_LINEFEED;
374
375 /// Add standard PHP footer
376 $result .= XMLDB_PHP_FOOTER;
377
378 return $result;
379 }
380
381 /**
382 * This function will generate all the PHP code needed to
383 * change the unsigned/signed of one field using XMLDB objects and functions
384 *
385 * @param XMLDBStructure structure object containing all the info
386 * @param string table table name
387 * @param string field field name to change unsigned/signed
388 */
389 function change_field_unsigned_php($structure, $table, $field) {
390
391 $result = '';
392 /// Validate if we can do it
393 if (!$table = $structure->getTable($table)) {
394 return false;
395 }
396 if (!$field = $table->getField($field)) {
397 return false;
398 }
399 if ($table->getAllErrors()) {
400 return false;
401 }
402
403 /// Calculate the unsigned tip text
404 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
405
406 /// Add the standard PHP header
407 $result .= XMLDB_PHP_HEADER;
408
409 /// Add contents
410 $result .= XMLDB_LINEFEED;
411 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
412 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
413 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
414 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
415
416 /// Launch the proper DDL
417 $result .= XMLDB_LINEFEED;
418 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
419 $result .= ' $status = $status && change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
420
421 /// Add standard PHP footer
422 $result .= XMLDB_PHP_FOOTER;
423
424 return $result;
425 }
426
427 /**
428 * This function will generate all the PHP code needed to
429 * change the nullability of one field using XMLDB objects and functions
430 *
431 * @param XMLDBStructure structure object containing all the info
432 * @param string table table name
433 * @param string field field name to change null/not null
434 */
435 function change_field_notnull_php($structure, $table, $field) {
436
437 $result = '';
438 /// Validate if we can do it
439 if (!$table = $structure->getTable($table)) {
440 return false;
441 }
442 if (!$field = $table->getField($field)) {
443 return false;
444 }
445 if ($table->getAllErrors()) {
446 return false;
447 }
448
449 /// Calculate the notnull tip text
450 $notnull = $field->getNotnull() ? 'not null' : 'null';
451
452 /// Add the standard PHP header
453 $result .= XMLDB_PHP_HEADER;
454
455 /// Add contents
456 $result .= XMLDB_LINEFEED;
457 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
458 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
459 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
460 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
461
462 /// Launch the proper DDL
463 $result .= XMLDB_LINEFEED;
464 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
465 $result .= ' $status = $status && change_field_notnull($table, $field);' . XMLDB_LINEFEED;
466
467 /// Add standard PHP footer
468 $result .= XMLDB_PHP_FOOTER;
469
470 return $result;
471 }
472
473 /**
474 * This function will generate all the PHP code needed to
475 * change the default of one field using XMLDB objects and functions
476 *
477 * @param XMLDBStructure structure object containing all the info
478 * @param string table table name
479 * @param string field field name to change null/not null
480 */
481 function change_field_default_php($structure, $table, $field) {
482
483 $result = '';
484 /// Validate if we can do it
485 if (!$table = $structure->getTable($table)) {
486 return false;
487 }
488 if (!$field = $table->getField($field)) {
489 return false;
490 }
491 if ($table->getAllErrors()) {
492 return false;
493 }
494
495 /// Calculate the default tip text
496 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
497
498 /// Add the standard PHP header
499 $result .= XMLDB_PHP_HEADER;
500
501 /// Add contents
502 $result .= XMLDB_LINEFEED;
503 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
504 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
505 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
506 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
507
508 /// Launch the proper DDL
509 $result .= XMLDB_LINEFEED;
510 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
511 $result .= ' $status = $status && change_field_default($table, $field);' . XMLDB_LINEFEED;
512
513 /// Add standard PHP footer
514 $result .= XMLDB_PHP_FOOTER;
515
516 return $result;
517 }
518
c5a6ae17 519}
520?>