function chado_pub_form

2.x tripal_pub.chado_node.inc chado_pub_form($node, $form_state)
3.x tripal_pub.chado_node.inc chado_pub_form($node, $form_state)
1.x pub_form.inc chado_pub_form($node, $form_state)

Implements hook_form().

Related topics

File

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

Code

function chado_pub_form($node, $form_state) {
  $form = array();

  // Check to make sure that the tripal_pub vocabulary is loaded. If not, then
  // warn the user that they should load it before continuing.
  $pub_cv = chado_select_record('cv', array('cv_id'), array('name' => 'tripal_pub'));
  if (count($pub_cv) == 0) {
    drupal_set_message(t('The Tripal Pub vocabulary is currently not loaded. ' .
      'This vocabulary is required to be loaded before adding ' .
      'publications.  <br>Please !import', 
    array('!import' => l('load the Tripal Publication vocabulary', 'admin/tripal/loaders/obo_loader'))), 'warning');
  }

  // Default values can come in the following ways:
  //
  // 1) as elements of the $node object.  This occurs when editing an existing pub
  // 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
  $pub_id = null;
  $title = '';
  $pyear = '';
  $uniquename = '';
  $type_id = '';
  $is_obsolete = '';

  // some of the fields in the pub table should show up in the properties
  // form elements to make the form more seemless.  We will add them
  // to this array.
  $more_props = array();

  // if we are editing an existing node then the pub is already part of the node
  if (property_exists($node, 'pub')) {
    $pub = $node->pub;
    $pub = chado_expand_var($pub, 'field', 'pub.title');
    $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
    $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
    $pub_id = $pub->pub_id;

    $title = $pub->title;
    $pyear = $pub->pyear;
    $uniquename = $pub->uniquename;
    $type_id = $pub->type_id->cvterm_id;
    $is_obsolete = $pub->is_obsolete;

    // if the obsolete value is set by the database then it is in the form of
    // 't' or 'f', we need to convert to 1 or 0
    $is_obsolete = $is_obsolete == 't' ? 1 : $is_obsolete;
    $is_obsolete = $is_obsolete == 'f' ? 0 : $is_obsolete;

    // set the organism_id in the form
    $form['pub_id'] = array(
      '#type' => 'value',
      '#value' => $pub->pub_id,
    );

    // get fields from the pub table and convert them to properties. We will add these to the $more_props
    // array which gets passed in to the tripal_core_properties_form() API call further down
    if ($pub->volumetitle) {
      $cvterm = tripal_get_cvterm(array(
        'name' => 'Volume Title',
        'cv_id' => array('name' => 'tripal_pub')
      ));
      $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volumetitle);
    }
    if ($pub->volume) {
      $cvterm = tripal_get_cvterm(array(
        'name' => 'Volume',
        'cv_id' => array('name' => 'tripal_pub')
      ));
      $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volume);
    }
    if ($pub->series_name) {
      switch ($pub->type_id->name) {
        case 'Journal Article':
          $cvterm = tripal_get_cvterm(array(
            'name' => 'Journal Name',
            'cv_id' => array('name' => 'tripal_pub')
          ));
          $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
          break;
        case 'Conference Proceedings':
          $cvterm = tripal_get_cvterm(array(
            'name' => 'Conference Name',
            'cv_id' => array('name' => 'tripal_pub')
          ));
          $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
          break;
        default:
          $cvterm = tripal_get_cvterm(array(
            'name' => 'Series Name',
            'cv_id' => array('tripal_pub')
          ));
          $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
      }
    }
    if ($pub->issue) {
      $cvterm = tripal_get_cvterm(array(
        'name' => 'Issue',
        'cv_id' => array('name' => 'tripal_pub')
      ));
      $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->issue);
    }
    if ($pub->pages) {
      $cvterm = tripal_get_cvterm(array(
        'name' => 'Pages',
        'cv_id' => array('name' => 'tripal_pub')
      ));
      $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
    }
    if ($pub->miniref) {
      // not sure what to do with this one
    }
    if ($pub->publisher) {
      $cvterm = tripal_get_cvterm(array(
        'name' => 'Publisher',
        'cv_id' => array('name' => 'tripal_pub')
      ));
      $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->publisher);
    }
    if ($pub->pubplace) {
      $cvterm = tripal_get_cvterm(array(
        'name' => 'Published Location',
        'cv_id' => array('name' => 'tripal_pub')
      ));
      $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
    }
  }
  // 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']['pubtitle'])) {
    $title = $form_state['values']['pubtitle'];
    $pyear = $form_state['values']['pyear'];
    $uniquename = $form_state['values']['uniquename'];
    $type_id = $form_state['values']['type_id'];
    $is_obsolete = $form_state['values']['is_obsolete'];
  }
  // 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']['pubtitle'];
    $uniquename = $form_state['input']['uniquename'];
    $type_id = $form_state['input']['type_id'];
    $is_obsolete = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : '';
  }

  $form['pubtitle'] = array(
    '#type' => 'textarea',
    '#title' => t('Publication Title'),
    '#default_value' => $title,
    '#required' => TRUE,
  );

  $type_cv = tripal_get_default_cv('pub', 'type_id');
  if ($type_cv and $type_cv->name == 'tripal_pub') {

    // get the list of publication types.  In the Tripal publication
    // ontologies these are all grouped under the term 'Publication Type'
    // we want the default to be 'Journal Article'
    $sql = "
      SELECT
        CVTS.cvterm_id, CVTS.name
      FROM {cvtermpath} CVTP
        INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
        INNER JOIN {cvterm} CVTO ON CVTP.object_id  = CVTO.cvterm_id
        INNER JOIN {cv}          ON CVTO.cv_id      = CV.cv_id
      WHERE
        CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
        NOT CVTS.is_obsolete = 1
      ORDER BY CVTS.name ASC
    ";
    $results = chado_query($sql);
    $pub_types = array();
    while ($pub_type = $results->fetchObject()) {
      $pub_types[$pub_type->cvterm_id] = $pub_type->name;
      // if we don't have a default type then set the default to be 'Journal Article'
      if (strcmp($pub_type->name, "Journal Article") == 0 and !$type_id) {
        $type_id = $pub_type->cvterm_id;
      }
    }
  }
  else {
    $pub_types = tripal_get_cvterm_default_select_options('pub', 'type_id', 'publication types');
    $pub_types[0] = 'Select a Type';
  }

  $form['type_id'] = array(
    '#type' => 'select',
    '#title' => t('Publication Type'),
    '#options' => $pub_types,
    '#required' => TRUE,
    '#default_value' => $type_id,
  );
  $form['pyear'] = array(
    '#type' => 'textfield',
    '#title' => t('Publication Year'),
    '#default_value' => $pyear,
    '#required' => TRUE,
    '#size' => 5,
    '#description' => t('Enter the year of publication. Also, if available, please add a <b>Publication Date</b> property to specify the full date of publication.'),
  );
  $form['uniquename'] = array(
    '#type' => 'textarea',
    '#title' => t('Citation'),
    '#default_value' => $uniquename,
    '#description' => t('All publications must have a unique citation. ' .
      '<b>Please enter the full citation for this publication or leave blank and one will be generated ' .
      'automatically if possible</b>.  For PubMed style citations list ' .
      'the last name of the author followed by initials. Each author should be separated by a comma. Next comes ' .
      'the title, followed by the series title (e.g. journal name), publication date (4 digit year, 3 character Month, day), volume, issue and page numbers. You may also use HTML to provide a link in the citation. ' .
      'Below is an example: <pre>Medeiros PM, Ladio AH, Santos AM, Albuquerque UP. <a href="http://www.ncbi.nlm.nih.gov/pubmed/23462414" target="_blank">Does the selection of medicinal plants by Brazilian local populations ' .
      'suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  );
  $form['is_obsolete'] = array(
    '#type' => 'checkbox',
    '#title' => t('Is Obsolete? (Check for Yes)'),
    '#default_value' => $is_obsolete,
  );

  // Properties Form
  // ----------------------------------
  $select_options = array();
  $prop_cv = tripal_get_default_cv('pubprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;

  // if the poperty cv is 'tripal_pub' then we need to pass in our own select_options
  // for only a subset of the vocabulary
  if ($prop_cv and $prop_cv->name == 'tripal_pub') {
    $select_options[] = 'Select a Property';
    $sql = "
      SELECT
        DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
      FROM {cvtermpath} CVTP
        INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
        INNER JOIN {cvterm} CVTO ON CVTP.object_id  = CVTO.cvterm_id
        INNER JOIN {cv}          ON CVTO.cv_id      = CV.cv_id
      WHERE CV.name = 'tripal_pub' and
        (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
        NOT CVTS.is_obsolete = 1
      ORDER BY CVTS.name ASC
    ";
    $prop_types = chado_query($sql);
    while ($prop = $prop_types->fetchObject()) {
      // add all properties except the Citation. That property is set via the uniquename field
      if ($prop->name == 'Citation') {
        continue;
      }
      // Publication Dbxref's are handled by the dbxref form addition below
      if ($prop->name == 'Publication Dbxref') {
        continue;
      }
      $select_options[$prop->cvterm_id] = $prop->name;
    }
  }

  $details = array(
    'property_table' => 'pubprop',
    'chado_id' => $pub_id,
    'cv_id' => $cv_id,
    'select_options' => $select_options,
    'default_properties' => $more_props,
  );
  chado_add_node_form_properties($form, $form_state, $details);

  // RELATIONSHIPS FORM
  //---------------------------------------------
  $relationship_cv = tripal_get_default_cv('pub_relationship', 'type_id');
  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  $details = array(
    'relationship_table' => 'pub_relationship', // the name of the _relationship table
    'base_table' => 'pub', // the name of your chado base table
    'base_foreign_key' => 'pub_id', // the name of the key in your base chado table
    'base_key_value' => $pub_id, // the value of pub_id for this record
    'nodetype' => 'pub', // the human-readable name of your node type
    'cv_id' => $cv_id, // the cv.cv_id of the cv containing the relationships
    'base_name_field' => 'uniquename', // the base table field you want to be used as the name
  );
  // Adds the form elements to your current form
  chado_add_node_form_relationships($form, $form_state, $details);


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

  return $form;

}