Added support for add_field, drop_field and rename_field PHP code generation.

This commit is contained in:
stronk7 2006-09-12 17:45:25 +00:00
parent cb78437d64
commit 3be91b01b6

View file

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