1<?php 2// This file is part of Moodle - http://moodle.org/ 3// 4// Moodle is free software: you can redistribute it and/or modify 5// it under the terms of the GNU General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// Moodle is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU General Public License for more details. 13// 14// You should have received a copy of the GNU General Public License 15// along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17/** 18 * @package tool_xmldb 19 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 21 */ 22 23/** 24 * This class will provide the interface for all the edit field actions 25 * 26 * @package tool_xmldb 27 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30class edit_field 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 $this->sesskey_protected = false; // This action doesn't need sesskey protection 40 41 // Get needed strings 42 $this->loadStrings(array( 43 'change' => 'tool_xmldb', 44 'float2numbernote' => 'tool_xmldb', 45 'vieworiginal' => 'tool_xmldb', 46 'viewedited' => 'tool_xmldb', 47 'yes' => '', 48 'no' => '', 49 'back' => 'tool_xmldb' 50 )); 51 } 52 53 /** 54 * Invoke method, every class will have its own 55 * returns true/false on completion, setting both 56 * errormsg and output as necessary 57 */ 58 function invoke() { 59 parent::invoke(); 60 61 $result = true; 62 63 // Set own core attributes 64 $this->does_generate = ACTION_GENERATE_HTML; 65 66 // These are always here 67 global $CFG, $XMLDB, $OUTPUT; 68 69 // Do the job, setting result as needed 70 // Get the dir containing the file 71 $dirpath = required_param('dir', PARAM_PATH); 72 $dirpath = $CFG->dirroot . $dirpath; 73 74 // Get the correct dirs 75 if (!empty($XMLDB->dbdirs)) { 76 $dbdir = $XMLDB->dbdirs[$dirpath]; 77 } else { 78 return false; 79 } 80 if (!empty($XMLDB->editeddirs)) { 81 $editeddir = $XMLDB->editeddirs[$dirpath]; 82 $structure = $editeddir->xml_file->getStructure(); 83 } 84 85 // Fetch request data 86 $tableparam = required_param('table', PARAM_CLEAN); 87 if (!$table = $structure->getTable($tableparam)) { 88 $this->errormsg = 'Wrong table specified: ' . $tableparam; 89 return false; 90 } 91 $fieldparam = required_param('field', PARAM_CLEAN); 92 if (!$field = $table->getField($fieldparam)) { 93 // Arriving here from a name change, looking for the new field name 94 $fieldparam = required_param('name', PARAM_CLEAN); 95 $field = $table->getField($fieldparam); 96 } 97 98 $dbdir = $XMLDB->dbdirs[$dirpath]; 99 $origstructure = $dbdir->xml_file->getStructure(); 100 101 $o = ''; // Output starts 102 103 // If field is XMLDB_TYPE_FLOAT, comment about to migrate it to XMLDB_TYPE_NUMBER 104 if ($field->getType() == XMLDB_TYPE_FLOAT) { 105 $o .= '<p>' . $this->str['float2numbernote'] . '</p>'; 106 } 107 108 // Add the main 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 ="table" value="' . $tableparam .'" />'; 113 $o.= ' <input type="hidden" name ="field" value="' . $fieldparam .'" />'; 114 $o.= ' <input type="hidden" name ="sesskey" value="' . sesskey() .'" />'; 115 $o.= ' <input type="hidden" name ="action" value="edit_field_save" />'; 116 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />'; 117 $o.= ' <table id="formelements" class="boxaligncenter">'; 118 // XMLDB field name 119 // If the field has dependencies, we cannot change its name 120 $disabled = ''; 121 if ($structure->getFieldUses($table->getName(), $field->getName())) { 122 $o.= ' <input type="hidden" name ="name" value="' . s($field->getName()) .'" />'; 123 $o.= ' <tr valign="top"><td>Name:</td><td colspan="2">' . s($field->getName()) . '</td></tr>'; 124 } else { 125 $o.= ' <tr valign="top"><td><label for="name" accesskey="n">Name:</label></td><td colspan="2"><input name="name" type="text" size="'.xmldb_field::NAME_MAX_LENGTH.'" maxlength="'.xmldb_field::NAME_MAX_LENGTH.'" id="name" value="' . s($field->getName()) . '" /></td></tr>'; 126 } 127 // XMLDB field comment 128 $o .= ' <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td colspan="2"> 129 <textarea name="comment" rows="3" cols="80" id="comment" class="form-control">' . 130 s($field->getComment()) . '</textarea></td></tr>'; 131 // xmldb_field Type 132 $typeoptions = array (XMLDB_TYPE_INTEGER => $field->getXMLDBTypeName(XMLDB_TYPE_INTEGER), 133 XMLDB_TYPE_NUMBER => $field->getXMLDBTypeName(XMLDB_TYPE_NUMBER), 134 XMLDB_TYPE_FLOAT => $field->getXMLDBTypeName(XMLDB_TYPE_FLOAT), 135 XMLDB_TYPE_DATETIME=> $field->getXMLDBTypeName(XMLDB_TYPE_DATETIME), 136 XMLDB_TYPE_CHAR => $field->getXMLDBTypeName(XMLDB_TYPE_CHAR), 137 XMLDB_TYPE_TEXT => $field->getXMLDBTypeName(XMLDB_TYPE_TEXT), 138 XMLDB_TYPE_BINARY => $field->getXMLDBTypeName(XMLDB_TYPE_BINARY)); 139 // If current field isn't float, delete such column type to avoid its creation from the interface 140 // Note that float fields are supported completely but it's possible than in a next future 141 // we delete them completely from Moodle DB, using, exclusively, number(x,y) types 142 if ($field->getType() != XMLDB_TYPE_FLOAT) { 143 unset ($typeoptions[XMLDB_TYPE_FLOAT]); 144 } 145 // Also we hide datetimes. Only edition of them is allowed (and retrofit) but not new creation 146 if ($field->getType() != XMLDB_TYPE_DATETIME) { 147 unset ($typeoptions[XMLDB_TYPE_DATETIME]); 148 } 149 $select = html_writer::select($typeoptions, 'type', $field->getType(), false); 150 $o.= ' <tr valign="top"><td><label for="menutype" accesskey="t">Type:</label></td>'; 151 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 152 // xmldb_field Length 153 $o.= ' <tr valign="top"><td><label for="length" accesskey="l">Length:</label></td>'; 154 $o.= ' <td colspan="2"><input name="length" type="text" size="6" maxlength="6" id="length" value="' . s($field->getLength()) . '" /><span id="lengthtip"></span></td></tr>'; 155 // xmldb_field Decimals 156 $o.= ' <tr valign="top"><td><label for="decimals" accesskey="d">Decimals:</label></td>'; 157 $o.= ' <td colspan="2"><input name="decimals" type="text" size="6" maxlength="6" id="decimals" value="' . s($field->getDecimals()) . '" /><span id="decimalstip"></span></td></tr>'; 158 // xmldb_field NotNull 159 $notnulloptions = array (0 => 'null', 'not null'); 160 $select = html_writer::select($notnulloptions, 'notnull', $field->getNotNull(), false); 161 $o.= ' <tr valign="top"><td><label for="menunotnull" accesskey="n">Not Null:</label></td>'; 162 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 163 // xmldb_field Sequence 164 $sequenceoptions = array (0 => $this->str['no'], 1 => 'auto-numbered'); 165 $select = html_writer::select($sequenceoptions, 'sequence', $field->getSequence(), false); 166 $o.= ' <tr valign="top"><td><label for="menusequence" accesskey="s">Sequence:</label></td>'; 167 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 168 // xmldb_field Default 169 $o.= ' <tr valign="top"><td><label for="default" accesskey="d">Default:</label></td>'; 170 $o.= ' <td colspan="2"><input type="text" name="default" size="30" maxlength="80" id="default" value="' . s($field->getDefault()) . '" /></td></tr>'; 171 // Change button 172 $o .= ' <tr valign="top"><td> </td><td colspan="2"><input type="submit" value="' . $this->str['change'] . 173 '" class="btn btn-secondary" /></td></tr>'; 174 $o.= ' </table>'; 175 $o.= '</div></form>'; 176 // Calculate the buttons 177 $b = ' <p class="centerpara buttons">'; 178 // The view original XML button 179 if ($table->getField($fieldparam)) { 180 $b .= ' <a href="index.php?action=view_field_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&select=original&table=' . $tableparam . '&field=' . $fieldparam . '">[' . $this->str['vieworiginal'] . ']</a>'; 181 } else { 182 $b .= ' [' . $this->str['vieworiginal'] . ']'; 183 } 184 // The view edited XML button 185 if ($field->hasChanged()) { 186 $b .= ' <a href="index.php?action=view_field_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&select=edited&table=' . $tableparam . '&field=' . $fieldparam . '">[' . $this->str['viewedited'] . ']</a>'; 187 } else { 188 $b .= ' [' . $this->str['viewedited'] . ']'; 189 } 190 // The back to edit table button 191 $b .= ' <a href="index.php?action=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; 192 $b .= '</p>'; 193 $o .= $b; 194 195 $this->output = $o; 196 197 // Launch postaction if exists (leave this here!) 198 if ($this->getPostAction() && $result) { 199 return $this->launch($this->getPostAction()); 200 } 201 202 // Return ok if arrived here 203 return $result; 204 } 205} 206 207