Added to more tests to check float <---> char conversion
[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!)',
123 $optionspacer . 'change_field_type (not imp!)',
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;
1934b522 204 case 'change_field_precision':
313b5a35 205 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
206 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
207 } else {
208 $o.= $this->str['mustselectonefield'];
209 }
1934b522 210 break;
211 case 'change_field_unsigned':
313b5a35 212 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
213 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
214 } else {
215 $o.= $this->str['mustselectonefield'];
216 }
1934b522 217 break;
218 case 'change_field_notnull':
313b5a35 219 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
220 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
221 } else {
222 $o.= $this->str['mustselectonefield'];
223 }
1934b522 224 break;
225 case 'change_field_default':
313b5a35 226 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
227 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
228 } else {
229 $o.= $this->str['mustselectonefield'];
230 }
1934b522 231 break;
ceac2552 232 case 'add_key':
233 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
234 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
235 } else {
236 $o.= $this->str['mustselectonekey'];
237 }
238 break;
239 case 'drop_key':
240 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
241 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
242 } else {
243 $o.= $this->str['mustselectonekey'];
244 }
245 break;
246 case 'rename_key':
247 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
248 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
249 } else {
250 $o.= $this->str['mustselectonekey'];
251 }
252 break;
253 case 'add_index':
254 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
255 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
256 } else {
257 $o.= $this->str['mustselectoneindex'];
258 }
259 break;
260 case 'drop_index':
261 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
262 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
263 } else {
264 $o.= $this->str['mustselectoneindex'];
265 }
266 break;
267 case 'rename_index':
268 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
269 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
270 } else {
271 $o.= $this->str['mustselectoneindex'];
272 }
273 break;
eafc2b70 274 }
c5a6ae17 275 }
276 $o.= '</textarea></td></tr>';
277 $o.= ' </table>';
278
279 $this->output = $o;
280
281 /// Launch postaction if exists (leave this here!)
eef868d1 282 if ($this->getPostAction() && $result) {
c5a6ae17 283 return $this->launch($this->getPostAction());
284 }
285
286 /// Return ok if arrived here
287 return $result;
288 }
289
290 /**
eef868d1 291 * This function will generate all the PHP code needed to
3be91b01 292 * create one field using XMLDB objects and functions
eef868d1 293 *
c5a6ae17 294 * @param XMLDBStructure structure object containing all the info
3be91b01 295 * @param string table table name
296 * @param string field field name to be created
297 * @return string PHP code to be used to create the field
c5a6ae17 298 */
3be91b01 299 function add_field_php($structure, $table, $field) {
c5a6ae17 300
301 $result = '';
302 /// Validate if we can do it
303 if (!$table = $structure->getTable($table)) {
304 return false;
305 }
3be91b01 306 if (!$field = $table->getField($field)) {
307 return false;
308 }
c5a6ae17 309 if ($table->getAllErrors()) {
310 return false;
311 }
312
313 /// Add the standard PHP header
314 $result .= XMLDB_PHP_HEADER;
315
316 /// Add contents
317 $result .= XMLDB_LINEFEED;
3be91b01 318 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 319 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 320 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
321 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 322
323 /// Launch the proper DDL
324 $result .= XMLDB_LINEFEED;
3be91b01 325 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
326 $result .= ' $status = $status && add_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 327
328 /// Add standard PHP footer
329 $result .= XMLDB_PHP_FOOTER;
330
331 return $result;
332 }
333
334 /**
eef868d1 335 * This function will generate all the PHP code needed to
3be91b01 336 * drop one field using XMLDB objects and functions
eef868d1 337 *
c5a6ae17 338 * @param XMLDBStructure structure object containing all the info
3be91b01 339 * @param string table table name
340 * @param string field field name to be dropped
341 * @return string PHP code to be used to drop the field
c5a6ae17 342 */
3be91b01 343 function drop_field_php($structure, $table, $field) {
c5a6ae17 344
345 $result = '';
346 /// Validate if we can do it
347 if (!$table = $structure->getTable($table)) {
348 return false;
349 }
3be91b01 350 if (!$field = $table->getField($field)) {
351 return false;
352 }
c5a6ae17 353 if ($table->getAllErrors()) {
354 return false;
355 }
356
357 /// Add the standard PHP header
358 $result .= XMLDB_PHP_HEADER;
359
360 /// Add contents
361 $result .= XMLDB_LINEFEED;
3be91b01 362 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 363 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 364 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 365
366 /// Launch the proper DDL
367 $result .= XMLDB_LINEFEED;
3be91b01 368 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
369 $result .= ' $status = $status && drop_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 370
371 /// Add standard PHP footer
372 $result .= XMLDB_PHP_FOOTER;
373
374 return $result;
375 }
376
377 /**
eef868d1 378 * This function will generate all the PHP code needed to
3be91b01 379 * rename one field using XMLDB objects and functions
eef868d1 380 *
c5a6ae17 381 * @param XMLDBStructure structure object containing all the info
3be91b01 382 * @param string table table name
383 * @param string field field name to be renamed
ceac2552 384 * @return string PHP code to be used to rename the field
c5a6ae17 385 */
3be91b01 386 function rename_field_php($structure, $table, $field) {
c5a6ae17 387
388 $result = '';
389 /// Validate if we can do it
390 if (!$table = $structure->getTable($table)) {
391 return false;
392 }
3be91b01 393 if (!$field = $table->getField($field)) {
394 return false;
395 }
c5a6ae17 396 if ($table->getAllErrors()) {
397 return false;
398 }
399
400 /// Add the standard PHP header
401 $result .= XMLDB_PHP_HEADER;
402
403 /// Add contents
404 $result .= XMLDB_LINEFEED;
3be91b01 405 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
c5a6ae17 406 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 407 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 408
409 /// Launch the proper DDL
410 $result .= XMLDB_LINEFEED;
3be91b01 411 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
412 $result .= ' $status = $status && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 413
414 /// Add standard PHP footer
415 $result .= XMLDB_PHP_FOOTER;
416
417 return $result;
418 }
3be91b01 419
1934b522 420 /**
421 * This function will generate all the PHP code needed to
422 * change the precision of one field using XMLDB objects and functions
423 *
424 * @param XMLDBStructure structure object containing all the info
425 * @param string table table name
426 * @param string field field name to change precision
427 */
428 function change_field_precision_php($structure, $table, $field) {
429
430 $result = '';
431 /// Validate if we can do it
432 if (!$table = $structure->getTable($table)) {
433 return false;
434 }
435 if (!$field = $table->getField($field)) {
436 return false;
437 }
438 if ($table->getAllErrors()) {
439 return false;
440 }
441
442 /// Calculate the precision tip text
443 $precision = '(' . $field->getLength();
444 if ($field->getDecimals()) {
445 $precision .= ', ' . $field->getDecimals();
446 }
447 $precision .= ')';
448
449 /// Add the standard PHP header
450 $result .= XMLDB_PHP_HEADER;
451
452 /// Add contents
453 $result .= XMLDB_LINEFEED;
454 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
455 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
456 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
457 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
458
459 /// Launch the proper DDL
460 $result .= XMLDB_LINEFEED;
461 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
462 $result .= ' $status = $status && change_field_precision($table, $field);' . XMLDB_LINEFEED;
463
464 /// Add standard PHP footer
465 $result .= XMLDB_PHP_FOOTER;
466
467 return $result;
468 }
469
470 /**
471 * This function will generate all the PHP code needed to
472 * change the unsigned/signed of one field using XMLDB objects and functions
473 *
474 * @param XMLDBStructure structure object containing all the info
475 * @param string table table name
476 * @param string field field name to change unsigned/signed
477 */
478 function change_field_unsigned_php($structure, $table, $field) {
479
480 $result = '';
481 /// Validate if we can do it
482 if (!$table = $structure->getTable($table)) {
483 return false;
484 }
485 if (!$field = $table->getField($field)) {
486 return false;
487 }
488 if ($table->getAllErrors()) {
489 return false;
490 }
491
492 /// Calculate the unsigned tip text
493 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
494
495 /// Add the standard PHP header
496 $result .= XMLDB_PHP_HEADER;
497
498 /// Add contents
499 $result .= XMLDB_LINEFEED;
500 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
501 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
502 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
503 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
504
505 /// Launch the proper DDL
506 $result .= XMLDB_LINEFEED;
507 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
508 $result .= ' $status = $status && change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
509
510 /// Add standard PHP footer
511 $result .= XMLDB_PHP_FOOTER;
512
513 return $result;
514 }
515
516 /**
517 * This function will generate all the PHP code needed to
518 * change the nullability of one field using XMLDB objects and functions
519 *
520 * @param XMLDBStructure structure object containing all the info
521 * @param string table table name
522 * @param string field field name to change null/not null
523 */
524 function change_field_notnull_php($structure, $table, $field) {
525
526 $result = '';
527 /// Validate if we can do it
528 if (!$table = $structure->getTable($table)) {
529 return false;
530 }
531 if (!$field = $table->getField($field)) {
532 return false;
533 }
534 if ($table->getAllErrors()) {
535 return false;
536 }
537
538 /// Calculate the notnull tip text
539 $notnull = $field->getNotnull() ? 'not null' : 'null';
540
541 /// Add the standard PHP header
542 $result .= XMLDB_PHP_HEADER;
543
544 /// Add contents
545 $result .= XMLDB_LINEFEED;
546 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
547 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
548 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
549 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
550
551 /// Launch the proper DDL
552 $result .= XMLDB_LINEFEED;
553 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
554 $result .= ' $status = $status && change_field_notnull($table, $field);' . XMLDB_LINEFEED;
555
556 /// Add standard PHP footer
557 $result .= XMLDB_PHP_FOOTER;
558
559 return $result;
560 }
561
562 /**
563 * This function will generate all the PHP code needed to
564 * change the default of one field using XMLDB objects and functions
565 *
566 * @param XMLDBStructure structure object containing all the info
567 * @param string table table name
568 * @param string field field name to change null/not null
569 */
570 function change_field_default_php($structure, $table, $field) {
571
572 $result = '';
573 /// Validate if we can do it
574 if (!$table = $structure->getTable($table)) {
575 return false;
576 }
577 if (!$field = $table->getField($field)) {
578 return false;
579 }
580 if ($table->getAllErrors()) {
581 return false;
582 }
583
584 /// Calculate the default tip text
585 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
586
587 /// Add the standard PHP header
588 $result .= XMLDB_PHP_HEADER;
589
590 /// Add contents
591 $result .= XMLDB_LINEFEED;
592 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
593 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
594 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
595 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
596
597 /// Launch the proper DDL
598 $result .= XMLDB_LINEFEED;
599 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
600 $result .= ' $status = $status && change_field_default($table, $field);' . XMLDB_LINEFEED;
601
602 /// Add standard PHP footer
603 $result .= XMLDB_PHP_FOOTER;
604
605 return $result;
606 }
607
ceac2552 608 /**
609 * This function will generate all the PHP code needed to
610 * create one key using XMLDB objects and functions
611 *
612 * @param XMLDBStructure structure object containing all the info
613 * @param string table table name
614 * @param string key key name to be created
615 * @return string PHP code to be used to create the key
616 */
617 function add_key_php($structure, $table, $key) {
618
619 $result = '';
620 /// Validate if we can do it
621 if (!$table = $structure->getTable($table)) {
622 return false;
623 }
624 if (!$key = $table->getKey($key)) {
625 return false;
626 }
627 if ($table->getAllErrors()) {
628 return false;
629 }
630
631 /// Add the standard PHP header
632 $result .= XMLDB_PHP_HEADER;
633
634 /// Add contents
635 $result .= XMLDB_LINEFEED;
636 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
637 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
638 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
639 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
640
641 /// Launch the proper DDL
642 $result .= XMLDB_LINEFEED;
643 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED;
644 $result .= ' $status = $status && add_key($table, $key);' . XMLDB_LINEFEED;
645
646 /// Add standard PHP footer
647 $result .= XMLDB_PHP_FOOTER;
648
649 return $result;
650 }
651
652 /**
653 * This function will generate all the PHP code needed to
654 * drop one key using XMLDB objects and functions
655 *
656 * @param XMLDBStructure structure object containing all the info
657 * @param string table table name
658 * @param string key key name to be dropped
659 * @return string PHP code to be used to drop the key
660 */
661 function drop_key_php($structure, $table, $key) {
662
663 $result = '';
664 /// Validate if we can do it
665 if (!$table = $structure->getTable($table)) {
666 return false;
667 }
668 if (!$key = $table->getKey($key)) {
669 return false;
670 }
671 if ($table->getAllErrors()) {
672 return false;
673 }
674
675 /// Add the standard PHP header
676 $result .= XMLDB_PHP_HEADER;
677
678 /// Add contents
679 $result .= XMLDB_LINEFEED;
680 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
681 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
682 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
683 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
684
685 /// Launch the proper DDL
686 $result .= XMLDB_LINEFEED;
687 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
688 $result .= ' $status = $status && drop_key($table, $key);' . XMLDB_LINEFEED;
689
690 /// Add standard PHP footer
691 $result .= XMLDB_PHP_FOOTER;
692
693 return $result;
694 }
695
696 /**
697 * This function will generate all the PHP code needed to
698 * rename one key using XMLDB objects and functions
699 *
700 * @param XMLDBStructure structure object containing all the info
701 * @param string table table name
702 * @param string key key name to be renamed
703 * @return string PHP code to be used to rename the key
704 */
705 function rename_key_php($structure, $table, $key) {
706
707 $result = '';
708 /// Validate if we can do it
709 if (!$table = $structure->getTable($table)) {
710 return false;
711 }
712 if (!$key = $table->getKey($key)) {
713 return false;
714 }
715 if ($table->getAllErrors()) {
716 return false;
717 }
718
719 /// Add the standard PHP header
720 $result .= XMLDB_PHP_HEADER;
721
722 /// Add contents
723 $result .= XMLDB_LINEFEED;
724 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
725 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
726 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED;
727 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
728
729 /// Launch the proper DDL
730 $result .= XMLDB_LINEFEED;
731 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
732 $result .= ' $status = $status && rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
733
734 /// Add standard PHP footer
735 $result .= XMLDB_PHP_FOOTER;
736
737 return $result;
738 }
739
740 /**
741 * This function will generate all the PHP code needed to
742 * create one index using XMLDB objects and functions
743 *
744 * @param XMLDBStructure structure object containing all the info
745 * @param string table table name
746 * @param string index index name to be created
747 * @return string PHP code to be used to create the index
748 */
749 function add_index_php($structure, $table, $index) {
750
751 $result = '';
752 /// Validate if we can do it
753 if (!$table = $structure->getTable($table)) {
754 return false;
755 }
756 if (!$index = $table->getIndex($index)) {
757 return false;
758 }
759 if ($table->getAllErrors()) {
760 return false;
761 }
762
763 /// Add the standard PHP header
764 $result .= XMLDB_PHP_HEADER;
765
766 /// Add contents
767 $result .= XMLDB_LINEFEED;
768 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
769 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
770 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
97bd35b2 771 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 772
773 /// Launch the proper DDL
774 $result .= XMLDB_LINEFEED;
775 $result .= ' /// Launch add index ' . $index->getName() . XMLDB_LINEFEED;
776 $result .= ' $status = $status && add_index($table, $index);' . XMLDB_LINEFEED;
777
778 /// Add standard PHP footer
779 $result .= XMLDB_PHP_FOOTER;
780
781 return $result;
782 }
783
784 /**
785 * This function will generate all the PHP code needed to
786 * drop one index using XMLDB objects and functions
787 *
788 * @param XMLDBStructure structure object containing all the info
789 * @param string table table name
790 * @param string index index name to be dropped
791 * @return string PHP code to be used to drop the index
792 */
793 function drop_index_php($structure, $table, $index) {
794
795 $result = '';
796 /// Validate if we can do it
797 if (!$table = $structure->getTable($table)) {
798 return false;
799 }
800 if (!$index = $table->getIndex($index)) {
801 return false;
802 }
803 if ($table->getAllErrors()) {
804 return false;
805 }
806
807 /// Add the standard PHP header
808 $result .= XMLDB_PHP_HEADER;
809
810 /// Add contents
811 $result .= XMLDB_LINEFEED;
812 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
813 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
814 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
815 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
816
817 /// Launch the proper DDL
818 $result .= XMLDB_LINEFEED;
819 $result .= ' /// Launch drop index ' . $index->getName() . XMLDB_LINEFEED;
820 $result .= ' $status = $status && drop_index($table, $index);' . XMLDB_LINEFEED;
821
822 /// Add standard PHP footer
823 $result .= XMLDB_PHP_FOOTER;
824
825 return $result;
826 }
827
828 /**
829 * This function will generate all the PHP code needed to
830 * rename one index using XMLDB objects and functions
831 *
832 * @param XMLDBStructure structure object containing all the info
833 * @param string table table name
834 * @param string index index name to be renamed
835 * @return string PHP code to be used to rename the index
836 */
837 function rename_index_php($structure, $table, $index) {
838
839 $result = '';
840 /// Validate if we can do it
841 if (!$table = $structure->getTable($table)) {
842 return false;
843 }
844 if (!$index = $table->getIndex($index)) {
845 return false;
846 }
847 if ($table->getAllErrors()) {
848 return false;
849 }
850
851 /// Add the standard PHP header
852 $result .= XMLDB_PHP_HEADER;
853
854 /// Add contents
855 $result .= XMLDB_LINEFEED;
856 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
857 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
858 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED;
859 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
860
861 /// Launch the proper DDL
862 $result .= XMLDB_LINEFEED;
863 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
864 $result .= ' $status = $status && rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
865
866 /// Add standard PHP footer
867 $result .= XMLDB_PHP_FOOTER;
868
869 return $result;
870 }
871
c5a6ae17 872}
873?>