NOBUG xmldb editor - make php code generated to follow the 2-slash coding style
[moodle.git] / admin / xmldb / actions / view_structure_php / view_structure_php.class.php
CommitLineData
11e167a9 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package xmldb-editor
20 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
25 * This class will show the PHP needed (upgrade block) to perform
26 * the desired DDL action with the specified table
27 *
28 * @package xmldb-editor
29 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
b0c7d558 32class view_structure_php extends XMLDBAction {
33
eef868d1 34 /**
b0c7d558 35 * Init method, every subclass will have its own
36 */
37 function init() {
38 parent::init();
39
40 /// Set own custom attributes
f25d53a9 41 $this->sesskey_protected = false; // This action doesn't need sesskey protection
b0c7d558 42
43 /// Get needed strings
44 $this->loadStrings(array(
45 'selectaction' => 'xmldb',
46 'selecttable' => 'xmldb',
47 'view' => 'xmldb',
48 'back' => 'xmldb'
49 ));
50 }
51
52 /**
53 * Invoke method, every class will have its own
54 * returns true/false on completion, setting both
55 * errormsg and output as necessary
56 */
57 function invoke() {
58 parent::invoke();
59
60 $result = true;
61
62 /// Set own core attributes
63 $this->does_generate = ACTION_GENERATE_HTML;
64
65 /// These are always here
b324b47e 66 global $CFG, $XMLDB, $OUTPUT;
b0c7d558 67
68 /// Do the job, setting result as needed
69 /// Get the dir containing the file
70 $dirpath = required_param('dir', PARAM_PATH);
294ce987 71 $dirpath = $CFG->dirroot . $dirpath;
b0c7d558 72
73 /// Get the correct dirs
74 if (!empty($XMLDB->dbdirs)) {
75 $dbdir =& $XMLDB->dbdirs[$dirpath];
76 } else {
77 return false;
78 }
79 if (!empty($XMLDB->editeddirs)) {
80 $editeddir =& $XMLDB->editeddirs[$dirpath];
81 $structure =& $editeddir->xml_file->getStructure();
82 }
83 /// ADD YOUR CODE HERE
eef868d1 84
b0c7d558 85 $tables =& $structure->getTables();
86 $table = reset($tables);
87 $defaulttable = null;
88 if ($table) {
89 $defaulttable = $table->getName();
90 }
91
92 /// Get parameters
93 $commandparam = optional_param('command', 'create_table', PARAM_PATH);
94 $tableparam = optional_param('table', $defaulttable, PARAM_PATH);
95
96 /// The back to edit xml button
007456de 97 $b = ' <p class="centerpara buttons">';
b0c7d558 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
eef868d1 103 $commands = array('create_table',
b0c7d558 104 'drop_table',
ef856b2d 105 'rename_table');
b0c7d558 106 foreach ($commands as $command) {
107 $popcommands[$command] = str_replace('_', ' ', $command);
108 }
109 /// Calculate the popup of tables
110 foreach ($tables as $table) {
111 $poptables[$table->getName()] = $table->getName();
112 }
113 /// Now build the form
114 $o.= '<form id="form" action="index.php" method="post">';
007456de 115 $o.='<div>';
b0c7d558 116 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
117 $o.= ' <input type="hidden" name ="action" value="view_structure_php" />';
007456de 118 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
d776d59e 119 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . html_writer::select($popcommands, 'command', $commandparam, false) . '&nbsp;<label for="table" accesskey="t">' . $this->str['selecttable'] . ' </label>' .html_writer::select($poptables, 'table', $tableparam, false) . '</td></tr>';
b0c7d558 120 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
121 $o.= ' </table>';
007456de 122 $o.= '</div></form>';
123 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
b0c7d558 124 $o.= ' <tr><td><textarea cols="80" rows="32">';
125 /// Based on current params, call the needed function
126 switch ($commandparam) {
127 case 'create_table':
128 $o.= s($this->create_table_php($structure, $tableparam));
129 break;
130 case 'drop_table':
131 $o.= s($this->drop_table_php($structure, $tableparam));
132 break;
133 case 'rename_table':
134 $o.= s($this->rename_table_php($structure, $tableparam));
135 break;
136 }
137 $o.= '</textarea></td></tr>';
138 $o.= ' </table>';
139
140 $this->output = $o;
141
142 /// Launch postaction if exists (leave this here!)
eef868d1 143 if ($this->getPostAction() && $result) {
b0c7d558 144 return $this->launch($this->getPostAction());
145 }
146
147 /// Return ok if arrived here
148 return $result;
149 }
150
151 /**
eef868d1 152 * This function will generate all the PHP code needed to
b0c7d558 153 * create one table using XMLDB objects and functions
eef868d1 154 *
46293bd7 155 * @param xmldb_structure structure object containing all the info
b0c7d558 156 * @param string table table code to be created
157 * @return string PHP code to be used to create the table
158 */
159 function create_table_php($structure, $table) {
160
161 $result = '';
162 /// Validate if we can do it
3ab430fb 163 if (!$table = $structure->getTable($table)) {
164 return false;
165 }
166 if ($table->getAllErrors()) {
167 return false;
168 }
b0c7d558 169
170 /// Add the standard PHP header
171 $result .= XMLDB_PHP_HEADER;
172
173 /// Add contents
3ab430fb 174 $result .= XMLDB_LINEFEED;
2a973285 175 $result .= ' // Define table ' . $table->getName() . ' to be created' . XMLDB_LINEFEED;
a8cb94f6 176 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3ab430fb 177 $result .= XMLDB_LINEFEED;
2a973285 178 $result .= ' // Adding fields to table ' . $table->getName() . XMLDB_LINEFEED;
3ab430fb 179 /// Iterate over each field
180 foreach ($table->getFields() as $field) {
181 /// The field header, with name
5eaa41e8 182 $result .= ' $table->add_field(' . "'" . $field->getName() . "', ";
3ab430fb 183 /// The field PHP specs
184 $result .= $field->getPHP(false);
185 /// The end of the line
186 $result .= ');' . XMLDB_LINEFEED;
187 }
188 /// Iterate over each key
189 if ($keys = $table->getKeys()) {
190 $result .= XMLDB_LINEFEED;
2a973285 191 $result .= ' // Adding keys to table ' . $table->getName() . XMLDB_LINEFEED;
3ab430fb 192 foreach ($keys as $key) {
193 /// The key header, with name
5eaa41e8 194 $result .= ' $table->add_key(' . "'" . $key->getName() . "', ";
3ab430fb 195 /// The key PHP specs
196 $result .= $key->getPHP();
197 /// The end of the line
198 $result .= ');' . XMLDB_LINEFEED;
199 }
200 }
201 /// Iterate over each index
202 if ($indexes = $table->getIndexes()) {
203 $result .= XMLDB_LINEFEED;
2a973285 204 $result .= ' // Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED;
3ab430fb 205 foreach ($indexes as $index) {
206 /// The index header, with name
5eaa41e8 207 $result .= ' $table->add_index(' . "'" . $index->getName() . "', ";
3ab430fb 208 /// The index PHP specs
209 $result .= $index->getPHP();
210 /// The end of the line
211 $result .= ');' . XMLDB_LINEFEED;
212 }
213 }
214
215 /// Launch the proper DDL
216 $result .= XMLDB_LINEFEED;
2a973285 217 $result .= ' // Conditionally launch create table for ' . $table->getName() . XMLDB_LINEFEED;
63dd5a62 218 $result .= ' if (!$dbman->table_exists($table)) {' . XMLDB_LINEFEED;
ef088e70 219 $result .= ' $dbman->create_table($table);' . XMLDB_LINEFEED;
70080fde 220 $result .= ' }' . XMLDB_LINEFEED;
b0c7d558 221
dcbef199 222 /// Add the proper upgrade_xxxx_savepoint call
223 $result .= $this->upgrade_savepoint_php ($structure);
224
b0c7d558 225 /// Add standard PHP footer
226 $result .= XMLDB_PHP_FOOTER;
227
228 return $result;
229 }
230
231 /**
eef868d1 232 * This function will generate all the PHP code needed to
b0c7d558 233 * drop one table using XMLDB objects and functions
eef868d1 234 *
46293bd7 235 * @param xmldb_structure structure object containing all the info
b0c7d558 236 * @param string table table code to be dropped
237 * @return string PHP code to be used to drop the table
238 */
239 function drop_table_php($structure, $table) {
240
241 $result = '';
242 /// Validate if we can do it
3ab430fb 243 if (!$table = $structure->getTable($table)) {
244 return false;
245 }
246 if ($table->getAllErrors()) {
247 return false;
248 }
b0c7d558 249
250 /// Add the standard PHP header
251 $result .= XMLDB_PHP_HEADER;
252
253 /// Add contents
386c23c9 254 $result .= XMLDB_LINEFEED;
2a973285 255 $result .= ' // Define table ' . $table->getName() . ' to be dropped' . XMLDB_LINEFEED;
a8cb94f6 256 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
386c23c9 257
258 /// Launch the proper DDL
259 $result .= XMLDB_LINEFEED;
2a973285 260 $result .= ' // Conditionally launch drop table for ' . $table->getName() . XMLDB_LINEFEED;
63dd5a62 261 $result .= ' if ($dbman->table_exists($table)) {' . XMLDB_LINEFEED;
ef088e70 262 $result .= ' $dbman->drop_table($table);' . XMLDB_LINEFEED;
70080fde 263 $result .= ' }' . XMLDB_LINEFEED;
b0c7d558 264
dcbef199 265 /// Add the proper upgrade_xxxx_savepoint call
266 $result .= $this->upgrade_savepoint_php ($structure);
267
b0c7d558 268 /// Add standard PHP footer
269 $result .= XMLDB_PHP_FOOTER;
270
271 return $result;
272 }
273
274 /**
eef868d1 275 * This function will generate all the PHP code needed to
b0c7d558 276 * rename one table using XMLDB objects and functions
eef868d1 277 *
46293bd7 278 * @param xmldb_structure structure object containing all the info
b0c7d558 279 * @param string table table code to be renamed
280 * @return string PHP code to be used to rename the table
281 */
282 function rename_table_php($structure, $table) {
283
284 $result = '';
285 /// Validate if we can do it
3ab430fb 286 if (!$table = $structure->getTable($table)) {
287 return false;
288 }
289 if ($table->getAllErrors()) {
290 return false;
291 }
b0c7d558 292
293 /// Add the standard PHP header
294 $result .= XMLDB_PHP_HEADER;
295
296 /// Add contents
386c23c9 297 $result .= XMLDB_LINEFEED;
2a973285 298 $result .= ' // Define table ' . $table->getName() . ' to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
a8cb94f6 299 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
386c23c9 300
301 /// Launch the proper DDL
302 $result .= XMLDB_LINEFEED;
2a973285 303 $result .= ' // Launch rename table for ' . $table->getName() . XMLDB_LINEFEED;
ef088e70 304 $result .= ' $dbman->rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED;
b0c7d558 305
dcbef199 306 /// Add the proper upgrade_xxxx_savepoint call
307 $result .= $this->upgrade_savepoint_php ($structure);
308
b0c7d558 309 /// Add standard PHP footer
310 $result .= XMLDB_PHP_FOOTER;
311
312 return $result;
313 }
dcbef199 314
b0c7d558 315}
0df0df23 316