Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/view_structure_php/view_structure_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_structure_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             'selecttable' => 'tool_xmldb',
00048             'view' => 'tool_xmldb',
00049             'back' => 'tool_xmldb'
00050         ));
00051     }
00052 
00058     function invoke() {
00059         parent::invoke();
00060 
00061         $result = true;
00062 
00063         // Set own core attributes
00064         $this->does_generate = ACTION_GENERATE_HTML;
00065 
00066         // These are always here
00067         global $CFG, $XMLDB, $OUTPUT;
00068 
00069         // Do the job, setting result as needed
00070         // Get the dir containing the file
00071         $dirpath = required_param('dir', PARAM_PATH);
00072         $dirpath = $CFG->dirroot . $dirpath;
00073 
00074         // Get the correct dirs
00075         if (!empty($XMLDB->dbdirs)) {
00076             $dbdir =& $XMLDB->dbdirs[$dirpath];
00077         } else {
00078             return false;
00079         }
00080         if (!empty($XMLDB->editeddirs)) {
00081             $editeddir =& $XMLDB->editeddirs[$dirpath];
00082             $structure =& $editeddir->xml_file->getStructure();
00083         }
00084 
00085         $tables =& $structure->getTables();
00086         $table = reset($tables);
00087         $defaulttable = null;
00088         if ($table) {
00089             $defaulttable = $table->getName();
00090         }
00091 
00092         // Get parameters
00093         $commandparam = optional_param('command', 'create_table', PARAM_PATH);
00094         $tableparam = optional_param('table', $defaulttable, PARAM_PATH);
00095 
00096         // The back to edit xml button
00097         $b = ' <p class="centerpara buttons">';
00098         $b .= '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
00099         $b .= '</p>';
00100         $o = $b;
00101 
00102         // Calculate the popup of commands
00103         $commands = array('create_table',
00104                          'drop_table',
00105                          'rename_table');
00106         foreach ($commands as $command) {
00107             $popcommands[$command] = str_replace('_', ' ', $command);
00108         }
00109         // Calculate the popup of tables
00110         foreach ($tables as $table) {
00111             $poptables[$table->getName()] = $table->getName();
00112         }
00113         // Now build the form
00114         $o.= '<form id="form" action="index.php" method="post">';
00115         $o.='<div>';
00116         $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
00117         $o.= '    <input type="hidden" name ="action" value="view_structure_php" />';
00118         $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
00119         $o.= '      <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . html_writer::select($popcommands, 'command', $commandparam, false) . '&nbsp;<label for="table" accesskey="t">' . $this->str['selecttable'] . ' </label>' .html_writer::select($poptables, 'table', $tableparam, false) . '</td></tr>';
00120         $o.= '      <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
00121         $o.= '    </table>';
00122         $o.= '</div></form>';
00123         $o.= '    <table id="phpcode" class="boxaligncenter" cellpadding="5">';
00124         $o.= '      <tr><td><textarea cols="80" rows="32">';
00125         // Based on current params, call the needed function
00126         switch ($commandparam) {
00127             case 'create_table':
00128                 $o.= s($this->create_table_php($structure, $tableparam));
00129                 break;
00130             case 'drop_table':
00131                 $o.= s($this->drop_table_php($structure, $tableparam));
00132                 break;
00133             case 'rename_table':
00134                 $o.= s($this->rename_table_php($structure, $tableparam));
00135                 break;
00136         }
00137         $o.= '</textarea></td></tr>';
00138         $o.= '    </table>';
00139 
00140         $this->output = $o;
00141 
00142         // Launch postaction if exists (leave this here!)
00143         if ($this->getPostAction() && $result) {
00144             return $this->launch($this->getPostAction());
00145         }
00146 
00147         // Return ok if arrived here
00148         return $result;
00149     }
00150 
00159     function create_table_php($structure, $table) {
00160 
00161         $result = '';
00162         // Validate if we can do it
00163         if (!$table = $structure->getTable($table)) {
00164             return false;
00165         }
00166         if ($table->getAllErrors()) {
00167             return false;
00168         }
00169 
00170         // Add the standard PHP header
00171         $result .= XMLDB_PHP_HEADER;
00172 
00173         // Add contents
00174         $result .= XMLDB_LINEFEED;
00175         $result .= '        // Define table ' . $table->getName() . ' to be created' . XMLDB_LINEFEED;
00176         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00177         $result .= XMLDB_LINEFEED;
00178         $result .= '        // Adding fields to table ' . $table->getName() . XMLDB_LINEFEED;
00179         // Iterate over each field
00180         foreach ($table->getFields() as $field) {
00181             // The field header, with name
00182             $result .= '        $table->add_field(' . "'" . $field->getName() . "', ";
00183             // The field PHP specs
00184             $result .= $field->getPHP(false);
00185             // The end of the line
00186             $result .= ');' . XMLDB_LINEFEED;
00187         }
00188         // Iterate over each key
00189         if ($keys = $table->getKeys()) {
00190             $result .= XMLDB_LINEFEED;
00191             $result .= '        // Adding keys to table ' . $table->getName() . XMLDB_LINEFEED;
00192             foreach ($keys as $key) {
00193                 // The key header, with name
00194                 $result .= '        $table->add_key(' . "'" . $key->getName() . "', ";
00195                 // The key PHP specs
00196                 $result .= $key->getPHP();
00197                 // The end of the line
00198                 $result .= ');' . XMLDB_LINEFEED;
00199             }
00200         }
00201         // Iterate over each index
00202         if ($indexes = $table->getIndexes()) {
00203             $result .= XMLDB_LINEFEED;
00204             $result .= '        // Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED;
00205             foreach ($indexes as $index) {
00206                 // The index header, with name
00207                 $result .= '        $table->add_index(' . "'" . $index->getName() . "', ";
00208                 // The index PHP specs
00209                 $result .= $index->getPHP();
00210                 // The end of the line
00211                 $result .= ');' . XMLDB_LINEFEED;
00212             }
00213         }
00214 
00215         // Launch the proper DDL
00216         $result .= XMLDB_LINEFEED;
00217         $result .= '        // Conditionally launch create table for ' . $table->getName() . XMLDB_LINEFEED;
00218         $result .= '        if (!$dbman->table_exists($table)) {' . XMLDB_LINEFEED;
00219         $result .= '            $dbman->create_table($table);' . XMLDB_LINEFEED;
00220         $result .= '        }' . XMLDB_LINEFEED;
00221 
00222         // Add the proper upgrade_xxxx_savepoint call
00223         $result .= $this->upgrade_savepoint_php ($structure);
00224 
00225         // Add standard PHP footer
00226         $result .= XMLDB_PHP_FOOTER;
00227 
00228         return $result;
00229     }
00230 
00239     function drop_table_php($structure, $table) {
00240 
00241         $result = '';
00242         // Validate if we can do it
00243         if (!$table = $structure->getTable($table)) {
00244             return false;
00245         }
00246         if ($table->getAllErrors()) {
00247             return false;
00248         }
00249 
00250         // Add the standard PHP header
00251         $result .= XMLDB_PHP_HEADER;
00252 
00253         // Add contents
00254         $result .= XMLDB_LINEFEED;
00255         $result .= '        // Define table ' . $table->getName() . ' to be dropped' . XMLDB_LINEFEED;
00256         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00257 
00258         // Launch the proper DDL
00259         $result .= XMLDB_LINEFEED;
00260         $result .= '        // Conditionally launch drop table for ' . $table->getName() . XMLDB_LINEFEED;
00261         $result .= '        if ($dbman->table_exists($table)) {' . XMLDB_LINEFEED;
00262         $result .= '            $dbman->drop_table($table);' . XMLDB_LINEFEED;
00263         $result .= '        }' . XMLDB_LINEFEED;
00264 
00265         // Add the proper upgrade_xxxx_savepoint call
00266         $result .= $this->upgrade_savepoint_php ($structure);
00267 
00268         // Add standard PHP footer
00269         $result .= XMLDB_PHP_FOOTER;
00270 
00271         return $result;
00272     }
00273 
00282     function rename_table_php($structure, $table) {
00283 
00284         $result = '';
00285         // Validate if we can do it
00286         if (!$table = $structure->getTable($table)) {
00287             return false;
00288         }
00289         if ($table->getAllErrors()) {
00290             return false;
00291         }
00292 
00293         // Add the standard PHP header
00294         $result .= XMLDB_PHP_HEADER;
00295 
00296         // Add contents
00297         $result .= XMLDB_LINEFEED;
00298         $result .= '        // Define table ' . $table->getName() . ' to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
00299         $result .= '        $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
00300 
00301         // Launch the proper DDL
00302         $result .= XMLDB_LINEFEED;
00303         $result .= '        // Launch rename table for ' . $table->getName() . XMLDB_LINEFEED;
00304         $result .= '        $dbman->rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED;
00305 
00306         // Add the proper upgrade_xxxx_savepoint call
00307         $result .= $this->upgrade_savepoint_php ($structure);
00308 
00309         // Add standard PHP footer
00310         $result .= XMLDB_PHP_FOOTER;
00311 
00312         return $result;
00313     }
00314 
00315 }
00316 
 All Data Structures Namespaces Files Functions Variables Enumerations