. /** * @package tool_xmldb * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ /** * This class provides the interface for all the edit table actions * * Main page of edit table actions, from here fields/indexes/keys edition * can be invoked, plus links to PHP code generator, view SQL, rearrange * elements and so on. * * @package tool_xmldb * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class edit_table extends XMLDBAction { /** * Init method, every subclass will have its own */ function init() { parent::init(); // Set own custom attributes $this->sesskey_protected = false; // This action doesn't need sesskey protection // Get needed strings $this->loadStrings(array( 'addpersistent' => 'tool_xmldb', 'change' => 'tool_xmldb', 'vieworiginal' => 'tool_xmldb', 'viewedited' => 'tool_xmldb', 'viewsqlcode' => 'tool_xmldb', 'viewphpcode' => 'tool_xmldb', 'newfield' => 'tool_xmldb', 'newkey' => 'tool_xmldb', 'newindex' => 'tool_xmldb', 'fields' => 'tool_xmldb', 'keys' => 'tool_xmldb', 'indexes' => 'tool_xmldb', 'edit' => 'tool_xmldb', 'up' => 'tool_xmldb', 'down' => 'tool_xmldb', 'delete' => 'tool_xmldb', 'reserved' => 'tool_xmldb', 'back' => 'tool_xmldb', 'viewxml' => 'tool_xmldb', 'pendingchanges' => 'tool_xmldb', 'pendingchangescannotbesaved' => 'tool_xmldb', 'save' => 'tool_xmldb' )); } /** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { global $OUTPUT, $PAGE; parent::invoke(); $result = true; // Set own core attributes $this->does_generate = ACTION_GENERATE_HTML; // These are always here global $CFG, $XMLDB; // Do the job, setting result as needed // Get the dir containing the file $dirpath = required_param('dir', PARAM_PATH); $dirpath = $CFG->dirroot . $dirpath; // Get the correct dirs if (!empty($XMLDB->dbdirs)) { $dbdir = $XMLDB->dbdirs[$dirpath]; } else { return false; } // Check if the dir exists and copy it from dbdirs // (because we need straight load in case of saving from here) if (!isset($XMLDB->editeddirs[$dirpath])) { $XMLDB->editeddirs[$dirpath] = unserialize(serialize($dbdir)); } if (!empty($XMLDB->editeddirs)) { $editeddir = $XMLDB->editeddirs[$dirpath]; $structure = $editeddir->xml_file->getStructure(); } $tableparam = required_param('table', PARAM_CLEAN); if (!$table = $structure->getTable($tableparam)) { // Arriving here from a name change, looking for the new table name $tableparam = required_param('name', PARAM_CLEAN); $table = $structure->getTable($tableparam); } $dbdir = $XMLDB->dbdirs[$dirpath]; $origstructure = $dbdir->xml_file->getStructure(); // Add the main form $o = '
'; $o.= '
'; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o .= ' '; // If the table is being used, we cannot rename it if ($structure->getTableUses($table->getName())) { $o.= ' '; } else { $o.= ' '; } $o .= ' '; $o .= ' '; $o.= '
Name:' . s($table->getName()) .'
 
'; $o.= '
'; // Calculate the pending changes / save message $e = ''; $cansavenow = false; if ($structure->hasChanged()) { if (!is_writeable($dirpath . '/install.xml') || !is_writeable($dirpath)) { $e .= '

' . $this->str['pendingchangescannotbesaved'] . '

'; } else { $e .= '

' . $this->str['pendingchanges'] . '

'; $cansavenow = true; } } // Calculate the buttons $b = '

'; // The view original XML button if ($origstructure->getTable($tableparam)) { $b .= ' [' . $this->str['vieworiginal'] . ']'; } else { $b .= ' [' . $this->str['vieworiginal'] . ']'; } // The view edited XML button if ($table->hasChanged()) { $b .= ' [' . $this->str['viewedited'] . ']'; } else { $b .= ' [' . $this->str['viewedited'] . ']'; } // The new field button $b .= ' [' . $this->str['newfield'] . ']'; // The new key button $b .= ' [' . $this->str['newkey'] . ']'; // The new index button $b .= ' [' . $this->str['newindex'] . ']'; $b .= '

'; $b .= '

'; // The view sql code button $b .= '[' .$this->str['viewsqlcode'] . ']'; // The view php code button $b .= ' [' . $this->str['viewphpcode'] . ']'; // The add persistent fields button. $url = new \moodle_url('/admin/tool/xmldb/index.php', [ 'action' => 'add_persistent_mandatory', 'sesskey' => sesskey(), 'table' => $tableparam, 'dir'=> str_replace($CFG->dirroot, '', $dirpath) ]); $b .= ' ' . \html_writer::link($url, '[' . $this->str['addpersistent'] . ']'); // The save button (if possible) if ($cansavenow) { $b .= ' [' . $this->str['save'] . ']'; } // The back to edit xml file button $b .= ' [' . $this->str['back'] . ']'; $b .= '

'; $o .= $e . $b; require_once("$CFG->libdir/ddl/sql_generator.php"); $reserved_words = sql_generator::getAllReservedWords(); // Delete any 'changeme' field/key/index $table->deleteField('changeme'); $table->deleteKey('changeme'); $table->deleteIndex('changeme'); // Add the fields list $fields = $table->getFields(); if (!empty($fields)) { $o .= '

' . $this->str['fields'] . '

'; $o .= ''; $row = 0; foreach ($fields as $field) { // Drag element up/down. $move = (count($fields) > 1) ? html_writer::span($OUTPUT->render_from_template('core/drag_handle', ['movetitle' => get_string('movecontent', 'moodle', $field->getName())]), '', ['data-action' => 'move_updown_field', 'data-dir' => str_replace($CFG->dirroot, '', $dirpath), 'data-table' => $table->getName(), 'data-field' => $field->getName()]) : ''; // The field name (link to edit - if the field has no uses) if (!$structure->getFieldUses($table->getName(), $field->getName())) { $f = '' . $field->getName() . ''; } else { $f = $field->getName(); } // Calculate buttons $b = ''; // Print table row $o .= ''; $row = ($row + 1) % 2; } $o .= '
'; // The edit button (if the field has no uses) if (!$structure->getFieldUses($table->getName(), $field->getName())) { $b .= '[' . $this->str['edit'] . ']'; } else { $b .= '[' . $this->str['edit'] . ']'; } $b .= ''; // The delete button (if we have more than one and it isn't used if (count($fields) > 1 && !$structure->getFieldUses($table->getName(), $field->getName())) { $b .= '[' . $this->str['delete'] . ']'; } else { $b .= '[' . $this->str['delete'] . ']'; } $b .= ''; // The view xml button $b .= '[' . $this->str['viewxml'] . ']'; // Detect if the table name is a reserved word if (array_key_exists($field->getName(), $reserved_words)) { $b .= ' ' . $this->str['reserved'] . ''; } // The readable info $r = '' . $field->readableInfo() . '
' . $move . $f . $b . $r . '
'; $PAGE->requires->js_call_amd('tool_xmldb/move', 'init', ['listfields', 'move_updown_field']); } // Add the keys list $keys = $table->getKeys(); if (!empty($keys)) { $o .= '

' . $this->str['keys'] . '

'; $o .= ''; $row = 0; foreach ($keys as $key) { // Drag element up/down. $move = (count($keys) > 1) ? html_writer::span($OUTPUT->render_from_template('core/drag_handle', ['movetitle' => get_string('movecontent', 'moodle', $key->getName())]), '', ['data-action' => 'move_updown_key', 'data-dir' => str_replace($CFG->dirroot, '', $dirpath), 'data-table' => $table->getName(), 'data-key' => $key->getName()]) : ''; // The key name (link to edit - if the key has no uses) if (!$structure->getKeyUses($table->getName(), $key->getName())) { $k = '' . $key->getName() . ''; } else { $k = $key->getName(); } // Calculate buttons $b = ''; // Print table row $o .= ''; $row = ($row + 1) % 2; } $o .= '
'; // The edit button (if the key hasn't uses) if (!$structure->getKeyUses($table->getName(), $key->getName())) { $b .= '[' . $this->str['edit'] . ']'; } else { $b .= '[' . $this->str['edit'] . ']'; } $b .= ''; // The delete button (if the key hasn't uses) if (!$structure->getKeyUses($table->getName(), $key->getName())) { $b .= '[' . $this->str['delete'] . ']'; } else { $b .= '[' . $this->str['delete'] . ']'; } $b .= ''; // The view xml button $b .= '[' . $this->str['viewxml'] . ']'; // The readable info $r = '' . $key->readableInfo() . '
' . $move . $k . $b . $r .'
'; $PAGE->requires->js_call_amd('tool_xmldb/move', 'init', ['listkeys', 'move_updown_key']); } // Add the indexes list $indexes = $table->getIndexes(); if (!empty($indexes)) { $o .= '

' . $this->str['indexes'] . '

'; $o .= ''; $row = 0; foreach ($indexes as $index) { // Drag element up/down. $move = (count($indexes) > 1) ? html_writer::span($OUTPUT->render_from_template('core/drag_handle', ['movetitle' => get_string('movecontent', 'moodle', $index->getName())]), '', ['data-action' => 'move_updown_index', 'data-dir' => str_replace($CFG->dirroot, '', $dirpath), 'data-table' => $table->getName(), 'data-index' => $index->getName()]) : ''; // The index name (link to edit) $i = '' . $index->getName() . ''; // Calculate buttons $b = ''; // Print table row $o .= ''; $row = ($row + 1) % 2; } $o .= '
'; // The edit button $b .= '[' . $this->str['edit'] . ']'; $b .= ''; // The delete button $b .= '[' . $this->str['delete'] . ']'; $b .= ''; // The view xml button $b .= '[' . $this->str['viewxml'] . ']'; // The readable info $r = '' . $index->readableInfo() . '
' . $move . $i . $b . $r .'
'; $PAGE->requires->js_call_amd('tool_xmldb/move', 'init', ['listindexes', 'move_updown_index']); } $this->output = $o; // Launch postaction if exists (leave this here!) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } // Return ok if arrived here return $result; } }