Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / xmldb / actions / new_table_from_mysql / new_table_from_mysql.class.php
blob4155403f861eabd3dea2e05337d9862eae76c852
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
27 /// This class will ask and retrofit all the information from one
28 /// mysql table present in the Moodle DB to one XMLDBTable structure
30 class new_table_from_mysql extends XMLDBAction {
32 /**
33 * Init method, every subclass will have its own
35 function init() {
36 parent::init();
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'createtable' => 'xmldb',
43 'aftertable' => 'xmldb',
44 'create' => 'xmldb',
45 'back' => 'xmldb'
46 ));
49 /**
50 * Invoke method, every class will have its own
51 * returns true/false on completion, setting both
52 * errormsg and output as necessary
54 function invoke() {
55 parent::invoke();
57 $result = true;
59 /// Set own core attributes
60 $this->does_generate = ACTION_GENERATE_HTML;
62 /// These are always here
63 global $CFG, $XMLDB, $db;
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);
70 /// Get the correct dirs
71 if (!empty($XMLDB->dbdirs)) {
72 $dbdir =& $XMLDB->dbdirs[$dirpath];
73 } else {
74 return false;
76 if (!empty($XMLDB->editeddirs)) {
77 $editeddir =& $XMLDB->editeddirs[$dirpath];
78 $structure =& $editeddir->xml_file->getStructure();
80 /// ADD YOUR CODE HERE
81 $tableparam = optional_param('table', NULL, PARAM_CLEAN);
83 /// If no table, show form
84 if (!$tableparam) {
85 /// No postaction here
86 $this->postaction = NULL;
87 /// Get list of tables
88 $dbtables = $db->MetaTables('TABLES');
89 $selecttables = array();
90 foreach ($dbtables as $dbtable) {
91 $dbtable = strtolower(str_replace($CFG->prefix, '', $dbtable));
92 $i = $structure->findTableInArray($dbtable);
93 if ($i === NULL) {
94 $selecttables[$dbtable] = $dbtable;
97 /// Get list of after tables
98 $aftertables = array();
99 if ($tables =& $structure->getTables()) {
100 foreach ($tables as $aftertable) {
101 $aftertables[$aftertable->getName()] = $aftertable->getName();
104 if (!$selecttables) {
105 $this->errormsg = 'No tables available to be retrofitted';
106 return false;
108 /// Now build the form
109 $o = '<form id="form" action="index.php" method="post">';
110 $o .= '<div>';
111 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
112 $o.= ' <input type="hidden" name ="action" value="new_table_from_mysql" />';
113 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />';
114 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
115 $o.= ' <tr><td><label for="table" accesskey="t">' . $this->str['createtable'] .' </label>' . choose_from_menu($selecttables, 'table', '', 'choose', '', 0, true) . '<label for="after" accesskey="a">' . $this->str['aftertable'] . ' </label>' .choose_from_menu($aftertables, 'after', '', 'choose', '', 0, true) . '</td></tr>';
116 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['create'] . '" /></td></tr>';
117 $o.= ' <tr><td colspan="2" align="center"><a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a></td></tr>';
118 $o.= ' </table>';
119 $o.= '</div></form>';
121 $this->output = $o;
124 /// If table, retrofit information and, if everything works,
125 /// go to the table edit action
126 } else {
127 /// Get some params (table is mandatory here)
128 $tableparam = required_param('table', PARAM_CLEAN);
129 $afterparam = required_param('after', PARAM_CLEAN);
131 /// Create one new XMLDBTable
132 $table = new XMLDBTable(strtolower(trim($tableparam)));
133 $table->setComment($table->getName() . ' table retrofitted from MySQL');
134 /// Get fields info from ADODb
135 if(!$dbfields = $db->MetaColumns($CFG->prefix . $tableparam)) {
136 ///Try it without prefix if doesn't exist
137 $dbfields = $db->MetaColumns($tableparam);
139 if ($dbfields) {
140 foreach ($dbfields as $dbfield) {
141 /// Create new XMLDB field
142 $field = new XMLDBField(strtolower($dbfield->name));
143 /// Set field with info retrofitted
144 $field->setFromADOField($dbfield);
145 /// Add field to the table
146 $table->addField($field);
149 /// Get PK, UK and indexes info from ADODb
150 $dbindexes = $db->MetaIndexes($CFG->prefix . $tableparam, true);
151 if ($dbindexes) {
152 $lastkey = NULL; //To temp store the last key processed
153 foreach ($dbindexes as $indexname => $dbindex) {
154 /// Add the indexname to the array
155 $dbindex['name'] = $indexname;
156 /// We are handling one XMLDBKey (primaries + uniques)
157 if ($dbindex['unique']) {
158 $key = new XMLDBKey(strtolower($dbindex['name']));
159 /// Set key with info retrofitted
160 $key->setFromADOKey($dbindex);
161 /// Set default comment to PKs
162 if ($key->getType() == XMLDB_KEY_PRIMARY) {
163 $key->setComment('Primary key for ' . $table->getName());
165 /// Add key to the table
166 $table->addKey($key);
168 /// We are handling one XMLDBIndex (non-uniques)
169 } else {
170 $index = new XMLDBIndex(strtolower($dbindex['name']));
171 /// Set index with info retrofitted
172 $index->setFromADOIndex($dbindex);
173 /// Add index to the table
174 $table->addIndex($index);
178 /// Finally, add the whole retroffited table to the structure
179 /// in the place specified
180 $structure->addTable($table, $afterparam);
183 /// Launch postaction if exists (leave this here!)
184 if ($this->getPostAction() && $result) {
185 return $this->launch($this->getPostAction());
188 /// Return ok if arrived here
189 return $result;