function chado_stock_form

2.x tripal_stock.chado_node.inc chado_stock_form($node, $form_state)
3.x tripal_stock.chado_node.inc chado_stock_form($node, $form_state)
1.x tripal_stock.module chado_stock_form($node, $form_state)

Implements hook_form(). Creates the main Add/Edit/Delete Form for chado stocks

Parts to be added by this form name, uniquename, description, type => select from cvterm with key cvterm_id, organism => select from available with key organism_id main_db_reference => accession, version, description, db_name(select from dropdown)

Parameters

$node: An empty node object on insert OR the current stock node object on update

$form_state: The current state of the form

Return value

A description of the form to be rendered by drupal_get_form()

Related topics

File

tripal_stock/includes/tripal_stock.chado_node.inc, line 94
Stock Node Functionality

Code

function chado_stock_form($node, $form_state) {

  /* I don't think we need this... commenting out but leaving just in case
  // If existing stock then expand all fields needed using the chado API
  if (isset($node->nid)) {
    $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
    foreach ($fields_needed as $field_name) {
      // Check to see if it's excluded and expand it if so
      if (isset($node->expandable_fields)) {
        if (in_array($field_name, $node->expandable_fields)) {
          $node = chado_expand_var($node, 'field', $field_name);
        }
      }
    }
  }
  */
  //TODO: @lacey can you take a look at the above code?

  // Default values can come in the following ways:
  //
  // 1) as elements of the $node object.  This occurs when editing an existing stock
  // 2) in the $form_state['values'] array which occurs on a failed validation or
  //    ajax callbacks from non submit form elements
  // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  //    form elements and the form is being rebuilt
  //
  // set form field defaults
  $sname = '';
  $uniquename = '';
  $stock_id = 0;
  $type_id = 0;
  $organism_id = 0;
  $sdescription = '';
  $dbxref_accession = '';
  $dbxref_description = '';
  $dbxref_database = 0;

  // 1) if we are editing an existing node then the stock is already part of the node
  if (property_exists($node, 'stock')) {
    $sname = $node->stock->name;
    $uniquename = $node->stock->uniquename;
    $stock_id = $node->stock->stock_id;
    $type_id = $node->stock->type_id->cvterm_id;
    $organism_id = $node->stock->organism_id->organism_id;
    $sdescription = $node->stock->description;
    if (isset($node->stock->dbxref_id->db_id)) {
      $dbxref_accession = $node->stock->dbxref_id->accession;
      $dbxref_description = $node->stock->dbxref_id->description;
      $dbxref_database = $node->stock->dbxref_id->db_id->db_id;
    }
  }

  // 2) if we are re constructing the form from a failed validation or ajax callback
  // then use the $form_state['values'] values
  if (array_key_exists('values', $form_state) AND isset($form_state['values']['uniquename'])) {
    $sname = $form_state['values']['sname'];
    $uniquename = $form_state['values']['uniquename'];
    $stock_id = $form_state['values']['stock_id'];
    $type_id = $form_state['values']['type_id'];
    $organism_id = $form_state['values']['organism_id'];
    $sdescription = $form_state['values']['stock_description'];
    $dbxref_accession = $form_state['values']['accession'];
    $dbxref_description = $form_state['values']['db_description'];
    $dbxref_database = $form_state['values']['database'];
  }

  // 3) if we are re building the form from after submission (from ajax call) then
  // the values are in the $form_state['input'] array
  if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
    $sname = $form_state['input']['sname'];
    $uniquename = $form_state['input']['uniquename'];
    $stock_id = $form_state['input']['stock_id'];
    $type_id = $form_state['input']['type_id'];
    $organism_id = $form_state['input']['organism_id'];
    $sdescription = $form_state['input']['stock_description'];
    $dbxref_accession = $form_state['input']['accession'];
    $dbxref_description = $form_state['input']['db_description'];
    $dbxref_database = $form_state['input']['database'];
  }

  $form['sname'] = array(
    '#type' => 'textfield',
    '#title' => t('Stock Name'),
    '#description' => t('Enter a human-readable name for this stock.'),
    '#default_value' => $sname,
    '#required' => TRUE
  );

  $form['uniquename'] = array(
    '#type' => 'textfield',
    '#title' => t('Unique Name'),
    '#default_value' => $uniquename,
    '#description' => t('Enter a unique name for this stock. This name must be unique for the organism and stock type.'),
    '#required' => TRUE
  );

  if ($stock_id > 0) {
    $form['stock_id'] = array(
      '#type' => 'hidden',
      '#value' => $stock_id,
    );
  }

  // TODO: Should we make this a textfield with an autocomplete field like the
  // feature type_id field?.
  $st_cv = tripal_get_default_cv("stock", "type_id");
  $type_options = tripal_get_cvterm_default_select_options('stock', 'type_id', 'stock types');
  $type_options[0] = 'Select a Type';
  $st_message = tripal_set_message("To add additional items to the stock type drop down list,
     add a term to the " .
    l($st_cv->name . " controlled vocabulary", 
    "admin/tripal/chado/tripal_cv/cv/" . $st_cv->cv_id . "/cvterm/add", 
    array('attributes' => array('target' => '_blank'))
    ), 
  TRIPAL_INFO, array('return_html' => TRUE)
  );

  $form['type_id'] = array(
    '#type' => 'select',
    '#title' => t('Type of Stock'),
    '#description' => t('Select the stock type.'),
    '#options' => $type_options,
    '#default_value' => $type_id,
    '#required' => TRUE,
    '#suffix' => $st_message,
  );

  // get the list of organisms
  $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  $org_rset = chado_query($sql);
  $organisms = array();
  $organisms[''] = '';
  while ($organism = $org_rset->fetchObject()) {
    $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  }
  $form['organism_id'] = array(
    '#type' => 'select',
    '#title' => t('Organism'),
    '#default_value' => $organism_id,
    '#description' => t('Choose the organism with which this stock is associated.'),
    '#options' => $organisms,
    '#required' => TRUE
  );

  $form['stock_description'] = array(
    '#type' => 'text_format',
    '#title' => t('Notes'),
    '#default_value' => $sdescription,
    '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  );

  $form['database_reference'] = array(
    '#type' => 'fieldset',
    '#title' => t('Stock Database Reference'),
    '#description' => t('If this site is not the primary location for information
        about this stock, please provide the name of the database, the accession
        and an optional description using the fields below. If the database
        is not present in the list, then please ') .
      l(t('add the database '), 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank'))) .
      t('then refresh this page.'),
  );

  $db_options = tripal_get_db_select_options();
  $form['database_reference']['database'] = array(
    '#type' => 'select',
    '#title' => t('Database'),
    '#options' => $db_options,
    '#default_value' => $dbxref_database,
    '#description' => t('Select the remote database.')
  );

  $form['database_reference']['accession'] = array(
    '#type' => 'textfield',
    '#title' => t('Accession'),
    '#default_value' => $dbxref_accession,
    '#description' => t('Please enter the accession in the remote database for this stock.')
  );

  $form['database_reference']['db_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description of Database Reference'),
    '#default_value' => $dbxref_description,
    '#description' => t('Optionally enter a description about the database accession.')

  );



  // PROPERTIES FORM
  //---------------------------------------------
  $prop_cv = tripal_get_default_cv('stockprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  $details = array(
    'property_table' => 'stockprop',
    'chado_id' => $stock_id,
    'cv_id' => $cv_id
  );
  chado_add_node_form_properties($form, $form_state, $details);

  // ADDITIONAL DBXREFS FORM
  //---------------------------------------------
  $details = array(
    'linking_table' => 'stock_dbxref',
    'base_foreign_key' => 'stock_id',
    'base_key_value' => $stock_id
  );
  chado_add_node_form_dbxrefs($form, $form_state, $details);

  // RELATIONSHIPS FORM
  //---------------------------------------------
  $relationship_cv = tripal_get_default_cv('stock_relationship', 'type_id');
  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  $details = array(
    'relationship_table' => 'stock_relationship',
    'base_table' => 'stock',
    'base_foreign_key' => 'stock_id',
    'base_key_value' => $stock_id,
    'nodetype' => 'stock',
    'cv_id' => $cv_id
  );
  chado_add_node_form_relationships($form, $form_state, $details);

  return $form;
}