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 table actions
29 class edit_table
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',
44 'viewsqlcode' => 'xmldb',
45 'viewphpcode' => 'xmldb',
46 'newfield' => 'xmldb',
48 'newindex' => 'xmldb',
56 'reserved' => 'xmldb',
62 * Invoke method, every class will have its own
63 * returns true/false on completion, setting both
64 * errormsg and output as necessary
71 /// Set own core attributes
72 $this->does_generate
= ACTION_GENERATE_HTML
;
74 /// These are always here
77 /// Do the job, setting result as needed
78 /// Get the dir containing the file
79 $dirpath = required_param('dir', PARAM_PATH
);
80 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
82 /// Get the correct dirs
83 if (!empty($XMLDB->dbdirs
)) {
84 $dbdir =& $XMLDB->dbdirs
[$dirpath];
88 if (!empty($XMLDB->editeddirs
)) {
89 $editeddir =& $XMLDB->editeddirs
[$dirpath];
90 $structure =& $editeddir->xml_file
->getStructure();
93 /// ADD YOUR CODE HERE
94 $tableparam = required_param('table', PARAM_CLEAN
);
95 if (!$table =& $structure->getTable($tableparam)) {
96 /// Arriving here from a name change, looking for the new table name
97 $tableparam = required_param('name', PARAM_CLEAN
);
98 $table =& $structure->getTable($tableparam);
101 $dbdir =& $XMLDB->dbdirs
[$dirpath];
102 $origstructure =& $dbdir->xml_file
->getStructure();
104 /// Add the main form
105 $o = '<form id="form" action="index.php" method="post">';
107 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot
, '', $dirpath) . '" />';
108 $o.= ' <input type="hidden" name ="table" value="' . $tableparam .'" />';
109 $o.= ' <input type="hidden" name ="action" value="edit_table_save" />';
110 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />';
111 $o.= ' <table id="formelements" class="boxaligncenter">';
112 /// If the table is being used, we cannot rename it
113 if ($structure->getTableUses($table->getName())) {
114 $o.= ' <tr valign="top"><td>Name:</td><td><input type="hidden" name ="name" value="' . s($table->getName()) . '" />' . s($table->getName()) .'</td></tr>';
116 $o.= ' <tr valign="top"><td><label for="name" accesskey="p">Name:</label></td><td><input name="name" type="text" size="28" maxlength="28" id="name" value="' . s($table->getName()) . '" /></td></tr>';
118 $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>';
119 $o.= ' <tr valign="top"><td> </td><td><input type="submit" value="' .$this->str
['change'] . '" /></td></tr>';
121 $o.= '</div></form>';
122 /// Calculate the buttons
123 $b = ' <p class="centerpara buttons">';
124 /// The view original XML button
125 if ($origstructure->getTable($tableparam)) {
126 $b .= ' <a href="index.php?action=view_table_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&select=original&table=' . $tableparam . '">[' . $this->str
['vieworiginal'] . ']</a>';
128 $b .= ' [' . $this->str
['vieworiginal'] . ']';
130 /// The view edited XML button
131 if ($table->hasChanged()) {
132 $b .= ' <a href="index.php?action=view_table_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&select=edited&table=' . $tableparam . '">[' . $this->str
['viewedited'] . ']</a>';
134 $b .= ' [' . $this->str
['viewedited'] . ']';
136 /// The new field button
137 $b .= ' <a href="index.php?action=new_field&postaction=edit_field&table=' . $tableparam . '&field=changeme&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['newfield'] . ']</a>';
138 /// The new key button
139 $b .= ' <a href="index.php?action=new_key&postaction=edit_key&table=' . $tableparam . '&key=changeme&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['newkey'] . ']</a>';
140 /// The new index button
141 $b .= ' <a href="index.php?action=new_index&postaction=edit_index&table=' . $tableparam . '&index=changeme&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['newindex'] . ']</a>';
142 /// The back to edit xml file button
143 $b .= ' <a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['back'] . ']</a>';
145 $b .= ' <p class="centerpara buttons">';
146 /// The view sql code button
147 $b .= '<a href="index.php?action=view_table_sql&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' .$this->str
['viewsqlcode'] . ']</a>';
148 /// The view php code button
149 $b .= ' <a href="index.php?action=view_table_php&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['viewphpcode'] . ']</a>';
153 /// Join all the reserved words into one big array
154 /// Calculate list of available SQL generators
155 $plugins = get_list_of_plugins('lib/xmldb/classes/generators');
156 $reserved_words = array();
157 foreach($plugins as $plugin) {
158 $classname = 'XMLDB' . $plugin;
159 $generator = new $classname();
160 $reserved_words = array_merge($reserved_words, $generator->getReservedWords());
162 sort($reserved_words);
163 $reserved_words = array_unique($reserved_words);
165 /// Delete any 'changeme' field/key/index
166 $table->deleteField('changeme');
167 $table->deleteKey('changeme');
168 $table->deleteIndex('changeme');
170 /// Add the fields list
171 $fields =& $table->getFields();
172 if (!empty($fields)) {
173 $o .= '<h3 class="main">' . $this->str
['fields'] . '</h3>';
174 $o .= '<table id="listfields" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
176 foreach ($fields as $field) {
177 /// Calculate buttons
178 $b = '</td><td class="button cell">';
179 /// The edit button (if the field has no uses)
180 if (!$structure->getFieldUses($table->getName(), $field->getName())) {
181 $b .= '<a href="index.php?action=edit_field&field=' .$field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['edit'] . ']</a>';
183 $b .= '[' . $this->str
['edit'] . ']';
185 $b .= '</td><td class="button cell">';
187 if ($field->getPrevious()) {
188 $b .= '<a href="index.php?action=move_updown_field&direction=up&field=' . $field->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['up'] . ']</a>';
190 $b .= '[' . $this->str
['up'] . ']';
192 $b .= '</td><td class="button cell">';
194 if ($field->getNext()) {
195 $b .= '<a href="index.php?action=move_updown_field&direction=down&field=' . $field->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['down'] . ']</a>';
197 $b .= '[' . $this->str
['down'] . ']';
199 $b .= '</td><td class="button cell">';
200 /// The delete button (if we have more than one and it isn't used
201 if (count($fields) > 1 &&
202 !$structure->getFieldUses($table->getName(), $field->getName())) {
203 $b .= '<a href="index.php?action=delete_field&field=' . $field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['delete'] . ']</a>';
205 $b .= '[' . $this->str
['delete'] . ']';
207 /// Detect if the table name is a reserved word
208 if (in_array($field->getName(), $reserved_words)) {
209 $b .= ' <a href="index.php?action=view_reserved_words"><span class="error">' . $this->str
['reserved'] . '</span></a>';
211 /// The readable info
212 $r = '</td><td class="readableinfo cell">' . $field->readableInfo() . '</td>';
214 $o .= '<tr class="r' . $row . '"><td class="table cell"><a href="index.php?action=view_field_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&field=' . $field->getName() . '&table=' . $table->getName() . '&select=edited">' . $field->getName() . '</a>' . $b . $r . '</tr>';
215 $row = ($row +
1) %
2;
219 /// Add the keys list
220 $keys =& $table->getKeys();
222 $o .= '<h3 class="main">' . $this->str
['keys'] . '</h3>';
223 $o .= '<table id="listkeys" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
225 foreach ($keys as $key) {
226 /// Calculate buttons
227 $b = '</td><td class="button cell">';
228 /// The edit button (if the key hasn't uses)
229 if (!$structure->getKeyUses($table->getName(), $key->getName())) {
230 $b .= '<a href="index.php?action=edit_key&key=' .$key->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['edit'] . ']</a>';
232 $b .= '[' . $this->str
['edit'] . ']';
234 $b .= '</td><td class="button cell">';
236 if ($key->getPrevious()) {
237 $b .= '<a href="index.php?action=move_updown_key&direction=up&key=' . $key->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['up'] . ']</a>';
239 $b .= '[' . $this->str
['up'] . ']';
241 $b .= '</td><td class="button cell">';
243 if ($key->getNext()) {
244 $b .= '<a href="index.php?action=move_updown_key&direction=down&key=' . $key->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['down'] . ']</a>';
246 $b .= '[' . $this->str
['down'] . ']';
248 $b .= '</td><td class="button cell">';
249 /// The delete button (if the key hasn't uses)
250 if (!$structure->getKeyUses($table->getName(), $key->getName())) {
251 $b .= '<a href="index.php?action=delete_key&key=' . $key->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['delete'] . ']</a>';
253 $b .= '[' . $this->str
['delete'] . ']';
255 /// The readable info
256 $r = '</td><td class="readableinfo cell">' . $key->readableInfo() . '</td>';
258 $o .= '<tr class="r' . $row . '"><td class="table cell"><a href="index.php?action=view_key_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&key=' . $key->getName() . '&table=' . $table->getName() . '&select=edited">' . $key->getName() . '</a>' . $b . $r .'</tr>';
259 $row = ($row +
1) %
2;
263 /// Add the indexes list
264 $indexes =& $table->getIndexes();
265 if (!empty($indexes)) {
266 $o .= '<h3 class="main">' . $this->str
['indexes'] . '</h3>';
267 $o .= '<table id="listindexes" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
269 foreach ($indexes as $index) {
270 /// Calculate buttons
271 $b = '</td><td class="button cell">';
273 $b .= '<a href="index.php?action=edit_index&index=' .$index->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['edit'] . ']</a>';
274 $b .= '</td><td class="button cell">';
276 if ($index->getPrevious()) {
277 $b .= '<a href="index.php?action=move_updown_index&direction=up&index=' . $index->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['up'] . ']</a>';
279 $b .= '[' . $this->str
['up'] . ']';
281 $b .= '</td><td class="button cell">';
283 if ($index->getNext()) {
284 $b .= '<a href="index.php?action=move_updown_index&direction=down&index=' . $index->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['down'] . ']</a>';
286 $b .= '[' . $this->str
['down'] . ']';
288 $b .= '</td><td class="button cell">';
289 /// The delete button
290 $b .= '<a href="index.php?action=delete_index&index=' . $index->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['delete'] . ']</a>';
291 /// The readable info
292 $r = '</td><td class="readableinfo cell">' . $index->readableInfo() . '</td>';
294 $o .= '<tr class="r' . $row . '"><td class="table cell"><a href="index.php?action=view_index_xml&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&index=' . $index->getName() . '&table=' . $table->getName() . '&select=edited">' . $index->getName() . '</a>' . $b . $r .'</tr>';
295 $row = ($row +
1) %
2;
302 /// Launch postaction if exists (leave this here!)
303 if ($this->getPostAction() && $result) {
304 return $this->launch($this->getPostAction());
307 /// Return ok if arrived here