. /** * @package tool * @subpackage 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 will edit one loaded XML file * * Main page to start editing one XML file. From here it's possible to access * to tables edition plus PHP code generation and other utilities * * @package tool * @subpackage 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_xml_file 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( 'change' => 'tool_xmldb', 'edit' => 'tool_xmldb', 'up' => 'tool_xmldb', 'down' => 'tool_xmldb', 'delete' => 'tool_xmldb', 'vieworiginal' => 'tool_xmldb', 'viewedited' => 'tool_xmldb', 'tables' => 'tool_xmldb', 'newtable' => 'tool_xmldb', 'newtablefrommysql' => 'tool_xmldb', 'viewsqlcode' => 'tool_xmldb', 'viewphpcode' => 'tool_xmldb', 'reserved' => 'tool_xmldb', 'backtomainview' => '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() { parent::invoke(); $result = true; // Set own core attributes $this->does_generate = ACTION_GENERATE_HTML; // These are always here global $CFG, $XMLDB, $DB; // 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 dir if (!empty($XMLDB->dbdirs)) { $dbdir =& $XMLDB->dbdirs[$dirpath]; if ($dbdir) { // Only if the directory exists and it has been loaded if (!$dbdir->path_exists || !$dbdir->xml_loaded) { return false; } // Check if the in-memory object exists and create it if (empty($XMLDB->editeddirs)) { $XMLDB->editeddirs = array(); } // Check if the dir exists and copy it from dbdirs if (!isset($XMLDB->editeddirs[$dirpath])) { $XMLDB->editeddirs[$dirpath] = unserialize(serialize($dbdir)); } // Get it $editeddir =& $XMLDB->editeddirs[$dirpath]; $structure =& $editeddir->xml_file->getStructure(); // Add the main form $o = '
'; $o.= '
'; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= ' '; $o.= '
Path:' . s($structure->getPath()) . '
Version:' . s($structure->getVersion()) . '
 
'; $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 $b .= ' [' . $this->str['vieworiginal'] . ']'; // The view edited XML button if ($structure->hasChanged()) { $b .= ' [' . $this->str['viewedited'] . ']'; } else { $b .= ' [' . $this->str['viewedited'] . ']'; } // The new table button $b .= ' [' . $this->str['newtable'] . ']'; // The new from MySQL button if ($DB->get_dbfamily() == 'mysql') { $b .= ' [' . $this->str['newtablefrommysql'] . ']'; } else { $b .= ' [' . $this->str['newtablefrommysql'] . ']'; } // The view sql code button $b .= '[' .$this->str['viewsqlcode'] . ']'; // The view php code button $b .= ' [' . $this->str['viewphpcode'] . ']'; // The save button (if possible) if ($cansavenow) { $b .= ' [' . $this->str['save'] . ']'; } // The back to main menu button $b .= ' [' . $this->str['backtomainview'] . ']'; $b .= '

'; $o .= $e . $b; // Join all the reserved words into one big array // Calculate list of available SQL generators require_once("$CFG->libdir/ddl/sql_generator.php"); $reserved_words = sql_generator::getAllReservedWords(); // Add the tables list $tables = $structure->getTables(); if ($tables) { $o .= '

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

'; $o .= ''; $row = 0; foreach ($tables as $table) { // The table name (link to edit table) $t = '' . $table->getName() . ''; // Calculate buttons $b = ''; // Print table row $o .= ''; $row = ($row + 1) % 2; } $o .= '
'; // The edit button $b .= '[' . $this->str['edit'] . ']'; $b .= ''; // The up button if ($table->getPrevious()) { $b .= '[' . $this->str['up'] . ']'; } else { $b .= '[' . $this->str['up'] . ']'; } $b .= ''; // The down button if ($table->getNext()) { $b .= '[' . $this->str['down'] . ']'; } else { $b .= '[' . $this->str['down'] . ']'; } $b .= ''; // The delete button (if we have more than one and it isn't used) if (count($tables) > 1 && !$structure->getTableUses($table->getName())) { // !$structure->getTableUses($table->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($table->getName(), $reserved_words)) { $b .= ' ' . $this->str['reserved'] . ''; } $b .= '
' . $t . $b . '
'; } // Add the back to main $this->output = $o; } } // Launch postaction if exists (leave this unmodified) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } return $result; } }