Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / view_table_php / view_table_php.class.php
blob7d1b46bbd014be33e99c456c73caf146482baa89
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) 1999 onwards 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 the proper upgrade_xxxx_savepoint call
343 $result .= $this->upgrade_savepoint_php ($structure);
345 /// Add standard PHP footer
346 $result .= XMLDB_PHP_FOOTER;
348 return $result;
352 * This function will generate all the PHP code needed to
353 * drop one field using XMLDB objects and functions
355 * @param XMLDBStructure structure object containing all the info
356 * @param string table table name
357 * @param string field field name to be dropped
358 * @return string PHP code to be used to drop the field
360 function drop_field_php($structure, $table, $field) {
362 $result = '';
363 /// Validate if we can do it
364 if (!$table = $structure->getTable($table)) {
365 return false;
367 if (!$field = $table->getField($field)) {
368 return false;
370 if ($table->getAllErrors()) {
371 return false;
374 /// Add the standard PHP header
375 $result .= XMLDB_PHP_HEADER;
377 /// Add contents
378 $result .= XMLDB_LINEFEED;
379 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
380 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
381 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
383 /// Launch the proper DDL
384 $result .= XMLDB_LINEFEED;
385 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
386 $result .= ' $result = $result && drop_field($table, $field);' . XMLDB_LINEFEED;
388 /// Add the proper upgrade_xxxx_savepoint call
389 $result .= $this->upgrade_savepoint_php ($structure);
391 /// Add standard PHP footer
392 $result .= XMLDB_PHP_FOOTER;
394 return $result;
398 * This function will generate all the PHP code needed to
399 * rename one field using XMLDB objects and functions
401 * @param XMLDBStructure structure object containing all the info
402 * @param string table table name
403 * @param string field field name to be renamed
404 * @return string PHP code to be used to rename the field
406 function rename_field_php($structure, $table, $field) {
408 $result = '';
409 /// Validate if we can do it
410 if (!$table = $structure->getTable($table)) {
411 return false;
413 if (!$field = $table->getField($field)) {
414 return false;
416 if ($table->getAllErrors()) {
417 return false;
420 /// Add the standard PHP header
421 $result .= XMLDB_PHP_HEADER;
423 /// Add contents
424 $result .= XMLDB_LINEFEED;
425 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
426 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
427 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
428 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
430 /// Launch the proper DDL
431 $result .= XMLDB_LINEFEED;
432 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
433 $result .= ' $result = $result && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
435 /// Add the proper upgrade_xxxx_savepoint call
436 $result .= $this->upgrade_savepoint_php ($structure);
438 /// Add standard PHP footer
439 $result .= XMLDB_PHP_FOOTER;
441 return $result;
445 * This function will generate all the PHP code needed to
446 * change the type of one field using XMLDB objects and functions.
447 * Currently these conversions are supported:
448 * integer to char
449 * char to integer
450 * number to char
451 * char to number
452 * float to char
453 * char to float
455 * @param XMLDBStructure structure object containing all the info
456 * @param string table table name
457 * @param string field field name to change precision
459 function change_field_type_php($structure, $table, $field) {
461 $result = '';
462 /// Validate if we can do it
463 if (!$table = $structure->getTable($table)) {
464 return false;
466 if (!$field = $table->getField($field)) {
467 return false;
469 if ($table->getAllErrors()) {
470 return false;
473 /// Calculate the type tip text
474 $type = $field->getXMLDBTypeName($field->getType());
476 /// Add the standard PHP header
477 $result .= XMLDB_PHP_HEADER;
479 /// Add contents
480 $result .= XMLDB_LINEFEED;
481 $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED;
482 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
483 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
484 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
486 /// Launch the proper DDL
487 $result .= XMLDB_LINEFEED;
488 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED;
489 $result .= ' $result = $result && change_field_type($table, $field);' . XMLDB_LINEFEED;
491 /// Add the proper upgrade_xxxx_savepoint call
492 $result .= $this->upgrade_savepoint_php ($structure);
494 /// Add standard PHP footer
495 $result .= XMLDB_PHP_FOOTER;
497 return $result;
501 * This function will generate all the PHP code needed to
502 * change the precision of one field using XMLDB objects and functions
504 * @param XMLDBStructure structure object containing all the info
505 * @param string table table name
506 * @param string field field name to change precision
508 function change_field_precision_php($structure, $table, $field) {
510 $result = '';
511 /// Validate if we can do it
512 if (!$table = $structure->getTable($table)) {
513 return false;
515 if (!$field = $table->getField($field)) {
516 return false;
518 if ($table->getAllErrors()) {
519 return false;
522 /// Calculate the precision tip text
523 $precision = '(' . $field->getLength();
524 if ($field->getDecimals()) {
525 $precision .= ', ' . $field->getDecimals();
527 $precision .= ')';
529 /// Add the standard PHP header
530 $result .= XMLDB_PHP_HEADER;
532 /// Add contents
533 $result .= XMLDB_LINEFEED;
534 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
535 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
536 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
537 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
539 /// Launch the proper DDL
540 $result .= XMLDB_LINEFEED;
541 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
542 $result .= ' $result = $result && change_field_precision($table, $field);' . XMLDB_LINEFEED;
544 /// Add the proper upgrade_xxxx_savepoint call
545 $result .= $this->upgrade_savepoint_php ($structure);
547 /// Add standard PHP footer
548 $result .= XMLDB_PHP_FOOTER;
550 return $result;
554 * This function will generate all the PHP code needed to
555 * change the unsigned/signed of one field using XMLDB objects and functions
557 * @param XMLDBStructure structure object containing all the info
558 * @param string table table name
559 * @param string field field name to change unsigned/signed
561 function change_field_unsigned_php($structure, $table, $field) {
563 $result = '';
564 /// Validate if we can do it
565 if (!$table = $structure->getTable($table)) {
566 return false;
568 if (!$field = $table->getField($field)) {
569 return false;
571 if ($table->getAllErrors()) {
572 return false;
575 /// Calculate the unsigned tip text
576 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
578 /// Add the standard PHP header
579 $result .= XMLDB_PHP_HEADER;
581 /// Add contents
582 $result .= XMLDB_LINEFEED;
583 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
584 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
585 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
586 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
588 /// Launch the proper DDL
589 $result .= XMLDB_LINEFEED;
590 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
591 $result .= ' $result = $result && change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
593 /// Add the proper upgrade_xxxx_savepoint call
594 $result .= $this->upgrade_savepoint_php ($structure);
596 /// Add standard PHP footer
597 $result .= XMLDB_PHP_FOOTER;
599 return $result;
603 * This function will generate all the PHP code needed to
604 * change the nullability of one field using XMLDB objects and functions
606 * @param XMLDBStructure structure object containing all the info
607 * @param string table table name
608 * @param string field field name to change null/not null
610 function change_field_notnull_php($structure, $table, $field) {
612 $result = '';
613 /// Validate if we can do it
614 if (!$table = $structure->getTable($table)) {
615 return false;
617 if (!$field = $table->getField($field)) {
618 return false;
620 if ($table->getAllErrors()) {
621 return false;
624 /// Calculate the notnull tip text
625 $notnull = $field->getNotnull() ? 'not null' : 'null';
627 /// Add the standard PHP header
628 $result .= XMLDB_PHP_HEADER;
630 /// Add contents
631 $result .= XMLDB_LINEFEED;
632 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
633 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
634 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
635 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
637 /// Launch the proper DDL
638 $result .= XMLDB_LINEFEED;
639 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
640 $result .= ' $result = $result && change_field_notnull($table, $field);' . XMLDB_LINEFEED;
642 /// Add the proper upgrade_xxxx_savepoint call
643 $result .= $this->upgrade_savepoint_php ($structure);
645 /// Add standard PHP footer
646 $result .= XMLDB_PHP_FOOTER;
648 return $result;
652 * This function will generate all the PHP code needed to
653 * change the enum values (check constraint) of one field
654 * using XMLDB objects and functions
656 * @param XMLDBStructure structure object containing all the info
657 * @param string table table name
658 * @param string field field name to change its enum
660 function change_field_enum_php($structure, $table, $field) {
662 $result = '';
663 /// Validate if we can do it
664 if (!$table = $structure->getTable($table)) {
665 return false;
667 if (!$field = $table->getField($field)) {
668 return false;
670 if ($table->getAllErrors()) {
671 return false;
674 /// Calculate the enum tip text
675 $enum = $field->getEnum() ? implode(', ', $field->getEnumValues()) : 'none';
677 /// Add the standard PHP header
678 $result .= XMLDB_PHP_HEADER;
680 /// Add contents
681 $result .= XMLDB_LINEFEED;
682 $result .= ' /// Changing list of values (enum) of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $enum . XMLDB_LINEFEED;
683 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
684 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
685 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
687 /// Launch the proper DDL
688 $result .= XMLDB_LINEFEED;
689 $result .= ' /// Launch change of list of values for field ' . $field->getName() . XMLDB_LINEFEED;
690 $result .= ' $result = $result && change_field_enum($table, $field);' . XMLDB_LINEFEED;
692 /// Add the proper upgrade_xxxx_savepoint call
693 $result .= $this->upgrade_savepoint_php ($structure);
695 /// Add standard PHP footer
696 $result .= XMLDB_PHP_FOOTER;
698 return $result;
702 * This function will generate all the PHP code needed to
703 * change the default of one field using XMLDB objects and functions
705 * @param XMLDBStructure structure object containing all the info
706 * @param string table table name
707 * @param string field field name to change null/not null
709 function change_field_default_php($structure, $table, $field) {
711 $result = '';
712 /// Validate if we can do it
713 if (!$table = $structure->getTable($table)) {
714 return false;
716 if (!$field = $table->getField($field)) {
717 return false;
719 if ($table->getAllErrors()) {
720 return false;
723 /// Calculate the default tip text
724 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
726 /// Add the standard PHP header
727 $result .= XMLDB_PHP_HEADER;
729 /// Add contents
730 $result .= XMLDB_LINEFEED;
731 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
732 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
733 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
734 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
736 /// Launch the proper DDL
737 $result .= XMLDB_LINEFEED;
738 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
739 $result .= ' $result = $result && change_field_default($table, $field);' . XMLDB_LINEFEED;
741 /// Add the proper upgrade_xxxx_savepoint call
742 $result .= $this->upgrade_savepoint_php ($structure);
744 /// Add standard PHP footer
745 $result .= XMLDB_PHP_FOOTER;
747 return $result;
751 * This function will generate all the PHP code needed to
752 * create one key using XMLDB objects and functions
754 * @param XMLDBStructure structure object containing all the info
755 * @param string table table name
756 * @param string key key name to be created
757 * @return string PHP code to be used to create the key
759 function add_key_php($structure, $table, $key) {
761 $result = '';
762 /// Validate if we can do it
763 if (!$table = $structure->getTable($table)) {
764 return false;
766 if (!$key = $table->getKey($key)) {
767 return false;
769 if ($table->getAllErrors()) {
770 return false;
773 /// Add the standard PHP header
774 $result .= XMLDB_PHP_HEADER;
776 /// Add contents
777 $result .= XMLDB_LINEFEED;
778 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
779 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
780 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
781 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
783 /// Launch the proper DDL
784 $result .= XMLDB_LINEFEED;
785 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED;
786 $result .= ' $result = $result && add_key($table, $key);' . XMLDB_LINEFEED;
788 /// Add the proper upgrade_xxxx_savepoint call
789 $result .= $this->upgrade_savepoint_php ($structure);
791 /// Add standard PHP footer
792 $result .= XMLDB_PHP_FOOTER;
794 return $result;
798 * This function will generate all the PHP code needed to
799 * drop one key using XMLDB objects and functions
801 * @param XMLDBStructure structure object containing all the info
802 * @param string table table name
803 * @param string key key name to be dropped
804 * @return string PHP code to be used to drop the key
806 function drop_key_php($structure, $table, $key) {
808 $result = '';
809 /// Validate if we can do it
810 if (!$table = $structure->getTable($table)) {
811 return false;
813 if (!$key = $table->getKey($key)) {
814 return false;
816 if ($table->getAllErrors()) {
817 return false;
820 /// Add the standard PHP header
821 $result .= XMLDB_PHP_HEADER;
823 /// Add contents
824 $result .= XMLDB_LINEFEED;
825 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
826 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
827 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
828 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
830 /// Launch the proper DDL
831 $result .= XMLDB_LINEFEED;
832 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
833 $result .= ' $result = $result && drop_key($table, $key);' . XMLDB_LINEFEED;
835 /// Add the proper upgrade_xxxx_savepoint call
836 $result .= $this->upgrade_savepoint_php ($structure);
838 /// Add standard PHP footer
839 $result .= XMLDB_PHP_FOOTER;
841 return $result;
845 * This function will generate all the PHP code needed to
846 * rename one key using XMLDB objects and functions
848 * @param XMLDBStructure structure object containing all the info
849 * @param string table table name
850 * @param string key key name to be renamed
851 * @return string PHP code to be used to rename the key
853 function rename_key_php($structure, $table, $key) {
855 $result = '';
856 /// Validate if we can do it
857 if (!$table = $structure->getTable($table)) {
858 return false;
860 if (!$key = $table->getKey($key)) {
861 return false;
863 if ($table->getAllErrors()) {
864 return false;
867 /// Prepend warning. This function isn't usable!
868 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
870 /// Add the standard PHP header
871 $result .= XMLDB_PHP_HEADER;
873 /// Add contents
874 $result .= XMLDB_LINEFEED;
875 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
876 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
877 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
878 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
880 /// Launch the proper DDL
881 $result .= XMLDB_LINEFEED;
882 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
883 $result .= ' $result = $result && rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
885 /// Add the proper upgrade_xxxx_savepoint call
886 $result .= $this->upgrade_savepoint_php ($structure);
888 /// Add standard PHP footer
889 $result .= XMLDB_PHP_FOOTER;
891 return $result;
895 * This function will generate all the PHP code needed to
896 * create one index using XMLDB objects and functions
898 * @param XMLDBStructure structure object containing all the info
899 * @param string table table name
900 * @param string index index name to be created
901 * @return string PHP code to be used to create the index
903 function add_index_php($structure, $table, $index) {
905 $result = '';
906 /// Validate if we can do it
907 if (!$table = $structure->getTable($table)) {
908 return false;
910 if (!$index = $table->getIndex($index)) {
911 return false;
913 if ($table->getAllErrors()) {
914 return false;
917 /// Add the standard PHP header
918 $result .= XMLDB_PHP_HEADER;
920 /// Add contents
921 $result .= XMLDB_LINEFEED;
922 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
923 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
924 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
925 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
927 /// Launch the proper DDL
928 $result .= XMLDB_LINEFEED;
929 $result .= ' /// Launch add index ' . $index->getName() . XMLDB_LINEFEED;
930 $result .= ' $result = $result && add_index($table, $index);' . XMLDB_LINEFEED;
932 /// Add the proper upgrade_xxxx_savepoint call
933 $result .= $this->upgrade_savepoint_php ($structure);
935 /// Add standard PHP footer
936 $result .= XMLDB_PHP_FOOTER;
938 return $result;
942 * This function will generate all the PHP code needed to
943 * drop one index using XMLDB objects and functions
945 * @param XMLDBStructure structure object containing all the info
946 * @param string table table name
947 * @param string index index name to be dropped
948 * @return string PHP code to be used to drop the index
950 function drop_index_php($structure, $table, $index) {
952 $result = '';
953 /// Validate if we can do it
954 if (!$table = $structure->getTable($table)) {
955 return false;
957 if (!$index = $table->getIndex($index)) {
958 return false;
960 if ($table->getAllErrors()) {
961 return false;
964 /// Add the standard PHP header
965 $result .= XMLDB_PHP_HEADER;
967 /// Add contents
968 $result .= XMLDB_LINEFEED;
969 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
970 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
971 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
972 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
974 /// Launch the proper DDL
975 $result .= XMLDB_LINEFEED;
976 $result .= ' /// Launch drop index ' . $index->getName() . XMLDB_LINEFEED;
977 $result .= ' $result = $result && drop_index($table, $index);' . XMLDB_LINEFEED;
979 /// Add the proper upgrade_xxxx_savepoint call
980 $result .= $this->upgrade_savepoint_php ($structure);
982 /// Add standard PHP footer
983 $result .= XMLDB_PHP_FOOTER;
985 return $result;
989 * This function will generate all the PHP code needed to
990 * rename one index using XMLDB objects and functions
992 * @param XMLDBStructure structure object containing all the info
993 * @param string table table name
994 * @param string index index name to be renamed
995 * @return string PHP code to be used to rename the index
997 function rename_index_php($structure, $table, $index) {
999 $result = '';
1000 /// Validate if we can do it
1001 if (!$table = $structure->getTable($table)) {
1002 return false;
1004 if (!$index = $table->getIndex($index)) {
1005 return false;
1007 if ($table->getAllErrors()) {
1008 return false;
1011 /// Prepend warning. This function isn't usable!
1012 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
1014 /// Add the standard PHP header
1015 $result .= XMLDB_PHP_HEADER;
1017 /// Add contents
1018 $result .= XMLDB_LINEFEED;
1019 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
1020 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
1021 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
1022 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
1024 /// Launch the proper DDL
1025 $result .= XMLDB_LINEFEED;
1026 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
1027 $result .= ' $result = $result && rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
1029 /// Add the proper upgrade_xxxx_savepoint call
1030 $result .= $this->upgrade_savepoint_php ($structure);
1032 /// Add standard PHP footer
1033 $result .= XMLDB_PHP_FOOTER;
1035 return $result;
1039 * This function will generate the PHP code needed to
1040 * implement the upgrade_xxxx_savepoint() php calls in
1041 * upgrade code generated from the editor
1043 * @param XMLDBStructure structure object containing all the info
1044 * @return string PHP code to be used to stabilish a savepoint
1046 function upgrade_savepoint_php ($structure) {
1048 $path = $structure->getPath();
1050 $result = '';
1052 switch ($path) {
1053 case 'lib/db':
1054 $result = XMLDB_LINEFEED .
1055 ' /// Main savepoint reached' . XMLDB_LINEFEED .
1056 ' upgrade_main_savepoint($result, XXXXXXXXXX);' . XMLDB_LINEFEED;
1057 break;
1059 return $result;