]> git.mjollnir.org Git - moodle.git/commitdiff
Added support for add_field, drop_field and rename_field PHP code generation.
authorstronk7 <stronk7>
Tue, 12 Sep 2006 17:45:25 +0000 (17:45 +0000)
committerstronk7 <stronk7>
Tue, 12 Sep 2006 17:45:25 +0000 (17:45 +0000)
admin/xmldb/actions/view_table_php/view_table_php.class.php

index b51a35a8561f70701f624112d09582224f1cca6a..764b26d2bee2db7301ee46392858a4109cdb677e 100644 (file)
@@ -95,8 +95,8 @@ class view_table_php extends XMLDBAction {
 
     /// Get parameters
         $commandparam = optional_param('command', 'add_field', PARAM_PATH);
-        $fieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
-        $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $fieldkeyindexparam); ///Strip the initials
+        $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
+        $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
 
     /// The back to edit xml button
         $b = ' <p align="center" class="buttons">';
@@ -117,21 +117,21 @@ class view_table_php extends XMLDBAction {
     /// Calculate the popup of fields/keys/indexes
         $optionspacer = '&nbsp;&nbsp;&nbsp;';
         if ($fields) {
-            $poptables['fieldshead'] = 'Fields';
+            $popfields['fieldshead'] = 'Fields';
             foreach ($fields as $field) {
-                $poptables['f#' . $field->getName()] = $optionspacer . $field->getName();
+                $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
             }
         }
         if ($keys) {
-            $poptables['keyshead'] = 'Keys';
+            $popfields['keyshead'] = 'Keys';
             foreach ($keys as $key) {
-                $poptables['k#' . $key->getName()] = $optionspacer . $key->getName();
+                $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
             }
         }
         if ($indexes) {
-            $poptables['indexeshead'] = 'Indexes';
+            $popfields['indexeshead'] = 'Indexes';
             foreach ($indexes as $index) {
-                $poptables['i#' . $index->getName()] = $optionspacer . $index->getName();
+                $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
             }
         }
 
@@ -141,7 +141,7 @@ class view_table_php extends XMLDBAction {
         $o.= '    <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
         $o.= '    <input type="hidden" name ="action" value="view_table_php" />';
         $o.= '    <table id="formelements" align="center" cellpadding="5">';
-        $o.= '      <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .choose_from_menu($poptables, 'fieldkeyindex', 'f#' . $fieldkeyindexparam, '', '', 0, true) . '</td></tr>';
+        $o.= '      <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
         $o.= '      <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
         $o.= '    </table>';
         $o.= '</form>';
@@ -154,14 +154,14 @@ class view_table_php extends XMLDBAction {
          } else {
         /// Based on current params, call the needed function
             switch ($commandparam) {
-                case 'create_table':
-                    $o.= s($this->create_table_php($structure, $tableparam));
+                case 'add_field':
+                    $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
                     break;
-                case 'drop_table':
-                    $o.= s($this->drop_table_php($structure, $tableparam));
+                case 'drop_field':
+                    $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
                     break;
-                case 'rename_table':
-                    $o.= s($this->rename_table_php($structure, $tableparam));
+                case 'rename_field':
+                    $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
                     break;
             }
         }
@@ -181,19 +181,23 @@ class view_table_php extends XMLDBAction {
 
     /**
      * This function will generate all the PHP code needed to 
-     * create one table using XMLDB objects and functions
+     * create one field using XMLDB objects and functions
      * 
      * @param XMLDBStructure structure object containing all the info
-     * @param string table table code to be created
-     * @return string PHP code to be used to create the table
+     * @param string table table name
+     * @param string field field name to be created
+     * @return string PHP code to be used to create the field
      */
-    function create_table_php($structure, $table) {
+    function add_field_php($structure, $table, $field) {
 
         $result = '';
     /// Validate if we can do it
         if (!$table = $structure->getTable($table)) {
             return false;
         }
+        if (!$field = $table->getField($field)) {
+            return false;
+        }
         if ($table->getAllErrors()) {
             return false;
         }
@@ -203,50 +207,15 @@ class view_table_php extends XMLDBAction {
 
     /// Add contents
         $result .= XMLDB_LINEFEED;
-        $result .= '    /// Create table ' . $table->getName() . XMLDB_LINEFEED;
+        $result .= '    /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
         $result .= '        $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
-        $result .= XMLDB_LINEFEED;
-        $result .= '    /// Adding fields to table ' . $table->getName() . XMLDB_LINEFEED;
-    /// Iterate over each field
-        foreach ($table->getFields() as $field) {
-        /// The field header, with name
-            $result .= '        $table->addFieldInfo(' . "'" . $field->getName() . "', ";
-        /// The field PHP specs
-            $result .= $field->getPHP(false);
-        /// The end of the line
-            $result .= ');' . XMLDB_LINEFEED;
-        }
-    /// Iterate over each key
-        if ($keys = $table->getKeys()) {
-            $result .= XMLDB_LINEFEED;
-            $result .= '    /// Adding keys to table ' . $table->getName() . XMLDB_LINEFEED;
-            foreach ($keys as $key) {
-            /// The key header, with name
-                $result .= '        $table->addKeyInfo(' . "'" . $key->getName() . "', ";
-            /// The key PHP specs
-                $result .= $key->getPHP();
-            /// The end of the line
-                $result .= ');' . XMLDB_LINEFEED;
-            }
-        }
-    /// Iterate over each index
-        if ($indexes = $table->getIndexes()) {
-            $result .= XMLDB_LINEFEED;
-            $result .= '    /// Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED;
-            foreach ($indexes as $index) {
-            /// The index header, with name
-                $result .= '        $table->addIndexInfo(' . "'" . $index->getName() . "', ";
-            /// The index PHP specs
-                $result .= $index->getPHP();
-            /// The end of the line
-                $result .= ');' . XMLDB_LINEFEED;
-            }
-        }
+        $result .= '        $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
+        $result .= '        $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
 
     /// Launch the proper DDL
         $result .= XMLDB_LINEFEED;
-        $result .= '    /// Launch create table for ' . $table->getName() . XMLDB_LINEFEED;
-        $result .= '        $status = $status && create_table($table);' . XMLDB_LINEFEED;
+        $result .= '    /// Launch add field ' . $field->getName() . XMLDB_LINEFEED;
+        $result .= '        $status = $status && add_field($table, $field);' . XMLDB_LINEFEED;
 
     /// Add standard PHP footer
         $result .= XMLDB_PHP_FOOTER;
@@ -256,19 +225,23 @@ class view_table_php extends XMLDBAction {
 
     /**
      * This function will generate all the PHP code needed to 
-     * drop one table using XMLDB objects and functions
+     * drop one field using XMLDB objects and functions
      * 
      * @param XMLDBStructure structure object containing all the info
-     * @param string table table code to be dropped
-     * @return string PHP code to be used to drop the table
+     * @param string table table name
+     * @param string field field name to be dropped
+     * @return string PHP code to be used to drop the field
      */
-    function drop_table_php($structure, $table) {
+    function drop_field_php($structure, $table, $field) {
 
         $result = '';
     /// Validate if we can do it
         if (!$table = $structure->getTable($table)) {
             return false;
         }
+        if (!$field = $table->getField($field)) {
+            return false;
+        }
         if ($table->getAllErrors()) {
             return false;
         }
@@ -278,13 +251,14 @@ class view_table_php extends XMLDBAction {
 
     /// Add contents
         $result .= XMLDB_LINEFEED;
-        $result .= '    /// Create table ' . $table->getName() . XMLDB_LINEFEED;
+        $result .= '    /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
         $result .= '        $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
+        $result .= '        $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
 
     /// Launch the proper DDL
         $result .= XMLDB_LINEFEED;
-        $result .= '    /// Launch drop table for ' . $table->getName() . XMLDB_LINEFEED;
-        $result .= '        $status = $status && drop_table($table);' . XMLDB_LINEFEED;
+        $result .= '    /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED;
+        $result .= '        $status = $status && drop_field($table, $field);' . XMLDB_LINEFEED;
 
     /// Add standard PHP footer
         $result .= XMLDB_PHP_FOOTER;
@@ -294,19 +268,23 @@ class view_table_php extends XMLDBAction {
 
     /**
      * This function will generate all the PHP code needed to 
-     * rename one table using XMLDB objects and functions
+     * rename one field using XMLDB objects and functions
      * 
      * @param XMLDBStructure structure object containing all the info
-     * @param string table table code to be renamed
-     * @return string PHP code to be used to rename the table
+     * @param string table table name
+     * @param string field field name to be renamed
+     * @return string PHP code to be used to drop the field
      */
-    function rename_table_php($structure, $table) {
+    function rename_field_php($structure, $table, $field) {
 
         $result = '';
     /// Validate if we can do it
         if (!$table = $structure->getTable($table)) {
             return false;
         }
+        if (!$field = $table->getField($field)) {
+            return false;
+        }
         if ($table->getAllErrors()) {
             return false;
         }
@@ -316,18 +294,20 @@ class view_table_php extends XMLDBAction {
 
     /// Add contents
         $result .= XMLDB_LINEFEED;
-        $result .= '    /// Create table ' . $table->getName() . XMLDB_LINEFEED;
+        $result .= '    /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
         $result .= '        $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
+        $result .= '        $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
 
     /// Launch the proper DDL
         $result .= XMLDB_LINEFEED;
-        $result .= '    /// Launch rename table for ' . $table->getName() . XMLDB_LINEFEED;
-        $result .= '        $status = $status && rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED;
+        $result .= '    /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
+        $result .= '        $status = $status && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
 
     /// Add standard PHP footer
         $result .= XMLDB_PHP_FOOTER;
 
         return $result;
     }
+
 }
 ?>