updating index files MDL-3484
[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
32 /**
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',
45 'back' => 'xmldb'
46 ));
47 }
48
49 /**
50 * Invoke method, every class will have its own
51 * returns true/false on completion, setting both
52 * errormsg and output as necessary
53 */
54 function invoke() {
55 parent::invoke();
56
57 $result = true;
58
59 /// Set own core attributes
60 $this->does_generate = ACTION_GENERATE_HTML;
61
62 /// These are always here
63 global $CFG, $XMLDB;
64
65 /// Do the job, setting result as needed
66 /// Get the dir containing the file
67 $dirpath = required_param('dir', PARAM_PATH);
68 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
69
70 /// Get the correct dirs
71 if (!empty($XMLDB->dbdirs)) {
72 $dbdir =& $XMLDB->dbdirs[$dirpath];
73 } else {
74 return false;
75 }
76 if (!empty($XMLDB->editeddirs)) {
77 $editeddir =& $XMLDB->editeddirs[$dirpath];
78 $structure =& $editeddir->xml_file->getStructure();
79 }
80 /// ADD YOUR CODE HERE
81
82 $tableparam = required_param('table', PARAM_PATH);
83
84 $table =& $structure->getTable($tableparam);
85 $fields = $table->getFields();
86 $field = reset($fields);
87 $defaultfieldkeyindex = null;
88 if ($field) {
89 $defaultfieldkeyindex = $field->getName();
90 }
91
92 /// Get parameters
93 $commandparam = optional_param('command', 'add_field', PARAM_PATH);
94 $fieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
95
96 /// The back to edit xml button
97 $b = ' <p align="center" class="buttons">';
98 $b .= '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
99 $b .= '</p>';
100 $o = $b;
101
102 /// Calculate the popup of commands
103 $commands = array('add_field',
104 'drop_field',
105 'rename_field');
106 foreach ($commands as $command) {
107 $popcommands[$command] = str_replace('_', ' ', $command);
108 }
109 /// Calculate the popup of tables
110 foreach ($fields as $field) {
111 $poptables[$field->getName()] = $field->getName();
112 }
113 /// Now build the form
114 $o.= '<form id="form" action="index.php" method="post">';
115 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
116 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
117 $o.= ' <table id="formelements" align="center" cellpadding="5">';
118 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="table" accesskey="t">' . $this->str['selectfieldkeyindex'] . ' </label>' .choose_from_menu($poptables, 'table', $tableparam, '', '', 0, true) . '</td></tr>';
119 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
120 $o.= ' </table>';
121 $o.= '</form>';
122 $o.= ' <table id="phpcode" align="center" cellpadding="5">';
123 $o.= ' <tr><td><textarea cols="80" rows="32">';
124 /// Based on current params, call the needed function
125 switch ($commandparam) {
126 case 'create_table':
127 $o.= s($this->create_table_php($structure, $tableparam));
128 break;
129 case 'drop_table':
130 $o.= s($this->drop_table_php($structure, $tableparam));
131 break;
132 case 'rename_table':
133 $o.= s($this->rename_table_php($structure, $tableparam));
134 break;
135 }
136 $o.= '</textarea></td></tr>';
137 $o.= ' </table>';
138
139 $this->output = $o;
140
141 /// Launch postaction if exists (leave this here!)
142 if ($this->getPostAction() && $result) {
143 return $this->launch($this->getPostAction());
144 }
145
146 /// Return ok if arrived here
147 return $result;
148 }
149
150 /**
151 * This function will generate all the PHP code needed to
152 * create one table using XMLDB objects and functions
153 *
154 * @param XMLDBStructure structure object containing all the info
155 * @param string table table code to be created
156 * @return string PHP code to be used to create the table
157 */
158 function create_table_php($structure, $table) {
159
160 $result = '';
161 /// Validate if we can do it
162 if (!$table = $structure->getTable($table)) {
163 return false;
164 }
165 if ($table->getAllErrors()) {
166 return false;
167 }
168
169 /// Add the standard PHP header
170 $result .= XMLDB_PHP_HEADER;
171
172 /// Add contents
173 $result .= XMLDB_LINEFEED;
174 $result .= ' /// Create table ' . $table->getName() . XMLDB_LINEFEED;
175 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
176 $result .= XMLDB_LINEFEED;
177 $result .= ' /// Adding fields to table ' . $table->getName() . XMLDB_LINEFEED;
178 /// Iterate over each field
179 foreach ($table->getFields() as $field) {
180 /// The field header, with name
181 $result .= ' $table->addFieldInfo(' . "'" . $field->getName() . "', ";
182 /// The field PHP specs
183 $result .= $field->getPHP(false);
184 /// The end of the line
185 $result .= ');' . XMLDB_LINEFEED;
186 }
187 /// Iterate over each key
188 if ($keys = $table->getKeys()) {
189 $result .= XMLDB_LINEFEED;
190 $result .= ' /// Adding keys to table ' . $table->getName() . XMLDB_LINEFEED;
191 foreach ($keys as $key) {
192 /// The key header, with name
193 $result .= ' $table->addKeyInfo(' . "'" . $key->getName() . "', ";
194 /// The key PHP specs
195 $result .= $key->getPHP();
196 /// The end of the line
197 $result .= ');' . XMLDB_LINEFEED;
198 }
199 }
200 /// Iterate over each index
201 if ($indexes = $table->getIndexes()) {
202 $result .= XMLDB_LINEFEED;
203 $result .= ' /// Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED;
204 foreach ($indexes as $index) {
205 /// The index header, with name
206 $result .= ' $table->addIndexInfo(' . "'" . $index->getName() . "', ";
207 /// The index PHP specs
208 $result .= $index->getPHP();
209 /// The end of the line
210 $result .= ');' . XMLDB_LINEFEED;
211 }
212 }
213
214 /// Launch the proper DDL
215 $result .= XMLDB_LINEFEED;
216 $result .= ' /// Launch create table for ' . $table->getName() . XMLDB_LINEFEED;
217 $result .= ' $status = $status && create_table($table);' . XMLDB_LINEFEED;
218
219 /// Add standard PHP footer
220 $result .= XMLDB_PHP_FOOTER;
221
222 return $result;
223 }
224
225 /**
226 * This function will generate all the PHP code needed to
227 * drop one table using XMLDB objects and functions
228 *
229 * @param XMLDBStructure structure object containing all the info
230 * @param string table table code to be dropped
231 * @return string PHP code to be used to drop the table
232 */
233 function drop_table_php($structure, $table) {
234
235 $result = '';
236 /// Validate if we can do it
237 if (!$table = $structure->getTable($table)) {
238 return false;
239 }
240 if ($table->getAllErrors()) {
241 return false;
242 }
243
244 /// Add the standard PHP header
245 $result .= XMLDB_PHP_HEADER;
246
247 /// Add contents
248 $result .= XMLDB_LINEFEED;
249 $result .= ' /// Create table ' . $table->getName() . XMLDB_LINEFEED;
250 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
251
252 /// Launch the proper DDL
253 $result .= XMLDB_LINEFEED;
254 $result .= ' /// Launch drop table for ' . $table->getName() . XMLDB_LINEFEED;
255 $result .= ' $status = $status && drop_table($table);' . XMLDB_LINEFEED;
256
257 /// Add standard PHP footer
258 $result .= XMLDB_PHP_FOOTER;
259
260 return $result;
261 }
262
263 /**
264 * This function will generate all the PHP code needed to
265 * rename one table using XMLDB objects and functions
266 *
267 * @param XMLDBStructure structure object containing all the info
268 * @param string table table code to be renamed
269 * @return string PHP code to be used to rename the table
270 */
271 function rename_table_php($structure, $table) {
272
273 $result = '';
274 /// Validate if we can do it
275 if (!$table = $structure->getTable($table)) {
276 return false;
277 }
278 if ($table->getAllErrors()) {
279 return false;
280 }
281
282 /// Add the standard PHP header
283 $result .= XMLDB_PHP_HEADER;
284
285 /// Add contents
286 $result .= XMLDB_LINEFEED;
287 $result .= ' /// Create table ' . $table->getName() . XMLDB_LINEFEED;
288 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
289
290 /// Launch the proper DDL
291 $result .= XMLDB_LINEFEED;
292 $result .= ' /// Launch rename table for ' . $table->getName() . XMLDB_LINEFEED;
293 $result .= ' $status = $status && rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED;
294
295 /// Add standard PHP footer
296 $result .= XMLDB_PHP_FOOTER;
297
298 return $result;
299 }
300}
301?>