| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: view_table_php.class.php,v 1.14.2.1 2007/11/01 23:21:42 stronk7 Exp $ 2 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 /////////////////////////////////////////////////////////////////////////// 26 27 /// This class will show the PHP needed to perform the desired action 28 /// with the specified fields/keys/indexes. 29 30 class view_table_php extends XMLDBAction { 31 32 /** 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', 45 'table' => 'xmldb', 46 'selectonecommand' => 'xmldb', 47 'selectonefieldkeyindex' => 'xmldb', 48 'mustselectonefield' => 'xmldb', 49 'mustselectonekey' => 'xmldb', 50 'mustselectoneindex' => 'xmldb', 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); 89 90 $table =& $structure->getTable($tableparam); 91 $fields = $table->getFields(); 92 $field = reset($fields); 93 $defaultfieldkeyindex = null; 94 if ($field) { 95 $defaultfieldkeyindex = 'f#' . $field->getName(); 96 } 97 $keys = $table->getKeys(); 98 $indexes = $table->getIndexes(); 99 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 105 106 /// The back to edit xml button 107 $b = ' <p class="centerpara buttons">'; 108 $b .= '<a href="index.php?action=edit_table&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&table=' . $tableparam . '">[' . $this->str['back'] . ']</a>'; 109 $b .= '</p>'; 110 $o = $b; 111 112 /// The table currently being edited 113 $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>'; 114 115 /// To indent the menu selections 116 $optionspacer = ' '; 117 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); 139 } 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(); 145 } 146 } 147 if ($keys) { 148 $popfields['keyshead'] = 'Keys'; 149 foreach ($keys as $key) { 150 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName(); 151 } 152 } 153 if ($indexes) { 154 $popfields['indexeshead'] = 'Indexes'; 155 foreach ($indexes as $index) { 156 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName(); 157 } 158 } 159 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) . ' <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>'; 171 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']; 188 } 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']; 195 } 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']; 202 } 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']; 209 } 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']; 216 } 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']; 223 } 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']; 230 } 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']; 237 } 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']; 244 } 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']; 251 } 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']; 258 } 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']; 265 } 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']; 272 } 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']; 279 } 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']; 286 } 287 break; 288 } 289 } 290 $o.= '</textarea></td></tr>'; 291 $o.= ' </table>'; 292 293 $this->output = $o; 294 295 /// Launch postaction if exists (leave this here!) 296 if ($this->getPostAction() && $result) { 297 return $this->launch($this->getPostAction()); 298 } 299 300 /// Return ok if arrived here 301 return $result; 302 } 303 304 /** 305 * This function will generate all the PHP code needed to 306 * create one field using XMLDB objects and functions 307 * 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 312 */ 313 function add_field_php($structure, $table, $field) { 314 315 $result = ''; 316 /// Validate if we can do it 317 if (!$table = $structure->getTable($table)) { 318 return false; 319 } 320 if (!$field = $table->getField($field)) { 321 return false; 322 } 323 if ($table->getAllErrors()) { 324 return false; 325 } 326 327 /// Add the standard PHP header 328 $result .= XMLDB_PHP_HEADER; 329 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; 336 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; 341 342 /// Add the proper upgrade_xxxx_savepoint call 343 $result .= $this->upgrade_savepoint_php ($structure); 344 345 /// Add standard PHP footer 346 $result .= XMLDB_PHP_FOOTER; 347 348 return $result; 349 } 350 351 /** 352 * This function will generate all the PHP code needed to 353 * drop one field using XMLDB objects and functions 354 * 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 359 */ 360 function drop_field_php($structure, $table, $field) { 361 362 $result = ''; 363 /// Validate if we can do it 364 if (!$table = $structure->getTable($table)) { 365 return false; 366 } 367 if (!$field = $table->getField($field)) { 368 return false; 369 } 370 if ($table->getAllErrors()) { 371 return false; 372 } 373 374 /// Add the standard PHP header 375 $result .= XMLDB_PHP_HEADER; 376 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; 382 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; 387 388 /// Add the proper upgrade_xxxx_savepoint call 389 $result .= $this->upgrade_savepoint_php ($structure); 390 391 /// Add standard PHP footer 392 $result .= XMLDB_PHP_FOOTER; 393 394 return $result; 395 } 396 397 /** 398 * This function will generate all the PHP code needed to 399 * rename one field using XMLDB objects and functions 400 * 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 405 */ 406 function rename_field_php($structure, $table, $field) { 407 408 $result = ''; 409 /// Validate if we can do it 410 if (!$table = $structure->getTable($table)) { 411 return false; 412 } 413 if (!$field = $table->getField($field)) { 414 return false; 415 } 416 if ($table->getAllErrors()) { 417 return false; 418 } 419 420 /// Add the standard PHP header 421 $result .= XMLDB_PHP_HEADER; 422 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; 429 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; 434 435 /// Add the proper upgrade_xxxx_savepoint call 436 $result .= $this->upgrade_savepoint_php ($structure); 437 438 /// Add standard PHP footer 439 $result .= XMLDB_PHP_FOOTER; 440 441 return $result; 442 } 443 444 /** 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 454 * 455 * @param XMLDBStructure structure object containing all the info 456 * @param string table table name 457 * @param string field field name to change precision 458 */ 459 function change_field_type_php($structure, $table, $field) { 460 461 $result = ''; 462 /// Validate if we can do it 463 if (!$table = $structure->getTable($table)) { 464 return false; 465 } 466 if (!$field = $table->getField($field)) { 467 return false; 468 } 469 if ($table->getAllErrors()) { 470 return false; 471 } 472 473 /// Calculate the type tip text 474 $type = $field->getXMLDBTypeName($field->getType()); 475 476 /// Add the standard PHP header 477 $result .= XMLDB_PHP_HEADER; 478 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; 485 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; 490 491 /// Add the proper upgrade_xxxx_savepoint call 492 $result .= $this->upgrade_savepoint_php ($structure); 493 494 /// Add standard PHP footer 495 $result .= XMLDB_PHP_FOOTER; 496 497 return $result; 498 } 499 500 /** 501 * This function will generate all the PHP code needed to 502 * change the precision of one field using XMLDB objects and functions 503 * 504 * @param XMLDBStructure structure object containing all the info 505 * @param string table table name 506 * @param string field field name to change precision 507 */ 508 function change_field_precision_php($structure, $table, $field) { 509 510 $result = ''; 511 /// Validate if we can do it 512 if (!$table = $structure->getTable($table)) { 513 return false; 514 } 515 if (!$field = $table->getField($field)) { 516 return false; 517 } 518 if ($table->getAllErrors()) { 519 return false; 520 } 521 522 /// Calculate the precision tip text 523 $precision = '(' . $field->getLength(); 524 if ($field->getDecimals()) { 525 $precision .= ', ' . $field->getDecimals(); 526 } 527 $precision .= ')'; 528 529 /// Add the standard PHP header 530 $result .= XMLDB_PHP_HEADER; 531 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; 538 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; 543 544 /// Add the proper upgrade_xxxx_savepoint call 545 $result .= $this->upgrade_savepoint_php ($structure); 546 547 /// Add standard PHP footer 548 $result .= XMLDB_PHP_FOOTER; 549 550 return $result; 551 } 552 553 /** 554 * This function will generate all the PHP code needed to 555 * change the unsigned/signed of one field using XMLDB objects and functions 556 * 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 560 */ 561 function change_field_unsigned_php($structure, $table, $field) { 562 563 $result = ''; 564 /// Validate if we can do it 565 if (!$table = $structure->getTable($table)) { 566 return false; 567 } 568 if (!$field = $table->getField($field)) { 569 return false; 570 } 571 if ($table->getAllErrors()) { 572 return false; 573 } 574 575 /// Calculate the unsigned tip text 576 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed'; 577 578 /// Add the standard PHP header 579 $result .= XMLDB_PHP_HEADER; 580 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; 587 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; 592 593 /// Add the proper upgrade_xxxx_savepoint call 594 $result .= $this->upgrade_savepoint_php ($structure); 595 596 /// Add standard PHP footer 597 $result .= XMLDB_PHP_FOOTER; 598 599 return $result; 600 } 601 602 /** 603 * This function will generate all the PHP code needed to 604 * change the nullability of one field using XMLDB objects and functions 605 * 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 609 */ 610 function change_field_notnull_php($structure, $table, $field) { 611 612 $result = ''; 613 /// Validate if we can do it 614 if (!$table = $structure->getTable($table)) { 615 return false; 616 } 617 if (!$field = $table->getField($field)) { 618 return false; 619 } 620 if ($table->getAllErrors()) { 621 return false; 622 } 623 624 /// Calculate the notnull tip text 625 $notnull = $field->getNotnull() ? 'not null' : 'null'; 626 627 /// Add the standard PHP header 628 $result .= XMLDB_PHP_HEADER; 629 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; 636 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; 641 642 /// Add the proper upgrade_xxxx_savepoint call 643 $result .= $this->upgrade_savepoint_php ($structure); 644 645 /// Add standard PHP footer 646 $result .= XMLDB_PHP_FOOTER; 647 648 return $result; 649 } 650 651 /** 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 655 * 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 659 */ 660 function change_field_enum_php($structure, $table, $field) { 661 662 $result = ''; 663 /// Validate if we can do it 664 if (!$table = $structure->getTable($table)) { 665 return false; 666 } 667 if (!$field = $table->getField($field)) { 668 return false; 669 } 670 if ($table->getAllErrors()) { 671 return false; 672 } 673 674 /// Calculate the enum tip text 675 $enum = $field->getEnum() ? implode(', ', $field->getEnumValues()) : 'none'; 676 677 /// Add the standard PHP header 678 $result .= XMLDB_PHP_HEADER; 679 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; 686 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; 691 692 /// Add the proper upgrade_xxxx_savepoint call 693 $result .= $this->upgrade_savepoint_php ($structure); 694 695 /// Add standard PHP footer 696 $result .= XMLDB_PHP_FOOTER; 697 698 return $result; 699 } 700 701 /** 702 * This function will generate all the PHP code needed to 703 * change the default of one field using XMLDB objects and functions 704 * 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 708 */ 709 function change_field_default_php($structure, $table, $field) { 710 711 $result = ''; 712 /// Validate if we can do it 713 if (!$table = $structure->getTable($table)) { 714 return false; 715 } 716 if (!$field = $table->getField($field)) { 717 return false; 718 } 719 if ($table->getAllErrors()) { 720 return false; 721 } 722 723 /// Calculate the default tip text 724 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault(); 725 726 /// Add the standard PHP header 727 $result .= XMLDB_PHP_HEADER; 728 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; 735 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; 740 741 /// Add the proper upgrade_xxxx_savepoint call 742 $result .= $this->upgrade_savepoint_php ($structure); 743 744 /// Add standard PHP footer 745 $result .= XMLDB_PHP_FOOTER; 746 747 return $result; 748 } 749 750 /** 751 * This function will generate all the PHP code needed to 752 * create one key using XMLDB objects and functions 753 * 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 758 */ 759 function add_key_php($structure, $table, $key) { 760 761 $result = ''; 762 /// Validate if we can do it 763 if (!$table = $structure->getTable($table)) { 764 return false; 765 } 766 if (!$key = $table->getKey($key)) { 767 return false; 768 } 769 if ($table->getAllErrors()) { 770 return false; 771 } 772 773 /// Add the standard PHP header 774 $result .= XMLDB_PHP_HEADER; 775 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; 782 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; 787 788 /// Add the proper upgrade_xxxx_savepoint call 789 $result .= $this->upgrade_savepoint_php ($structure); 790 791 /// Add standard PHP footer 792 $result .= XMLDB_PHP_FOOTER; 793 794 return $result; 795 } 796 797 /** 798 * This function will generate all the PHP code needed to 799 * drop one key using XMLDB objects and functions 800 * 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 805 */ 806 function drop_key_php($structure, $table, $key) { 807 808 $result = ''; 809 /// Validate if we can do it 810 if (!$table = $structure->getTable($table)) { 811 return false; 812 } 813 if (!$key = $table->getKey($key)) { 814 return false; 815 } 816 if ($table->getAllErrors()) { 817 return false; 818 } 819 820 /// Add the standard PHP header 821 $result .= XMLDB_PHP_HEADER; 822 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; 829 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; 834 835 /// Add the proper upgrade_xxxx_savepoint call 836 $result .= $this->upgrade_savepoint_php ($structure); 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 * rename one key using XMLDB objects and functions 847 * 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 852 */ 853 function rename_key_php($structure, $table, $key) { 854 855 $result = ''; 856 /// Validate if we can do it 857 if (!$table = $structure->getTable($table)) { 858 return false; 859 } 860 if (!$key = $table->getKey($key)) { 861 return false; 862 } 863 if ($table->getAllErrors()) { 864 return false; 865 } 866 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; 869 870 /// Add the standard PHP header 871 $result .= XMLDB_PHP_HEADER; 872 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; 879 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; 884 885 /// Add the proper upgrade_xxxx_savepoint call 886 $result .= $this->upgrade_savepoint_php ($structure); 887 888 /// Add standard PHP footer 889 $result .= XMLDB_PHP_FOOTER; 890 891 return $result; 892 } 893 894 /** 895 * This function will generate all the PHP code needed to 896 * create one index using XMLDB objects and functions 897 * 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 902 */ 903 function add_index_php($structure, $table, $index) { 904 905 $result = ''; 906 /// Validate if we can do it 907 if (!$table = $structure->getTable($table)) { 908 return false; 909 } 910 if (!$index = $table->getIndex($index)) { 911 return false; 912 } 913 if ($table->getAllErrors()) { 914 return false; 915 } 916 917 /// Add the standard PHP header 918 $result .= XMLDB_PHP_HEADER; 919 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; 926 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; 931 932 /// Add the proper upgrade_xxxx_savepoint call 933 $result .= $this->upgrade_savepoint_php ($structure); 934 935 /// Add standard PHP footer 936 $result .= XMLDB_PHP_FOOTER; 937 938 return $result; 939 } 940 941 /** 942 * This function will generate all the PHP code needed to 943 * drop one index using XMLDB objects and functions 944 * 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 949 */ 950 function drop_index_php($structure, $table, $index) { 951 952 $result = ''; 953 /// Validate if we can do it 954 if (!$table = $structure->getTable($table)) { 955 return false; 956 } 957 if (!$index = $table->getIndex($index)) { 958 return false; 959 } 960 if ($table->getAllErrors()) { 961 return false; 962 } 963 964 /// Add the standard PHP header 965 $result .= XMLDB_PHP_HEADER; 966 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; 973 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; 978 979 /// Add the proper upgrade_xxxx_savepoint call 980 $result .= $this->upgrade_savepoint_php ($structure); 981 982 /// Add standard PHP footer 983 $result .= XMLDB_PHP_FOOTER; 984 985 return $result; 986 } 987 988 /** 989 * This function will generate all the PHP code needed to 990 * rename one index using XMLDB objects and functions 991 * 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 996 */ 997 function rename_index_php($structure, $table, $index) { 998 999 $result = ''; 1000 /// Validate if we can do it 1001 if (!$table = $structure->getTable($table)) { 1002 return false; 1003 } 1004 if (!$index = $table->getIndex($index)) { 1005 return false; 1006 } 1007 if ($table->getAllErrors()) { 1008 return false; 1009 } 1010 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; 1013 1014 /// Add the standard PHP header 1015 $result .= XMLDB_PHP_HEADER; 1016 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; 1023 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; 1028 1029 /// Add the proper upgrade_xxxx_savepoint call 1030 $result .= $this->upgrade_savepoint_php ($structure); 1031 1032 /// Add standard PHP footer 1033 $result .= XMLDB_PHP_FOOTER; 1034 1035 return $result; 1036 } 1037 1038 /** 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 1042 * 1043 * @param XMLDBStructure structure object containing all the info 1044 * @return string PHP code to be used to stabilish a savepoint 1045 */ 1046 function upgrade_savepoint_php ($structure) { 1047 1048 $path = $structure->getPath(); 1049 1050 $result = ''; 1051 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; 1058 } 1059 return $result; 1060 } 1061 1062 } 1063 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |