function tripal_chado_pub_search_form

3.x tripal_chado.pub_search.inc tripal_chado_pub_search_form($form, &$form_state)

Provides the form to search pubmed

2 string references to 'tripal_chado_pub_search_form'
tripal_chado_pub_search_form_submit in tripal_chado/includes/tripal_chado.pub_search.inc
Submit the tripal_chado_pub_search_form form
tripal_chado_pub_search_page in tripal_chado/includes/tripal_chado.pub_search.inc
The page that contains the publication search form and the results for the search

File

tripal_chado/includes/tripal_chado.pub_search.inc, line 193
Functions responsible for creating the publication search form that allows a user of the site to search for publications that are currently in Chado.

Code

function tripal_chado_pub_search_form($form, &$form_state) {
  // Default values can come in the following ways:
  //
  // 1) as elements of the $pub_importer object.  This occurs when editing an existing importer
  // 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 the default values. If the pub_import_id isn't already defined by the form values
  // and one is provided then look it up in the database
  $criteria = NULL;
  $num_criteria = 2;
  $from_year = '';
  $to_year = '';

  // if the session has variables then use those.  This should only happen when
  // the 'Test Criteria' button is clicked.
  if (array_key_exists('storage', $form_state)) {
    $num_criteria = $form_state['storage']['num_criteria'];
  }
  if (array_key_exists('tripal_chado_pub_search_form', $_SESSION)) {
    $num_criteria = $_SESSION['tripal_chado_pub_search_form']['num_criteria'] ? $_SESSION['tripal_chado_pub_search_form']['num_criteria'] : $num_criteria;
    $from_year = $_SESSION['tripal_chado_pub_search_form']['from_year'] ? $_SESSION['tripal_chado_pub_search_form']['from_year'] : '';
    $to_year = $_SESSION['tripal_chado_pub_search_form']['to_year'] ? $_SESSION['tripal_chado_pub_search_form']['to_year'] : '';
  }
  if (array_key_exists('values', $form_state)) {
    $from_year = $form_state['values']['from_year'] ? $form_state['values']['from_year'] : $from_year;
    $to_year = $form_state['values']['to_year'] ? $form_state['values']['to_year'] : $to_year;
  }
  if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
    $from_year = $form_state['input']['from_year'] ? $form_state['input']['from_year'] : $from_year;
    $to_year = $form_state['input']['to_year'] ? $form_state['input']['to_year'] : $to_year;
  }

  if (array_key_exists('triggering_element', $form_state) and 
    $form_state['triggering_element']['#name'] == 'add') {
    $num_criteria++;
  }
  if (array_key_exists('triggering_element', $form_state) and 
    $form_state['triggering_element']['#name'] == 'remove') {
    $num_criteria--;
  }

  $form_state['storage']['num_criteria'] = $num_criteria;

  $form['admin-instructions'] = array(
    '#markup' => tripal_set_message(
    t('Administrators, you can select the fields with which a user can use to search, by checking the desired fields on the ' .
      l('Publication Search Settings Page', 'admin/tripal/storage/chado/pub-search-config', array('attributes' => array('target' => '_blank'))) . '
      in the section titled "Search Options".  The selected fields will appear in the dropdowns below.'), 
    TRIPAL_INFO, 
    array('return_html' => 1)),
  );
  $form['instructions'] = array(
    '#markup' => t('To search for publications enter keywords in the text boxes below.
    You can limit your search by selecting the field in the dropdown box. Click the
    add and remove buttons to add additional fields for searching. '),
  );

  // get publication properties list
  $properties = array();
  $properties[] = 'Any Field';
  $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
  ";
  $allowed_fields = variable_get('tripal_pub_allowed_search_fields', array());
  $prop_types = chado_query($sql);
  foreach ($prop_types as $prop) {
    if (array_key_exists($prop->cvterm_id, $allowed_fields) and $allowed_fields[$prop->cvterm_id] > 0) {
      $properties[$prop->cvterm_id] = $prop->name;
    }
  }

  for ($i = 1; $i <= $num_criteria; $i++) {
    $search_terms = '';
    $scope = '';
    $operation = '';
    $mode = '';

    // first populate defaults using any values in the SESSION variable
    if (array_key_exists('tripal_chado_pub_search_form', $_SESSION)) {
      $search_terms = $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
      $scope = $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['scope'] : $scope;
      $mode = $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['mode'] : $mode;
      $operation = $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_chado_pub_search_form']['criteria'][$i]['operation'] : $operation;
    }
    if (array_key_exists('values', $form_state)) {
      $search_terms = array_key_exists("search_terms-$i", $form_state['values']) ? $form_state['values']["search_terms-$i"] : $search_terms;
      $scope = array_key_exists("scope-$i", $form_state['values']) ? $form_state['values']["scope-$i"] : $scope;
      $mode = array_key_exists("mode-$i", $form_state['values']) ? $form_state['values']["mode-$i"] : $mode;
      $operation = array_key_exists("operation-$i", $form_state['values']) ? $form_state['values']["operation-$i"] : $operation;
    }
    if (array_key_exists('input', $form_state)) {
      $search_terms = array_key_exists("search_terms-$i", $form_state['input']) ? $form_state['input']["search_terms-$i"] : $search_terms;
      $scope = array_key_exists("scope-$i", $form_state['input']) ? $form_state['input']["scope-$i"] : $scope;
      $mode = array_key_exists("mode-$i", $form_state['input']) ? $form_state['input']["mode-$i"] : $mode;
      $operation = array_key_exists("operation-$i", $form_state['input']) ? $form_state['input']["operation-$i"] : $operation;
    }

    // default to searching the title and abstract
    if (!$scope) {
      $scope = 'abstract';
    }

    $form['criteria'][$i]["search_terms-$i"] = array(
      '#type' => 'textfield',
      '#default_value' => $search_terms,
      '#required' => FALSE,
    );
    $form['criteria'][$i]["scope-$i"] = array(
      '#type' => 'select',
      '#options' => $properties,
      '#default_value' => $scope,
      '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
    );
    /*
     $form['criteria'][$i]["mode-$i"] = array(
     '#type'          => 'select',
     '#options'       => array(
     'Contains'    => 'Contains',
     'Starts With' => 'Starts With',
     'Ends With'   => 'Ends With',
     'Exactly'     => 'Exactly'),
     '#default_value' => $mode,
     );*/

    if ($i > 1) {
      $form['criteria'][$i]["operation-$i"] = array(
        '#type' => 'select',
        '#options' => array(
          'AND' => 'AND',
          'OR' => 'OR',
          'NOT' => 'NOT'),
        '#default_value' => $operation,
      );
    }
    if ($i == $num_criteria) {
      if ($i > 1) {
        $form['criteria'][$i]["remove-$i"] = array(
          '#type' => 'button',
          '#name' => 'remove',
          '#value' => t('Remove'),
          '#ajax' => array(
            'callback' => "tripal_pubs_search_form_ajax_update",
            'wrapper' => 'tripal-pub-search-form-criteria',
            'effect' => 'fade',
            'method' => 'replace',
            'prevent' => 'click'
          ),
          // When this button is clicked, the form will be validated and submitted.
          // Therefore, we set custom submit and validate functions to override the
          // default form submit.  In the validate function we set the form_state
          // to rebuild the form so the submit function never actually gets called,
          // but we need it or Drupal will run the default validate anyway.
          // we also set #limit_validation_errors to empty so fields that
          // are required that don't have values won't generate warnings.
          '#submit' => array('tripal_chado_pub_search_form_ajax_button_submit'),
          '#validate' => array('tripal_chado_pub_search_form_ajax_button_validate'),
          '#limit_validation_errors' => array(),
        );
      }
      $form['criteria'][$i]["add-$i"] = array(
        '#type' => 'button',
        '#name' => 'add',
        '#value' => t('Add'),
        '#ajax' => array(
          'callback' => "tripal_pubs_search_form_ajax_update",
          'wrapper' => 'tripal-pub-search-form-criteria',
          'effect' => 'fade',
          'method' => 'replace',
          'prevent' => 'click'
        ),
        // When this button is clicked, the form will be validated and submitted.
        // Therefore, we set custom submit and validate functions to override the
        // default form submit.  In the validate function we set the form_state
        // to rebuild the form so the submit function never actually gets called,
        // but we need it or Drupal will run the default validate anyway.
        // we also set #limit_validation_errors to empty so fields that
        // are required that don't have values won't generate warnings.
        '#submit' => array('tripal_chado_pub_search_form_ajax_button_submit'),
        '#validate' => array('tripal_chado_pub_search_form_ajax_button_validate'),
        '#limit_validation_errors' => array(),
      );
    }
  }
  $form['criteria']["date"] = array(
    '#type' => 'select',
    '#options' => array('Years' => 'Years'),
    '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  );
  $form['criteria']["from_year"] = array(
    '#type' => 'textfield',
    '#default_value' => $from_year,
    '#required' => FALSE,
    '#title' => 'from',
    '#size' => 4,
    '#maxlength' => 4,
  );
  $form['criteria']["to_year"] = array(
    '#type' => 'textfield',
    '#default_value' => $to_year,
    '#required' => FALSE,
    '#title' => 'to',
    '#size' => 4,
    '#maxlength' => 4,
  );

  $form['search'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
  );

  $form['criteria']['#theme'] = 'tripal_chado_pub_search_setup_form_elements';

  return $form;
}