Initialize the $message variable properly this time.
[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// //
b7064779 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
c5a6ae17 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
007456de 107 $b = ' <p class="centerpara 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',
49173d25 122 $optionspacer . 'rename_field',
4fae686b 123 $optionspacer . 'change_field_type',
313b5a35 124 $optionspacer . 'change_field_precision',
125 $optionspacer . 'change_field_unsigned',
126 $optionspacer . 'change_field_notnull',
49173d25 127 $optionspacer . 'change_field_enum',
313b5a35 128 $optionspacer . 'change_field_default',
129 'Keys',
ceac2552 130 $optionspacer . 'add_key',
131 $optionspacer . 'drop_key',
132 $optionspacer . 'rename_key',
133 'Indexes',
134 $optionspacer . 'add_index',
135 $optionspacer . 'drop_index',
136 $optionspacer . 'rename_index');
c5a6ae17 137 foreach ($commands as $command) {
313b5a35 138 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
c5a6ae17 139 }
eafc2b70 140 /// Calculate the popup of fields/keys/indexes
eafc2b70 141 if ($fields) {
3be91b01 142 $popfields['fieldshead'] = 'Fields';
eafc2b70 143 foreach ($fields as $field) {
3be91b01 144 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 145 }
146 }
147 if ($keys) {
3be91b01 148 $popfields['keyshead'] = 'Keys';
eafc2b70 149 foreach ($keys as $key) {
3be91b01 150 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 151 }
152 }
153 if ($indexes) {
3be91b01 154 $popfields['indexeshead'] = 'Indexes';
eafc2b70 155 foreach ($indexes as $index) {
3be91b01 156 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 157 }
c5a6ae17 158 }
eafc2b70 159
c5a6ae17 160 /// Now build the form
161 $o.= '<form id="form" action="index.php" method="post">';
007456de 162 $o.= '<div>';
c5a6ae17 163 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 164 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 165 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
007456de 166 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
3be91b01 167 $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 168 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
169 $o.= ' </table>';
007456de 170 $o.= '</div></form>';
eafc2b70 171
007456de 172 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
c5a6ae17 173 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 174 /// Check we have selected some field/key/index from the popup
175 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
176 $o.= s($this->str['selectonefieldkeyindex']);
313b5a35 177 /// Check we have selected some command from the popup
178 } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
179 $o.= s($this->str['selectonecommand']);
180 } else {
eafc2b70 181 /// Based on current params, call the needed function
182 switch ($commandparam) {
3be91b01 183 case 'add_field':
313b5a35 184 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
185 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
186 } else {
187 $o.= $this->str['mustselectonefield'];
188 }
eafc2b70 189 break;
3be91b01 190 case 'drop_field':
313b5a35 191 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
192 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
193 } else {
194 $o.= $this->str['mustselectonefield'];
195 }
eafc2b70 196 break;
3be91b01 197 case 'rename_field':
313b5a35 198 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
199 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
200 } else {
201 $o.= $this->str['mustselectonefield'];
202 }
eafc2b70 203 break;
4fae686b 204 case 'change_field_type':
205 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
206 $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam));
207 } else {
208 $o.= $this->str['mustselectonefield'];
209 }
210 break;
1934b522 211 case 'change_field_precision':
313b5a35 212 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
213 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
214 } else {
215 $o.= $this->str['mustselectonefield'];
216 }
1934b522 217 break;
218 case 'change_field_unsigned':
313b5a35 219 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
220 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
221 } else {
222 $o.= $this->str['mustselectonefield'];
223 }
1934b522 224 break;
225 case 'change_field_notnull':
313b5a35 226 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
227 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
228 } else {
229 $o.= $this->str['mustselectonefield'];
230 }
1934b522 231 break;
49173d25 232 case 'change_field_enum':
233 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
234 $o.= s($this->change_field_enum_php($structure, $tableparam, $fieldkeyindexparam));
235 } else {
236 $o.= $this->str['mustselectonefield'];
237 }
238 break;
1934b522 239 case 'change_field_default':
313b5a35 240 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
241 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
242 } else {
243 $o.= $this->str['mustselectonefield'];
244 }
1934b522 245 break;
ceac2552 246 case 'add_key':
247 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
248 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
249 } else {
250 $o.= $this->str['mustselectonekey'];
251 }
252 break;
253 case 'drop_key':
254 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
255 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
256 } else {
257 $o.= $this->str['mustselectonekey'];
258 }
259 break;
260 case 'rename_key':
261 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
262 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
263 } else {
264 $o.= $this->str['mustselectonekey'];
265 }
266 break;
267 case 'add_index':
268 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
269 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
270 } else {
271 $o.= $this->str['mustselectoneindex'];
272 }
273 break;
274 case 'drop_index':
275 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
276 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
277 } else {
278 $o.= $this->str['mustselectoneindex'];
279 }
280 break;
281 case 'rename_index':
282 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
283 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
284 } else {
285 $o.= $this->str['mustselectoneindex'];
286 }
287 break;
eafc2b70 288 }
c5a6ae17 289 }
290 $o.= '</textarea></td></tr>';
291 $o.= ' </table>';
292
293 $this->output = $o;
294
295 /// Launch postaction if exists (leave this here!)
eef868d1 296 if ($this->getPostAction() && $result) {
c5a6ae17 297 return $this->launch($this->getPostAction());
298 }
299
300 /// Return ok if arrived here
301 return $result;
302 }
303
304 /**
eef868d1 305 * This function will generate all the PHP code needed to
3be91b01 306 * create one field using XMLDB objects and functions
eef868d1 307 *
c5a6ae17 308 * @param XMLDBStructure structure object containing all the info
3be91b01 309 * @param string table table name
310 * @param string field field name to be created
311 * @return string PHP code to be used to create the field
c5a6ae17 312 */
3be91b01 313 function add_field_php($structure, $table, $field) {
c5a6ae17 314
315 $result = '';
316 /// Validate if we can do it
317 if (!$table = $structure->getTable($table)) {
318 return false;
319 }
3be91b01 320 if (!$field = $table->getField($field)) {
321 return false;
322 }
c5a6ae17 323 if ($table->getAllErrors()) {
324 return false;
325 }
326
327 /// Add the standard PHP header
328 $result .= XMLDB_PHP_HEADER;
329
330 /// Add contents
331 $result .= XMLDB_LINEFEED;
3be91b01 332 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 333 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 334 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
335 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 336
337 /// Launch the proper DDL
338 $result .= XMLDB_LINEFEED;
3be91b01 339 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 340 $result .= ' $result = $result && add_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 341
342 /// Add standard PHP footer
343 $result .= XMLDB_PHP_FOOTER;
344
345 return $result;
346 }
347
348 /**
eef868d1 349 * This function will generate all the PHP code needed to
3be91b01 350 * drop one field using XMLDB objects and functions
eef868d1 351 *
c5a6ae17 352 * @param XMLDBStructure structure object containing all the info
3be91b01 353 * @param string table table name
354 * @param string field field name to be dropped
355 * @return string PHP code to be used to drop the field
c5a6ae17 356 */
3be91b01 357 function drop_field_php($structure, $table, $field) {
c5a6ae17 358
359 $result = '';
360 /// Validate if we can do it
361 if (!$table = $structure->getTable($table)) {
362 return false;
363 }
3be91b01 364 if (!$field = $table->getField($field)) {
365 return false;
366 }
c5a6ae17 367 if ($table->getAllErrors()) {
368 return false;
369 }
370
371 /// Add the standard PHP header
372 $result .= XMLDB_PHP_HEADER;
373
374 /// Add contents
375 $result .= XMLDB_LINEFEED;
3be91b01 376 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 377 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 378 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 379
380 /// Launch the proper DDL
381 $result .= XMLDB_LINEFEED;
3be91b01 382 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 383 $result .= ' $result = $result && drop_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 384
385 /// Add standard PHP footer
386 $result .= XMLDB_PHP_FOOTER;
387
388 return $result;
389 }
390
391 /**
eef868d1 392 * This function will generate all the PHP code needed to
3be91b01 393 * rename one field using XMLDB objects and functions
eef868d1 394 *
c5a6ae17 395 * @param XMLDBStructure structure object containing all the info
3be91b01 396 * @param string table table name
397 * @param string field field name to be renamed
ceac2552 398 * @return string PHP code to be used to rename the field
c5a6ae17 399 */
3be91b01 400 function rename_field_php($structure, $table, $field) {
c5a6ae17 401
402 $result = '';
403 /// Validate if we can do it
404 if (!$table = $structure->getTable($table)) {
405 return false;
406 }
3be91b01 407 if (!$field = $table->getField($field)) {
408 return false;
409 }
c5a6ae17 410 if ($table->getAllErrors()) {
411 return false;
412 }
413
414 /// Add the standard PHP header
415 $result .= XMLDB_PHP_HEADER;
416
417 /// Add contents
418 $result .= XMLDB_LINEFEED;
3be91b01 419 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
c5a6ae17 420 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 421 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
704b9031 422 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 423
424 /// Launch the proper DDL
425 $result .= XMLDB_LINEFEED;
3be91b01 426 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 427 $result .= ' $result = $result && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 428
429 /// Add standard PHP footer
430 $result .= XMLDB_PHP_FOOTER;
431
432 return $result;
433 }
3be91b01 434
4fae686b 435 /**
436 * This function will generate all the PHP code needed to
437 * change the type of one field using XMLDB objects and functions.
438 * Currently these conversions are supported:
439 * integer to char
440 * char to integer
441 * number to char
442 * char to number
443 * float to char
444 * char to float
445 *
446 * @param XMLDBStructure structure object containing all the info
447 * @param string table table name
448 * @param string field field name to change precision
449 */
450 function change_field_type_php($structure, $table, $field) {
451
452 $result = '';
453 /// Validate if we can do it
454 if (!$table = $structure->getTable($table)) {
455 return false;
456 }
457 if (!$field = $table->getField($field)) {
458 return false;
459 }
460 if ($table->getAllErrors()) {
461 return false;
462 }
463
464 /// Calculate the type tip text
465 $type = $field->getXMLDBTypeName($field->getType());
466
467 /// Add the standard PHP header
468 $result .= XMLDB_PHP_HEADER;
469
470 /// Add contents
471 $result .= XMLDB_LINEFEED;
472 $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED;
473 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
474 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
475 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
476
477 /// Launch the proper DDL
478 $result .= XMLDB_LINEFEED;
479 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 480 $result .= ' $result = $result && change_field_type($table, $field);' . XMLDB_LINEFEED;
4fae686b 481
482 /// Add standard PHP footer
483 $result .= XMLDB_PHP_FOOTER;
484
485 return $result;
486 }
487
1934b522 488 /**
489 * This function will generate all the PHP code needed to
490 * change the precision of one field using XMLDB objects and functions
491 *
492 * @param XMLDBStructure structure object containing all the info
493 * @param string table table name
494 * @param string field field name to change precision
495 */
496 function change_field_precision_php($structure, $table, $field) {
497
498 $result = '';
499 /// Validate if we can do it
500 if (!$table = $structure->getTable($table)) {
501 return false;
502 }
503 if (!$field = $table->getField($field)) {
504 return false;
505 }
506 if ($table->getAllErrors()) {
507 return false;
508 }
509
510 /// Calculate the precision tip text
511 $precision = '(' . $field->getLength();
512 if ($field->getDecimals()) {
513 $precision .= ', ' . $field->getDecimals();
514 }
515 $precision .= ')';
516
517 /// Add the standard PHP header
518 $result .= XMLDB_PHP_HEADER;
519
520 /// Add contents
521 $result .= XMLDB_LINEFEED;
522 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
523 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
524 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
525 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
526
527 /// Launch the proper DDL
528 $result .= XMLDB_LINEFEED;
529 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 530 $result .= ' $result = $result && change_field_precision($table, $field);' . XMLDB_LINEFEED;
1934b522 531
532 /// Add standard PHP footer
533 $result .= XMLDB_PHP_FOOTER;
534
535 return $result;
536 }
537
538 /**
539 * This function will generate all the PHP code needed to
540 * change the unsigned/signed of one field using XMLDB objects and functions
541 *
542 * @param XMLDBStructure structure object containing all the info
543 * @param string table table name
544 * @param string field field name to change unsigned/signed
545 */
546 function change_field_unsigned_php($structure, $table, $field) {
547
548 $result = '';
549 /// Validate if we can do it
550 if (!$table = $structure->getTable($table)) {
551 return false;
552 }
553 if (!$field = $table->getField($field)) {
554 return false;
555 }
556 if ($table->getAllErrors()) {
557 return false;
558 }
559
560 /// Calculate the unsigned tip text
561 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
562
563 /// Add the standard PHP header
564 $result .= XMLDB_PHP_HEADER;
565
566 /// Add contents
567 $result .= XMLDB_LINEFEED;
568 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
569 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
570 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
571 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
572
573 /// Launch the proper DDL
574 $result .= XMLDB_LINEFEED;
575 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 576 $result .= ' $result = $result && change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
1934b522 577
578 /// Add standard PHP footer
579 $result .= XMLDB_PHP_FOOTER;
580
581 return $result;
582 }
583
584 /**
585 * This function will generate all the PHP code needed to
586 * change the nullability of one field using XMLDB objects and functions
587 *
588 * @param XMLDBStructure structure object containing all the info
589 * @param string table table name
590 * @param string field field name to change null/not null
591 */
592 function change_field_notnull_php($structure, $table, $field) {
593
594 $result = '';
595 /// Validate if we can do it
596 if (!$table = $structure->getTable($table)) {
597 return false;
598 }
599 if (!$field = $table->getField($field)) {
600 return false;
601 }
602 if ($table->getAllErrors()) {
603 return false;
604 }
605
606 /// Calculate the notnull tip text
607 $notnull = $field->getNotnull() ? 'not null' : 'null';
608
609 /// Add the standard PHP header
610 $result .= XMLDB_PHP_HEADER;
611
612 /// Add contents
613 $result .= XMLDB_LINEFEED;
614 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
615 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
616 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
617 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
618
619 /// Launch the proper DDL
620 $result .= XMLDB_LINEFEED;
621 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 622 $result .= ' $result = $result && change_field_notnull($table, $field);' . XMLDB_LINEFEED;
1934b522 623
624 /// Add standard PHP footer
625 $result .= XMLDB_PHP_FOOTER;
626
627 return $result;
628 }
629
49173d25 630 /**
631 * This function will generate all the PHP code needed to
632 * change the enum values (check constraint) of one field
633 * using XMLDB objects and functions
634 *
635 * @param XMLDBStructure structure object containing all the info
636 * @param string table table name
637 * @param string field field name to change its enum
638 */
639 function change_field_enum_php($structure, $table, $field) {
640
641 $result = '';
642 /// Validate if we can do it
643 if (!$table = $structure->getTable($table)) {
644 return false;
645 }
646 if (!$field = $table->getField($field)) {
647 return false;
648 }
649 if ($table->getAllErrors()) {
650 return false;
651 }
652
653 /// Calculate the enum tip text
654 $enum = $field->getEnum() ? implode(', ', $field->getEnumValues()) : 'none';
655
656 /// Add the standard PHP header
657 $result .= XMLDB_PHP_HEADER;
658
659 /// Add contents
660 $result .= XMLDB_LINEFEED;
661 $result .= ' /// Changing list of values (enum) of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $enum . XMLDB_LINEFEED;
662 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
663 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
664 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
665
666 /// Launch the proper DDL
667 $result .= XMLDB_LINEFEED;
668 $result .= ' /// Launch change of list of values for field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 669 $result .= ' $result = $result && change_field_enum($table, $field);' . XMLDB_LINEFEED;
49173d25 670
671 /// Add standard PHP footer
672 $result .= XMLDB_PHP_FOOTER;
673
674 return $result;
675 }
676
1934b522 677 /**
678 * This function will generate all the PHP code needed to
679 * change the default of one field using XMLDB objects and functions
680 *
681 * @param XMLDBStructure structure object containing all the info
682 * @param string table table name
683 * @param string field field name to change null/not null
684 */
685 function change_field_default_php($structure, $table, $field) {
686
687 $result = '';
688 /// Validate if we can do it
689 if (!$table = $structure->getTable($table)) {
690 return false;
691 }
692 if (!$field = $table->getField($field)) {
693 return false;
694 }
695 if ($table->getAllErrors()) {
696 return false;
697 }
698
699 /// Calculate the default tip text
700 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
701
702 /// Add the standard PHP header
703 $result .= XMLDB_PHP_HEADER;
704
705 /// Add contents
706 $result .= XMLDB_LINEFEED;
707 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
708 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
709 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
710 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
711
712 /// Launch the proper DDL
713 $result .= XMLDB_LINEFEED;
714 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
db9940b6 715 $result .= ' $result = $result && change_field_default($table, $field);' . XMLDB_LINEFEED;
1934b522 716
717 /// Add standard PHP footer
718 $result .= XMLDB_PHP_FOOTER;
719
720 return $result;
721 }
722
ceac2552 723 /**
724 * This function will generate all the PHP code needed to
725 * create one key using XMLDB objects and functions
726 *
727 * @param XMLDBStructure structure object containing all the info
728 * @param string table table name
729 * @param string key key name to be created
730 * @return string PHP code to be used to create the key
731 */
732 function add_key_php($structure, $table, $key) {
733
734 $result = '';
735 /// Validate if we can do it
736 if (!$table = $structure->getTable($table)) {
737 return false;
738 }
739 if (!$key = $table->getKey($key)) {
740 return false;
741 }
742 if ($table->getAllErrors()) {
743 return false;
744 }
745
746 /// Add the standard PHP header
747 $result .= XMLDB_PHP_HEADER;
748
749 /// Add contents
750 $result .= XMLDB_LINEFEED;
751 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
752 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
753 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
754 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
755
756 /// Launch the proper DDL
757 $result .= XMLDB_LINEFEED;
758 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED;
db9940b6 759 $result .= ' $result = $result && add_key($table, $key);' . XMLDB_LINEFEED;
ceac2552 760
761 /// Add standard PHP footer
762 $result .= XMLDB_PHP_FOOTER;
763
764 return $result;
765 }
766
767 /**
768 * This function will generate all the PHP code needed to
769 * drop one key using XMLDB objects and functions
770 *
771 * @param XMLDBStructure structure object containing all the info
772 * @param string table table name
773 * @param string key key name to be dropped
774 * @return string PHP code to be used to drop the key
775 */
776 function drop_key_php($structure, $table, $key) {
777
778 $result = '';
779 /// Validate if we can do it
780 if (!$table = $structure->getTable($table)) {
781 return false;
782 }
783 if (!$key = $table->getKey($key)) {
784 return false;
785 }
786 if ($table->getAllErrors()) {
787 return false;
788 }
789
790 /// Add the standard PHP header
791 $result .= XMLDB_PHP_HEADER;
792
793 /// Add contents
794 $result .= XMLDB_LINEFEED;
795 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
796 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
797 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
798 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
799
800 /// Launch the proper DDL
801 $result .= XMLDB_LINEFEED;
802 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
db9940b6 803 $result .= ' $result = $result && drop_key($table, $key);' . XMLDB_LINEFEED;
ceac2552 804
805 /// Add standard PHP footer
806 $result .= XMLDB_PHP_FOOTER;
807
808 return $result;
809 }
810
811 /**
812 * This function will generate all the PHP code needed to
813 * rename one key using XMLDB objects and functions
814 *
815 * @param XMLDBStructure structure object containing all the info
816 * @param string table table name
817 * @param string key key name to be renamed
818 * @return string PHP code to be used to rename the key
819 */
820 function rename_key_php($structure, $table, $key) {
821
822 $result = '';
823 /// Validate if we can do it
824 if (!$table = $structure->getTable($table)) {
825 return false;
826 }
827 if (!$key = $table->getKey($key)) {
828 return false;
829 }
830 if ($table->getAllErrors()) {
831 return false;
832 }
833
49173d25 834 /// Prepend warning. This function isn't usable!
835 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
836
ceac2552 837 /// Add the standard PHP header
838 $result .= XMLDB_PHP_HEADER;
839
840 /// Add contents
841 $result .= XMLDB_LINEFEED;
842 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
843 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
844 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
845 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
846
847 /// Launch the proper DDL
848 $result .= XMLDB_LINEFEED;
849 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
db9940b6 850 $result .= ' $result = $result && rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
ceac2552 851
852 /// Add standard PHP footer
853 $result .= XMLDB_PHP_FOOTER;
854
855 return $result;
856 }
857
858 /**
859 * This function will generate all the PHP code needed to
860 * create one index using XMLDB objects and functions
861 *
862 * @param XMLDBStructure structure object containing all the info
863 * @param string table table name
864 * @param string index index name to be created
865 * @return string PHP code to be used to create the index
866 */
867 function add_index_php($structure, $table, $index) {
868
869 $result = '';
870 /// Validate if we can do it
871 if (!$table = $structure->getTable($table)) {
872 return false;
873 }
874 if (!$index = $table->getIndex($index)) {
875 return false;
876 }
877 if ($table->getAllErrors()) {
878 return false;
879 }
880
881 /// Add the standard PHP header
882 $result .= XMLDB_PHP_HEADER;
883
884 /// Add contents
885 $result .= XMLDB_LINEFEED;
886 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
887 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
888 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
97bd35b2 889 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 890
891 /// Launch the proper DDL
892 $result .= XMLDB_LINEFEED;
893 $result .= ' /// Launch add index ' . $index->getName() . XMLDB_LINEFEED;
db9940b6 894 $result .= ' $result = $result && add_index($table, $index);' . XMLDB_LINEFEED;
ceac2552 895
896 /// Add standard PHP footer
897 $result .= XMLDB_PHP_FOOTER;
898
899 return $result;
900 }
901
902 /**
903 * This function will generate all the PHP code needed to
904 * drop one index using XMLDB objects and functions
905 *
906 * @param XMLDBStructure structure object containing all the info
907 * @param string table table name
908 * @param string index index name to be dropped
909 * @return string PHP code to be used to drop the index
910 */
911 function drop_index_php($structure, $table, $index) {
912
913 $result = '';
914 /// Validate if we can do it
915 if (!$table = $structure->getTable($table)) {
916 return false;
917 }
918 if (!$index = $table->getIndex($index)) {
919 return false;
920 }
921 if ($table->getAllErrors()) {
922 return false;
923 }
924
925 /// Add the standard PHP header
926 $result .= XMLDB_PHP_HEADER;
927
928 /// Add contents
929 $result .= XMLDB_LINEFEED;
930 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
931 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
932 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
933 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
934
935 /// Launch the proper DDL
936 $result .= XMLDB_LINEFEED;
937 $result .= ' /// Launch drop index ' . $index->getName() . XMLDB_LINEFEED;
db9940b6 938 $result .= ' $result = $result && drop_index($table, $index);' . XMLDB_LINEFEED;
ceac2552 939
940 /// Add standard PHP footer
941 $result .= XMLDB_PHP_FOOTER;
942
943 return $result;
944 }
945
946 /**
947 * This function will generate all the PHP code needed to
948 * rename one index using XMLDB objects and functions
949 *
950 * @param XMLDBStructure structure object containing all the info
951 * @param string table table name
952 * @param string index index name to be renamed
953 * @return string PHP code to be used to rename the index
954 */
955 function rename_index_php($structure, $table, $index) {
956
957 $result = '';
958 /// Validate if we can do it
959 if (!$table = $structure->getTable($table)) {
960 return false;
961 }
962 if (!$index = $table->getIndex($index)) {
963 return false;
964 }
965 if ($table->getAllErrors()) {
966 return false;
967 }
968
49173d25 969 /// Prepend warning. This function isn't usable!
970 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
971
ceac2552 972 /// Add the standard PHP header
973 $result .= XMLDB_PHP_HEADER;
974
975 /// Add contents
976 $result .= XMLDB_LINEFEED;
977 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
978 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
979 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
980 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
981
982 /// Launch the proper DDL
983 $result .= XMLDB_LINEFEED;
984 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
db9940b6 985 $result .= ' $result = $result && rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
ceac2552 986
987 /// Add standard PHP footer
988 $result .= XMLDB_PHP_FOOTER;
989
990 return $result;
991 }
992
c5a6ae17 993}
994?>