|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00036 class edit_table extends XMLDBAction { 00037 00041 function init() { 00042 parent::init(); 00043 00044 // Set own custom attributes 00045 $this->sesskey_protected = false; // This action doesn't need sesskey protection 00046 00047 // Get needed strings 00048 $this->loadStrings(array( 00049 'change' => 'tool_xmldb', 00050 'vieworiginal' => 'tool_xmldb', 00051 'viewedited' => 'tool_xmldb', 00052 'viewsqlcode' => 'tool_xmldb', 00053 'viewphpcode' => 'tool_xmldb', 00054 'newfield' => 'tool_xmldb', 00055 'newkey' => 'tool_xmldb', 00056 'newindex' => 'tool_xmldb', 00057 'fields' => 'tool_xmldb', 00058 'keys' => 'tool_xmldb', 00059 'indexes' => 'tool_xmldb', 00060 'edit' => 'tool_xmldb', 00061 'up' => 'tool_xmldb', 00062 'down' => 'tool_xmldb', 00063 'delete' => 'tool_xmldb', 00064 'reserved' => 'tool_xmldb', 00065 'back' => 'tool_xmldb', 00066 'viewxml' => 'tool_xmldb', 00067 'pendingchanges' => 'tool_xmldb', 00068 'pendingchangescannotbesaved' => 'tool_xmldb', 00069 'save' => 'tool_xmldb' 00070 )); 00071 } 00072 00078 function invoke() { 00079 parent::invoke(); 00080 00081 $result = true; 00082 00083 // Set own core attributes 00084 $this->does_generate = ACTION_GENERATE_HTML; 00085 00086 // These are always here 00087 global $CFG, $XMLDB; 00088 00089 // Do the job, setting result as needed 00090 // Get the dir containing the file 00091 $dirpath = required_param('dir', PARAM_PATH); 00092 $dirpath = $CFG->dirroot . $dirpath; 00093 00094 // Get the correct dirs 00095 if (!empty($XMLDB->dbdirs)) { 00096 $dbdir =& $XMLDB->dbdirs[$dirpath]; 00097 } else { 00098 return false; 00099 } 00100 // Check if the dir exists and copy it from dbdirs 00101 // (because we need straight load in case of saving from here) 00102 if (!isset($XMLDB->editeddirs[$dirpath])) { 00103 $XMLDB->editeddirs[$dirpath] = unserialize(serialize($dbdir)); 00104 } 00105 00106 if (!empty($XMLDB->editeddirs)) { 00107 $editeddir =& $XMLDB->editeddirs[$dirpath]; 00108 $structure =& $editeddir->xml_file->getStructure(); 00109 } 00110 00111 $tableparam = required_param('table', PARAM_CLEAN); 00112 if (!$table =& $structure->getTable($tableparam)) { 00113 // Arriving here from a name change, looking for the new table name 00114 $tableparam = required_param('name', PARAM_CLEAN); 00115 $table =& $structure->getTable($tableparam); 00116 } 00117 00118 $dbdir =& $XMLDB->dbdirs[$dirpath]; 00119 $origstructure =& $dbdir->xml_file->getStructure(); 00120 00121 // Add the main form 00122 $o = '<form id="form" action="index.php" method="post">'; 00123 $o.= '<div>'; 00124 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />'; 00125 $o.= ' <input type="hidden" name ="table" value="' . $tableparam .'" />'; 00126 $o.= ' <input type="hidden" name ="action" value="edit_table_save" />'; 00127 $o.= ' <input type="hidden" name ="sesskey" value="' . sesskey() .'" />'; 00128 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />'; 00129 $o.= ' <table id="formelements" class="boxaligncenter">'; 00130 // If the table is being used, we cannot rename it 00131 if ($structure->getTableUses($table->getName())) { 00132 $o.= ' <tr valign="top"><td>Name:</td><td><input type="hidden" name ="name" value="' . s($table->getName()) . '" />' . s($table->getName()) .'</td></tr>'; 00133 } else { 00134 $o.= ' <tr valign="top"><td><label for="name" accesskey="p">Name:</label></td><td><input name="name" type="text" size="28" maxlength="28" id="name" value="' . s($table->getName()) . '" /></td></tr>'; 00135 } 00136 $o.= ' <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td><textarea name="comment" rows="3" cols="80" id="comment">' . s($table->getComment()) . '</textarea></td></tr>'; 00137 $o.= ' <tr valign="top"><td> </td><td><input type="submit" value="' .$this->str['change'] . '" /></td></tr>'; 00138 $o.= ' </table>'; 00139 $o.= '</div></form>'; 00140 // Calculate the pending changes / save message 00141 $e = ''; 00142 $cansavenow = false; 00143 if ($structure->hasChanged()) { 00144 if (!is_writeable($dirpath . '/install.xml') || !is_writeable($dirpath)) { 00145 $e .= '<p class="centerpara error">' . $this->str['pendingchangescannotbesaved'] . '</p>'; 00146 } else { 00147 $e .= '<p class="centerpara warning">' . $this->str['pendingchanges'] . '</p>'; 00148 $cansavenow = true; 00149 } 00150 } 00151 // Calculate the buttons 00152 $b = ' <p class="centerpara buttons">'; 00153 // The view original XML button 00154 if ($origstructure->getTable($tableparam)) { 00155 $b .= ' <a href="index.php?action=view_table_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&select=original&table=' . $tableparam . '">[' . $this->str['vieworiginal'] . ']</a>'; 00156 } else { 00157 $b .= ' [' . $this->str['vieworiginal'] . ']'; 00158 } 00159 // The view edited XML button 00160 if ($table->hasChanged()) { 00161 $b .= ' <a href="index.php?action=view_table_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&select=edited&table=' . $tableparam . '">[' . $this->str['viewedited'] . ']</a>'; 00162 } else { 00163 $b .= ' [' . $this->str['viewedited'] . ']'; 00164 } 00165 // The new field button 00166 $b .= ' <a href="index.php?action=new_field&sesskey=' . sesskey() . '&postaction=edit_field&table=' . $tableparam . '&field=changeme&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newfield'] . ']</a>'; 00167 // The new key button 00168 $b .= ' <a href="index.php?action=new_key&sesskey=' . sesskey() . '&postaction=edit_key&table=' . $tableparam . '&key=changeme&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newkey'] . ']</a>'; 00169 // The new index button 00170 $b .= ' <a href="index.php?action=new_index&sesskey=' . sesskey() . '&postaction=edit_index&table=' . $tableparam . '&index=changeme&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newindex'] . ']</a>'; 00171 $b .= '</p>'; 00172 00173 $b .= ' <p class="centerpara buttons">'; 00174 // The view sql code button 00175 $b .= '<a href="index.php?action=view_table_sql&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' .$this->str['viewsqlcode'] . ']</a>'; 00176 // The view php code button 00177 $b .= ' <a href="index.php?action=view_table_php&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['viewphpcode'] . ']</a>'; 00178 // The save button (if possible) 00179 if ($cansavenow) { 00180 $b .= ' <a href="index.php?action=save_xml_file&sesskey=' . sesskey() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&time=' . time() . '&unload=false&postaction=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['save'] . ']</a>'; 00181 } 00182 // The back to edit xml file button 00183 $b .= ' <a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; 00184 $b .= '</p>'; 00185 $o .= $e . $b; 00186 00187 require_once("$CFG->libdir/ddl/sql_generator.php"); 00188 $reserved_words = sql_generator::getAllReservedWords(); 00189 00190 // Delete any 'changeme' field/key/index 00191 $table->deleteField('changeme'); 00192 $table->deleteKey('changeme'); 00193 $table->deleteIndex('changeme'); 00194 00195 // Add the fields list 00196 $fields =& $table->getFields(); 00197 if (!empty($fields)) { 00198 $o .= '<h3 class="main">' . $this->str['fields'] . '</h3>'; 00199 $o .= '<table id="listfields" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">'; 00200 $row = 0; 00201 foreach ($fields as $field) { 00202 // The field name (link to edit - if the field has no uses) 00203 if (!$structure->getFieldUses($table->getName(), $field->getName())) { 00204 $f = '<a href="index.php?action=edit_field&field=' .$field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $field->getName() . '</a>'; 00205 } else { 00206 $f = $field->getName(); 00207 } 00208 // Calculate buttons 00209 $b = '</td><td class="button cell">'; 00210 // The edit button (if the field has no uses) 00211 if (!$structure->getFieldUses($table->getName(), $field->getName())) { 00212 $b .= '<a href="index.php?action=edit_field&field=' .$field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>'; 00213 } else { 00214 $b .= '[' . $this->str['edit'] . ']'; 00215 } 00216 $b .= '</td><td class="button cell">'; 00217 // The up button 00218 if ($field->getPrevious()) { 00219 $b .= '<a href="index.php?action=move_updown_field&direction=up&sesskey=' . sesskey() . '&field=' . $field->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>'; 00220 } else { 00221 $b .= '[' . $this->str['up'] . ']'; 00222 } 00223 $b .= '</td><td class="button cell">'; 00224 // The down button 00225 if ($field->getNext()) { 00226 $b .= '<a href="index.php?action=move_updown_field&direction=down&sesskey=' . sesskey() . '&field=' . $field->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>'; 00227 } else { 00228 $b .= '[' . $this->str['down'] . ']'; 00229 } 00230 $b .= '</td><td class="button cell">'; 00231 // The delete button (if we have more than one and it isn't used 00232 if (count($fields) > 1 && 00233 !$structure->getFieldUses($table->getName(), $field->getName())) { 00234 $b .= '<a href="index.php?action=delete_field&sesskey=' . sesskey() . '&field=' . $field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>'; 00235 } else { 00236 $b .= '[' . $this->str['delete'] . ']'; 00237 } 00238 $b .= '</td><td class="button cell">'; 00239 // The view xml button 00240 $b .= '<a href="index.php?action=view_field_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&field=' . $field->getName() . '&table=' . $table->getName() . '&select=edited">[' . $this->str['viewxml'] . ']</a>'; 00241 // Detect if the table name is a reserved word 00242 if (array_key_exists($field->getName(), $reserved_words)) { 00243 $b .= ' <a href="index.php?action=view_reserved_words"><span class="error">' . $this->str['reserved'] . '</span></a>'; 00244 } 00245 // The readable info 00246 $r = '</td><td class="readableinfo cell">' . $field->readableInfo() . '</td>'; 00247 // Print table row 00248 $o .= '<tr class="r' . $row . '"><td class="table cell">' . $f . $b . $r . '</tr>'; 00249 $row = ($row + 1) % 2; 00250 } 00251 $o .= '</table>'; 00252 } 00253 // Add the keys list 00254 $keys =& $table->getKeys(); 00255 if (!empty($keys)) { 00256 $o .= '<h3 class="main">' . $this->str['keys'] . '</h3>'; 00257 $o .= '<table id="listkeys" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">'; 00258 $row = 0; 00259 foreach ($keys as $key) { 00260 // The key name (link to edit - if the key has no uses) 00261 if (!$structure->getKeyUses($table->getName(), $key->getName())) { 00262 $k = '<a href="index.php?action=edit_key&key=' .$key->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $key->getName() . '</a>'; 00263 } else { 00264 $k = $key->getName(); 00265 } 00266 // Calculate buttons 00267 $b = '</td><td class="button cell">'; 00268 // The edit button (if the key hasn't uses) 00269 if (!$structure->getKeyUses($table->getName(), $key->getName())) { 00270 $b .= '<a href="index.php?action=edit_key&key=' .$key->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>'; 00271 } else { 00272 $b .= '[' . $this->str['edit'] . ']'; 00273 } 00274 $b .= '</td><td class="button cell">'; 00275 // The up button 00276 if ($key->getPrevious()) { 00277 $b .= '<a href="index.php?action=move_updown_key&direction=up&sesskey=' . sesskey() . '&key=' . $key->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>'; 00278 } else { 00279 $b .= '[' . $this->str['up'] . ']'; 00280 } 00281 $b .= '</td><td class="button cell">'; 00282 // The down button 00283 if ($key->getNext()) { 00284 $b .= '<a href="index.php?action=move_updown_key&direction=down&sesskey=' . sesskey() . '&key=' . $key->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>'; 00285 } else { 00286 $b .= '[' . $this->str['down'] . ']'; 00287 } 00288 $b .= '</td><td class="button cell">'; 00289 // The delete button (if the key hasn't uses) 00290 if (!$structure->getKeyUses($table->getName(), $key->getName())) { 00291 $b .= '<a href="index.php?action=delete_key&sesskey=' . sesskey() . '&key=' . $key->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>'; 00292 } else { 00293 $b .= '[' . $this->str['delete'] . ']'; 00294 } 00295 $b .= '</td><td class="button cell">'; 00296 // The view xml button 00297 $b .= '<a href="index.php?action=view_key_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&key=' . $key->getName() . '&table=' . $table->getName() . '&select=edited">[' . $this->str['viewxml'] . ']</a>'; 00298 // The readable info 00299 $r = '</td><td class="readableinfo cell">' . $key->readableInfo() . '</td>'; 00300 // Print table row 00301 $o .= '<tr class="r' . $row . '"><td class="table cell">' . $k . $b . $r .'</tr>'; 00302 $row = ($row + 1) % 2; 00303 } 00304 $o .= '</table>'; 00305 } 00306 // Add the indexes list 00307 $indexes =& $table->getIndexes(); 00308 if (!empty($indexes)) { 00309 $o .= '<h3 class="main">' . $this->str['indexes'] . '</h3>'; 00310 $o .= '<table id="listindexes" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">'; 00311 $row = 0; 00312 foreach ($indexes as $index) { 00313 // The index name (link to edit) 00314 $i = '<a href="index.php?action=edit_index&index=' .$index->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">' . $index->getName() . '</a>'; 00315 // Calculate buttons 00316 $b = '</td><td class="button cell">'; 00317 // The edit button 00318 $b .= '<a href="index.php?action=edit_index&index=' .$index->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>'; 00319 $b .= '</td><td class="button cell">'; 00320 // The up button 00321 if ($index->getPrevious()) { 00322 $b .= '<a href="index.php?action=move_updown_index&direction=up&sesskey=' . sesskey() . '&index=' . $index->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['up'] . ']</a>'; 00323 } else { 00324 $b .= '[' . $this->str['up'] . ']'; 00325 } 00326 $b .= '</td><td class="button cell">'; 00327 // The down button 00328 if ($index->getNext()) { 00329 $b .= '<a href="index.php?action=move_updown_index&direction=down&sesskey=' . sesskey() . '&index=' . $index->getName() . '&table=' . $table->getName() . '&postaction=edit_table' . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['down'] . ']</a>'; 00330 } else { 00331 $b .= '[' . $this->str['down'] . ']'; 00332 } 00333 $b .= '</td><td class="button cell">'; 00334 // The delete button 00335 $b .= '<a href="index.php?action=delete_index&sesskey=' . sesskey() . '&index=' . $index->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>'; 00336 $b .= '</td><td class="button cell">'; 00337 // The view xml button 00338 $b .= '<a href="index.php?action=view_index_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&index=' . $index->getName() . '&table=' . $table->getName() . '&select=edited">[' . $this->str['viewxml'] . ']</a>'; 00339 // The readable info 00340 $r = '</td><td class="readableinfo cell">' . $index->readableInfo() . '</td>'; 00341 // Print table row 00342 $o .= '<tr class="r' . $row . '"><td class="table cell">' . $i . $b . $r .'</tr>'; 00343 $row = ($row + 1) % 2; 00344 } 00345 $o .= '</table>'; 00346 } 00347 00348 $this->output = $o; 00349 00350 // Launch postaction if exists (leave this here!) 00351 if ($this->getPostAction() && $result) { 00352 return $this->launch($this->getPostAction()); 00353 } 00354 00355 // Return ok if arrived here 00356 return $result; 00357 } 00358 } 00359