[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/xmldb/actions/edit_field/ -> edit_field.class.php (source)

   1  <?php // $Id: edit_field.class.php,v 1.7 2007/10/10 05:25:25 nicolasconnault Exp $
   2  
   3  ///////////////////////////////////////////////////////////////////////////
   4  //                                                                       //
   5  // NOTICE OF COPYRIGHT                                                   //
   6  //                                                                       //
   7  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   8  //          http://moodle.com                                            //
   9  //                                                                       //
  10  // Copyright (C) 1999 onwards 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  ///////////////////////////////////////////////////////////////////////////
  26  
  27  /// This class will provide the interface for all the edit field actions
  28  
  29  class edit_field extends XMLDBAction {
  30  
  31      /**
  32       * Init method, every subclass will have its own
  33       */
  34      function init() {
  35          parent::init();
  36  
  37      /// Set own custom attributes
  38  
  39      /// Get needed strings
  40          $this->loadStrings(array(
  41              'change' => 'xmldb',
  42              'vieworiginal' => 'xmldb',
  43              'viewedited' => 'xmldb',
  44              'yes' => '',
  45              'no' => '',
  46              'back' => 'xmldb'
  47          ));
  48      }
  49  
  50      /**
  51       * Invoke method, every class will have its own
  52       * returns true/false on completion, setting both
  53       * errormsg and output as necessary
  54       */
  55      function invoke() {
  56          parent::invoke();
  57  
  58          $result = true;
  59  
  60      /// Set own core attributes
  61          $this->does_generate = ACTION_GENERATE_HTML;
  62  
  63      /// These are always here
  64          global $CFG, $XMLDB;
  65  
  66      /// Do the job, setting result as needed
  67      /// Get the dir containing the file
  68          $dirpath = required_param('dir', PARAM_PATH);
  69          $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  70  
  71      /// Get the correct dirs
  72          if (!empty($XMLDB->dbdirs)) {
  73              $dbdir =& $XMLDB->dbdirs[$dirpath];
  74          } else {
  75              return false;
  76          }
  77          if (!empty($XMLDB->editeddirs)) {
  78              $editeddir =& $XMLDB->editeddirs[$dirpath];
  79              $structure =& $editeddir->xml_file->getStructure();
  80          }
  81  
  82      /// ADD YOUR CODE HERE
  83  
  84      /// Fetch request data
  85          $tableparam = required_param('table', PARAM_CLEAN);
  86          if (!$table =& $structure->getTable($tableparam)) {
  87              $this->errormsg = 'Wrong table specified: ' . $tableparm;
  88              return false;
  89          }
  90          $fieldparam = required_param('field', PARAM_CLEAN);
  91          if (!$field =& $table->getField($fieldparam)) {
  92          /// Arriving here from a name change, looking for the new field name
  93              $fieldparam = required_param('name', PARAM_CLEAN);
  94              $field =& $table->getField($fieldparam);
  95          }
  96  
  97          $dbdir =& $XMLDB->dbdirs[$dirpath];
  98          $origstructure =& $dbdir->xml_file->getStructure();
  99  
 100      /// Add the main form
 101          $o = '<form id="form" action="index.php" method="post">';
 102          $o.= '    <div>';
 103          $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
 104          $o.= '    <input type="hidden" name ="table" value="' . $tableparam .'" />';
 105          $o.= '    <input type="hidden" name ="field" value="' . $fieldparam .'" />';
 106          $o.= '    <input type="hidden" name ="action" value="edit_field_save" />';
 107          $o.= '    <input type="hidden" name ="postaction" value="edit_table" />';
 108          $o.= '    <table id="formelements" class="boxaligncenter">';
 109      /// XMLDB field name
 110      /// If the field has dependencies, we cannot change its name
 111          $disabled = '';
 112          if ($structure->getFieldUses($table->getName(), $field->getName())) {
 113              $o.= '      <input type="hidden" name ="name" value="' .  s($field->getName()) .'" />';
 114              $o.= '      <tr valign="top"><td>Name:</td><td colspan="2">' . s($field->getName()) . '</td></tr>';
 115          } else {
 116              $o.= '      <tr valign="top"><td><label for="name" accesskey="n">Name:</label></td><td colspan="2"><input name="name" type="text" size="30" maxlength="30" id="name" value="' . s($field->getName()) . '" /></td></tr>';
 117          }
 118      /// XMLDB field comment
 119          $o.= '      <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td colspan="2"><textarea name="comment" rows="3" cols="80" id="comment">' . s($field->getComment()) . '</textarea></td></tr>';
 120      /// XMLDBField Type
 121          $typeoptions = array (XMLDB_TYPE_INTEGER => $field->getXMLDBTypeName(XMLDB_TYPE_INTEGER),
 122                                XMLDB_TYPE_NUMBER  => $field->getXMLDBTypeName(XMLDB_TYPE_NUMBER),
 123                                XMLDB_TYPE_FLOAT   => $field->getXMLDBTypeName(XMLDB_TYPE_FLOAT),
 124                                XMLDB_TYPE_DATETIME=> $field->getXMLDBTypeName(XMLDB_TYPE_DATETIME),
 125                                XMLDB_TYPE_CHAR    => $field->getXMLDBTypeName(XMLDB_TYPE_CHAR),
 126                                XMLDB_TYPE_TEXT    => $field->getXMLDBTypeName(XMLDB_TYPE_TEXT),
 127                                XMLDB_TYPE_BINARY  => $field->getXMLDBTypeName(XMLDB_TYPE_BINARY));
 128      /// If current field isnt float, delete such column type to avoid its creation from the interface
 129      /// Note that float fields are supported completely but it's possible than in a next future
 130      /// we delete them completely from Moodle DB, using, exlusively, number(x,y) types
 131          if ($field->getType() != XMLDB_TYPE_FLOAT) {
 132              unset ($typeoptions[XMLDB_TYPE_FLOAT]);
 133          }
 134      /// Also we hide datetimes. Only edition of them is allowed (and retrofit) but not new creation
 135          if ($field->getType() != XMLDB_TYPE_DATETIME) {
 136              unset ($typeoptions[XMLDB_TYPE_DATETIME]);
 137          }
 138          $o.= '      <tr valign="top"><td><label for="menutype" accesskey="t">Type:</label></td>';
 139          $o.= '        <td colspan="2">' . choose_from_menu($typeoptions, 'type', $field->getType(), '', '', '', true) . '</td></tr>';
 140      /// XMLDBField Length
 141          $o.= '      <tr valign="top"><td><label for="length" accesskey="l">Length:</label></td>';
 142          $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>';
 143      /// XMLDBField Decimals
 144          $o.= '      <tr valign="top"><td><label for="decimals" accesskey="d">Decimals:</label></td>';
 145          $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>';
 146      /// XMLDBField Unsigned
 147          $unsignedoptions = array (0 => 'signed', 1 => 'unsigned');
 148          $o.= '      <tr valign="top"><td><label for="menuunsigned" accesskey="u">Unsigned:</label></td>';
 149          $o.= '        <td colspan="2">' . choose_from_menu($unsignedoptions, 'unsigned', $field->getUnsigned(), '', '', '', true) . '</td></tr>';
 150      /// XMLDBField NotNull
 151          $notnulloptions = array (0 => 'null', 'not null');
 152          $o.= '      <tr valign="top"><td><label for="menunotnull" accesskey="n">Not Null:</label></td>';
 153          $o.= '        <td colspan="2">' . choose_from_menu($notnulloptions, 'notnull', $field->getNotNull(), '', '', '', true) . '</td></tr>';
 154      /// XMLDBField Sequence
 155          $sequenceoptions = array (0 => $this->str['no'], 1 => 'auto-numbered');
 156          $o.= '      <tr valign="top"><td><label for="menusequence" accesskey="s">Sequence:</label></td>';
 157          $o.= '        <td colspan="2">' . choose_from_menu($sequenceoptions, 'sequence', $field->getSequence(), '', '', '', true) . '</td></tr>';
 158      /// XMLDBField Enum and enumvalues
 159          $enumoptions = array (0 => $this->str['no'], 1 => $this->str['yes']);
 160          $o.= '      <tr valign="top"><td><label for="menuenum" accesskey="s">Enum:</label></td>';
 161          $o.= '        <td>' . choose_from_menu($enumoptions, 'enum', $field->getEnum(), '', '', '', true) . '</td>';
 162          if (is_array($field->getEnumValues())) {
 163              $enumvalues = implode(', ', $field->getEnumValues());
 164          } else {
 165              $enumvalues = '';
 166          }
 167          $o.= '            <td><textarea name="enumvalues" rows="3" cols="70" id="enumvalues">' . s($enumvalues) . '</textarea></td></tr>';
 168      /// XMLDBField 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>&nbsp;</td><td colspan="2"><input type="submit" value="' .$this->str['change'] . '" /></td></tr>';
 173          $o.= '    </table>';
 174          $o.= '</div></form>';
 175      /// Calculate the buttons
 176          $b = ' <p class="centerpara buttons">';
 177      /// The view original XML button
 178          if ($table->getField($fieldparam)) {
 179              $b .= '&nbsp;<a href="index.php?action=view_field_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original&amp;table=' . $tableparam . '&amp;field=' . $fieldparam . '">[' . $this->str['vieworiginal'] . ']</a>';
 180          } else {
 181              $b .= '&nbsp;[' . $this->str['vieworiginal'] . ']';
 182          }
 183      /// The view edited XML button
 184          if ($field->hasChanged()) {
 185              $b .= '&nbsp;<a href="index.php?action=view_field_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited&amp;table=' . $tableparam . '&amp;field=' . $fieldparam . '">[' . $this->str['viewedited'] . ']</a>';
 186          } else {
 187              $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
 188          }
 189      /// The back to edit table button
 190          $b .= '&nbsp;<a href="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
 191          $b .= '</p>';
 192          $o .= $b;
 193  
 194          $this->output = $o;
 195  
 196      /// Launch postaction if exists (leave this here!)
 197          if ($this->getPostAction() && $result) {
 198              return $this->launch($this->getPostAction());
 199          }
 200  
 201      /// Return ok if arrived here
 202          return $result;
 203      }
 204  }
 205  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7