function tripal_custom_tables_form
2.x tripal_core.custom_tables.inc | tripal_custom_tables_form($form, &$form_state = NULL, $table_id = NULL) |
3.x tripal_chado.custom_tables.inc | tripal_custom_tables_form( |
1.x custom_tables.inc | tripal_custom_tables_form(&$form_state = NULL, $table_id = NULL) |
A Form to Create/Edit a Custom table.
Parameters
$form_state: The current state of the form (Form API)
$table_id: The unique ID of the Custom table to Edit or NULL if creating a new table
Return value
A form array (Form API)
Related topics
2 string references to 'tripal_custom_tables_form'
- tripal_core_menu in tripal_core/
tripal_core.module - Implements hook_menu(). Defines all menu items needed by Tripal Core
- tripal_custom_table_new_page in tripal_core/
includes/ tripal_core.custom_tables.inc - Renders the tripal_custom_tables_form.
File
- tripal_core/
includes/ tripal_core.custom_tables.inc, line 127 - Contains functions for creating, editing and deleting custom tables on the Tripal website.
Code
function tripal_custom_tables_form($form, &$form_state = NULL, $table_id = NULL) {
if (!$table_id) {
$action = 'Add';
}
else {
$action = 'Edit';
}
// get this requested table
$default_schema = '';
$default_force_drop = 0;
if (strcmp($action, 'Edit') == 0) {
$sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id ";
$results = db_query($sql, array(':table_id' => $table_id));
$custom_table = $results->fetchObject();
// if this is a materialized view then don't allow editing with this function
if (property_exists($custom_table, 'mview_id') and $custom_table->mview_id) {
drupal_set_message("This custom table is a materialized view. Please use the " . l('Materialized View', 'admin/tripal/schema/mviews') . " interface to edit it.", 'error');
drupal_goto("admin/tripal/schema/custom_tables");
return array();
}
// set the default values. If there is a value set in the
// form_state then let's use that, otherwise, we'll pull
// the values from the database
if (array_key_exists('values', $form_state)) {
$default_schema = $form_state['values']['schema'];
$default_force_drop = $form_state['values']['force_drop'];
}
if (!$default_schema) {
$default_schema = var_export(unserialize($custom_table->schema), 1);
$default_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $default_schema);
}
}
$form['return'] = array(
'#type' => 'markup',
'#markup' => "<p>" . l("Return to list of custom tables", "admin/tripal/schema/custom_tables") . "</p>",
);
// Build the form
$form['action'] = array(
'#type' => 'value',
'#value' => $action
);
$form['table_id'] = array(
'#type' => 'value',
'#value' => $table_id
);
$form['instructions'] = array(
'#type' => 'fieldset',
'#title' => 'Instructions',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['instructions']['text'] = array(
'#type' => 'item',
'#markup' => '<p>' . t('At times it is necessary to add a custom table
to the Chado schema. These are not offically sanctioned tables but may
be necessary for local data requirements. Avoid creating custom tables
when possible as other GMOD tools may not recognize these tables nor
the data in them. Linker tables or property tables are often a good
candidate for a custom table. For example a table to link stocks and
libraries (e.g. library_stock). Try to model linker or propery tables
after existing tables. If the table already exists it will not be
modified. To force dropping and recreation of the table
click the checkbox below. Tables are defined using the ' .
l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7',
array('attributes' => array('target' => '_blank'))) . '</p>' .
'<p>Please note that table names should be all lower-case.</p>'
),
);
$form['instructions']['example'] = array(
'#type' => 'item',
'#markup' => "Example library_stock table: <pre>
array (
'table' => 'library_stock',
'fields' => array (
'library_stock_id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'library_id' => array(
'type' => 'int',
'not null' => TRUE,
),
'stock_id' => array(
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array(
'library_stock_id'
),
'unique keys' => array(
'library_stock_c1' => array(
'library_id',
'stock_id'
),
),
'foreign keys' => array(
'library' => array(
'table' => 'library',
'columns' => array(
'library_id' => 'library_id',
),
),
'stock' => array(
'table' => 'stock',
'columns' => array(
'stock_id' => 'stock_id',
),
),
),
)
</pre>",
);
$form['force_drop'] = array(
'#type' => 'checkbox',
'#title' => t('Re-create table'),
'#description' => t('Check this box if your table already exists and you would like to drop it and recreate it.'),
'#default_value' => $default_force_drop,
);
$form['schema'] = array(
'#type' => 'textarea',
'#title' => t('Schema Array'),
'#description' => t('Please enter the ' . l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7', array('attributes' => array('target' => '_blank'))) . ' compatible array that defines the table.'),
'#required' => FALSE,
'#default_value' => $default_schema,
'#rows' => 25,
);
if ($action == 'Edit') {
$value = 'Save';
}
if ($action == 'Add') {
$value = 'Add';
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t($value),
'#executes_submit_callback' => TRUE,
);
return $form;
}