Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/view_table_php/view_table_php.class.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00033 class view_table_php extends XMLDBAction {
00034 
00038     function init() {
00039         parent::init();
00040 
00041         // Set own custom attributes
00042         $this->sesskey_protected = false; // This action doesn't need sesskey protection
00043 
00044         // Get needed strings
00045         $this->loadStrings(array(
00046             'selectaction' => 'tool_xmldb',
00047             'selectfieldkeyindex' => 'tool_xmldb',
00048             'view' => 'tool_xmldb',
00049             'table' => 'tool_xmldb',
00050             'selectonecommand' => 'tool_xmldb',
00051             'selectonefieldkeyindex' => 'tool_xmldb',
00052             'mustselectonefield' => 'tool_xmldb',
00053             'mustselectonekey' => 'tool_xmldb',
00054             'mustselectoneindex' => 'tool_xmldb',
00055             'back' => 'tool_xmldb'
00056         ));
00057     }
00058 
00064     function invoke() {
00065         parent::invoke();
00066 
00067         $result = true;
00068 
00069         // Set own core attributes
00070         $this->does_generate = ACTION_GENERATE_HTML;
00071 
00072         // These are always here
00073         global $CFG, $XMLDB, $OUTPUT;
00074 
00075         // Do the job, setting result as needed
00076         // Get the dir containing the file
00077         $dirpath = required_param('dir', PARAM_PATH);
00078         $dirpath = $CFG->dirroot . $dirpath;
00079 
00080         // Get the correct dirs
00081         if (!empty($XMLDB->dbdirs)) {
00082             $dbdir =& $XMLDB->dbdirs[$dirpath];
00083         } else {
00084             return false;
00085         }
00086         if (!empty($XMLDB->editeddirs)) {
00087             $editeddir =& $XMLDB->editeddirs[$dirpath];
00088             $structure =& $editeddir->xml_file->getStructure();
00089         }
00090 
00091         $tableparam = required_param('table', PARAM_PATH);
00092 
00093         $table =& $structure->getTable($tableparam);
00094         $fields = $table->getFields();
00095         $field = reset($fields);
00096         $defaultfieldkeyindex = null;
00097         if ($field) {
00098             $defaultfieldkeyindex = 'f#' . $field->getName();
00099         }
00100         $keys = $table->getKeys();
00101         $indexes = $table->getIndexes();
00102 
00103         // Get parameters
00104         $commandparam = optional_param('command', 'add_field', PARAM_PATH);
00105         $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
00106         $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); // Strip the initials
00107         $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
00108 
00109         // The back to edit xml button
00110         $b = ' <p class="centerpara buttons">';
00111         $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
00112         $b .= '</p>';
00113         $o = $b;
00114 
00115         // The table currently being edited
00116         $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
00117 
00118         // To indent the menu selections
00119         $optionspacer = '&nbsp;&nbsp;&nbsp;';
00120 
00121         // Calculate the popup of commands
00122         $commands = array('Fields',
00123                          $optionspacer . 'add_field',
00124                          $optionspacer . 'drop_field',
00125                          $optionspacer . 'rename_field',
00126                          $optionspacer . 'change_field_type',
00127                          $optionspacer . 'change_field_precision',
00128                          $optionspacer . 'change_field_unsigned',
00129                          $optionspacer . 'change_field_notnull',
00130                          $optionspacer . 'change_field_default',
00131                          $optionspacer . 'drop_enum_from_field', // TODO: Moodle 2.1 - Drop drop_enum_from_field
00132                          'Keys',
00133                          $optionspacer . 'add_key',
00134                          $optionspacer . 'drop_key',
00135                          $optionspacer . 'rename_key',
00136                          'Indexes',
00137                          $optionspacer . 'add_index',
00138                          $optionspacer . 'drop_index',
00139                          $optionspacer . 'rename_index');
00140         foreach ($commands as $command) {
00141             $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
00142         }
00143         // Calculate the popup of fields/keys/indexes
00144         if ($fields) {
00145             $popfields['fieldshead'] = 'Fields';
00146             foreach ($fields as $field) {
00147                 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
00148             }
00149         }
00150         if ($keys) {
00151             $popfields['keyshead'] = 'Keys';
00152             foreach ($keys as $key) {
00153                 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
00154             }
00155         }
00156         if ($indexes) {
00157             $popfields['indexeshead'] = 'Indexes';
00158             foreach ($indexes as $index) {
00159                 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
00160             }
00161         }
00162 
00163         // Now build the form
00164         $o.= '<form id="form" action="index.php" method="post">';
00165         $o.= '<div>';
00166         $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
00167         $o.= '    <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
00168         $o.= '    <input type="hidden" name ="action" value="view_table_php" />';
00169         $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
00170         $o.= '      <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . html_writer::select($popcommands, 'command', $commandparam, false) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .html_writer::select($popfields, 'fieldkeyindex', $origfieldkeyindexparam, false) . '</td></tr>';
00171         $o.= '      <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
00172         $o.= '    </table>';
00173         $o.= '</div></form>';
00174 
00175         $o.= '    <table id="phpcode" class="boxaligncenter" cellpadding="5">';
00176         $o.= '      <tr><td><textarea cols="80" rows="32">';
00177         // Check we have selected some field/key/index from the popup
00178         if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
00179             $o.= s($this->str['selectonefieldkeyindex']);
00180         // Check we have selected some command from the popup
00181         } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
00182             $o.= s($this->str['selectonecommand']);
00183         } else {
00184             // Based on current params, call the needed function
00185             switch ($commandparam) {
00186                 case 'add_field':
00187                     if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
00188                         $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
00189                     } else {
00190                         $o.= $this->str['mustselectonefield'];
00191                     }
00192                     break;
00193                 case 'drop_field':
00194                     if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
00195                         $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
00196                     } else {
00197                         $o.= $this->str['mustselectonefield'];
00198                     }
00199                     break;
00200                 case 'rename_field':
00201                     if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
00202                         $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
00203                     } else {
00204                         $o.= $this->str['mustselectonefield'];
00205                     }
00206                     break;
00207                 case 'change_field_type':
00208                     if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
00209                         $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam));
00210                     } else {
00211                         $o.= $this->str['mustselectonefield'];
00212                     }
00213                     break;
00214                 case 'change_field_precision':
00215                     if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
00216                         $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
00217                     } else {
00218                         $o.= $this->str['mustselectonefield'];
00219                     }
00220                     break;
00221                 case 'change_field_unsigned':
00222                     if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
00223                         $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
00224                     } else {
00225                         $o.= $this->str['mustselectonefield'];
00226                     }
00227                     break;
00228                 case 'change_field_notnull':
00229                     if ($fieldkeyindexinitial == 'f') { // Only if we have got one field
00230                         $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
00231                     } else {
00232                         $o.= $this->str['mustselectonefield'];
00233                     }
00234                     break;
00235                 case 'drop_enum_from_field': // TODO: Moodle 2.1 - Drop drop_enum_from_field
00236                     if ($fieldkeyindexinitial == 'f') { // Only if we have got one field
00237                         $o.= s($this->drop_enum_from_field_php($structure, $tableparam, $fieldkeyindexparam));
00238                     } else {
00239                         $o.= $this->str['mustselectonefield'];
00240                     }
00241                     break;
00242                 case 'change_field_default':
00243                     if ($fieldkeyindexinitial == 'f') { // Only if we have got one field
00244                         $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
00245                     } else {
00246                         $o.= $this->str['mustselectonefield'];
00247                     }
00248                     break;
00249                 case 'add_key':
00250                     if ($fieldkeyindexinitial == 'k') { // Only if we have got one key
00251                         $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
00252                     } else {
00253                         $o.= $this->str['mustselectonekey'];
00254                     }
00255                     break;
00256                 case 'drop_key':
00257                     if ($fieldkeyindexinitial == 'k') { // Only if we have got one key
00258                         $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
00259                     } else {
00260                         $o.= $this->str['mustselectonekey'];
00261                     }
00262                     break;
00263                 case 'rename_key':
00264                     if ($fieldkeyindexinitial == 'k') { // Only if we have got one key
00265                         $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
00266                     } else {
00267                         $o.= $this->str['mustselectonekey'];
00268                     }
00269                     break;
00270                 case 'add_index':
00271                     if ($fieldkeyindexinitial == 'i') { // Only if we have got one index
00272                         $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
00273                     } else {
00274                         $o.= $this->str['mustselectoneindex'];
00275                     }
00276                     break;
00277                 case 'drop_index':
00278                     if ($fieldkeyindexinitial == 'i') { // Only if we have got one index
00279                         $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
00280                     } else {
00281                         $o.= $this->str['mustselectoneindex'];
00282                     }
00283                     break;
00284                 case 'rename_index':
00285                     if ($fieldkeyindexinitial == 'i') { // Only if we have got one index
00286                         $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
00287                     } else {
00288                         $o.= $this->str['mustselectoneindex'];
00289                     }
00290                     break;
00291             }
00292         }
00293         $o.= '</textarea></td></tr>';
00294         $o.= '    </table>';
00295 
00296         $this->output = $o;
00297 
00298         // Launch postaction if exists (leave this here!)
00299         if ($this->getPostAction() && $result) {
00300             return $this->launch($this->getPostAction());
00301         }
00302 
00303         // Return ok if arrived here
00304         return $result;
00305     }
00306 
00316     function add_field_php($structure, $table, $field) {
00317 
00318         $result = '';
00319         // Validate if we can do it
00320         if (!$table = $structure->getTable($table)) {
00321             return false;
00322         }
00323         if (!$field = $table->getField($field)) {
00324             return false;
00325         }
00326         if ($table->getAllErrors()) {
00327             return false;
00328         }
00329 
00330         // Add the standard PHP header
00331         $result .= XMLDB_PHP_HEADER;
00332 
00333         // Add contents
00334         $result .= XMLDB_LINEFEED;
00335         $result .= '        // Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
00336         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00337         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00338 
00339         // Launch the proper DDL
00340         $result .= XMLDB_LINEFEED;
00341         $result .= '        // Conditionally launch add field ' . $field->getName() . XMLDB_LINEFEED;
00342         $result .= '        if (!$dbman->field_exists($table, $field)) {'. XMLDB_LINEFEED;
00343         $result .= '            $dbman->add_field($table, $field);' . XMLDB_LINEFEED;
00344         $result .= '        }'. XMLDB_LINEFEED;
00345 
00346         // Add the proper upgrade_xxxx_savepoint call
00347         $result .= $this->upgrade_savepoint_php ($structure);
00348 
00349         // Add standard PHP footer
00350         $result .= XMLDB_PHP_FOOTER;
00351 
00352         return $result;
00353     }
00354 
00364     function drop_field_php($structure, $table, $field) {
00365 
00366         $result = '';
00367         // Validate if we can do it
00368         if (!$table = $structure->getTable($table)) {
00369             return false;
00370         }
00371         if (!$field = $table->getField($field)) {
00372             return false;
00373         }
00374         if ($table->getAllErrors()) {
00375             return false;
00376         }
00377 
00378         // Add the standard PHP header
00379         $result .= XMLDB_PHP_HEADER;
00380 
00381         // Add contents
00382         $result .= XMLDB_LINEFEED;
00383         $result .= '        // Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
00384         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00385         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
00386 
00387         // Launch the proper DDL
00388         $result .= XMLDB_LINEFEED;
00389         $result .= '        // Conditionally launch drop field ' . $field->getName() . XMLDB_LINEFEED;
00390         $result .= '        if ($dbman->field_exists($table, $field)) {' . XMLDB_LINEFEED;
00391         $result .= '            $dbman->drop_field($table, $field);' . XMLDB_LINEFEED;
00392         $result .= '        }' . XMLDB_LINEFEED;
00393 
00394         // Add the proper upgrade_xxxx_savepoint call
00395         $result .= $this->upgrade_savepoint_php ($structure);
00396 
00397         // Add standard PHP footer
00398         $result .= XMLDB_PHP_FOOTER;
00399 
00400         return $result;
00401     }
00402 
00412     function rename_field_php($structure, $table, $field) {
00413 
00414         $result = '';
00415         // Validate if we can do it
00416         if (!$table = $structure->getTable($table)) {
00417             return false;
00418         }
00419         if (!$field = $table->getField($field)) {
00420             return false;
00421         }
00422         if ($table->getAllErrors()) {
00423             return false;
00424         }
00425 
00426         // Add the standard PHP header
00427         $result .= XMLDB_PHP_HEADER;
00428 
00429         // Add contents
00430         $result .= XMLDB_LINEFEED;
00431         $result .= '        // Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
00432         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00433         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00434 
00435         // Launch the proper DDL
00436         $result .= XMLDB_LINEFEED;
00437         $result .= '        // Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
00438         $result .= '        $dbman->rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
00439 
00440         // Add the proper upgrade_xxxx_savepoint call
00441         $result .= $this->upgrade_savepoint_php ($structure);
00442 
00443         // Add standard PHP footer
00444         $result .= XMLDB_PHP_FOOTER;
00445 
00446         return $result;
00447     }
00448 
00464     function change_field_type_php($structure, $table, $field) {
00465 
00466         $result = '';
00467         // Validate if we can do it
00468         if (!$table = $structure->getTable($table)) {
00469             return false;
00470         }
00471         if (!$field = $table->getField($field)) {
00472             return false;
00473         }
00474         if ($table->getAllErrors()) {
00475             return false;
00476         }
00477 
00478         // Calculate the type tip text
00479         $type = $field->getXMLDBTypeName($field->getType());
00480 
00481         // Add the standard PHP header
00482         $result .= XMLDB_PHP_HEADER;
00483 
00484         // Add contents
00485         $result .= XMLDB_LINEFEED;
00486         $result .= '        // Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED;
00487         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00488         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00489 
00490         // Launch the proper DDL
00491         $result .= XMLDB_LINEFEED;
00492         $result .= '        // Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED;
00493         $result .= '        $dbman->change_field_type($table, $field);' . XMLDB_LINEFEED;
00494 
00495         // Add the proper upgrade_xxxx_savepoint call
00496         $result .= $this->upgrade_savepoint_php ($structure);
00497 
00498         // Add standard PHP footer
00499         $result .= XMLDB_PHP_FOOTER;
00500 
00501         return $result;
00502     }
00503 
00512     function change_field_precision_php($structure, $table, $field) {
00513 
00514         $result = '';
00515         // Validate if we can do it
00516         if (!$table = $structure->getTable($table)) {
00517             return false;
00518         }
00519         if (!$field = $table->getField($field)) {
00520             return false;
00521         }
00522         if ($table->getAllErrors()) {
00523             return false;
00524         }
00525 
00526         // Calculate the precision tip text
00527         $precision = '(' . $field->getLength();
00528         if ($field->getDecimals()) {
00529             $precision .= ', ' . $field->getDecimals();
00530         }
00531         $precision .= ')';
00532 
00533         // Add the standard PHP header
00534         $result .= XMLDB_PHP_HEADER;
00535 
00536         // Add contents
00537         $result .= XMLDB_LINEFEED;
00538         $result .= '        // Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
00539         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00540         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " .$field->getPHP(true) . ');' . XMLDB_LINEFEED;
00541 
00542         // Launch the proper DDL
00543         $result .= XMLDB_LINEFEED;
00544         $result .= '        // Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
00545         $result .= '        $dbman->change_field_precision($table, $field);' . XMLDB_LINEFEED;
00546 
00547         // Add the proper upgrade_xxxx_savepoint call
00548         $result .= $this->upgrade_savepoint_php ($structure);
00549 
00550         // Add standard PHP footer
00551         $result .= XMLDB_PHP_FOOTER;
00552 
00553         return $result;
00554     }
00555 
00564     function change_field_unsigned_php($structure, $table, $field) {
00565 
00566         $result = '';
00567         // Validate if we can do it
00568         if (!$table = $structure->getTable($table)) {
00569             return false;
00570         }
00571         if (!$field = $table->getField($field)) {
00572             return false;
00573         }
00574         if ($table->getAllErrors()) {
00575             return false;
00576         }
00577 
00578         // Calculate the unsigned tip text
00579         $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
00580 
00581         // Add the standard PHP header
00582         $result .= XMLDB_PHP_HEADER;
00583 
00584         // Add contents
00585         $result .= XMLDB_LINEFEED;
00586         $result .= '        // Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
00587         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00588         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00589 
00590         // Launch the proper DDL
00591         $result .= XMLDB_LINEFEED;
00592         $result .= '        // Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
00593         $result .= '        $dbman->change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
00594 
00595         // Add the proper upgrade_xxxx_savepoint call
00596         $result .= $this->upgrade_savepoint_php ($structure);
00597 
00598         // Add standard PHP footer
00599         $result .= XMLDB_PHP_FOOTER;
00600 
00601         return $result;
00602     }
00603 
00612     function change_field_notnull_php($structure, $table, $field) {
00613 
00614         $result = '';
00615         // Validate if we can do it
00616         if (!$table = $structure->getTable($table)) {
00617             return false;
00618         }
00619         if (!$field = $table->getField($field)) {
00620             return false;
00621         }
00622         if ($table->getAllErrors()) {
00623             return false;
00624         }
00625 
00626         // Calculate the notnull tip text
00627         $notnull = $field->getNotnull() ? 'not null' : 'null';
00628 
00629         // Add the standard PHP header
00630         $result .= XMLDB_PHP_HEADER;
00631 
00632         // Add contents
00633         $result .= XMLDB_LINEFEED;
00634         $result .= '        // Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
00635         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00636         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00637 
00638         // Launch the proper DDL
00639         $result .= XMLDB_LINEFEED;
00640         $result .= '        // Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
00641         $result .= '        $dbman->change_field_notnull($table, $field);' . XMLDB_LINEFEED;
00642 
00643         // Add the proper upgrade_xxxx_savepoint call
00644         $result .= $this->upgrade_savepoint_php ($structure);
00645 
00646         // Add standard PHP footer
00647         $result .= XMLDB_PHP_FOOTER;
00648 
00649         return $result;
00650     }
00651 
00666     function drop_enum_from_field_php($structure, $table, $field) {
00667 
00668         $result = '';
00669         // Validate if we can do it
00670         if (!$table = $structure->getTable($table)) {
00671             return false;
00672         }
00673         if (!$field = $table->getField($field)) {
00674             return false;
00675         }
00676         if ($table->getAllErrors()) {
00677             return false;
00678         }
00679 
00680         // Add the standard PHP header
00681         $result .= XMLDB_PHP_HEADER;
00682 
00683         // Add contents
00684         $result .= XMLDB_LINEFEED;
00685         $result .= '        // Drop list of values (enum) from field ' . $field->getName() . ' on table ' . $table->getName() . XMLDB_LINEFEED;
00686         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00687         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00688 
00689         // Launch the proper DDL
00690         $result .= XMLDB_LINEFEED;
00691         $result .= '        // Launch drop of list of values from field ' . $field->getName() . XMLDB_LINEFEED;
00692         $result .= '        $dbman->drop_enum_from_field($table, $field);' . XMLDB_LINEFEED;
00693 
00694         // Add the proper upgrade_xxxx_savepoint call
00695         $result .= $this->upgrade_savepoint_php ($structure);
00696 
00697         // Add standard PHP footer
00698         $result .= XMLDB_PHP_FOOTER;
00699 
00700         return $result;
00701     }
00702 
00711     function change_field_default_php($structure, $table, $field) {
00712 
00713         $result = '';
00714         // Validate if we can do it
00715         if (!$table = $structure->getTable($table)) {
00716             return false;
00717         }
00718         if (!$field = $table->getField($field)) {
00719             return false;
00720         }
00721         if ($table->getAllErrors()) {
00722             return false;
00723         }
00724 
00725         // Calculate the default tip text
00726         $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
00727 
00728         // Add the standard PHP header
00729         $result .= XMLDB_PHP_HEADER;
00730 
00731         // Add contents
00732         $result .= XMLDB_LINEFEED;
00733         $result .= '        // Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
00734         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00735         $result .= '        $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
00736 
00737         // Launch the proper DDL
00738         $result .= XMLDB_LINEFEED;
00739         $result .= '        // Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
00740         $result .= '        $dbman->change_field_default($table, $field);' . XMLDB_LINEFEED;
00741 
00742         // Add the proper upgrade_xxxx_savepoint call
00743         $result .= $this->upgrade_savepoint_php ($structure);
00744 
00745         // Add standard PHP footer
00746         $result .= XMLDB_PHP_FOOTER;
00747 
00748         return $result;
00749     }
00750 
00760     function add_key_php($structure, $table, $key) {
00761 
00762         $result = '';
00763         // Validate if we can do it
00764         if (!$table = $structure->getTable($table)) {
00765             return false;
00766         }
00767         if (!$key = $table->getKey($key)) {
00768             return false;
00769         }
00770         if ($table->getAllErrors()) {
00771             return false;
00772         }
00773 
00774         // Add the standard PHP header
00775         $result .= XMLDB_PHP_HEADER;
00776 
00777         // Add contents
00778         $result .= XMLDB_LINEFEED;
00779         $result .= '        // Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
00780         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00781         $result .= '        $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
00782 
00783         // Launch the proper DDL
00784         $result .= XMLDB_LINEFEED;
00785         $result .= '        // Launch add key ' . $key->getName() . XMLDB_LINEFEED;
00786         $result .= '        $dbman->add_key($table, $key);' . XMLDB_LINEFEED;
00787 
00788         // Add the proper upgrade_xxxx_savepoint call
00789         $result .= $this->upgrade_savepoint_php ($structure);
00790 
00791         // Add standard PHP footer
00792         $result .= XMLDB_PHP_FOOTER;
00793 
00794         return $result;
00795     }
00796 
00806     function drop_key_php($structure, $table, $key) {
00807 
00808         $result = '';
00809         // Validate if we can do it
00810         if (!$table = $structure->getTable($table)) {
00811             return false;
00812         }
00813         if (!$key = $table->getKey($key)) {
00814             return false;
00815         }
00816         if ($table->getAllErrors()) {
00817             return false;
00818         }
00819 
00820         // Add the standard PHP header
00821         $result .= XMLDB_PHP_HEADER;
00822 
00823         // Add contents
00824         $result .= XMLDB_LINEFEED;
00825         $result .= '        // Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
00826         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00827         $result .= '        $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
00828 
00829         // Launch the proper DDL
00830         $result .= XMLDB_LINEFEED;
00831         $result .= '        // Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
00832         $result .= '        $dbman->drop_key($table, $key);' . XMLDB_LINEFEED;
00833 
00834         // Add the proper upgrade_xxxx_savepoint call
00835         $result .= $this->upgrade_savepoint_php ($structure);
00836 
00837         // Add standard PHP footer
00838         $result .= XMLDB_PHP_FOOTER;
00839 
00840         return $result;
00841     }
00842 
00852     function rename_key_php($structure, $table, $key) {
00853 
00854         $result = '';
00855         // Validate if we can do it
00856         if (!$table = $structure->getTable($table)) {
00857             return false;
00858         }
00859         if (!$key = $table->getKey($key)) {
00860             return false;
00861         }
00862         if ($table->getAllErrors()) {
00863             return false;
00864         }
00865 
00866         // Prepend warning. This function isn't usable!
00867         $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
00868 
00869         // Add the standard PHP header
00870         $result .= XMLDB_PHP_HEADER;
00871 
00872         // Add contents
00873         $result .= XMLDB_LINEFEED;
00874         $result .= '        // Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
00875         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00876         $result .= '        $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
00877 
00878         // Launch the proper DDL
00879         $result .= XMLDB_LINEFEED;
00880         $result .= '        // Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
00881         $result .= '        $dbman->rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
00882 
00883         // Add the proper upgrade_xxxx_savepoint call
00884         $result .= $this->upgrade_savepoint_php ($structure);
00885 
00886         // Add standard PHP footer
00887         $result .= XMLDB_PHP_FOOTER;
00888 
00889         return $result;
00890     }
00891 
00901     function add_index_php($structure, $table, $index) {
00902 
00903         $result = '';
00904         // Validate if we can do it
00905         if (!$table = $structure->getTable($table)) {
00906             return false;
00907         }
00908         if (!$index = $table->getIndex($index)) {
00909             return false;
00910         }
00911         if ($table->getAllErrors()) {
00912             return false;
00913         }
00914 
00915         // Add the standard PHP header
00916         $result .= XMLDB_PHP_HEADER;
00917 
00918         // Add contents
00919         $result .= XMLDB_LINEFEED;
00920         $result .= '        // Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
00921         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00922         $result .= '        $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
00923 
00924         // Launch the proper DDL
00925         $result .= XMLDB_LINEFEED;
00926         $result .= '        // Conditionally launch add index ' . $index->getName() . XMLDB_LINEFEED;
00927         $result .= '        if (!$dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
00928         $result .= '            $dbman->add_index($table, $index);' . XMLDB_LINEFEED;
00929         $result .= '        }' . XMLDB_LINEFEED;
00930 
00931         // Add the proper upgrade_xxxx_savepoint call
00932         $result .= $this->upgrade_savepoint_php ($structure);
00933 
00934         // Add standard PHP footer
00935         $result .= XMLDB_PHP_FOOTER;
00936 
00937         return $result;
00938     }
00939 
00949     function drop_index_php($structure, $table, $index) {
00950 
00951         $result = '';
00952         // Validate if we can do it
00953         if (!$table = $structure->getTable($table)) {
00954             return false;
00955         }
00956         if (!$index = $table->getIndex($index)) {
00957             return false;
00958         }
00959         if ($table->getAllErrors()) {
00960             return false;
00961         }
00962 
00963         // Add the standard PHP header
00964         $result .= XMLDB_PHP_HEADER;
00965 
00966         // Add contents
00967         $result .= XMLDB_LINEFEED;
00968         $result .= '        // Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
00969         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00970         $result .= '        $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
00971 
00972         // Launch the proper DDL
00973         $result .= XMLDB_LINEFEED;
00974         $result .= '        // Conditionally launch drop index ' . $index->getName() . XMLDB_LINEFEED;
00975         $result .= '        if ($dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
00976         $result .= '            $dbman->drop_index($table, $index);' . XMLDB_LINEFEED;
00977         $result .= '        }' . XMLDB_LINEFEED;
00978 
00979         // Add the proper upgrade_xxxx_savepoint call
00980         $result .= $this->upgrade_savepoint_php ($structure);
00981 
00982         // Add standard PHP footer
00983         $result .= XMLDB_PHP_FOOTER;
00984 
00985         return $result;
00986     }
00987 
00997     function rename_index_php($structure, $table, $index) {
00998 
00999         $result = '';
01000         // Validate if we can do it
01001         if (!$table = $structure->getTable($table)) {
01002             return false;
01003         }
01004         if (!$index = $table->getIndex($index)) {
01005             return false;
01006         }
01007         if ($table->getAllErrors()) {
01008             return false;
01009         }
01010 
01011         // Prepend warning. This function isn't usable!
01012         $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
01013 
01014         // Add the standard PHP header
01015         $result .= XMLDB_PHP_HEADER;
01016 
01017         // Add contents
01018         $result .= XMLDB_LINEFEED;
01019         $result .= '        // Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
01020         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
01021         $result .= '        $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
01022 
01023         // Launch the proper DDL
01024         $result .= XMLDB_LINEFEED;
01025         $result .= '        // Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
01026         $result .= '        $dbman->rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
01027 
01028         // Add the proper upgrade_xxxx_savepoint call
01029         $result .= $this->upgrade_savepoint_php ($structure);
01030 
01031         // Add standard PHP footer
01032         $result .= XMLDB_PHP_FOOTER;
01033 
01034         return $result;
01035     }
01036 
01037 }
01038 
 All Data Structures Namespaces Files Functions Variables Enumerations