function chado_analysis_form

2.x tripal_analysis.chado_node.inc chado_analysis_form($node, &$form_state)
3.x tripal_analysis.chado_node.inc chado_analysis_form($node, &$form_state)
1.x tripal_analysis.form.inc chado_analysis_form(&$node, $form_state = NULL)

Implements hook_form(). When editing or creating a new node of type 'chado_analysis' we need a form. This function creates the form that will be used for this.

Related topics

File

tripal_analysis/includes/tripal_analysis.chado_node.inc, line 61
Implements Drupal Node hooks to create the chado_analysis node content type.

Code

function chado_analysis_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 analysis
  // 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
  $analysis_id = null;
  $analysisname = '';
  $program = '';
  $programversion = '';
  $algorithm = '';
  $sourcename = '';
  $sourceversion = '';
  $sourceuri = '';
  $timeexecuted = '';
  $description = '';
  $d_removed = array(); // lists removed properties
  $num_new = 0; // the number of new rows

  // if we are editing an existing node then the analysis is already part of the node
  if (property_exists($node, 'analysis')) {
    $analysis = $node->analysis;
    $analysis = chado_expand_var($analysis, 'field', 'analysis.description');
    $analysis_id = $analysis->analysis_id;

    // get form defaults
    $analysisname = $analysis->name;
    $program = $analysis->program;
    $programversion = $analysis->programversion;
    $algorithm = $analysis->algorithm;
    $sourcename = $analysis->sourcename;
    $sourceversion = $analysis->sourceversion;
    $sourceuri = $analysis->sourceuri;
    $timeexecuted = $analysis->timeexecuted;
    $description = $analysis->description;
    $analysis_type = $node->type;


    // set the analysis_id in the form
    $form['analysis_id'] = array(
      '#type' => 'value',
      '#value' => $analysis->analysis_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)) {
    $analysisname = $form_state['values']['analysisname'];
    $program = $form_state['values']['program'];
    $programversion = $form_state['values']['programversion'];
    $algorithm = $form_state['values']['algorithm'];
    $sourcename = $form_state['values']['sourcename'];
    $sourceversion = $form_state['values']['sourceversion'];
    $sourceuri = $form_state['values']['sourceuri'];
    $timeexecuted = $form_state['values']['timeexecuted'];
    $description = $form_state['values']['description'];
    $d_removed = $form_state['values']['removed'];
    $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
    $analysis_type = $form_state['values']['analysis_type'];
  }
  // 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'])) {
    $analysisname = $form_state['input']['analysisname'];
    $program = $form_state['input']['program'];
    $programversion = $form_state['input']['programversion'];
    $algorithm = $form_state['input']['algorithm'];
    $sourcename = $form_state['input']['sourcename'];
    $sourceversion = $form_state['input']['sourceversion'];
    $sourceuri = $form_state['input']['sourceuri'];
    $timeexecuted = $form_state['input']['timeexecuted'];
    $description = $form_state['input']['description'];
    $d_removed = isset($form_state['input']['removed']) ? $form_state['input']['removed'] : array();
    $num_new = isset($form_state['input']['num_new']) ? $form_state['input']['num_new'] : 0;
    $analysis_type = isset($form_state['input']['analysis_type']) ? $form_state['input']['analysis_type'] : '';
  }

  $form['title'] = array(
    '#type' => 'value',
    '#default_value' => $node->title,
  );
  $form['instructions'] = array(
    '#markup' => t('When adding any type of data it is good to associate it with
        an analysis so that site visitors can identify the source of the data including
        necessary materials and methods.  The fields below imply that all analyses
        are derived from some software package. But, data can also be derived via retreival
        from an external source or an analysis pipeline with multipel software components.
        In these cases, provide values for the fields below that best makes sense
     '),
  );
  $form['analysisname'] = array(
    '#type' => 'textfield',
    '#title' => t('Analysis Name'),
    '#required' => TRUE,
    '#default_value' => $analysisname,
    '#description' => t("This should be a brief name that
       describes the analysis succintly. This name will helps the user find analyses."),
  );
  $form['program'] = array(
    '#type' => 'textfield',
    '#title' => t('Program, Pipeline or Method Name'),
    '#required' => TRUE,
    '#default_value' => $program,
    '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package, provide a very brief description of the pipeline or method."),
  );
  $form['programversion'] = array(
    '#type' => 'textfield',
    '#title' => t('Program, Pipeline  or Method Version'),
    '#required' => TRUE,
    '#default_value' => $programversion,
    '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available or applicable."),
  );
  $form['algorithm'] = array(
    '#type' => 'textfield',
    '#title' => t('Algorithm'),
    '#required' => FALSE,
    '#default_value' => $algorithm,
    '#description' => t("Algorithm name, e.g. blast."),
  );
  $form['sourcename'] = array(
    '#type' => 'textfield',
    '#title' => t('Source Name'),
    '#required' => TRUE,
    '#default_value' => $sourcename,
    '#description' => t('The name of the source data.  This could be a file name, data set name or a
         small description for how the data was collected.  For long descriptions use the description field below'),
  );
  $form['sourceversion'] = array(
    '#type' => 'textfield',
    '#title' => t('Source Version'),
    '#required' => FALSE,
    '#default_value' => $sourceversion,
    '#description' => t('If the source dataset has a version, include it here'),
  );
  $form['sourceuri'] = array(
    '#type' => 'textfield',
    '#title' => t('Source URI'),
    '#required' => FALSE,
    '#default_value' => $sourceuri,
    '#description' => t("This is a permanent URL or URI for the source of the analysis.
       Someone could recreate the analysis directly by going to this URI and
       fetching the source data (e.g. the blast database, or the training model)."),
  );

  // Get time saved in chado
  $default_time = $timeexecuted;
  $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  // If the time is not set, use current time
  if (!$default_time) {
    $default_time = REQUEST_TIME;
    $year = format_date($default_time, 'custom', 'Y');
    $month = format_date($default_time, 'custom', 'n');
    $day = format_date($default_time, 'custom', 'j');
  }
  $form['timeexecuted'] = array(
    '#type' => 'date',
    '#title' => t('Time Executed'),
    '#required' => TRUE,
    '#default_value' => array(
      'year' => $year,
      'month' => $month,
      'day' => $day,
    ),
  );
  $form['description'] = array(
    '#type' => 'text_format',
    '#rows' => 15,
    '#title' => t('Materials & Methods (Description and/or Program Settings)'),
    '#required' => FALSE,
    '#default_value' => $description,
    '#description' => t('Please provide all necessary information to allow
       someone to recreate the analysis, including materials and methods
       for collection of the source data and performing the analysis'),
  );

  /*
  // get node types from analysis extension modules
  $sql = "SELECT modulename FROM {tripal_analysis}";
  $modules = db_query($sql);
  $node_types = array();
  $node_types['chado_analysis'] = 'Analysis';
  foreach($modules as $module) {
    $mtypes = call_user_func($module->modulename . "_node_info");
    foreach ($mtypes as $mtypename => $mtype) {
      $node_types[$mtypename] = $mtype['name'];
    }
  }

  if (count($node_types) > 0) {
    $form['analysis_type'] = array(
      '#title'         => t('Analysis Type'),
      '#type'          => t('select'),
      '#description'   => t("You can change this analysis type to be any other analysis type currently supported by Tripal.  Simply change this value and click 'Save'.  Then click 'Edit' again to supply additional values."),
      '#required'      => TRUE,
      '#default_value' => $analysis_type,
      '#options'       => $node_types,
    );
  } */

  // Properties Form
  // ----------------------------------
  $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the analysis_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  $prop_cv = tripal_get_default_cv('analysisprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  $details = array(
    'property_table' => 'analysisprop', // the name of the prop table
    'chado_id' => $analysis_id, // the value of analysis_id for this record
    'cv_id' => $cv_id, // the cv.cv_id of the cv governing analysisprop.type_id
    'fieldset_title' => 'Properties',
    'additional_instructions' => $instructions
  );
  chado_add_node_form_properties($form, $form_state, $details);

  return $form;
}