function tripal_pub_search_page

2.x tripal_pub.pub_search.inc tripal_pub_search_page()
1.x pub_search.inc tripal_pub_search_page()

The page that contains the publication search form and the results for the search

Related topics

1 string reference to 'tripal_pub_search_page'
tripal_pub_menu in tripal_pub/tripal_pub.module
Implements hook_menu().

File

tripal_pub/includes/tripal_pub.pub_search.inc, line 15
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_pub_search_page() {

  // This line may not be required, but on some sites the $_SESSION
  // variable wasn't being set for anonymous users. This line solves that
  // problem
  drupal_session_start();

  $limit = 25;

  // generate the search form
  $form = drupal_get_form('tripal_pub_search_form');
  $output = drupal_render($form);

  // retrieve any results
  if (array_key_exists('tripal_pub_search_form', $_SESSION) and 
    $_SESSION['tripal_pub_search_form']['perform_search']) {
    $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'];
    $from_year = $_SESSION['tripal_pub_search_form']['from_year'];
    $to_year = $_SESSION['tripal_pub_search_form']['to_year'];

    $search_array = array();
    $search_array['num_criteria'] = $num_criteria;
    $search_array['from_year'] = $from_year;
    $search_array['to_year'] = $to_year;
    for ($i = 0; $i <= $num_criteria; $i++) {
      $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'];
      $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'];
      $search_array['criteria'][$i]['mode'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'];
      $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'];
    }

    // get the list of publications from the remote database using the search criteria.
    $page = isset($_GET['page']) ? $_GET['page'] : '0';
    $offset = $page * $limit;
    $total_records = 0;
    $pubs = tripal_search_publications($search_array, $offset, $limit, $total_records);
    pager_default_initialize($total_records, $limit, 0);

    // iterate through the results and construct the table displaying the publications
    $rows = array();
    $i = $page * $limit + 1;
    foreach ($pubs as $pub) {
      // get the citation for this publication
      $values = array(
        'pub_id' => $pub->pub_id,
        'type_id' => array(
          'name' => 'Citation',
        ),
      );
      $citation_rec = chado_generate_var('pubprop', $values);
      $citation_rec = chado_expand_var($citation_rec, 'field', 'pubprop.value');

      // if we have the citation then use it, otherwise, just use the title
      $title = htmlspecialchars($pub->title);
      $result = $title;
      if ($pub->nid) {
        $result = l($title, 'node/' . $pub->nid, array('attributes' => array('target' => '_blank')));
      }
      if ($citation_rec->value) {
        $citation = htmlspecialchars($citation_rec->value);
        $result .= '<br>' . $citation;
      }
      $rows[] = array(
        number_format($i) . ".",
        $pub->pyear,
        $result
      );
      $i++;
    }

    if (count($rows) == 0) {
      $rows[] = array(
        array(
          'data' => 'No results found',
          'colspan' => 3
        )
      );
    }

    $headers = array('', 'Year', 'Publication');
    $table = array(
      'header' => $headers,
      'rows' => $rows,
      'attributes' => array(
        'id' => 'tripal-pub-search-results-table',
        'border' => '0',
        'class' => array('tripal-data-table')
      ),
      'sticky' => TRUE,
      'caption' => '',
      'colgroups' => array(),
      'empty' => '',
    );
    $results = theme_table($table);

    // generate the pager
    $pager = array(
      'tags' => array(),
      'element' => 0,
      'parameters' => array(),
      'quantity' => $limit,
    );
    $pager = theme_pager($pager);

    // join all to form the results
    $output .= "<p><b>Found " . number_format($total_records) .
      " Results</b></br>" . $results . $pager;
  }
  return $output;
}