Enablig rename_table() PHP generator
[moodle.git] / admin / xmldb / actions / view_table_php / view_table_php.class.php
CommitLineData
c5a6ae17 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12// //
13// This program is free software; you can redistribute it and/or modify //
14// it under the terms of the GNU General Public License as published by //
15// the Free Software Foundation; either version 2 of the License, or //
16// (at your option) any later version. //
17// //
18// This program is distributed in the hope that it will be useful, //
19// but WITHOUT ANY WARRANTY; without even the implied warranty of //
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21// GNU General Public License for more details: //
22// //
23// http://www.gnu.org/copyleft/gpl.html //
24// //
25///////////////////////////////////////////////////////////////////////////
26
27/// This class will show the PHP needed to perform the desired action
28/// with the specified fields/keys/indexes.
29
30class view_table_php extends XMLDBAction {
31
eef868d1 32 /**
c5a6ae17 33 * Init method, every subclass will have its own
34 */
35 function init() {
36 parent::init();
37
38 /// Set own custom attributes
39
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selectfieldkeyindex' => 'xmldb',
44 'view' => 'xmldb',
eafc2b70 45 'table' => 'xmldb',
313b5a35 46 'selectonecommand' => 'xmldb',
eafc2b70 47 'selectonefieldkeyindex' => 'xmldb',
313b5a35 48 'mustselectonefield' => 'xmldb',
49 'mustselectonekey' => 'xmldb',
50 'mustselectoneindex' => 'xmldb',
c5a6ae17 51 'back' => 'xmldb'
52 ));
53 }
54
55 /**
56 * Invoke method, every class will have its own
57 * returns true/false on completion, setting both
58 * errormsg and output as necessary
59 */
60 function invoke() {
61 parent::invoke();
62
63 $result = true;
64
65 /// Set own core attributes
66 $this->does_generate = ACTION_GENERATE_HTML;
67
68 /// These are always here
69 global $CFG, $XMLDB;
70
71 /// Do the job, setting result as needed
72 /// Get the dir containing the file
73 $dirpath = required_param('dir', PARAM_PATH);
74 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
75
76 /// Get the correct dirs
77 if (!empty($XMLDB->dbdirs)) {
78 $dbdir =& $XMLDB->dbdirs[$dirpath];
79 } else {
80 return false;
81 }
82 if (!empty($XMLDB->editeddirs)) {
83 $editeddir =& $XMLDB->editeddirs[$dirpath];
84 $structure =& $editeddir->xml_file->getStructure();
85 }
86 /// ADD YOUR CODE HERE
87
88 $tableparam = required_param('table', PARAM_PATH);
eef868d1 89
c5a6ae17 90 $table =& $structure->getTable($tableparam);
91 $fields = $table->getFields();
92 $field = reset($fields);
93 $defaultfieldkeyindex = null;
94 if ($field) {
eafc2b70 95 $defaultfieldkeyindex = 'f#' . $field->getName();
c5a6ae17 96 }
eafc2b70 97 $keys = $table->getKeys();
98 $indexes = $table->getIndexes();
c5a6ae17 99
100 /// Get parameters
101 $commandparam = optional_param('command', 'add_field', PARAM_PATH);
3be91b01 102 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
103 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
313b5a35 104 $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
c5a6ae17 105
106 /// The back to edit xml button
107 $b = ' <p align="center" class="buttons">';
eafc2b70 108 $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
c5a6ae17 109 $b .= '</p>';
110 $o = $b;
111
eafc2b70 112 /// The table currently being edited
113 $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
114
313b5a35 115 /// To indent the menu selections
116 $optionspacer = '&nbsp;&nbsp;&nbsp;';
117
c5a6ae17 118 /// Calculate the popup of commands
313b5a35 119 $commands = array('Fields',
120 $optionspacer . 'add_field',
121 $optionspacer . 'drop_field',
122 $optionspacer . 'rename_field (not imp!)',
4fae686b 123 $optionspacer . 'change_field_type',
313b5a35 124 $optionspacer . 'change_field_precision',
125 $optionspacer . 'change_field_unsigned',
126 $optionspacer . 'change_field_notnull',
127 $optionspacer . 'change_field_sequence (not imp!)',
128 $optionspacer . 'change_field_enum (not imp!)',
129 $optionspacer . 'change_field_default',
130 'Keys',
ceac2552 131 $optionspacer . 'add_key',
132 $optionspacer . 'drop_key',
133 $optionspacer . 'rename_key',
134 'Indexes',
135 $optionspacer . 'add_index',
136 $optionspacer . 'drop_index',
137 $optionspacer . 'rename_index');
c5a6ae17 138 foreach ($commands as $command) {
313b5a35 139 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
c5a6ae17 140 }
eafc2b70 141 /// Calculate the popup of fields/keys/indexes
eafc2b70 142 if ($fields) {
3be91b01 143 $popfields['fieldshead'] = 'Fields';
eafc2b70 144 foreach ($fields as $field) {
3be91b01 145 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 146 }
147 }
148 if ($keys) {
3be91b01 149 $popfields['keyshead'] = 'Keys';
eafc2b70 150 foreach ($keys as $key) {
3be91b01 151 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 152 }
153 }
154 if ($indexes) {
3be91b01 155 $popfields['indexeshead'] = 'Indexes';
eafc2b70 156 foreach ($indexes as $index) {
3be91b01 157 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 158 }
c5a6ae17 159 }
eafc2b70 160
c5a6ae17 161 /// Now build the form
162 $o.= '<form id="form" action="index.php" method="post">';
163 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 164 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 165 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
166 $o.= ' <table id="formelements" align="center" cellpadding="5">';
3be91b01 167 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
c5a6ae17 168 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
169 $o.= ' </table>';
170 $o.= '</form>';
eafc2b70 171
c5a6ae17 172 $o.= ' <table id="phpcode" align="center" cellpadding="5">';
173 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 174 /// Check we have selected some field/key/index from the popup
175 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
176 $o.= s($this->str['selectonefieldkeyindex']);
313b5a35 177 /// Check we have selected some command from the popup
178 } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
179 $o.= s($this->str['selectonecommand']);
180 } else {
eafc2b70 181 /// Based on current params, call the needed function
182 switch ($commandparam) {
3be91b01 183 case 'add_field':
313b5a35 184 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
185 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
186 } else {
187 $o.= $this->str['mustselectonefield'];
188 }
eafc2b70 189 break;
3be91b01 190 case 'drop_field':
313b5a35 191 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
192 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
193 } else {
194 $o.= $this->str['mustselectonefield'];
195 }
eafc2b70 196 break;
3be91b01 197 case 'rename_field':
313b5a35 198 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
199 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
200 } else {
201 $o.= $this->str['mustselectonefield'];
202 }
eafc2b70 203 break;
4fae686b 204 case 'change_field_type':
205 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
206 $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam));
207 } else {
208 $o.= $this->str['mustselectonefield'];
209 }
210 break;
1934b522 211 case 'change_field_precision':
313b5a35 212 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
213 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
214 } else {
215 $o.= $this->str['mustselectonefield'];
216 }
1934b522 217 break;
218 case 'change_field_unsigned':
313b5a35 219 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
220 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
221 } else {
222 $o.= $this->str['mustselectonefield'];
223 }
1934b522 224 break;
225 case 'change_field_notnull':
313b5a35 226 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
227 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
228 } else {
229 $o.= $this->str['mustselectonefield'];
230 }
1934b522 231 break;
232 case 'change_field_default':
313b5a35 233 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
234 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
235 } else {
236 $o.= $this->str['mustselectonefield'];
237 }
1934b522 238 break;
ceac2552 239 case 'add_key':
240 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
241 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
242 } else {
243 $o.= $this->str['mustselectonekey'];
244 }
245 break;
246 case 'drop_key':
247 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
248 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
249 } else {
250 $o.= $this->str['mustselectonekey'];
251 }
252 break;
253 case 'rename_key':
254 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
255 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
256 } else {
257 $o.= $this->str['mustselectonekey'];
258 }
259 break;
260 case 'add_index':
261 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
262 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
263 } else {
264 $o.= $this->str['mustselectoneindex'];
265 }
266 break;
267 case 'drop_index':
268 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
269 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
270 } else {
271 $o.= $this->str['mustselectoneindex'];
272 }
273 break;
274 case 'rename_index':
275 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
276 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
277 } else {
278 $o.= $this->str['mustselectoneindex'];
279 }
280 break;
eafc2b70 281 }
c5a6ae17 282 }
283 $o.= '</textarea></td></tr>';
284 $o.= ' </table>';
285
286 $this->output = $o;
287
288 /// Launch postaction if exists (leave this here!)
eef868d1 289 if ($this->getPostAction() && $result) {
c5a6ae17 290 return $this->launch($this->getPostAction());
291 }
292
293 /// Return ok if arrived here
294 return $result;
295 }
296
297 /**
eef868d1 298 * This function will generate all the PHP code needed to
3be91b01 299 * create one field using XMLDB objects and functions
eef868d1 300 *
c5a6ae17 301 * @param XMLDBStructure structure object containing all the info
3be91b01 302 * @param string table table name
303 * @param string field field name to be created
304 * @return string PHP code to be used to create the field
c5a6ae17 305 */
3be91b01 306 function add_field_php($structure, $table, $field) {
c5a6ae17 307
308 $result = '';
309 /// Validate if we can do it
310 if (!$table = $structure->getTable($table)) {
311 return false;
312 }
3be91b01 313 if (!$field = $table->getField($field)) {
314 return false;
315 }
c5a6ae17 316 if ($table->getAllErrors()) {
317 return false;
318 }
319
320 /// Add the standard PHP header
321 $result .= XMLDB_PHP_HEADER;
322
323 /// Add contents
324 $result .= XMLDB_LINEFEED;
3be91b01 325 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 326 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 327 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
328 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 329
330 /// Launch the proper DDL
331 $result .= XMLDB_LINEFEED;
3be91b01 332 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
333 $result .= ' $status = $status && add_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 334
335 /// Add standard PHP footer
336 $result .= XMLDB_PHP_FOOTER;
337
338 return $result;
339 }
340
341 /**
eef868d1 342 * This function will generate all the PHP code needed to
3be91b01 343 * drop one field using XMLDB objects and functions
eef868d1 344 *
c5a6ae17 345 * @param XMLDBStructure structure object containing all the info
3be91b01 346 * @param string table table name
347 * @param string field field name to be dropped
348 * @return string PHP code to be used to drop the field
c5a6ae17 349 */
3be91b01 350 function drop_field_php($structure, $table, $field) {
c5a6ae17 351
352 $result = '';
353 /// Validate if we can do it
354 if (!$table = $structure->getTable($table)) {
355 return false;
356 }
3be91b01 357 if (!$field = $table->getField($field)) {
358 return false;
359 }
c5a6ae17 360 if ($table->getAllErrors()) {
361 return false;
362 }
363
364 /// Add the standard PHP header
365 $result .= XMLDB_PHP_HEADER;
366
367 /// Add contents
368 $result .= XMLDB_LINEFEED;
3be91b01 369 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 370 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 371 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 372
373 /// Launch the proper DDL
374 $result .= XMLDB_LINEFEED;
3be91b01 375 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
376 $result .= ' $status = $status && drop_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 377
378 /// Add standard PHP footer
379 $result .= XMLDB_PHP_FOOTER;
380
381 return $result;
382 }
383
384 /**
eef868d1 385 * This function will generate all the PHP code needed to
3be91b01 386 * rename one field using XMLDB objects and functions
eef868d1 387 *
c5a6ae17 388 * @param XMLDBStructure structure object containing all the info
3be91b01 389 * @param string table table name
390 * @param string field field name to be renamed
ceac2552 391 * @return string PHP code to be used to rename the field
c5a6ae17 392 */
3be91b01 393 function rename_field_php($structure, $table, $field) {
c5a6ae17 394
395 $result = '';
396 /// Validate if we can do it
397 if (!$table = $structure->getTable($table)) {
398 return false;
399 }
3be91b01 400 if (!$field = $table->getField($field)) {
401 return false;
402 }
c5a6ae17 403 if ($table->getAllErrors()) {
404 return false;
405 }
406
407 /// Add the standard PHP header
408 $result .= XMLDB_PHP_HEADER;
409
410 /// Add contents
411 $result .= XMLDB_LINEFEED;
3be91b01 412 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
c5a6ae17 413 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 414 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 415
416 /// Launch the proper DDL
417 $result .= XMLDB_LINEFEED;
3be91b01 418 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
419 $result .= ' $status = $status && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 420
421 /// Add standard PHP footer
422 $result .= XMLDB_PHP_FOOTER;
423
424 return $result;
425 }
3be91b01 426
4fae686b 427 /**
428 * This function will generate all the PHP code needed to
429 * change the type of one field using XMLDB objects and functions.
430 * Currently these conversions are supported:
431 * integer to char
432 * char to integer
433 * number to char
434 * char to number
435 * float to char
436 * char to float
437 *
438 * @param XMLDBStructure structure object containing all the info
439 * @param string table table name
440 * @param string field field name to change precision
441 */
442 function change_field_type_php($structure, $table, $field) {
443
444 $result = '';
445 /// Validate if we can do it
446 if (!$table = $structure->getTable($table)) {
447 return false;
448 }
449 if (!$field = $table->getField($field)) {
450 return false;
451 }
452 if ($table->getAllErrors()) {
453 return false;
454 }
455
456 /// Calculate the type tip text
457 $type = $field->getXMLDBTypeName($field->getType());
458
459 /// Add the standard PHP header
460 $result .= XMLDB_PHP_HEADER;
461
462 /// Add contents
463 $result .= XMLDB_LINEFEED;
464 $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED;
465 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
466 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
467 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
468
469 /// Launch the proper DDL
470 $result .= XMLDB_LINEFEED;
471 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED;
472 $result .= ' $status = $status && change_field_type($table, $field);' . XMLDB_LINEFEED;
473
474 /// Add standard PHP footer
475 $result .= XMLDB_PHP_FOOTER;
476
477 return $result;
478 }
479
1934b522 480 /**
481 * This function will generate all the PHP code needed to
482 * change the precision of one field using XMLDB objects and functions
483 *
484 * @param XMLDBStructure structure object containing all the info
485 * @param string table table name
486 * @param string field field name to change precision
487 */
488 function change_field_precision_php($structure, $table, $field) {
489
490 $result = '';
491 /// Validate if we can do it
492 if (!$table = $structure->getTable($table)) {
493 return false;
494 }
495 if (!$field = $table->getField($field)) {
496 return false;
497 }
498 if ($table->getAllErrors()) {
499 return false;
500 }
501
502 /// Calculate the precision tip text
503 $precision = '(' . $field->getLength();
504 if ($field->getDecimals()) {
505 $precision .= ', ' . $field->getDecimals();
506 }
507 $precision .= ')';
508
509 /// Add the standard PHP header
510 $result .= XMLDB_PHP_HEADER;
511
512 /// Add contents
513 $result .= XMLDB_LINEFEED;
514 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
515 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
516 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
517 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
518
519 /// Launch the proper DDL
520 $result .= XMLDB_LINEFEED;
521 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
522 $result .= ' $status = $status && change_field_precision($table, $field);' . XMLDB_LINEFEED;
523
524 /// Add standard PHP footer
525 $result .= XMLDB_PHP_FOOTER;
526
527 return $result;
528 }
529
530 /**
531 * This function will generate all the PHP code needed to
532 * change the unsigned/signed of one field using XMLDB objects and functions
533 *
534 * @param XMLDBStructure structure object containing all the info
535 * @param string table table name
536 * @param string field field name to change unsigned/signed
537 */
538 function change_field_unsigned_php($structure, $table, $field) {
539
540 $result = '';
541 /// Validate if we can do it
542 if (!$table = $structure->getTable($table)) {
543 return false;
544 }
545 if (!$field = $table->getField($field)) {
546 return false;
547 }
548 if ($table->getAllErrors()) {
549 return false;
550 }
551
552 /// Calculate the unsigned tip text
553 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
554
555 /// Add the standard PHP header
556 $result .= XMLDB_PHP_HEADER;
557
558 /// Add contents
559 $result .= XMLDB_LINEFEED;
560 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
561 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
562 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
563 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
564
565 /// Launch the proper DDL
566 $result .= XMLDB_LINEFEED;
567 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
568 $result .= ' $status = $status && change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
569
570 /// Add standard PHP footer
571 $result .= XMLDB_PHP_FOOTER;
572
573 return $result;
574 }
575
576 /**
577 * This function will generate all the PHP code needed to
578 * change the nullability of one field using XMLDB objects and functions
579 *
580 * @param XMLDBStructure structure object containing all the info
581 * @param string table table name
582 * @param string field field name to change null/not null
583 */
584 function change_field_notnull_php($structure, $table, $field) {
585
586 $result = '';
587 /// Validate if we can do it
588 if (!$table = $structure->getTable($table)) {
589 return false;
590 }
591 if (!$field = $table->getField($field)) {
592 return false;
593 }
594 if ($table->getAllErrors()) {
595 return false;
596 }
597
598 /// Calculate the notnull tip text
599 $notnull = $field->getNotnull() ? 'not null' : 'null';
600
601 /// Add the standard PHP header
602 $result .= XMLDB_PHP_HEADER;
603
604 /// Add contents
605 $result .= XMLDB_LINEFEED;
606 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
607 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
608 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
609 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
610
611 /// Launch the proper DDL
612 $result .= XMLDB_LINEFEED;
613 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
614 $result .= ' $status = $status && change_field_notnull($table, $field);' . XMLDB_LINEFEED;
615
616 /// Add standard PHP footer
617 $result .= XMLDB_PHP_FOOTER;
618
619 return $result;
620 }
621
622 /**
623 * This function will generate all the PHP code needed to
624 * change the default of one field using XMLDB objects and functions
625 *
626 * @param XMLDBStructure structure object containing all the info
627 * @param string table table name
628 * @param string field field name to change null/not null
629 */
630 function change_field_default_php($structure, $table, $field) {
631
632 $result = '';
633 /// Validate if we can do it
634 if (!$table = $structure->getTable($table)) {
635 return false;
636 }
637 if (!$field = $table->getField($field)) {
638 return false;
639 }
640 if ($table->getAllErrors()) {
641 return false;
642 }
643
644 /// Calculate the default tip text
645 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
646
647 /// Add the standard PHP header
648 $result .= XMLDB_PHP_HEADER;
649
650 /// Add contents
651 $result .= XMLDB_LINEFEED;
652 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
653 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
654 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
655 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
656
657 /// Launch the proper DDL
658 $result .= XMLDB_LINEFEED;
659 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
660 $result .= ' $status = $status && change_field_default($table, $field);' . XMLDB_LINEFEED;
661
662 /// Add standard PHP footer
663 $result .= XMLDB_PHP_FOOTER;
664
665 return $result;
666 }
667
ceac2552 668 /**
669 * This function will generate all the PHP code needed to
670 * create one key using XMLDB objects and functions
671 *
672 * @param XMLDBStructure structure object containing all the info
673 * @param string table table name
674 * @param string key key name to be created
675 * @return string PHP code to be used to create the key
676 */
677 function add_key_php($structure, $table, $key) {
678
679 $result = '';
680 /// Validate if we can do it
681 if (!$table = $structure->getTable($table)) {
682 return false;
683 }
684 if (!$key = $table->getKey($key)) {
685 return false;
686 }
687 if ($table->getAllErrors()) {
688 return false;
689 }
690
691 /// Add the standard PHP header
692 $result .= XMLDB_PHP_HEADER;
693
694 /// Add contents
695 $result .= XMLDB_LINEFEED;
696 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
697 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
698 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
699 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
700
701 /// Launch the proper DDL
702 $result .= XMLDB_LINEFEED;
703 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED;
704 $result .= ' $status = $status && add_key($table, $key);' . XMLDB_LINEFEED;
705
706 /// Add standard PHP footer
707 $result .= XMLDB_PHP_FOOTER;
708
709 return $result;
710 }
711
712 /**
713 * This function will generate all the PHP code needed to
714 * drop one key using XMLDB objects and functions
715 *
716 * @param XMLDBStructure structure object containing all the info
717 * @param string table table name
718 * @param string key key name to be dropped
719 * @return string PHP code to be used to drop the key
720 */
721 function drop_key_php($structure, $table, $key) {
722
723 $result = '';
724 /// Validate if we can do it
725 if (!$table = $structure->getTable($table)) {
726 return false;
727 }
728 if (!$key = $table->getKey($key)) {
729 return false;
730 }
731 if ($table->getAllErrors()) {
732 return false;
733 }
734
735 /// Add the standard PHP header
736 $result .= XMLDB_PHP_HEADER;
737
738 /// Add contents
739 $result .= XMLDB_LINEFEED;
740 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
741 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
742 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
743 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
744
745 /// Launch the proper DDL
746 $result .= XMLDB_LINEFEED;
747 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
748 $result .= ' $status = $status && drop_key($table, $key);' . XMLDB_LINEFEED;
749
750 /// Add standard PHP footer
751 $result .= XMLDB_PHP_FOOTER;
752
753 return $result;
754 }
755
756 /**
757 * This function will generate all the PHP code needed to
758 * rename one key using XMLDB objects and functions
759 *
760 * @param XMLDBStructure structure object containing all the info
761 * @param string table table name
762 * @param string key key name to be renamed
763 * @return string PHP code to be used to rename the key
764 */
765 function rename_key_php($structure, $table, $key) {
766
767 $result = '';
768 /// Validate if we can do it
769 if (!$table = $structure->getTable($table)) {
770 return false;
771 }
772 if (!$key = $table->getKey($key)) {
773 return false;
774 }
775 if ($table->getAllErrors()) {
776 return false;
777 }
778
779 /// Add the standard PHP header
780 $result .= XMLDB_PHP_HEADER;
781
782 /// Add contents
783 $result .= XMLDB_LINEFEED;
784 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
785 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
786 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
787 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
788
789 /// Launch the proper DDL
790 $result .= XMLDB_LINEFEED;
791 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
792 $result .= ' $status = $status && rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
793
794 /// Add standard PHP footer
795 $result .= XMLDB_PHP_FOOTER;
796
797 return $result;
798 }
799
800 /**
801 * This function will generate all the PHP code needed to
802 * create one index using XMLDB objects and functions
803 *
804 * @param XMLDBStructure structure object containing all the info
805 * @param string table table name
806 * @param string index index name to be created
807 * @return string PHP code to be used to create the index
808 */
809 function add_index_php($structure, $table, $index) {
810
811 $result = '';
812 /// Validate if we can do it
813 if (!$table = $structure->getTable($table)) {
814 return false;
815 }
816 if (!$index = $table->getIndex($index)) {
817 return false;
818 }
819 if ($table->getAllErrors()) {
820 return false;
821 }
822
823 /// Add the standard PHP header
824 $result .= XMLDB_PHP_HEADER;
825
826 /// Add contents
827 $result .= XMLDB_LINEFEED;
828 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
829 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
830 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
97bd35b2 831 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 832
833 /// Launch the proper DDL
834 $result .= XMLDB_LINEFEED;
835 $result .= ' /// Launch add index ' . $index->getName() . XMLDB_LINEFEED;
836 $result .= ' $status = $status && add_index($table, $index);' . XMLDB_LINEFEED;
837
838 /// Add standard PHP footer
839 $result .= XMLDB_PHP_FOOTER;
840
841 return $result;
842 }
843
844 /**
845 * This function will generate all the PHP code needed to
846 * drop one index using XMLDB objects and functions
847 *
848 * @param XMLDBStructure structure object containing all the info
849 * @param string table table name
850 * @param string index index name to be dropped
851 * @return string PHP code to be used to drop the index
852 */
853 function drop_index_php($structure, $table, $index) {
854
855 $result = '';
856 /// Validate if we can do it
857 if (!$table = $structure->getTable($table)) {
858 return false;
859 }
860 if (!$index = $table->getIndex($index)) {
861 return false;
862 }
863 if ($table->getAllErrors()) {
864 return false;
865 }
866
867 /// Add the standard PHP header
868 $result .= XMLDB_PHP_HEADER;
869
870 /// Add contents
871 $result .= XMLDB_LINEFEED;
872 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
873 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
874 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
875 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
876
877 /// Launch the proper DDL
878 $result .= XMLDB_LINEFEED;
879 $result .= ' /// Launch drop index ' . $index->getName() . XMLDB_LINEFEED;
880 $result .= ' $status = $status && drop_index($table, $index);' . XMLDB_LINEFEED;
881
882 /// Add standard PHP footer
883 $result .= XMLDB_PHP_FOOTER;
884
885 return $result;
886 }
887
888 /**
889 * This function will generate all the PHP code needed to
890 * rename one index using XMLDB objects and functions
891 *
892 * @param XMLDBStructure structure object containing all the info
893 * @param string table table name
894 * @param string index index name to be renamed
895 * @return string PHP code to be used to rename the index
896 */
897 function rename_index_php($structure, $table, $index) {
898
899 $result = '';
900 /// Validate if we can do it
901 if (!$table = $structure->getTable($table)) {
902 return false;
903 }
904 if (!$index = $table->getIndex($index)) {
905 return false;
906 }
907 if ($table->getAllErrors()) {
908 return false;
909 }
910
911 /// Add the standard PHP header
912 $result .= XMLDB_PHP_HEADER;
913
914 /// Add contents
915 $result .= XMLDB_LINEFEED;
916 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
917 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
918 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
919 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
920
921 /// Launch the proper DDL
922 $result .= XMLDB_LINEFEED;
923 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
924 $result .= ' $status = $status && rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
925
926 /// Add standard PHP footer
927 $result .= XMLDB_PHP_FOOTER;
928
929 return $result;
930 }
931
c5a6ae17 932}
933?>