[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/xmldb/actions/edit_xml_file/ -> edit_xml_file.class.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @package    tool_xmldb
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  /**
  24   * This class will edit one loaded XML file
  25   *
  26   * Main page to start editing one XML file. From here it's possible to access
  27   * to tables edition plus PHP code generation and other utilities
  28   *
  29   * @package    tool_xmldb
  30   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class edit_xml_file extends XMLDBAction {
  34  
  35      /**
  36       * Init method, every subclass will have its own
  37       */
  38      function init() {
  39          parent::init();
  40  
  41          // Set own custom attributes
  42          $this->sesskey_protected = false; // This action doesn't need sesskey protection
  43  
  44          // Get needed strings
  45          $this->loadStrings(array(
  46              'change' => 'tool_xmldb',
  47              'edit' => 'tool_xmldb',
  48              'up' => 'tool_xmldb',
  49              'down' => 'tool_xmldb',
  50              'delete' => 'tool_xmldb',
  51              'vieworiginal' => 'tool_xmldb',
  52              'viewedited' => 'tool_xmldb',
  53              'tables' => 'tool_xmldb',
  54              'newtable' => 'tool_xmldb',
  55              'newtablefrommysql' => 'tool_xmldb',
  56              'viewsqlcode' => 'tool_xmldb',
  57              'viewphpcode' => 'tool_xmldb',
  58              'reserved' => 'tool_xmldb',
  59              'backtomainview' => 'tool_xmldb',
  60              'viewxml' => 'tool_xmldb',
  61              'pendingchanges' => 'tool_xmldb',
  62              'pendingchangescannotbesaved' => 'tool_xmldb',
  63              'save' => 'tool_xmldb'
  64          ));
  65      }
  66  
  67      /**
  68       * Invoke method, every class will have its own
  69       * returns true/false on completion, setting both
  70       * errormsg and output as necessary
  71       */
  72      function invoke() {
  73          parent::invoke();
  74  
  75          $result = true;
  76  
  77          // Set own core attributes
  78          $this->does_generate = ACTION_GENERATE_HTML;
  79  
  80          // These are always here
  81          global $CFG, $XMLDB, $DB;
  82  
  83          // Do the job, setting $result as needed
  84  
  85          // Get the dir containing the file
  86          $dirpath = required_param('dir', PARAM_PATH);
  87          $dirpath = $CFG->dirroot . $dirpath;
  88  
  89          // Get the correct dir
  90          if (!empty($XMLDB->dbdirs)) {
  91              $dbdir = $XMLDB->dbdirs[$dirpath];
  92              if ($dbdir) {
  93                  // Only if the directory exists and it has been loaded
  94                  if (!$dbdir->path_exists || !$dbdir->xml_loaded) {
  95                      return false;
  96                  }
  97                  // Check if the in-memory object exists and create it
  98                  if (empty($XMLDB->editeddirs)) {
  99                      $XMLDB->editeddirs = array();
 100                  }
 101                  // Check if the dir exists and copy it from dbdirs
 102                  if (!isset($XMLDB->editeddirs[$dirpath])) {
 103                      $XMLDB->editeddirs[$dirpath] = unserialize(serialize($dbdir));
 104                  }
 105                  // Get it
 106                  $editeddir = $XMLDB->editeddirs[$dirpath];
 107                  $structure = $editeddir->xml_file->getStructure();
 108  
 109                  // Add the main form
 110                  $o = '<form id="form" action="index.php" method="post">';
 111                  $o.= '<div>';
 112                  $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
 113                  $o.= '    <input type="hidden" name ="action" value="edit_xml_file_save" />';
 114                  $o.= '    <input type="hidden" name ="postaction" value="edit_xml_file" />';
 115                  $o.= '    <input type="hidden" name ="path" value="' . s($structure->getPath()) .'" />';
 116                  $o.= '    <input type="hidden" name ="version" value="' . s($structure->getVersion()) .'" />';
 117                  $o.= '    <input type="hidden" name ="sesskey" value="' . sesskey() .'" />';
 118                  $o.= '    <table id="formelements" class="boxaligncenter">';
 119                  $o.= '      <tr valign="top"><td>Path:</td><td>' . s($structure->getPath()) . '</td></tr>';
 120                  $o.= '      <tr valign="top"><td>Version:</td><td>' . s($structure->getVersion()) . '</td></tr>';
 121                  $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>';
 122                  $o.= '      <tr><td>&nbsp;</td><td><input type="submit" value="' .$this->str['change'] . '" /></td></tr>';
 123                  $o.= '    </table>';
 124                  $o.= '</div></form>';
 125                  // Calculate the pending changes / save message
 126                  $e = '';
 127                  $cansavenow = false;
 128                  if ($structure->hasChanged()) {
 129                      if (!is_writeable($dirpath . '/install.xml') || !is_writeable($dirpath)) {
 130                          $e .= '<p class="centerpara error">' . $this->str['pendingchangescannotbesaved'] . '</p>';
 131                      } else {
 132                          $e .= '<p class="centerpara warning">' . $this->str['pendingchanges'] . '</p>';
 133                          $cansavenow = true;
 134                      }
 135                  }
 136                  // Calculate the buttons
 137                  $b = ' <p class="centerpara buttons">';
 138                  // The view original XML button
 139                  $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>';
 140                  // The view edited XML button
 141                  if ($structure->hasChanged()) {
 142                      $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>';
 143                  } else {
 144                      $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
 145                  }
 146                  // The new table button
 147                  $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>';
 148                  // The new from MySQL button
 149                  if ($DB->get_dbfamily() == 'mysql') {
 150                      $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>';
 151                  } else {
 152                      $b .= '&nbsp;[' . $this->str['newtablefrommysql'] . ']';
 153                  }
 154  
 155                  // The view sql code button
 156                  $b .= '<a href="index.php?action=view_structure_sql&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' .$this->str['viewsqlcode'] . ']</a>';
 157                  // The view php code button
 158                  $b .= '&nbsp;<a href="index.php?action=view_structure_php&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['viewphpcode'] . ']</a>';
 159                  // The save button (if possible)
 160                  if ($cansavenow) {
 161                      $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>';
 162                  }
 163  
 164                  // The back to main menu button
 165                  $b .= '&nbsp;<a href="index.php?action=main_view#lastused">[' . $this->str['backtomainview'] . ']</a>';
 166                  $b .= '</p>';
 167                  $o .= $e . $b;
 168  
 169                  // Join all the reserved words into one big array
 170                  // Calculate list of available SQL generators
 171                  require_once("$CFG->libdir/ddl/sql_generator.php");
 172                  $reserved_words = sql_generator::getAllReservedWords();
 173  
 174                  // Add the tables list
 175                  $tables = $structure->getTables();
 176                  if ($tables) {
 177                      $o .= '<h3 class="main">' . $this->str['tables'] . '</h3>';
 178                      $o .= '<table id="listtables" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
 179                      $row = 0;
 180                      foreach ($tables as $table) {
 181                          // The table name (link to edit table)
 182                          $t = '<a href="index.php?action=edit_table&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $table->getName() . '</a>';
 183                          // Calculate buttons
 184                          $b = '</td><td class="button cell">';
 185                          // The edit button
 186                          $b .= '<a href="index.php?action=edit_table&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
 187                          $b .= '</td><td class="button cell">';
 188                          // The up button
 189                          if ($table->getPrevious()) {
 190                              $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>';
 191                          } else {
 192                              $b .= '[' . $this->str['up'] . ']';
 193                          }
 194                          $b .= '</td><td class="button cell">';
 195                          // The down button
 196                          if ($table->getNext()) {
 197                              $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>';
 198                          } else {
 199                              $b .= '[' . $this->str['down'] . ']';
 200                          }
 201                          $b .= '</td><td class="button cell">';
 202                          // The delete button (if we have more than one and it isn't used)
 203                          if (count($tables) > 1 &&
 204                              !$structure->getTableUses($table->getName())) {
 205                                  // !$structure->getTableUses($table->getName())) {
 206                              $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>';
 207                          } else {
 208                              $b .= '[' . $this->str['delete'] . ']';
 209                          }
 210                          $b .= '</td><td class="button cell">';
 211                          // The view xml button
 212                          $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>';
 213                          // Detect if the table name is a reserved word
 214                           if (array_key_exists($table->getName(), $reserved_words)) {
 215                               $b .= '&nbsp;<a href="index.php?action=view_reserved_words"><span class="error">' . $this->str['reserved'] . '</span></a>';
 216                           }
 217                          $b .= '</td>';
 218                          // Print table row
 219                          $o .= '<tr class="r' . $row . '"><td class="table cell">' . $t . $b . '</tr>';
 220                          $row = ($row + 1) % 2;
 221                      }
 222                      $o .= '</table>';
 223                  }
 224              // Add the back to main
 225              $this->output = $o;
 226              }
 227          }
 228  
 229          // Launch postaction if exists (leave this unmodified)
 230          if ($this->getPostAction() && $result) {
 231              return $this->launch($this->getPostAction());
 232          }
 233  
 234          return $result;
 235      }
 236  }
 237  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1