function chado_feature_form

2.x tripal_feature.chado_node.inc chado_feature_form($node, &$form_state)
3.x tripal_feature.chado_node.inc chado_feature_form($node, &$form_state)
1.x tripal_feature.module chado_feature_form($node, $param)

Implementation of hook_form().

Related topics

File

tripal_feature/includes/tripal_feature.chado_node.inc, line 45
Implementation of hooks to create a feature content type

Code

function chado_feature_form($node, &$form_state) {

  $form = array();

  // Default values can come in the following ways:
  //
  // 1) as elements of the $node object.  This occurs when editing an existing feature
  // 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
  $feature = null;
  $feature_id = null;
  $uniquename = '';
  $fname = '';
  $feature_type = '';
  $organism_id = '';
  $residues = '';
  $is_obsolete = '';
  $analyses = '';
  $references = '';
  $synonyms = '';

  // if we are editing an existing node then the feature is already part of the node
  if (property_exists($node, 'feature')) {
    $feature = $node->feature;
    $feature = chado_expand_var($feature, 'field', 'feature.residues');
    $feature_id = $feature->feature_id;
    $uniquename = $feature->uniquename;
    $fname = $feature->name;
    $feature_type = $feature->type_id->name;
    $organism_id = $feature->organism_id->organism_id;
    $residues = $feature->residues;
    $is_obsolete = $feature->is_obsolete;

    // get the synonyms from a previous post
    $synonyms = '';
    if (property_exists($node, 'synonyms')) {
      $synonyms = $node->synonyms;
    }

    // get synonyms from the database if we don't already have them
    if (!$synonyms) {
      $options = array('return_array' => 1);
      $feature = chado_expand_var($feature, 'table', 'feature_synonym', $options);
      $feature_synonyms = (isset($feature->feature_synonym)) ? $feature->feature_synonym : array();
      foreach ($feature_synonyms as $index => $synonym) {
        $synonyms .= $synonym->synonym_id->name . "\n";
      }
    }
    // keep track of the feature id
    $form['feature_id'] = array(
      '#type' => 'value',
      '#value' => $feature_id,
    );
  }
  // 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'])) {
    $uniquename = $form_state['values']['uniquename'];
    $fname = $form_state['values']['fname'];
    $feature_type = $form_state['values']['feature_type'];
    $organism_id = $form_state['values']['organism_id'];
    $residues = $form_state['values']['residues'];
    $is_obsolete = $form_state['values']['is_obsolete'];
    $synonyms = $form_state['values']['synonyms'];
  }
  // 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'])) {
    $uniquename = $form_state['input']['uniquename'];
    $fname = $form_state['input']['fname'];
    $feature_type = $form_state['input']['feature_type'];
    $organism_id = $form_state['input']['organism_id'];
    $residues = $form_state['input']['residues'];
    $is_obsolete = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : FALSE;
    $synonyms = $form_state['input']['synonyms'];
  }

  $form['fname'] = array(
    '#type' => 'textfield',
    '#title' => t('Feature Name'),
    '#required' => TRUE,
    '#default_value' => $fname,
    '#description' => t('Enter the name used by humans to refer to this feature.'),
    '#maxlength' => 255
  );
  $form['uniquename'] = array(
    '#type' => 'textfield',
    '#title' => t('Unique Feature Name'),
    '#required' => TRUE,
    '#default_value' => $uniquename,
    '#description' => t('Enter a unique name for this feature.  This name must be unique for the organism and feature type.'),
    '#maxlength' => 255
  );

  //$type_options = tripal_get_cvterm_default_select_options('feature', 'type_id', 'feature types');
  //$type_options[0] = 'Select a Type';
  $type_cv = tripal_get_default_cv('feature', 'type_id');
  $cv_id = $type_cv->cv_id;

  $form['feature_type'] = array(
    '#title' => t('Feature Type'),
    '#type' => 'textfield',
    '#description' => t("Choose the feature type."),
    '#required' => TRUE,
    '#default_value' => $feature_type,
    '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  );

  // 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(
    '#title' => t('Organism'),
    '#type' => t('select'),
    '#description' => t("Choose the organism with which this feature is associated"),
    '#required' => TRUE,
    '#default_value' => $organism_id,
    '#options' => $organisms,
  );

  // Get synonyms
  $syn_text = '';
  if ($synonyms) {
    if (is_array($synonyms)) {
      foreach ($synonyms as $synonym) {
        $syn_text .= "$synonym->name\n";
      }
    }
    else {
      $syn_text = $synonyms;
    }
  }
  $form['synonyms'] = array(
    '#type' => 'textarea',
    '#title' => t('Synonyms'),
    '#required' => FALSE,
    '#default_value' => $syn_text,
    '#description' => t('Enter alternate names (synonmys) for this feature to help in searching and identification. You may enter as many alternate names as needed each on different lines.'),
  );

  $form['residues'] = array(
    '#type' => 'textarea',
    '#title' => t('Residues'),
    '#required' => FALSE,
    '#default_value' => $residues,
    '#description' => t('Enter the nucelotide sequences for this feature'),
  );

  $checked = '';
  if ($is_obsolete == 't') {
    $checked = '1';
  }
  $form['is_obsolete'] = array(
    '#type' => 'checkbox',
    '#title' => t('Is Obsolete'),
    '#required' => FALSE,
    '#default_value' => $checked,
    '#description' => t('Check this box if this sequence should be retired'),
  );

  // PROPERTIES FORM
  //---------------------------------------------
  $prop_cv = tripal_get_default_cv('featureprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  $details = array(
    'property_table' => 'featureprop', // the name of the prop table
    'chado_id' => $feature_id, // the value of feature_id for this record
    'cv_id' => $cv_id // the cv.cv_id of the cv governing featureprop.type_id
  );
  chado_add_node_form_properties($form, $form_state, $details);

  // ADDITIONAL DBXREFS FORM
  //---------------------------------------------
  $details = array(
    'linking_table' => 'feature_dbxref', // the name of the _dbxref table
    'base_foreign_key' => 'feature_id', // the name of the key in your base chado table
    'base_key_value' => $feature_id // the value of feature_id for this record
  );
  chado_add_node_form_dbxrefs($form, $form_state, $details);

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

  return $form;
}