Replaced XMLDB own implementation of the concat function to use
[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',
313b5a35 46 'selectonecommand' => 'xmldb',
eafc2b70 47 'selectonefieldkeyindex' => 'xmldb',
313b5a35 48 'mustselectonefield' => 'xmldb',
49 'mustselectonekey' => 'xmldb',
50 'mustselectoneindex' => 'xmldb',
c5a6ae17 51 'back' => 'xmldb'
52 ));
53 }
54
55 /**
56 * Invoke method, every class will have its own
57 * returns true/false on completion, setting both
58 * errormsg and output as necessary
59 */
60 function invoke() {
61 parent::invoke();
62
63 $result = true;
64
65 /// Set own core attributes
66 $this->does_generate = ACTION_GENERATE_HTML;
67
68 /// These are always here
69 global $CFG, $XMLDB;
70
71 /// Do the job, setting result as needed
72 /// Get the dir containing the file
73 $dirpath = required_param('dir', PARAM_PATH);
74 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
75
76 /// Get the correct dirs
77 if (!empty($XMLDB->dbdirs)) {
78 $dbdir =& $XMLDB->dbdirs[$dirpath];
79 } else {
80 return false;
81 }
82 if (!empty($XMLDB->editeddirs)) {
83 $editeddir =& $XMLDB->editeddirs[$dirpath];
84 $structure =& $editeddir->xml_file->getStructure();
85 }
86 /// ADD YOUR CODE HERE
87
88 $tableparam = required_param('table', PARAM_PATH);
eef868d1 89
c5a6ae17 90 $table =& $structure->getTable($tableparam);
91 $fields = $table->getFields();
92 $field = reset($fields);
93 $defaultfieldkeyindex = null;
94 if ($field) {
eafc2b70 95 $defaultfieldkeyindex = 'f#' . $field->getName();
c5a6ae17 96 }
eafc2b70 97 $keys = $table->getKeys();
98 $indexes = $table->getIndexes();
c5a6ae17 99
100 /// Get parameters
101 $commandparam = optional_param('command', 'add_field', PARAM_PATH);
3be91b01 102 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
103 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
313b5a35 104 $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
c5a6ae17 105
106 /// The back to edit xml button
107 $b = ' <p align="center" class="buttons">';
eafc2b70 108 $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
c5a6ae17 109 $b .= '</p>';
110 $o = $b;
111
eafc2b70 112 /// The table currently being edited
113 $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
114
313b5a35 115 /// To indent the menu selections
116 $optionspacer = '&nbsp;&nbsp;&nbsp;';
117
c5a6ae17 118 /// Calculate the popup of commands
313b5a35 119 $commands = array('Fields',
120 $optionspacer . 'add_field',
121 $optionspacer . 'drop_field',
122 $optionspacer . 'rename_field (not imp!)',
123 $optionspacer . 'change_field_type (not imp!)',
124 $optionspacer . 'change_field_precision',
125 $optionspacer . 'change_field_unsigned',
126 $optionspacer . 'change_field_notnull',
127 $optionspacer . 'change_field_sequence (not imp!)',
128 $optionspacer . 'change_field_enum (not imp!)',
129 $optionspacer . 'change_field_default',
130 'Keys',
131 'Indexes');
c5a6ae17 132 foreach ($commands as $command) {
313b5a35 133 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
c5a6ae17 134 }
eafc2b70 135 /// Calculate the popup of fields/keys/indexes
eafc2b70 136 if ($fields) {
3be91b01 137 $popfields['fieldshead'] = 'Fields';
eafc2b70 138 foreach ($fields as $field) {
3be91b01 139 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 140 }
141 }
142 if ($keys) {
3be91b01 143 $popfields['keyshead'] = 'Keys';
eafc2b70 144 foreach ($keys as $key) {
3be91b01 145 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 146 }
147 }
148 if ($indexes) {
3be91b01 149 $popfields['indexeshead'] = 'Indexes';
eafc2b70 150 foreach ($indexes as $index) {
3be91b01 151 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 152 }
c5a6ae17 153 }
eafc2b70 154
c5a6ae17 155 /// Now build the form
156 $o.= '<form id="form" action="index.php" method="post">';
157 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 158 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 159 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
160 $o.= ' <table id="formelements" align="center" cellpadding="5">';
3be91b01 161 $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 162 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
163 $o.= ' </table>';
164 $o.= '</form>';
eafc2b70 165
c5a6ae17 166 $o.= ' <table id="phpcode" align="center" cellpadding="5">';
167 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 168 /// Check we have selected some field/key/index from the popup
169 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
170 $o.= s($this->str['selectonefieldkeyindex']);
313b5a35 171 /// Check we have selected some command from the popup
172 } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
173 $o.= s($this->str['selectonecommand']);
174 } else {
eafc2b70 175 /// Based on current params, call the needed function
176 switch ($commandparam) {
3be91b01 177 case 'add_field':
313b5a35 178 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
179 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
180 } else {
181 $o.= $this->str['mustselectonefield'];
182 }
eafc2b70 183 break;
3be91b01 184 case 'drop_field':
313b5a35 185 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
186 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
187 } else {
188 $o.= $this->str['mustselectonefield'];
189 }
eafc2b70 190 break;
3be91b01 191 case 'rename_field':
313b5a35 192 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
193 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
194 } else {
195 $o.= $this->str['mustselectonefield'];
196 }
eafc2b70 197 break;
1934b522 198 case 'change_field_precision':
313b5a35 199 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
200 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
201 } else {
202 $o.= $this->str['mustselectonefield'];
203 }
1934b522 204 break;
205 case 'change_field_unsigned':
313b5a35 206 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
207 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
208 } else {
209 $o.= $this->str['mustselectonefield'];
210 }
1934b522 211 break;
212 case 'change_field_notnull':
313b5a35 213 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
214 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
215 } else {
216 $o.= $this->str['mustselectonefield'];
217 }
1934b522 218 break;
219 case 'change_field_default':
313b5a35 220 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
221 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
222 } else {
223 $o.= $this->str['mustselectonefield'];
224 }
1934b522 225 break;
eafc2b70 226 }
c5a6ae17 227 }
228 $o.= '</textarea></td></tr>';
229 $o.= ' </table>';
230
231 $this->output = $o;
232
233 /// Launch postaction if exists (leave this here!)
eef868d1 234 if ($this->getPostAction() && $result) {
c5a6ae17 235 return $this->launch($this->getPostAction());
236 }
237
238 /// Return ok if arrived here
239 return $result;
240 }
241
242 /**
eef868d1 243 * This function will generate all the PHP code needed to
3be91b01 244 * create one field using XMLDB objects and functions
eef868d1 245 *
c5a6ae17 246 * @param XMLDBStructure structure object containing all the info
3be91b01 247 * @param string table table name
248 * @param string field field name to be created
249 * @return string PHP code to be used to create the field
c5a6ae17 250 */
3be91b01 251 function add_field_php($structure, $table, $field) {
c5a6ae17 252
253 $result = '';
254 /// Validate if we can do it
255 if (!$table = $structure->getTable($table)) {
256 return false;
257 }
3be91b01 258 if (!$field = $table->getField($field)) {
259 return false;
260 }
c5a6ae17 261 if ($table->getAllErrors()) {
262 return false;
263 }
264
265 /// Add the standard PHP header
266 $result .= XMLDB_PHP_HEADER;
267
268 /// Add contents
269 $result .= XMLDB_LINEFEED;
3be91b01 270 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 271 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 272 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
273 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 274
275 /// Launch the proper DDL
276 $result .= XMLDB_LINEFEED;
3be91b01 277 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
278 $result .= ' $status = $status && add_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 279
280 /// Add standard PHP footer
281 $result .= XMLDB_PHP_FOOTER;
282
283 return $result;
284 }
285
286 /**
eef868d1 287 * This function will generate all the PHP code needed to
3be91b01 288 * drop one field using XMLDB objects and functions
eef868d1 289 *
c5a6ae17 290 * @param XMLDBStructure structure object containing all the info
3be91b01 291 * @param string table table name
292 * @param string field field name to be dropped
293 * @return string PHP code to be used to drop the field
c5a6ae17 294 */
3be91b01 295 function drop_field_php($structure, $table, $field) {
c5a6ae17 296
297 $result = '';
298 /// Validate if we can do it
299 if (!$table = $structure->getTable($table)) {
300 return false;
301 }
3be91b01 302 if (!$field = $table->getField($field)) {
303 return false;
304 }
c5a6ae17 305 if ($table->getAllErrors()) {
306 return false;
307 }
308
309 /// Add the standard PHP header
310 $result .= XMLDB_PHP_HEADER;
311
312 /// Add contents
313 $result .= XMLDB_LINEFEED;
3be91b01 314 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 315 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 316 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 317
318 /// Launch the proper DDL
319 $result .= XMLDB_LINEFEED;
3be91b01 320 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
321 $result .= ' $status = $status && drop_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 322
323 /// Add standard PHP footer
324 $result .= XMLDB_PHP_FOOTER;
325
326 return $result;
327 }
328
329 /**
eef868d1 330 * This function will generate all the PHP code needed to
3be91b01 331 * rename one field using XMLDB objects and functions
eef868d1 332 *
c5a6ae17 333 * @param XMLDBStructure structure object containing all the info
3be91b01 334 * @param string table table name
335 * @param string field field name to be renamed
336 * @return string PHP code to be used to drop the field
c5a6ae17 337 */
3be91b01 338 function rename_field_php($structure, $table, $field) {
c5a6ae17 339
340 $result = '';
341 /// Validate if we can do it
342 if (!$table = $structure->getTable($table)) {
343 return false;
344 }
3be91b01 345 if (!$field = $table->getField($field)) {
346 return false;
347 }
c5a6ae17 348 if ($table->getAllErrors()) {
349 return false;
350 }
351
352 /// Add the standard PHP header
353 $result .= XMLDB_PHP_HEADER;
354
355 /// Add contents
356 $result .= XMLDB_LINEFEED;
3be91b01 357 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
c5a6ae17 358 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 359 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 360
361 /// Launch the proper DDL
362 $result .= XMLDB_LINEFEED;
3be91b01 363 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
364 $result .= ' $status = $status && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 365
366 /// Add standard PHP footer
367 $result .= XMLDB_PHP_FOOTER;
368
369 return $result;
370 }
3be91b01 371
1934b522 372 /**
373 * This function will generate all the PHP code needed to
374 * change the precision of one field using XMLDB objects and functions
375 *
376 * @param XMLDBStructure structure object containing all the info
377 * @param string table table name
378 * @param string field field name to change precision
379 */
380 function change_field_precision_php($structure, $table, $field) {
381
382 $result = '';
383 /// Validate if we can do it
384 if (!$table = $structure->getTable($table)) {
385 return false;
386 }
387 if (!$field = $table->getField($field)) {
388 return false;
389 }
390 if ($table->getAllErrors()) {
391 return false;
392 }
393
394 /// Calculate the precision tip text
395 $precision = '(' . $field->getLength();
396 if ($field->getDecimals()) {
397 $precision .= ', ' . $field->getDecimals();
398 }
399 $precision .= ')';
400
401 /// Add the standard PHP header
402 $result .= XMLDB_PHP_HEADER;
403
404 /// Add contents
405 $result .= XMLDB_LINEFEED;
406 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
407 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
408 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
409 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
410
411 /// Launch the proper DDL
412 $result .= XMLDB_LINEFEED;
413 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
414 $result .= ' $status = $status && change_field_precision($table, $field);' . XMLDB_LINEFEED;
415
416 /// Add standard PHP footer
417 $result .= XMLDB_PHP_FOOTER;
418
419 return $result;
420 }
421
422 /**
423 * This function will generate all the PHP code needed to
424 * change the unsigned/signed of one field using XMLDB objects and functions
425 *
426 * @param XMLDBStructure structure object containing all the info
427 * @param string table table name
428 * @param string field field name to change unsigned/signed
429 */
430 function change_field_unsigned_php($structure, $table, $field) {
431
432 $result = '';
433 /// Validate if we can do it
434 if (!$table = $structure->getTable($table)) {
435 return false;
436 }
437 if (!$field = $table->getField($field)) {
438 return false;
439 }
440 if ($table->getAllErrors()) {
441 return false;
442 }
443
444 /// Calculate the unsigned tip text
445 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
446
447 /// Add the standard PHP header
448 $result .= XMLDB_PHP_HEADER;
449
450 /// Add contents
451 $result .= XMLDB_LINEFEED;
452 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
453 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
454 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
455 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
456
457 /// Launch the proper DDL
458 $result .= XMLDB_LINEFEED;
459 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
460 $result .= ' $status = $status && change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
461
462 /// Add standard PHP footer
463 $result .= XMLDB_PHP_FOOTER;
464
465 return $result;
466 }
467
468 /**
469 * This function will generate all the PHP code needed to
470 * change the nullability of one field using XMLDB objects and functions
471 *
472 * @param XMLDBStructure structure object containing all the info
473 * @param string table table name
474 * @param string field field name to change null/not null
475 */
476 function change_field_notnull_php($structure, $table, $field) {
477
478 $result = '';
479 /// Validate if we can do it
480 if (!$table = $structure->getTable($table)) {
481 return false;
482 }
483 if (!$field = $table->getField($field)) {
484 return false;
485 }
486 if ($table->getAllErrors()) {
487 return false;
488 }
489
490 /// Calculate the notnull tip text
491 $notnull = $field->getNotnull() ? 'not null' : 'null';
492
493 /// Add the standard PHP header
494 $result .= XMLDB_PHP_HEADER;
495
496 /// Add contents
497 $result .= XMLDB_LINEFEED;
498 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
499 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
500 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
501 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
502
503 /// Launch the proper DDL
504 $result .= XMLDB_LINEFEED;
505 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
506 $result .= ' $status = $status && change_field_notnull($table, $field);' . XMLDB_LINEFEED;
507
508 /// Add standard PHP footer
509 $result .= XMLDB_PHP_FOOTER;
510
511 return $result;
512 }
513
514 /**
515 * This function will generate all the PHP code needed to
516 * change the default of one field using XMLDB objects and functions
517 *
518 * @param XMLDBStructure structure object containing all the info
519 * @param string table table name
520 * @param string field field name to change null/not null
521 */
522 function change_field_default_php($structure, $table, $field) {
523
524 $result = '';
525 /// Validate if we can do it
526 if (!$table = $structure->getTable($table)) {
527 return false;
528 }
529 if (!$field = $table->getField($field)) {
530 return false;
531 }
532 if ($table->getAllErrors()) {
533 return false;
534 }
535
536 /// Calculate the default tip text
537 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
538
539 /// Add the standard PHP header
540 $result .= XMLDB_PHP_HEADER;
541
542 /// Add contents
543 $result .= XMLDB_LINEFEED;
544 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
545 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
546 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
547 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
548
549 /// Launch the proper DDL
550 $result .= XMLDB_LINEFEED;
551 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
552 $result .= ' $status = $status && change_field_default($table, $field);' . XMLDB_LINEFEED;
553
554 /// Add standard PHP footer
555 $result .= XMLDB_PHP_FOOTER;
556
557 return $result;
558 }
559
c5a6ae17 560}
561?>