Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / xmldb / actions / view_table_php / view_table_php.class.php
blob9185ccc5943d5be24a096a8bb2c7577c347076cc
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
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 {
32 /**
33 * Init method, every subclass will have its own
35 function init() {
36 parent::init();
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selectfieldkeyindex' => 'xmldb',
44 'view' => 'xmldb',
45 'table' => 'xmldb',
46 'selectonecommand' => 'xmldb',
47 'selectonefieldkeyindex' => 'xmldb',
48 'mustselectonefield' => 'xmldb',
49 'mustselectonekey' => 'xmldb',
50 'mustselectoneindex' => 'xmldb',
51 'back' => 'xmldb'
52 ));
55 /**
56 * Invoke method, every class will have its own
57 * returns true/false on completion, setting both
58 * errormsg and output as necessary
60 function invoke() {
61 parent::invoke();
63 $result = true;
65 /// Set own core attributes
66 $this->does_generate = ACTION_GENERATE_HTML;
68 /// These are always here
69 global $CFG, $XMLDB;
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];
79 } else {
80 return false;
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;
94 if ($field) {
95 $defaultfieldkeyindex = 'f#' . $field->getName();
97 $keys = $table->getKeys();
98 $indexes = $table->getIndexes();
100 /// Get parameters
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&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
109 $b .= '</p>';
110 $o = $b;
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 = '&nbsp;&nbsp;&nbsp;';
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',
129 'Keys',
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');
137 foreach ($commands as $command) {
138 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
140 /// Calculate the popup of fields/keys/indexes
141 if ($fields) {
142 $popfields['fieldshead'] = 'Fields';
143 foreach ($fields as $field) {
144 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
147 if ($keys) {
148 $popfields['keyshead'] = 'Keys';
149 foreach ($keys as $key) {
150 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
153 if ($indexes) {
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">';
162 $o.= '<div>';
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) . '&nbsp;<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>';
169 $o.= ' </table>';
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']);
180 } else {
181 /// Based on current params, call the needed function
182 switch ($commandparam) {
183 case 'add_field':
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'];
189 break;
190 case 'drop_field':
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'];
196 break;
197 case 'rename_field':
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'];
203 break;
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'];
210 break;
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));
214 } else {
215 $o.= $this->str['mustselectonefield'];
217 break;
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));
221 } else {
222 $o.= $this->str['mustselectonefield'];
224 break;
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));
228 } else {
229 $o.= $this->str['mustselectonefield'];
231 break;
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'];
238 break;
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));
242 } else {
243 $o.= $this->str['mustselectonefield'];
245 break;
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'];
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'];
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'];
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'];
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'];
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'];
287 break;
290 $o.= '</textarea></td></tr>';
291 $o.= ' </table>';
293 $this->output = $o;
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
301 return $result;
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) {
315 $result = '';
316 /// Validate if we can do it
317 if (!$table = $structure->getTable($table)) {
318 return false;
320 if (!$field = $table->getField($field)) {
321 return false;
323 if ($table->getAllErrors()) {
324 return false;
327 /// Add the standard PHP header
328 $result .= XMLDB_PHP_HEADER;
330 /// Add contents
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;
345 return $result;
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) {
359 $result = '';
360 /// Validate if we can do it
361 if (!$table = $structure->getTable($table)) {
362 return false;
364 if (!$field = $table->getField($field)) {
365 return false;
367 if ($table->getAllErrors()) {
368 return false;
371 /// Add the standard PHP header
372 $result .= XMLDB_PHP_HEADER;
374 /// Add contents
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;
388 return $result;
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) {
402 $result = '';
403 /// Validate if we can do it
404 if (!$table = $structure->getTable($table)) {
405 return false;
407 if (!$field = $table->getField($field)) {
408 return false;
410 if ($table->getAllErrors()) {
411 return false;
414 /// Add the standard PHP header
415 $result .= XMLDB_PHP_HEADER;
417 /// Add contents
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;
432 return $result;
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
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) {
452 $result = '';
453 /// Validate if we can do it
454 if (!$table = $structure->getTable($table)) {
455 return false;
457 if (!$field = $table->getField($field)) {
458 return false;
460 if ($table->getAllErrors()) {
461 return false;
464 /// Calculate the type tip text
465 $type = $field->getXMLDBTypeName($field->getType());
467 /// Add the standard PHP header
468 $result .= XMLDB_PHP_HEADER;
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;
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;
485 return $result;
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) {
498 $result = '';
499 /// Validate if we can do it
500 if (!$table = $structure->getTable($table)) {
501 return false;
503 if (!$field = $table->getField($field)) {
504 return false;
506 if ($table->getAllErrors()) {
507 return false;
510 /// Calculate the precision tip text
511 $precision = '(' . $field->getLength();
512 if ($field->getDecimals()) {
513 $precision .= ', ' . $field->getDecimals();
515 $precision .= ')';
517 /// Add the standard PHP header
518 $result .= XMLDB_PHP_HEADER;
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;
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;
535 return $result;
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) {
548 $result = '';
549 /// Validate if we can do it
550 if (!$table = $structure->getTable($table)) {
551 return false;
553 if (!$field = $table->getField($field)) {
554 return false;
556 if ($table->getAllErrors()) {
557 return false;
560 /// Calculate the unsigned tip text
561 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
563 /// Add the standard PHP header
564 $result .= XMLDB_PHP_HEADER;
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;
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;
581 return $result;
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) {
594 $result = '';
595 /// Validate if we can do it
596 if (!$table = $structure->getTable($table)) {
597 return false;
599 if (!$field = $table->getField($field)) {
600 return false;
602 if ($table->getAllErrors()) {
603 return false;
606 /// Calculate the notnull tip text
607 $notnull = $field->getNotnull() ? 'not null' : 'null';
609 /// Add the standard PHP header
610 $result .= XMLDB_PHP_HEADER;
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;
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;
627 return $result;
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) {
641 $result = '';
642 /// Validate if we can do it
643 if (!$table = $structure->getTable($table)) {
644 return false;
646 if (!$field = $table->getField($field)) {
647 return false;
649 if ($table->getAllErrors()) {
650 return false;
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;
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;
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;
674 return $result;
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) {
687 $result = '';
688 /// Validate if we can do it
689 if (!$table = $structure->getTable($table)) {
690 return false;
692 if (!$field = $table->getField($field)) {
693 return false;
695 if ($table->getAllErrors()) {
696 return false;
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;
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;
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;
720 return $result;
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) {
734 $result = '';
735 /// Validate if we can do it
736 if (!$table = $structure->getTable($table)) {
737 return false;
739 if (!$key = $table->getKey($key)) {
740 return false;
742 if ($table->getAllErrors()) {
743 return false;
746 /// Add the standard PHP header
747 $result .= XMLDB_PHP_HEADER;
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;
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;
764 return $result;
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) {
778 $result = '';
779 /// Validate if we can do it
780 if (!$table = $structure->getTable($table)) {
781 return false;
783 if (!$key = $table->getKey($key)) {
784 return false;
786 if ($table->getAllErrors()) {
787 return false;
790 /// Add the standard PHP header
791 $result .= XMLDB_PHP_HEADER;
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;
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;
808 return $result;
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) {
822 $result = '';
823 /// Validate if we can do it
824 if (!$table = $structure->getTable($table)) {
825 return false;
827 if (!$key = $table->getKey($key)) {
828 return false;
830 if ($table->getAllErrors()) {
831 return false;
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;
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;
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;
855 return $result;
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) {
869 $result = '';
870 /// Validate if we can do it
871 if (!$table = $structure->getTable($table)) {
872 return false;
874 if (!$index = $table->getIndex($index)) {
875 return false;
877 if ($table->getAllErrors()) {
878 return false;
881 /// Add the standard PHP header
882 $result .= XMLDB_PHP_HEADER;
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;
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;
899 return $result;
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) {
913 $result = '';
914 /// Validate if we can do it
915 if (!$table = $structure->getTable($table)) {
916 return false;
918 if (!$index = $table->getIndex($index)) {
919 return false;
921 if ($table->getAllErrors()) {
922 return false;
925 /// Add the standard PHP header
926 $result .= XMLDB_PHP_HEADER;
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;
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;
943 return $result;
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) {
957 $result = '';
958 /// Validate if we can do it
959 if (!$table = $structure->getTable($table)) {
960 return false;
962 if (!$index = $table->getIndex($index)) {
963 return false;
965 if ($table->getAllErrors()) {
966 return false;
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;
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;
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;
990 return $result;