From 3be91b01b69ad5d27996f6beaef35efa3586acf4 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Tue, 12 Sep 2006 17:45:25 +0000 Subject: [PATCH] Added support for add_field, drop_field and rename_field PHP code generation. --- .../view_table_php/view_table_php.class.php | 126 ++++++++---------- 1 file changed, 53 insertions(+), 73 deletions(-) diff --git a/admin/xmldb/actions/view_table_php/view_table_php.class.php b/admin/xmldb/actions/view_table_php/view_table_php.class.php index b51a35a856..764b26d2be 100644 --- a/admin/xmldb/actions/view_table_php/view_table_php.class.php +++ b/admin/xmldb/actions/view_table_php/view_table_php.class.php @@ -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 = '

'; @@ -117,21 +117,21 @@ class view_table_php extends XMLDBAction { /// Calculate the popup of fields/keys/indexes $optionspacer = '   '; 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.= ' '; $o.= ' '; $o.= ' '; - $o.= ' '; + $o.= ' '; $o.= ' '; $o.= '
' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . ' ' .choose_from_menu($poptables, 'fieldkeyindex', 'f#' . $fieldkeyindexparam, '', '', 0, true) . '
' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . ' ' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '
'; $o.= ''; @@ -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; } + } ?> -- 2.39.5