function chado_project_form

2.x tripal_project.chado_node.inc chado_project_form(&$node, $form_state)
3.x tripal_project.chado_node.inc chado_project_form(&$node, $form_state)
1.x tripal_project.module chado_project_form(&$node, $form_state)

Implementation of hook_form().

This form takes the Project Title information and description from the user.

@parm $node The initialized node

@parm $form_state The state of the form, that has the user entered information that is neccessary for adding information to the project

Return value

$form An array as described by the Drupal Form API

Related topics

File

legacy/tripal_project/includes/tripal_project.chado_node.inc, line 58
Implement the project node content type

Code

function chado_project_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 project
  // 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
  $project_id = null;
  $title = '';
  $description = '';

  // if we are editing an existing node then the project is already part of the node
  if (property_exists($node, 'project')) {
    $project = $node->project;
    // get the project default values.  When this module was first created
    // the project description was incorrectly stored in the $node->body field.
    // It is better to store it in the Chado tables.  However, the 'description'
    // field of the project table is only 255 characters.  So, we are going
    // to follow the same as the project module and store the description in
    // the projectprop table and leave the project.description field blank.
    // however, for backwards compatibitily, we check to see if the description
    // is in the $node->body field. If it is we'll use that.  When the node is
    // edited the text will be moved out of the body and into the projectprop
    // table where it should belong.
    if (property_exists($node, 'body')) {
      $description = $node->body;
    }
    else {
      $description = $project->description;
    }
    if (!$description) {
      $projectprop = chado_get_property(
      array('table' => 'project', 'id' => $project->project_id), 
      array('type_name' => 'Project Description', 'cv_name' => 'project_property')
      );
      $description = $projectprop->value;
    }

    $title = $project->name;
    $project_id = $project->project_id;

    // keep track of the project id if we have.  If we do have one then
    // this is an update as opposed to an insert.
    $form['project_id'] = array(
      '#type' => 'value',
      '#value' => $project_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)) {
    $title = $form_state['values']['title'];
    $description = $form_state['values']['description'];
  }
  // 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'])) {
    $title = $form_state['input']['title'];
    $description = $form_state['input']['description'];
  }

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Project Title'),
    '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
    '#required' => TRUE,
    '#default_value' => $node->title,
  );

  $form['description'] = array(
    '#type' => 'text_format',
    '#title' => t('Project Description'),
    '#description' => t('A brief description of the project'),
    '#required' => TRUE,
    '#default_value' => $description,
  );

  // Properties Form
  // ----------------------------------
  $select_options = array();
  $prop_cv = tripal_get_default_cv('projectprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  if ($prop_cv = 'project_property') {
    // if this is the project_property CV then
    // we want to exclude the project description from being loaded as a stored property
    // because we want to use the property to replace the project.description field as it is
    // only 255 characters which isn't large enough. We don't want the user to set it
    // as a property even though it will be stored as a property.
    $cv_result = chado_select_record('cv', array('cv_id'), array('name' => 'project_property'));
    $cv_id = $cv_result[0]->cv_id;
    $select_options = tripal_get_cvterm_select_options($cv_id);
    $descrip_id = array_search('Project Description', $select_options);
    unset($select_options[$descrip_id]);
  }

  $instructions = t('To add properties to the drop down list, you must ' . l("add terms to the project_property vocabulary", "admin/tripal/vocab/cvterm/add") . ".");
  $details = array(
    'property_table' => 'projectprop',
    'chado_id' => $project_id,
    'cv_id' => $cv_id,
    'additional_instructions' => $instructions,
    'select_options' => $select_options
  );
  chado_add_node_form_properties($form, $form_state, $details);

  // RELATIONSHIPS FORM
  //---------------------------------------------
  $relationship_cv = tripal_get_default_cv('project_relationship', 'type_id');
  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  $details = array(
    'relationship_table' => 'project_relationship', // the name of the _relationship table
    'base_table' => 'project', // the name of your chado base table
    'base_foreign_key' => 'project_id', // the name of the key in your base chado table
    'base_key_value' => $project_id, // the value of example_id for this record
    'nodetype' => 'project', // the human-readable name of your node type
    'cv_id' => $cv_id, // the cv.cv_id of the cv governing example_relationship.type_id
    'base_name_field' => 'name', // the base table field you want to be used as the name
    'subject_field_name' => 'subject_project_id',
    'object_field_name' => 'object_project_id'
  );
  // Adds the form elements to your current form
  chado_add_node_form_relationships($form, $form_state, $details);

  return $form;

}