[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/xmldb/actions/edit_table/ -> edit_table.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 provides the interface for all the edit table actions
  25   *
  26   * Main page of edit table actions, from here fields/indexes/keys edition
  27   * can be invoked, plus links to PHP code generator, view SQL, rearrange
  28   * elements and so on.
  29   *
  30   * @package    tool_xmldb
  31   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class edit_table extends XMLDBAction {
  35  
  36      /**
  37       * Init method, every subclass will have its own
  38       */
  39      function init() {
  40          parent::init();
  41  
  42          // Set own custom attributes
  43          $this->sesskey_protected = false; // This action doesn't need sesskey protection
  44  
  45          // Get needed strings
  46          $this->loadStrings(array(
  47              'change' => 'tool_xmldb',
  48              'vieworiginal' => 'tool_xmldb',
  49              'viewedited' => 'tool_xmldb',
  50              'viewsqlcode' => 'tool_xmldb',
  51              'viewphpcode' => 'tool_xmldb',
  52              'newfield' => 'tool_xmldb',
  53              'newkey' => 'tool_xmldb',
  54              'newindex' => 'tool_xmldb',
  55              'fields' => 'tool_xmldb',
  56              'keys' => 'tool_xmldb',
  57              'indexes' => 'tool_xmldb',
  58              'edit' => 'tool_xmldb',
  59              'up' => 'tool_xmldb',
  60              'down' => 'tool_xmldb',
  61              'delete' => 'tool_xmldb',
  62              'reserved' => 'tool_xmldb',
  63              'back' => 'tool_xmldb',
  64              'viewxml' => 'tool_xmldb',
  65              'pendingchanges' => 'tool_xmldb',
  66              'pendingchangescannotbesaved' => 'tool_xmldb',
  67              'save' => 'tool_xmldb'
  68          ));
  69      }
  70  
  71      /**
  72       * Invoke method, every class will have its own
  73       * returns true/false on completion, setting both
  74       * errormsg and output as necessary
  75       */
  76      function invoke() {
  77          parent::invoke();
  78  
  79          $result = true;
  80  
  81          // Set own core attributes
  82          $this->does_generate = ACTION_GENERATE_HTML;
  83  
  84          // These are always here
  85          global $CFG, $XMLDB;
  86  
  87          // Do the job, setting result as needed
  88          // Get the dir containing the file
  89          $dirpath = required_param('dir', PARAM_PATH);
  90          $dirpath = $CFG->dirroot . $dirpath;
  91  
  92          // Get the correct dirs
  93          if (!empty($XMLDB->dbdirs)) {
  94              $dbdir = $XMLDB->dbdirs[$dirpath];
  95          } else {
  96              return false;
  97          }
  98          // Check if the dir exists and copy it from dbdirs
  99          // (because we need straight load in case of saving from here)
 100          if (!isset($XMLDB->editeddirs[$dirpath])) {
 101              $XMLDB->editeddirs[$dirpath] = unserialize(serialize($dbdir));
 102          }
 103  
 104          if (!empty($XMLDB->editeddirs)) {
 105              $editeddir = $XMLDB->editeddirs[$dirpath];
 106              $structure = $editeddir->xml_file->getStructure();
 107          }
 108  
 109          $tableparam = required_param('table', PARAM_CLEAN);
 110          if (!$table = $structure->getTable($tableparam)) {
 111              // Arriving here from a name change, looking for the new table name
 112              $tableparam = required_param('name', PARAM_CLEAN);
 113              $table = $structure->getTable($tableparam);
 114          }
 115  
 116          $dbdir = $XMLDB->dbdirs[$dirpath];
 117          $origstructure = $dbdir->xml_file->getStructure();
 118  
 119          // Add the main form
 120          $o = '<form id="form" action="index.php" method="post">';
 121          $o.= '<div>';
 122          $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
 123          $o.= '    <input type="hidden" name ="table" value="' . $tableparam .'" />';
 124          $o.= '    <input type="hidden" name ="action" value="edit_table_save" />';
 125          $o.= '    <input type="hidden" name ="sesskey" value="' . sesskey() .'" />';
 126          $o.= '    <input type="hidden" name ="postaction" value="edit_table" />';
 127          $o.= '    <table id="formelements" class="boxaligncenter">';
 128          // If the table is being used, we cannot rename it
 129          if ($structure->getTableUses($table->getName())) {
 130              $o.= '      <tr valign="top"><td>Name:</td><td><input type="hidden" name ="name" value="' . s($table->getName()) . '" />' . s($table->getName()) .'</td></tr>';
 131          } else {
 132              $o.= '      <tr valign="top"><td><label for="name" accesskey="p">Name:</label></td><td><input name="name" type="text" size="'.xmldb_table::NAME_MAX_LENGTH.'" maxlength="'.xmldb_table::NAME_MAX_LENGTH.'" id="name" value="' . s($table->getName()) . '" /></td></tr>';
 133          }
 134          $o.= '      <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td><textarea name="comment" rows="3" cols="80" id="comment">' . s($table->getComment()) . '</textarea></td></tr>';
 135          $o.= '      <tr valign="top"><td>&nbsp;</td><td><input type="submit" value="' .$this->str['change'] . '" /></td></tr>';
 136          $o.= '    </table>';
 137          $o.= '</div></form>';
 138          // Calculate the pending changes / save message
 139          $e = '';
 140          $cansavenow = false;
 141          if ($structure->hasChanged()) {
 142              if (!is_writeable($dirpath . '/install.xml') || !is_writeable($dirpath)) {
 143                  $e .= '<p class="centerpara error">' . $this->str['pendingchangescannotbesaved'] . '</p>';
 144              } else {
 145                  $e .= '<p class="centerpara warning">' . $this->str['pendingchanges'] . '</p>';
 146                  $cansavenow = true;
 147              }
 148          }
 149          // Calculate the buttons
 150          $b = ' <p class="centerpara buttons">';
 151          // The view original XML button
 152          if ($origstructure->getTable($tableparam)) {
 153              $b .= '&nbsp;<a href="index.php?action=view_table_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original&amp;table=' . $tableparam . '">[' . $this->str['vieworiginal'] . ']</a>';
 154          } else {
 155              $b .= '&nbsp;[' . $this->str['vieworiginal'] . ']';
 156          }
 157          // The view edited XML button
 158          if ($table->hasChanged()) {
 159              $b .= '&nbsp;<a href="index.php?action=view_table_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited&amp;table=' . $tableparam . '">[' . $this->str['viewedited'] . ']</a>';
 160          } else {
 161              $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
 162          }
 163          // The new field button
 164          $b .= '&nbsp;<a href="index.php?action=new_field&amp;sesskey=' . sesskey() . '&amp;postaction=edit_field&amp;table=' . $tableparam . '&amp;field=changeme&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newfield'] . ']</a>';
 165          // The new key button
 166          $b .= '&nbsp;<a href="index.php?action=new_key&amp;sesskey=' . sesskey() . '&amp;postaction=edit_key&amp;table=' . $tableparam . '&amp;key=changeme&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newkey'] . ']</a>';
 167          // The new index button
 168          $b .= '&nbsp;<a href="index.php?action=new_index&amp;sesskey=' . sesskey() . '&amp;postaction=edit_index&amp;table=' . $tableparam . '&amp;index=changeme&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newindex'] . ']</a>';
 169          $b .= '</p>';
 170  
 171          $b .= ' <p class="centerpara buttons">';
 172          // The view sql code button
 173          $b .= '<a href="index.php?action=view_table_sql&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' .$this->str['viewsqlcode'] . ']</a>';
 174          // The view php code button
 175          $b .= '&nbsp;<a href="index.php?action=view_table_php&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['viewphpcode'] . ']</a>';
 176          // The save button (if possible)
 177          if ($cansavenow) {
 178              $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_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['save'] . ']</a>';
 179          }
 180          // The back to edit xml file button
 181          $b .= '&nbsp;<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
 182          $b .= '</p>';
 183          $o .= $e . $b;
 184  
 185          require_once("$CFG->libdir/ddl/sql_generator.php");
 186          $reserved_words = sql_generator::getAllReservedWords();
 187  
 188          // Delete any 'changeme' field/key/index
 189          $table->deleteField('changeme');
 190          $table->deleteKey('changeme');
 191          $table->deleteIndex('changeme');
 192  
 193          // Add the fields list
 194          $fields = $table->getFields();
 195          if (!empty($fields)) {
 196              $o .= '<h3 class="main">' . $this->str['fields'] . '</h3>';
 197              $o .= '<table id="listfields" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
 198              $row = 0;
 199              foreach ($fields as $field) {
 200                  // The field name (link to edit - if the field has no uses)
 201                  if (!$structure->getFieldUses($table->getName(), $field->getName())) {
 202                      $f = '<a href="index.php?action=edit_field&amp;field=' .$field->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $field->getName() . '</a>';
 203                  } else {
 204                      $f = $field->getName();
 205                  }
 206                  // Calculate buttons
 207                  $b = '</td><td class="button cell">';
 208                  // The edit button (if the field has no uses)
 209                  if (!$structure->getFieldUses($table->getName(), $field->getName())) {
 210                      $b .= '<a href="index.php?action=edit_field&amp;field=' .$field->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
 211                  } else {
 212                      $b .= '[' . $this->str['edit'] . ']';
 213                  }
 214                  $b .= '</td><td class="button cell">';
 215                  // The up button
 216                  if ($field->getPrevious()) {
 217                      $b .= '<a href="index.php?action=move_updown_field&amp;direction=up&amp;sesskey=' . sesskey() . '&amp;field=' . $field->getName() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_table' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>';
 218                  } else {
 219                      $b .= '[' . $this->str['up'] . ']';
 220                  }
 221                  $b .= '</td><td class="button cell">';
 222                  // The down button
 223                  if ($field->getNext()) {
 224                      $b .= '<a href="index.php?action=move_updown_field&amp;direction=down&amp;sesskey=' . sesskey() . '&amp;field=' . $field->getName() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_table' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>';
 225                  } else {
 226                      $b .= '[' . $this->str['down'] . ']';
 227                  }
 228                  $b .= '</td><td class="button cell">';
 229                  // The delete button (if we have more than one and it isn't used
 230                  if (count($fields) > 1 &&
 231                  !$structure->getFieldUses($table->getName(), $field->getName())) {
 232                      $b .= '<a href="index.php?action=delete_field&amp;sesskey=' . sesskey() . '&amp;field=' . $field->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
 233                  } else {
 234                      $b .= '[' . $this->str['delete'] . ']';
 235                  }
 236                  $b .= '</td><td class="button cell">';
 237                  // The view xml button
 238                  $b .= '<a href="index.php?action=view_field_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;field=' . $field->getName() . '&amp;table=' . $table->getName() . '&amp;select=edited">[' . $this->str['viewxml'] . ']</a>';
 239                  // Detect if the table name is a reserved word
 240                  if (array_key_exists($field->getName(), $reserved_words)) {
 241                      $b .= '&nbsp;<a href="index.php?action=view_reserved_words"><span class="error">' . $this->str['reserved'] . '</span></a>';
 242                  }
 243                  // The readable info
 244                  $r = '</td><td class="readableinfo cell">' . $field->readableInfo() . '</td>';
 245                  // Print table row
 246                  $o .= '<tr class="r' . $row . '"><td class="table cell">' . $f . $b . $r . '</tr>';
 247                  $row = ($row + 1) % 2;
 248              }
 249              $o .= '</table>';
 250          }
 251          // Add the keys list
 252          $keys = $table->getKeys();
 253          if (!empty($keys)) {
 254              $o .= '<h3 class="main">' . $this->str['keys'] . '</h3>';
 255              $o .= '<table id="listkeys" border="0"  cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
 256              $row = 0;
 257              foreach ($keys as $key) {
 258                  // The key name (link to edit - if the key has no uses)
 259                  if (!$structure->getKeyUses($table->getName(), $key->getName())) {
 260                      $k = '<a href="index.php?action=edit_key&amp;key=' .$key->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $key->getName() . '</a>';
 261                  } else {
 262                      $k = $key->getName();
 263                  }
 264                  // Calculate buttons
 265                  $b = '</td><td class="button cell">';
 266                  // The edit button (if the key hasn't uses)
 267                  if (!$structure->getKeyUses($table->getName(), $key->getName())) {
 268                      $b .= '<a href="index.php?action=edit_key&amp;key=' .$key->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
 269                  } else {
 270                      $b .= '[' . $this->str['edit'] . ']';
 271                  }
 272                  $b .= '</td><td class="button cell">';
 273                  // The up button
 274                  if ($key->getPrevious()) {
 275                      $b .= '<a href="index.php?action=move_updown_key&amp;direction=up&amp;sesskey=' . sesskey() . '&amp;key=' . $key->getName() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_table' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>';
 276                  } else {
 277                      $b .= '[' . $this->str['up'] . ']';
 278                  }
 279                  $b .= '</td><td class="button cell">';
 280                  // The down button
 281                  if ($key->getNext()) {
 282                      $b .= '<a href="index.php?action=move_updown_key&amp;direction=down&amp;sesskey=' . sesskey() . '&amp;key=' . $key->getName() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_table' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>';
 283                  } else {
 284                      $b .= '[' . $this->str['down'] . ']';
 285                  }
 286                  $b .= '</td><td class="button cell">';
 287                  // The delete button (if the key hasn't uses)
 288                  if (!$structure->getKeyUses($table->getName(), $key->getName())) {
 289                      $b .= '<a href="index.php?action=delete_key&amp;sesskey=' . sesskey() . '&amp;key=' . $key->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
 290                  } else {
 291                      $b .= '[' . $this->str['delete'] . ']';
 292                  }
 293                  $b .= '</td><td class="button cell">';
 294                  // The view xml button
 295                  $b .= '<a href="index.php?action=view_key_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;key=' . $key->getName() . '&amp;table=' . $table->getName() . '&amp;select=edited">[' . $this->str['viewxml'] . ']</a>';
 296                  // The readable info
 297                  $r = '</td><td class="readableinfo cell">' . $key->readableInfo() . '</td>';
 298                  // Print table row
 299              $o .= '<tr class="r' . $row . '"><td class="table cell">' . $k . $b . $r .'</tr>';
 300                  $row = ($row + 1) % 2;
 301              }
 302              $o .= '</table>';
 303          }
 304         // Add the indexes list
 305          $indexes = $table->getIndexes();
 306          if (!empty($indexes)) {
 307              $o .= '<h3 class="main">' . $this->str['indexes'] . '</h3>';
 308              $o .= '<table id="listindexes" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
 309              $row = 0;
 310              foreach ($indexes as $index) {
 311                  // The index name (link to edit)
 312                  $i = '<a href="index.php?action=edit_index&amp;index=' .$index->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $index->getName() . '</a>';
 313                  // Calculate buttons
 314                  $b = '</td><td class="button cell">';
 315                  // The edit button
 316                  $b .= '<a href="index.php?action=edit_index&amp;index=' .$index->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
 317                  $b .= '</td><td class="button cell">';
 318                  // The up button
 319                  if ($index->getPrevious()) {
 320                      $b .= '<a href="index.php?action=move_updown_index&amp;direction=up&amp;sesskey=' . sesskey() . '&amp;index=' . $index->getName() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_table' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>';
 321                  } else {
 322                      $b .= '[' . $this->str['up'] . ']';
 323                  }
 324                  $b .= '</td><td class="button cell">';
 325                  // The down button
 326                  if ($index->getNext()) {
 327                      $b .= '<a href="index.php?action=move_updown_index&amp;direction=down&amp;sesskey=' . sesskey() . '&amp;index=' . $index->getName() . '&amp;table=' . $table->getName() . '&amp;postaction=edit_table' . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>';
 328                  } else {
 329                      $b .= '[' . $this->str['down'] . ']';
 330                  }
 331                  $b .= '</td><td class="button cell">';
 332                  // The delete button
 333                      $b .= '<a href="index.php?action=delete_index&amp;sesskey=' . sesskey() . '&amp;index=' . $index->getName() . '&amp;table=' . $table->getName() . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
 334                  $b .= '</td><td class="button cell">';
 335                  // The view xml button
 336                  $b .= '<a href="index.php?action=view_index_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;index=' . $index->getName() . '&amp;table=' . $table->getName() . '&amp;select=edited">[' . $this->str['viewxml'] . ']</a>';
 337                  // The readable info
 338                  $r = '</td><td class="readableinfo cell">' . $index->readableInfo() . '</td>';
 339                  // Print table row
 340              $o .= '<tr class="r' . $row . '"><td class="table cell">' . $i . $b . $r .'</tr>';
 341                  $row = ($row + 1) % 2;
 342              }
 343              $o .= '</table>';
 344          }
 345  
 346          $this->output = $o;
 347  
 348          // Launch postaction if exists (leave this here!)
 349          if ($this->getPostAction() && $result) {
 350              return $this->launch($this->getPostAction());
 351          }
 352  
 353          // Return ok if arrived here
 354          return $result;
 355      }
 356  }
 357  


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