3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
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. //
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: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will show the PHP needed to perform the desired action
28 /// with the specified fields/keys/indexes.
30 class view_table_php
extends XMLDBAction
{
33 * Init method, every subclass will have its own
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selectfieldkeyindex' => 'xmldb',
46 'selectonecommand' => 'xmldb',
47 'selectonefieldkeyindex' => 'xmldb',
48 'mustselectonefield' => 'xmldb',
49 'mustselectonekey' => 'xmldb',
50 'mustselectoneindex' => 'xmldb',
56 * Invoke method, every class will have its own
57 * returns true/false on completion, setting both
58 * errormsg and output as necessary
65 /// Set own core attributes
66 $this->does_generate
= ACTION_GENERATE_HTML
;
68 /// These are always here
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);
76 /// Get the correct dirs
77 if (!empty($XMLDB->dbdirs
)) {
78 $dbdir =& $XMLDB->dbdirs
[$dirpath];
82 if (!empty($XMLDB->editeddirs
)) {
83 $editeddir =& $XMLDB->editeddirs
[$dirpath];
84 $structure =& $editeddir->xml_file
->getStructure();
86 /// ADD YOUR CODE HERE
88 $tableparam = required_param('table', PARAM_PATH
);
90 $table =& $structure->getTable($tableparam);
91 $fields = $table->getFields();
92 $field = reset($fields);
93 $defaultfieldkeyindex = null;
95 $defaultfieldkeyindex = 'f#' . $field->getName();
97 $keys = $table->getKeys();
98 $indexes = $table->getIndexes();
101 $commandparam = optional_param('command', 'add_field', PARAM_PATH
);
102 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH
);
103 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
104 $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
106 /// The back to edit xml button
107 $b = ' <p class="centerpara buttons">';
108 $b .= '<a href="index.php?action=edit_table&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&table=' . $tableparam . '">[' . $this->str
['back'] . ']</a>';
112 /// The table currently being edited
113 $o .= '<h3 class="main">' . $this->str
['table'] . ': ' . s($tableparam) . '</h3>';
115 /// To indent the menu selections
116 $optionspacer = ' ';
118 /// Calculate the popup of commands
119 $commands = array('Fields',
120 $optionspacer . 'add_field',
121 $optionspacer . 'drop_field',
122 $optionspacer . 'rename_field',
123 $optionspacer . 'change_field_type',
124 $optionspacer . 'change_field_precision',
125 $optionspacer . 'change_field_unsigned',
126 $optionspacer . 'change_field_notnull',
127 $optionspacer . 'change_field_enum',
128 $optionspacer . 'change_field_default',
130 $optionspacer . 'add_key',
131 $optionspacer . 'drop_key',
132 $optionspacer . 'rename_key',
134 $optionspacer . 'add_index',
135 $optionspacer . 'drop_index',
136 $optionspacer . 'rename_index');
137 foreach ($commands as $command) {
138 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
140 /// Calculate the popup of fields/keys/indexes
142 $popfields['fieldshead'] = 'Fields';
143 foreach ($fields as $field) {
144 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
148 $popfields['keyshead'] = 'Keys';
149 foreach ($keys as $key) {
150 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
154 $popfields['indexeshead'] = 'Indexes';
155 foreach ($indexes as $index) {
156 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
160 /// Now build the form
161 $o.= '<form id="form" action="index.php" method="post">';
163 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot
, '', $dirpath) . '" />';
164 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
165 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
166 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
167 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str
['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . ' <label for="fieldkeyindex" accesskey="f">' . $this->str
['selectfieldkeyindex'] . ' </label>' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
168 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str
['view'] . '" /></td></tr>';
170 $o.= '</div></form>';
172 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
173 $o.= ' <tr><td><textarea cols="80" rows="32">';
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']);
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']);
181 /// Based on current params, call the needed function
182 switch ($commandparam) {
184 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
185 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
187 $o.= $this->str
['mustselectonefield'];
191 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
192 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
194 $o.= $this->str
['mustselectonefield'];
198 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
199 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
201 $o.= $this->str
['mustselectonefield'];
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));
208 $o.= $this->str
['mustselectonefield'];
211 case 'change_field_precision':
212 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
213 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
215 $o.= $this->str
['mustselectonefield'];
218 case 'change_field_unsigned':
219 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
220 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
222 $o.= $this->str
['mustselectonefield'];
225 case 'change_field_notnull':
226 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
227 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
229 $o.= $this->str
['mustselectonefield'];
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));
236 $o.= $this->str
['mustselectonefield'];
239 case 'change_field_default':
240 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
241 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
243 $o.= $this->str
['mustselectonefield'];
247 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
248 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
250 $o.= $this->str
['mustselectonekey'];
254 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
255 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
257 $o.= $this->str
['mustselectonekey'];
261 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
262 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
264 $o.= $this->str
['mustselectonekey'];
268 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
269 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
271 $o.= $this->str
['mustselectoneindex'];
275 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
276 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
278 $o.= $this->str
['mustselectoneindex'];
282 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
283 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
285 $o.= $this->str
['mustselectoneindex'];
290 $o.= '</textarea></td></tr>';
295 /// Launch postaction if exists (leave this here!)
296 if ($this->getPostAction() && $result) {
297 return $this->launch($this->getPostAction());
300 /// Return ok if arrived here
305 * This function will generate all the PHP code needed to
306 * create one field using XMLDB objects and functions
308 * @param XMLDBStructure structure object containing all the info
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
313 function add_field_php($structure, $table, $field) {
316 /// Validate if we can do it
317 if (!$table = $structure->getTable($table)) {
320 if (!$field = $table->getField($field)) {
323 if ($table->getAllErrors()) {
327 /// Add the standard PHP header
328 $result .= XMLDB_PHP_HEADER
;
331 $result .= XMLDB_LINEFEED
;
332 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED
;
333 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
334 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
335 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
337 /// Launch the proper DDL
338 $result .= XMLDB_LINEFEED
;
339 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED
;
340 $result .= ' $result = $result && add_field($table, $field);' . XMLDB_LINEFEED
;
342 /// Add standard PHP footer
343 $result .= XMLDB_PHP_FOOTER
;
349 * This function will generate all the PHP code needed to
350 * drop one field using XMLDB objects and functions
352 * @param XMLDBStructure structure object containing all the info
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
357 function drop_field_php($structure, $table, $field) {
360 /// Validate if we can do it
361 if (!$table = $structure->getTable($table)) {
364 if (!$field = $table->getField($field)) {
367 if ($table->getAllErrors()) {
371 /// Add the standard PHP header
372 $result .= XMLDB_PHP_HEADER
;
375 $result .= XMLDB_LINEFEED
;
376 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED
;
377 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
378 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
380 /// Launch the proper DDL
381 $result .= XMLDB_LINEFEED
;
382 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED
;
383 $result .= ' $result = $result && drop_field($table, $field);' . XMLDB_LINEFEED
;
385 /// Add standard PHP footer
386 $result .= XMLDB_PHP_FOOTER
;
392 * This function will generate all the PHP code needed to
393 * rename one field using XMLDB objects and functions
395 * @param XMLDBStructure structure object containing all the info
396 * @param string table table name
397 * @param string field field name to be renamed
398 * @return string PHP code to be used to rename the field
400 function rename_field_php($structure, $table, $field) {
403 /// Validate if we can do it
404 if (!$table = $structure->getTable($table)) {
407 if (!$field = $table->getField($field)) {
410 if ($table->getAllErrors()) {
414 /// Add the standard PHP header
415 $result .= XMLDB_PHP_HEADER
;
418 $result .= XMLDB_LINEFEED
;
419 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED
;
420 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
421 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
422 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
424 /// Launch the proper DDL
425 $result .= XMLDB_LINEFEED
;
426 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED
;
427 $result .= ' $result = $result && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED
;
429 /// Add standard PHP footer
430 $result .= XMLDB_PHP_FOOTER
;
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:
446 * @param XMLDBStructure structure object containing all the info
447 * @param string table table name
448 * @param string field field name to change precision
450 function change_field_type_php($structure, $table, $field) {
453 /// Validate if we can do it
454 if (!$table = $structure->getTable($table)) {
457 if (!$field = $table->getField($field)) {
460 if ($table->getAllErrors()) {
464 /// Calculate the type tip text
465 $type = $field->getXMLDBTypeName($field->getType());
467 /// Add the standard PHP header
468 $result .= XMLDB_PHP_HEADER
;
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
;
477 /// Launch the proper DDL
478 $result .= XMLDB_LINEFEED
;
479 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED
;
480 $result .= ' $result = $result && change_field_type($table, $field);' . XMLDB_LINEFEED
;
482 /// Add standard PHP footer
483 $result .= XMLDB_PHP_FOOTER
;
489 * This function will generate all the PHP code needed to
490 * change the precision of one field using XMLDB objects and functions
492 * @param XMLDBStructure structure object containing all the info
493 * @param string table table name
494 * @param string field field name to change precision
496 function change_field_precision_php($structure, $table, $field) {
499 /// Validate if we can do it
500 if (!$table = $structure->getTable($table)) {
503 if (!$field = $table->getField($field)) {
506 if ($table->getAllErrors()) {
510 /// Calculate the precision tip text
511 $precision = '(' . $field->getLength();
512 if ($field->getDecimals()) {
513 $precision .= ', ' . $field->getDecimals();
517 /// Add the standard PHP header
518 $result .= XMLDB_PHP_HEADER
;
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
;
527 /// Launch the proper DDL
528 $result .= XMLDB_LINEFEED
;
529 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED
;
530 $result .= ' $result = $result && change_field_precision($table, $field);' . XMLDB_LINEFEED
;
532 /// Add standard PHP footer
533 $result .= XMLDB_PHP_FOOTER
;
539 * This function will generate all the PHP code needed to
540 * change the unsigned/signed of one field using XMLDB objects and functions
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
546 function change_field_unsigned_php($structure, $table, $field) {
549 /// Validate if we can do it
550 if (!$table = $structure->getTable($table)) {
553 if (!$field = $table->getField($field)) {
556 if ($table->getAllErrors()) {
560 /// Calculate the unsigned tip text
561 $unsigned = $field->getUnsigned() ?
'unsigned' : 'signed';
563 /// Add the standard PHP header
564 $result .= XMLDB_PHP_HEADER
;
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
;
573 /// Launch the proper DDL
574 $result .= XMLDB_LINEFEED
;
575 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED
;
576 $result .= ' $result = $result && change_field_unsigned($table, $field);' . XMLDB_LINEFEED
;
578 /// Add standard PHP footer
579 $result .= XMLDB_PHP_FOOTER
;
585 * This function will generate all the PHP code needed to
586 * change the nullability of one field using XMLDB objects and functions
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
592 function change_field_notnull_php($structure, $table, $field) {
595 /// Validate if we can do it
596 if (!$table = $structure->getTable($table)) {
599 if (!$field = $table->getField($field)) {
602 if ($table->getAllErrors()) {
606 /// Calculate the notnull tip text
607 $notnull = $field->getNotnull() ?
'not null' : 'null';
609 /// Add the standard PHP header
610 $result .= XMLDB_PHP_HEADER
;
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
;
619 /// Launch the proper DDL
620 $result .= XMLDB_LINEFEED
;
621 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED
;
622 $result .= ' $result = $result && change_field_notnull($table, $field);' . XMLDB_LINEFEED
;
624 /// Add standard PHP footer
625 $result .= XMLDB_PHP_FOOTER
;
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
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
639 function change_field_enum_php($structure, $table, $field) {
642 /// Validate if we can do it
643 if (!$table = $structure->getTable($table)) {
646 if (!$field = $table->getField($field)) {
649 if ($table->getAllErrors()) {
653 /// Calculate the enum tip text
654 $enum = $field->getEnum() ?
implode(', ', $field->getEnumValues()) : 'none';
656 /// Add the standard PHP header
657 $result .= XMLDB_PHP_HEADER
;
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
;
666 /// Launch the proper DDL
667 $result .= XMLDB_LINEFEED
;
668 $result .= ' /// Launch change of list of values for field ' . $field->getName() . XMLDB_LINEFEED
;
669 $result .= ' $result = $result && change_field_enum($table, $field);' . XMLDB_LINEFEED
;
671 /// Add standard PHP footer
672 $result .= XMLDB_PHP_FOOTER
;
678 * This function will generate all the PHP code needed to
679 * change the default of one field using XMLDB objects and functions
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
685 function change_field_default_php($structure, $table, $field) {
688 /// Validate if we can do it
689 if (!$table = $structure->getTable($table)) {
692 if (!$field = $table->getField($field)) {
695 if ($table->getAllErrors()) {
699 /// Calculate the default tip text
700 $default = $field->getDefault() === null ?
'drop it' : $field->getDefault();
702 /// Add the standard PHP header
703 $result .= XMLDB_PHP_HEADER
;
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
;
712 /// Launch the proper DDL
713 $result .= XMLDB_LINEFEED
;
714 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED
;
715 $result .= ' $result = $result && change_field_default($table, $field);' . XMLDB_LINEFEED
;
717 /// Add standard PHP footer
718 $result .= XMLDB_PHP_FOOTER
;
724 * This function will generate all the PHP code needed to
725 * create one key using XMLDB objects and functions
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
732 function add_key_php($structure, $table, $key) {
735 /// Validate if we can do it
736 if (!$table = $structure->getTable($table)) {
739 if (!$key = $table->getKey($key)) {
742 if ($table->getAllErrors()) {
746 /// Add the standard PHP header
747 $result .= XMLDB_PHP_HEADER
;
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
;
756 /// Launch the proper DDL
757 $result .= XMLDB_LINEFEED
;
758 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED
;
759 $result .= ' $result = $result && add_key($table, $key);' . XMLDB_LINEFEED
;
761 /// Add standard PHP footer
762 $result .= XMLDB_PHP_FOOTER
;
768 * This function will generate all the PHP code needed to
769 * drop one key using XMLDB objects and functions
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
776 function drop_key_php($structure, $table, $key) {
779 /// Validate if we can do it
780 if (!$table = $structure->getTable($table)) {
783 if (!$key = $table->getKey($key)) {
786 if ($table->getAllErrors()) {
790 /// Add the standard PHP header
791 $result .= XMLDB_PHP_HEADER
;
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
;
800 /// Launch the proper DDL
801 $result .= XMLDB_LINEFEED
;
802 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED
;
803 $result .= ' $result = $result && drop_key($table, $key);' . XMLDB_LINEFEED
;
805 /// Add standard PHP footer
806 $result .= XMLDB_PHP_FOOTER
;
812 * This function will generate all the PHP code needed to
813 * rename one key using XMLDB objects and functions
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
820 function rename_key_php($structure, $table, $key) {
823 /// Validate if we can do it
824 if (!$table = $structure->getTable($table)) {
827 if (!$key = $table->getKey($key)) {
830 if ($table->getAllErrors()) {
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
;
837 /// Add the standard PHP header
838 $result .= XMLDB_PHP_HEADER
;
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
;
847 /// Launch the proper DDL
848 $result .= XMLDB_LINEFEED
;
849 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED
;
850 $result .= ' $result = $result && rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED
;
852 /// Add standard PHP footer
853 $result .= XMLDB_PHP_FOOTER
;
859 * This function will generate all the PHP code needed to
860 * create one index using XMLDB objects and functions
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
867 function add_index_php($structure, $table, $index) {
870 /// Validate if we can do it
871 if (!$table = $structure->getTable($table)) {
874 if (!$index = $table->getIndex($index)) {
877 if ($table->getAllErrors()) {
881 /// Add the standard PHP header
882 $result .= XMLDB_PHP_HEADER
;
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
;
889 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED
;
891 /// Launch the proper DDL
892 $result .= XMLDB_LINEFEED
;
893 $result .= ' /// Launch add index ' . $index->getName() . XMLDB_LINEFEED
;
894 $result .= ' $result = $result && add_index($table, $index);' . XMLDB_LINEFEED
;
896 /// Add standard PHP footer
897 $result .= XMLDB_PHP_FOOTER
;
903 * This function will generate all the PHP code needed to
904 * drop one index using XMLDB objects and functions
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
911 function drop_index_php($structure, $table, $index) {
914 /// Validate if we can do it
915 if (!$table = $structure->getTable($table)) {
918 if (!$index = $table->getIndex($index)) {
921 if ($table->getAllErrors()) {
925 /// Add the standard PHP header
926 $result .= XMLDB_PHP_HEADER
;
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
;
935 /// Launch the proper DDL
936 $result .= XMLDB_LINEFEED
;
937 $result .= ' /// Launch drop index ' . $index->getName() . XMLDB_LINEFEED
;
938 $result .= ' $result = $result && drop_index($table, $index);' . XMLDB_LINEFEED
;
940 /// Add standard PHP footer
941 $result .= XMLDB_PHP_FOOTER
;
947 * This function will generate all the PHP code needed to
948 * rename one index using XMLDB objects and functions
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
955 function rename_index_php($structure, $table, $index) {
958 /// Validate if we can do it
959 if (!$table = $structure->getTable($table)) {
962 if (!$index = $table->getIndex($index)) {
965 if ($table->getAllErrors()) {
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
;
972 /// Add the standard PHP header
973 $result .= XMLDB_PHP_HEADER
;
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
;
982 /// Launch the proper DDL
983 $result .= XMLDB_LINEFEED
;
984 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED
;
985 $result .= ' $result = $result && rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED
;
987 /// Add standard PHP footer
988 $result .= XMLDB_PHP_FOOTER
;