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($form, &$form_state = NULL, $table_id = NULL)
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

1 string reference 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

File

tripal_core/includes/custom_tables.inc, line 104
Contains functions for creating, editing and deleting custom tables on the Tripal website.

Code

function tripal_custom_tables_form(&$form_state = NULL, $table_id = NULL) {

  if (!$table_id) {
    $action = 'Add';
  }
  else {
    $action = 'Edit';
  }

  // get this requested table
  if (strcmp($action, 'Edit') == 0) {
    $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d ";
    $custom_table = db_fetch_object(db_query($sql, $table_id));

    // 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
    $default_schema = $form_state['values']['schema'];
    $default_force_drop = $form_state['values']['force_drop'];

    if (!$default_table_name) {
      $default_table = $custom_table->table_name;
    }
    if (!$default_schema) {
      $default_schema = var_export(unserialize($custom_table->schema), 1);
      $default_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $default_schema);
    }
  }

  // Build the form
  $form['action'] = array(
    '#type' => 'value',
    '#value' => $action
  );

  $form['table_id'] = array(
    '#type' => 'value',
    '#value' => $table_id
  );

  $form['instructions'] = array(
    '#type' => 'markup',
    '#value' => 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) would be
       a good custom table. 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.
    '),
  );

  $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 Drupal Schema API 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,
  );
  $form['#redirect'] = 'admin/tripal/custom_tables';

  $form['example'] = array(
    '#type' => 'markup',
    '#value' => "<br>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>",
  );


  return $form;
}