3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will provide the interface for all the edit key actions
29 class edit_key
extends XMLDBAction
{
32 * Init method, every subclass will have its own
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
42 'vieworiginal' => 'xmldb',
43 'viewedited' => 'xmldb',
51 * Invoke method, every class will have its own
52 * returns true/false on completion, setting both
53 * errormsg and output as necessary
60 /// Set own core attributes
61 $this->does_generate
= ACTION_GENERATE_HTML
;
63 /// These are always here
66 /// Do the job, setting result as needed
67 /// Get the dir containing the file
68 $dirpath = required_param('dir', PARAM_PATH
);
69 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
71 /// Get the correct dirs
72 if (!empty($XMLDB->dbdirs
)) {
73 $dbdir =& $XMLDB->dbdirs
[$dirpath];
77 if (!empty($XMLDB->editeddirs
)) {
78 $editeddir =& $XMLDB->editeddirs
[$dirpath];
79 $structure =& $editeddir->xml_file
->getStructure();
82 /// ADD YOUR CODE HERE
84 /// Fetch request data
85 $tableparam = required_param('table', PARAM_CLEAN
);
86 if (!$table =& $structure->getTable($tableparam)) {
87 $this->errormsg
= 'Wrong table specified: ' . $tableparm;
90 $keyparam = required_param('key', PARAM_CLEAN
);
91 if (!$key =& $table->getKey($keyparam)) {
92 /// Arriving here from a name change, looking for the new key name
93 $keyparam = required_param('name', PARAM_CLEAN
);
94 $key =& $table->getKey($keyparam);
97 $dbdir =& $XMLDB->dbdirs
[$dirpath];
98 $origstructure =& $dbdir->xml_file
->getStructure();
100 /// Add the main form
101 $o = '<form id="form" action="index.php" method="post">';
103 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot
, '', $dirpath) . '" />';
104 $o.= ' <input type="hidden" name ="table" value="' . $tableparam .'" />';
105 $o.= ' <input type="hidden" name ="key" value="' . $keyparam .'" />';
106 $o.= ' <input type="hidden" name ="action" value="edit_key_save" />';
107 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />';
108 $o.= ' <table id="formelements" class="boxaligncenter">';
110 /// If the key has dependencies, we cannot change its name
112 if ($structure->getKeyUses($table->getName(), $key->getName())) {
113 $disabled = ' disabled="disabled " ';
115 $o.= ' <tr valign="top"><td><label for="name" accesskey="n">Name:</label></td><td colspan="2"><input name="name" type="text" size="30" id="name"' . $disabled . ' value="' . s($key->getName()) . '" /></td></tr>';
116 /// XMLDB key comment
117 $o.= ' <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td colspan="2"><textarea name="comment" rows="3" cols="80" id="comment">' . s($key->getComment()) . '</textarea></td></tr>';
119 $typeoptions = array (XMLDB_KEY_PRIMARY
=> $key->getXMLDBKeyName(XMLDB_KEY_PRIMARY
),
120 XMLDB_KEY_UNIQUE
=> $key->getXMLDBKeyName(XMLDB_KEY_UNIQUE
),
121 XMLDB_KEY_FOREIGN
=> $key->getXMLDBKeyName(XMLDB_KEY_FOREIGN
),
122 XMLDB_KEY_FOREIGN_UNIQUE
=> $key->getXMLDBKeyName(XMLDB_KEY_FOREIGN_UNIQUE
));
123 /// Only show the XMLDB_KEY_FOREIGN_UNIQUE if the Key has that type
124 /// if ($key->getType() != XMLDB_KEY_FOREIGN_UNIQUE) {
125 /// unset ($typeoptions[XMLDB_KEY_FOREIGN_UNIQUE);
127 $o.= ' <tr valign="top"><td><label for="menutype" accesskey="t">Type:</label></td>';
128 $o.= ' <td colspan="2">' . choose_from_menu($typeoptions, 'type', $key->getType(), '', '', '', true) . '</td></tr>';
130 $o.= ' <tr valign="top"><td><label for="fields" accesskey="f">Fields:</label></td>';
131 $o.= ' <td colspan="2"><input name="fields" type="text" size="40" maxlength="80" id="fields" value="' . s(implode(', ', $key->getFields())) . '" /></td></tr>';
132 /// XMLDBKey Reftable
133 $o.= ' <tr valign="top"><td><label for="reftable" accesskey="t">Reftable:</label></td>';
134 $o.= ' <td colspan="2"><input name="reftable" type="text" size="20" maxlength="40" id="reftable" value="' . s($key->getReftable()) . '" /></td></tr>';
135 /// XMLDBKey Reffields
136 $o.= ' <tr valign="top"><td><label for="reffields" accesskey="t">Reffields:</label></td>';
137 $o.= ' <td colspan="2"><input name="reffields" type="text" size="40" maxlength="80" id="reffields" value="' . s(implode(', ', $key->getRefFields())) . '" /></td></tr>';
139 $o.= ' <tr valign="top"><td> </td><td colspan="2"><input type="submit" value="' .$this->str
['change'] . '" /></td></tr>';
141 $o.= '</div></form>';
142 /// Calculate the buttons
143 $b = ' <p class="centerpara buttons">';
144 /// The view original XML button
145 if ($table->getKey($keyparam)) {
146 $b .= ' <a href="index.php?action=view_key_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&select=original&table=' . $tableparam . '&key=' . $keyparam . '">[' . $this->str
['vieworiginal'] . ']</a>';
148 $b .= ' [' . $this->str
['vieworiginal'] . ']';
150 /// The view edited XML button
151 if ($key->hasChanged()) {
152 $b .= ' <a href="index.php?action=view_key_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&select=edited&table=' . $tableparam . '&key=' . $keyparam . '">[' . $this->str
['viewedited'] . ']</a>';
154 $b .= ' [' . $this->str
['viewedited'] . ']';
156 /// The back to edit table button
157 $b .= ' <a href="index.php?action=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['back'] . ']</a>';
163 /// Launch postaction if exists (leave this here!)
164 if ($this->getPostAction() && $result) {
165 return $this->launch($this->getPostAction());
168 /// Return ok if arrived here