3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards 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 show the SQL generated for the selected RDBMS for
30 class view_table_sql
extends XMLDBAction
{
33 * Init method, every subclass will have its own
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectdb' => 'xmldb',
48 * Invoke method, every class will have its own
49 * returns true/false on completion, setting both
50 * errormsg and output as necessary
57 /// Set own core attributes
58 $this->does_generate
= ACTION_GENERATE_HTML
;
60 /// These are always here
63 /// Do the job, setting result as needed
64 /// Get the dir containing the file
65 $dirpath = required_param('dir', PARAM_PATH
);
66 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
68 /// Get the correct dirs
69 if (!empty($XMLDB->dbdirs
)) {
70 $dbdir =& $XMLDB->dbdirs
[$dirpath];
74 if (!empty($XMLDB->editeddirs
)) {
75 $editeddir =& $XMLDB->editeddirs
[$dirpath];
76 $structure =& $editeddir->xml_file
->getStructure();
78 /// ADD YOUR CODE HERE
81 $tableparam = required_param('table', PARAM_PATH
);
82 if (!$table =& $structure->getTable($tableparam)) {
83 $this->errormsg
= 'Wrong table specified: ' . $tableparm;
86 $generatorparam = optional_param('generator', null, PARAM_ALPHANUM
);
87 if (empty($generatorparam)) {
88 $generatorparam = $CFG->dbtype
;
91 /// Calculate list of available SQL generators
92 $plugins = get_list_of_plugins('lib/xmldb/classes/generators');
93 $generators = array();
94 foreach($plugins as $plugin) {
95 $generators[$plugin] = $plugin;
97 /// Check we have the selected generator
98 if (!in_array($generatorparam, $generators)) {
99 $generatorparam = reset($generators);
102 /// The back to edit table button
103 $b = ' <p class="centerpara buttons">';
104 $b .= '<a href="index.php?action=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['back'] . ']</a>';
108 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
109 $o.= ' <tr><td align="center">' . $this->str
['selectdb'];
111 /// Show the popup of generators
112 $url = 'index.php?action=view_table_sql&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&generator=';
113 $o.= popup_form($url, $generators, 'selectgenerator', $generatorparam, '', '', '' , true);
115 $o.= ' <tr><td><textarea cols="80" rows="32">';
116 /// Get an array of statements
117 if ($starr = $table->getCreateTableSQL($generatorparam, $CFG->prefix
)) {
119 foreach ($starr as $st) {
120 $sqltext .= s($st) . "\n\n";
122 $sqltext = trim($sqltext);
125 $o.= '</textarea></td></tr>';
130 /// Launch postaction if exists (leave this here!)
131 if ($this->getPostAction() && $result) {
132 return $this->launch($this->getPostAction());
135 /// Return ok if arrived here