mark rename table as not implemented
[moodle.git] / admin / xmldb / actions / view_table_php / view_table_php.class.php
CommitLineData
c5a6ae17 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12// //
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. //
17// //
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: //
22// //
23// http://www.gnu.org/copyleft/gpl.html //
24// //
25///////////////////////////////////////////////////////////////////////////
26
27/// This class will show the PHP needed to perform the desired action
28/// with the specified fields/keys/indexes.
29
30class view_table_php extends XMLDBAction {
31
eef868d1 32 /**
c5a6ae17 33 * Init method, every subclass will have its own
34 */
35 function init() {
36 parent::init();
37
38 /// Set own custom attributes
39
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selectfieldkeyindex' => 'xmldb',
44 'view' => 'xmldb',
eafc2b70 45 'table' => 'xmldb',
46 'selectonefieldkeyindex' => 'xmldb',
c5a6ae17 47 'back' => 'xmldb'
48 ));
49 }
50
51 /**
52 * Invoke method, every class will have its own
53 * returns true/false on completion, setting both
54 * errormsg and output as necessary
55 */
56 function invoke() {
57 parent::invoke();
58
59 $result = true;
60
61 /// Set own core attributes
62 $this->does_generate = ACTION_GENERATE_HTML;
63
64 /// These are always here
65 global $CFG, $XMLDB;
66
67 /// Do the job, setting result as needed
68 /// Get the dir containing the file
69 $dirpath = required_param('dir', PARAM_PATH);
70 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
71
72 /// Get the correct dirs
73 if (!empty($XMLDB->dbdirs)) {
74 $dbdir =& $XMLDB->dbdirs[$dirpath];
75 } else {
76 return false;
77 }
78 if (!empty($XMLDB->editeddirs)) {
79 $editeddir =& $XMLDB->editeddirs[$dirpath];
80 $structure =& $editeddir->xml_file->getStructure();
81 }
82 /// ADD YOUR CODE HERE
83
84 $tableparam = required_param('table', PARAM_PATH);
eef868d1 85
c5a6ae17 86 $table =& $structure->getTable($tableparam);
87 $fields = $table->getFields();
88 $field = reset($fields);
89 $defaultfieldkeyindex = null;
90 if ($field) {
eafc2b70 91 $defaultfieldkeyindex = 'f#' . $field->getName();
c5a6ae17 92 }
eafc2b70 93 $keys = $table->getKeys();
94 $indexes = $table->getIndexes();
c5a6ae17 95
96 /// Get parameters
97 $commandparam = optional_param('command', 'add_field', PARAM_PATH);
3be91b01 98 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
99 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
c5a6ae17 100
101 /// The back to edit xml button
102 $b = ' <p align="center" class="buttons">';
eafc2b70 103 $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
c5a6ae17 104 $b .= '</p>';
105 $o = $b;
106
eafc2b70 107 /// The table currently being edited
108 $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
109
c5a6ae17 110 /// Calculate the popup of commands
eef868d1 111 $commands = array('add_field',
c5a6ae17 112 'drop_field',
113 'rename_field');
114 foreach ($commands as $command) {
115 $popcommands[$command] = str_replace('_', ' ', $command);
116 }
eafc2b70 117 /// Calculate the popup of fields/keys/indexes
118 $optionspacer = '&nbsp;&nbsp;&nbsp;';
119 if ($fields) {
3be91b01 120 $popfields['fieldshead'] = 'Fields';
eafc2b70 121 foreach ($fields as $field) {
3be91b01 122 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 123 }
124 }
125 if ($keys) {
3be91b01 126 $popfields['keyshead'] = 'Keys';
eafc2b70 127 foreach ($keys as $key) {
3be91b01 128 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 129 }
130 }
131 if ($indexes) {
3be91b01 132 $popfields['indexeshead'] = 'Indexes';
eafc2b70 133 foreach ($indexes as $index) {
3be91b01 134 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 135 }
c5a6ae17 136 }
eafc2b70 137
c5a6ae17 138 /// Now build the form
139 $o.= '<form id="form" action="index.php" method="post">';
140 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 141 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 142 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
143 $o.= ' <table id="formelements" align="center" cellpadding="5">';
3be91b01 144 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
c5a6ae17 145 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
146 $o.= ' </table>';
147 $o.= '</form>';
eafc2b70 148
c5a6ae17 149 $o.= ' <table id="phpcode" align="center" cellpadding="5">';
150 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 151 /// Check we have selected some field/key/index from the popup
152 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
153 $o.= s($this->str['selectonefieldkeyindex']);
154 } else {
155 /// Based on current params, call the needed function
156 switch ($commandparam) {
3be91b01 157 case 'add_field':
158 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
eafc2b70 159 break;
3be91b01 160 case 'drop_field':
161 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
eafc2b70 162 break;
3be91b01 163 case 'rename_field':
164 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
eafc2b70 165 break;
166 }
c5a6ae17 167 }
168 $o.= '</textarea></td></tr>';
169 $o.= ' </table>';
170
171 $this->output = $o;
172
173 /// Launch postaction if exists (leave this here!)
eef868d1 174 if ($this->getPostAction() && $result) {
c5a6ae17 175 return $this->launch($this->getPostAction());
176 }
177
178 /// Return ok if arrived here
179 return $result;
180 }
181
182 /**
eef868d1 183 * This function will generate all the PHP code needed to
3be91b01 184 * create one field using XMLDB objects and functions
eef868d1 185 *
c5a6ae17 186 * @param XMLDBStructure structure object containing all the info
3be91b01 187 * @param string table table name
188 * @param string field field name to be created
189 * @return string PHP code to be used to create the field
c5a6ae17 190 */
3be91b01 191 function add_field_php($structure, $table, $field) {
c5a6ae17 192
193 $result = '';
194 /// Validate if we can do it
195 if (!$table = $structure->getTable($table)) {
196 return false;
197 }
3be91b01 198 if (!$field = $table->getField($field)) {
199 return false;
200 }
c5a6ae17 201 if ($table->getAllErrors()) {
202 return false;
203 }
204
205 /// Add the standard PHP header
206 $result .= XMLDB_PHP_HEADER;
207
208 /// Add contents
209 $result .= XMLDB_LINEFEED;
3be91b01 210 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 211 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 212 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
213 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 214
215 /// Launch the proper DDL
216 $result .= XMLDB_LINEFEED;
3be91b01 217 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
218 $result .= ' $status = $status && add_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 219
220 /// Add standard PHP footer
221 $result .= XMLDB_PHP_FOOTER;
222
223 return $result;
224 }
225
226 /**
eef868d1 227 * This function will generate all the PHP code needed to
3be91b01 228 * drop one field using XMLDB objects and functions
eef868d1 229 *
c5a6ae17 230 * @param XMLDBStructure structure object containing all the info
3be91b01 231 * @param string table table name
232 * @param string field field name to be dropped
233 * @return string PHP code to be used to drop the field
c5a6ae17 234 */
3be91b01 235 function drop_field_php($structure, $table, $field) {
c5a6ae17 236
237 $result = '';
238 /// Validate if we can do it
239 if (!$table = $structure->getTable($table)) {
240 return false;
241 }
3be91b01 242 if (!$field = $table->getField($field)) {
243 return false;
244 }
c5a6ae17 245 if ($table->getAllErrors()) {
246 return false;
247 }
248
249 /// Add the standard PHP header
250 $result .= XMLDB_PHP_HEADER;
251
252 /// Add contents
253 $result .= XMLDB_LINEFEED;
3be91b01 254 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
c5a6ae17 255 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 256 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 257
258 /// Launch the proper DDL
259 $result .= XMLDB_LINEFEED;
3be91b01 260 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
261 $result .= ' $status = $status && drop_field($table, $field);' . XMLDB_LINEFEED;
c5a6ae17 262
263 /// Add standard PHP footer
264 $result .= XMLDB_PHP_FOOTER;
265
266 return $result;
267 }
268
269 /**
eef868d1 270 * This function will generate all the PHP code needed to
3be91b01 271 * rename one field using XMLDB objects and functions
eef868d1 272 *
c5a6ae17 273 * @param XMLDBStructure structure object containing all the info
3be91b01 274 * @param string table table name
275 * @param string field field name to be renamed
276 * @return string PHP code to be used to drop the field
c5a6ae17 277 */
3be91b01 278 function rename_field_php($structure, $table, $field) {
c5a6ae17 279
280 $result = '';
281 /// Validate if we can do it
282 if (!$table = $structure->getTable($table)) {
283 return false;
284 }
3be91b01 285 if (!$field = $table->getField($field)) {
286 return false;
287 }
c5a6ae17 288 if ($table->getAllErrors()) {
289 return false;
290 }
291
292 /// Add the standard PHP header
293 $result .= XMLDB_PHP_HEADER;
294
295 /// Add contents
296 $result .= XMLDB_LINEFEED;
3be91b01 297 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
c5a6ae17 298 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3be91b01 299 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 300
301 /// Launch the proper DDL
302 $result .= XMLDB_LINEFEED;
3be91b01 303 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
304 $result .= ' $status = $status && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 305
306 /// Add standard PHP footer
307 $result .= XMLDB_PHP_FOOTER;
308
309 return $result;
310 }
3be91b01 311
c5a6ae17 312}
313?>