Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/edit_xml_file/edit_xml_file.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 
00035 class edit_xml_file extends XMLDBAction {
00036 
00040     function init() {
00041         parent::init();
00042 
00043         // Set own custom attributes
00044         $this->sesskey_protected = false; // This action doesn't need sesskey protection
00045 
00046         // Get needed strings
00047         $this->loadStrings(array(
00048             'change' => 'tool_xmldb',
00049             'edit' => 'tool_xmldb',
00050             'up' => 'tool_xmldb',
00051             'down' => 'tool_xmldb',
00052             'delete' => 'tool_xmldb',
00053             'vieworiginal' => 'tool_xmldb',
00054             'viewedited' => 'tool_xmldb',
00055             'tables' => 'tool_xmldb',
00056             'newtable' => 'tool_xmldb',
00057             'newtablefrommysql' => 'tool_xmldb',
00058             'viewsqlcode' => 'tool_xmldb',
00059             'viewphpcode' => 'tool_xmldb',
00060             'reserved' => 'tool_xmldb',
00061             'backtomainview' => 'tool_xmldb',
00062             'viewxml' => 'tool_xmldb',
00063             'pendingchanges' => 'tool_xmldb',
00064             'pendingchangescannotbesaved' => 'tool_xmldb',
00065             'save' => 'tool_xmldb'
00066         ));
00067     }
00068 
00074     function invoke() {
00075         parent::invoke();
00076 
00077         $result = true;
00078 
00079         // Set own core attributes
00080         $this->does_generate = ACTION_GENERATE_HTML;
00081 
00082         // These are always here
00083         global $CFG, $XMLDB, $DB;
00084 
00085         // Do the job, setting $result as needed
00086 
00087         // Get the dir containing the file
00088         $dirpath = required_param('dir', PARAM_PATH);
00089         $dirpath = $CFG->dirroot . $dirpath;
00090 
00091         // Get the correct dir
00092         if (!empty($XMLDB->dbdirs)) {
00093             $dbdir =& $XMLDB->dbdirs[$dirpath];
00094             if ($dbdir) {
00095                 // Only if the directory exists and it has been loaded
00096                 if (!$dbdir->path_exists || !$dbdir->xml_loaded) {
00097                     return false;
00098                 }
00099                 // Check if the in-memory object exists and create it
00100                 if (empty($XMLDB->editeddirs)) {
00101                     $XMLDB->editeddirs = array();
00102                 }
00103                 // Check if the dir exists and copy it from dbdirs
00104                 if (!isset($XMLDB->editeddirs[$dirpath])) {
00105                     $XMLDB->editeddirs[$dirpath] = unserialize(serialize($dbdir));
00106                 }
00107                 // Get it
00108                 $editeddir =& $XMLDB->editeddirs[$dirpath];
00109                 $structure =& $editeddir->xml_file->getStructure();
00110 
00111                 // Add the main form
00112                 $o = '<form id="form" action="index.php" method="post">';
00113                 $o.= '<div>';
00114                 $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
00115                 $o.= '    <input type="hidden" name ="action" value="edit_xml_file_save" />';
00116                 $o.= '    <input type="hidden" name ="postaction" value="edit_xml_file" />';
00117                 $o.= '    <input type="hidden" name ="path" value="' . s($structure->getPath()) .'" />';
00118                 $o.= '    <input type="hidden" name ="version" value="' . s($structure->getVersion()) .'" />';
00119                 $o.= '    <input type="hidden" name ="sesskey" value="' . sesskey() .'" />';
00120                 $o.= '    <table id="formelements" class="boxaligncenter">';
00121                 $o.= '      <tr valign="top"><td>Path:</td><td>' . s($structure->getPath()) . '</td></tr>';
00122                 $o.= '      <tr valign="top"><td>Version:</td><td>' . s($structure->getVersion()) . '</td></tr>';
00123                 $o.= '      <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td><textarea name="comment" rows="3" cols="80" id="comment">' . $structure->getComment() . '</textarea></td></tr>';
00124                 $o.= '      <tr><td>&nbsp;</td><td><input type="submit" value="' .$this->str['change'] . '" /></td></tr>';
00125                 $o.= '    </table>';
00126                 $o.= '</div></form>';
00127                 // Calculate the pending changes / save message
00128                 $e = '';
00129                 $cansavenow = false;
00130                 if ($structure->hasChanged()) {
00131                     if (!is_writeable($dirpath . '/install.xml') || !is_writeable($dirpath)) {
00132                         $e .= '<p class="centerpara error">' . $this->str['pendingchangescannotbesaved'] . '</p>';
00133                     } else {
00134                         $e .= '<p class="centerpara warning">' . $this->str['pendingchanges'] . '</p>';
00135                         $cansavenow = true;
00136                     }
00137                 }
00138                 // Calculate the buttons
00139                 $b = ' <p class="centerpara buttons">';
00140                 // The view original XML button
00141                 $b .= '&nbsp;<a href="index.php?action=view_structure_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original">[' . $this->str['vieworiginal'] . ']</a>';
00142                 // The view edited XML button
00143                 if ($structure->hasChanged()) {
00144                     $b .= '&nbsp;<a href="index.php?action=view_structure_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited">[' . $this->str['viewedited'] . ']</a>';
00145                 } else {
00146                     $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
00147                 }
00148                 // The new table button
00149                 $b .= '&nbsp;<a href="index.php?action=new_table&amp;sesskey=' . sesskey() . '&amp;postaction=edit_table&amp;table=changeme&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newtable'] . ']</a>';
00150                 // The new from MySQL button
00151                 if ($DB->get_dbfamily() == 'mysql') {
00152                     $b .= '&nbsp;<a href="index.php?action=new_table_from_mysql&amp;sesskey=' . sesskey() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newtablefrommysql'] . ']</a>';
00153                 } else {
00154                     $b .= '&nbsp;[' . $this->str['newtablefrommysql'] . ']';
00155                 }
00156 
00157                 // The view sql code button
00158                 $b .= '<a href="index.php?action=view_structure_sql&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' .$this->str['viewsqlcode'] . ']</a>';
00159                 // The view php code button
00160                 $b .= '&nbsp;<a href="index.php?action=view_structure_php&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['viewphpcode'] . ']</a>';
00161                 // The save button (if possible)
00162                 if ($cansavenow) {
00163                     $b .= '&nbsp;<a href="index.php?action=save_xml_file&amp;sesskey=' . sesskey() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;time=' . time() . '&amp;unload=false&amp;postaction=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['save'] . ']</a>';
00164                 }
00165 
00166                 // The back to main menu button
00167                 $b .= '&nbsp;<a href="index.php?action=main_view#lastused">[' . $this->str['backtomainview'] . ']</a>';
00168                 $b .= '</p>';
00169                 $o .= $e . $b;
00170 
00171                 // Join all the reserved words into one big array
00172                 // Calculate list of available SQL generators
00173                 require_once("$CFG->libdir/ddl/sql_generator.php");
00174                 $reserved_words = sql_generator::getAllReservedWords();
00175 
00176                 // Add the tables list
00177                 $tables = $structure->getTables();
00178                 if ($tables) {
00179                     $o .= '<h3 class="main">' . $this->str['tables'] . '</h3>';
00180                     $o .= '<table id="listtables" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
00181                     $row = 0;
00182                     foreach ($tables as $table) {
00183                         // The table name (link to edit table)
00184                         $t = '<a href="index.php?action=edit_table&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $table->getName() . '</a>';
00185                         // Calculate buttons
00186                         $b = '</td><td class="button cell">';
00187                         // The edit button
00188                         $b .= '<a href="index.php?action=edit_table&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
00189                         $b .= '</td><td class="button cell">';
00190                         // The up button
00191                         if ($table->getPrevious()) {
00192                             $b .= '<a href="index.php?action=move_updown_table&amp;direction=up&amp;sesskey=' . sesskey() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_xml_file' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>';
00193                         } else {
00194                             $b .= '[' . $this->str['up'] . ']';
00195                         }
00196                         $b .= '</td><td class="button cell">';
00197                         // The down button
00198                         if ($table->getNext()) {
00199                             $b .= '<a href="index.php?action=move_updown_table&amp;direction=down&amp;sesskey=' . sesskey() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_xml_file' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>';
00200                         } else {
00201                             $b .= '[' . $this->str['down'] . ']';
00202                         }
00203                         $b .= '</td><td class="button cell">';
00204                         // The delete button (if we have more than one and it isn't used)
00205                         if (count($tables) > 1 &&
00206                             !$structure->getTableUses($table->getName())) {
00207                                 // !$structure->getTableUses($table->getName())) {
00208                             $b .= '<a href="index.php?action=delete_table&amp;sesskey=' . sesskey() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
00209                         } else {
00210                             $b .= '[' . $this->str['delete'] . ']';
00211                         }
00212                         $b .= '</td><td class="button cell">';
00213                         // The view xml button
00214                         $b .= '<a href="index.php?action=view_table_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $table->getName() . '&amp;select=edited">[' . $this->str['viewxml'] . ']</a>';
00215                         // Detect if the table name is a reserved word
00216                          if (array_key_exists($table->getName(), $reserved_words)) {
00217                              $b .= '&nbsp;<a href="index.php?action=view_reserved_words"><span class="error">' . $this->str['reserved'] . '</span></a>';
00218                          }
00219                         $b .= '</td>';
00220                         // Print table row
00221                         $o .= '<tr class="r' . $row . '"><td class="table cell">' . $t . $b . '</tr>';
00222                         $row = ($row + 1) % 2;
00223                     }
00224                     $o .= '</table>';
00225                 }
00226             // Add the back to main
00227             $this->output = $o;
00228             }
00229         }
00230 
00231         // Launch postaction if exists (leave this unmodified)
00232         if ($this->getPostAction() && $result) {
00233             return $this->launch($this->getPostAction());
00234         }
00235 
00236         return $result;
00237     }
00238 }
00239 
 All Data Structures Namespaces Files Functions Variables Enumerations