Explorar o código

Working on Pub API... checking in to pull

Stephen Ficklin %!s(int64=11) %!d(string=hai) anos
pai
achega
e17f584dd7

+ 3 - 3
tripal_analysis/theme/tripal_analysis/tripal_analysis_teaser.tpl.php

@@ -3,11 +3,11 @@ $node = $variables['node'];
 $analysis = $variables['node']->analysis;
 $analysis = tripal_core_expand_chado_vars($analysis,'field','analysis.description'); ?>
 
-<div class="tripal_analysis-teaser tripal-teaser"> 
-  <div class="tripal-analysis-teaser-title tripal-teaser-title"><?php 
+<div class="tripal_analysis_blast-teaser tripal-teaser"> 
+  <div class="tripal-analysis-blast-teaser-title tripal-teaser-title"><?php 
     print l($node->title, "node/$node->nid", array('html' => TRUE));?>
   </div>
-  <div class="tripal-analysis-teaser-text tripal-teaser-text"><?php 
+  <div class="tripal-analysis-blast-teaser-text tripal-teaser-text"><?php 
     print substr($analysis->description, 0, 650);
     if (strlen($analysis->description) > 650) {
       print "... " . l("[more]", "node/$node->nid");

+ 21 - 13
tripal_core/api/tripal_core.chado_nodes.dbxrefs.api.inc

@@ -124,17 +124,20 @@
  */
 function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
 
-  // Set Defaults for optional fields
+  // Set defaults for optional fields
   $details['fieldset_title'] = 'Additional Database References';
   $details['additional_instructions'] = '';
 
-  // Get Property Types for the Select List
+  // Get the list of databases to display in the dropdown. 
   if (isset($details['select_options'])) {
+    // the callee has provided a list
     $db_options = $details['select_options'];
   }
   else {
+    // get the list of databases from the db table
     $db_options = array(0 => 'Select a Database');
-    $select = chado_select_record('db', array('db_id','name'), array(), array('order_by' => array('name' => 'ASC')));
+    $options = array('order_by' => array('name' => 'ASC'));
+    $select = chado_select_record('db', array('db_id','name'), array(), $options);
     foreach($select as $db) {
       $db_options[$db->db_id] = $db->name;
     }
@@ -196,16 +199,18 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
   }
   else {
     $existing_dbxrefs = chado_query(
-      "SELECT dbxref.dbxref_id, db.name as db_name, db.db_id as db_id, dbxref.accession as accession, dbxref.description as description, dbxref.version
-        FROM {dbxref} dbxref
-        LEFT JOIN {db} db ON db.db_id = dbxref.db_id
-        LEFT JOIN {".$details['linking_table']."} linking_table ON linking_table.dbxref_id = dbxref.dbxref_id
+      "SELECT 
+         db.name as db_name, db.db_id as db_id,
+         dbxref.dbxref_id, dbxref.accession as accession, 
+         dbxref.description as description, dbxref.version
+       FROM {dbxref} dbxref
+         LEFT JOIN {db} db ON db.db_id = dbxref.db_id
+         LEFT JOIN {".$details['linking_table']."} linking_table ON linking_table.dbxref_id = dbxref.dbxref_id
        WHERE linking_table.".$details['base_foreign_key']."= :base_key_value
        ORDER BY db.name ASC, dbxref.version ASC",
        array(':base_key_value' => $details['base_key_value'])
     );
   }
-
   /* The format of the $existing_dbxref's array is either:
    *
    * From the chado_additional_dbxrefs array:
@@ -238,13 +243,16 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
    * an element to the form for each one.
    */
   foreach ($existing_dbxrefs as $dbxref) {
+    
     if (array_key_exists($dbxref->db_id, $db_options)) {
 
-      // Since the version is part of the key, when it is '' we need to use something
-      // in the key to indicate this case. Otherwise, you wouldn't be able to select
-      // those elements from the array (ie: $form['addtl_dbxrefs']['dbxref_table'][9999]['']
-      // doesn't work as expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE]
-      // is much better)
+      /* Since the dbxref version is part of the unique constraint, when it is 
+       * empty we need to use something in the key to indicate this case. Otherwise, 
+       * you wouldn't be able to select those elements from the array 
+       * (ie: $form['addtl_dbxrefs']['dbxref_table'][9999][''] doesn't work as 
+       * expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE] 
+       * is much better)
+       */
       $version = (!empty($dbxref->version)) ? $dbxref->version : 'NONE';
 
       $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id] = array(

+ 7 - 4
tripal_db/api/tripal_db.api.inc

@@ -388,9 +388,11 @@ function chado_insert_dbxref($values) {
  * @param $record_id
  *   The primary key of the basetable to associate the dbxref with. This should be in integer.
  * @param $dbxref
- *   An associative array describing the dbxref. Valid keys include: 'accession' => the
- *   accession for the dbxref, 'db_name' => the name of the database the dbxref belongs to;
- *   'db_id' => the primary key of the database the dbxref belongs to.
+ *   An associative array describing the dbxref. Valid keys include: 
+ *     'accession' => the accession for the dbxref, 
+ *     'db_name' => the name of the database the dbxref belongs to;
+ *     'db_id' => the db_id from the databaes table of Chado. Substitutes for 'db_name'
+ *     'dbxref_id' => the dbxref_id of the dbxref to assocaite.  Substitutes for all other keys.
  * @param $options
  *   An associative array of options. Valid keys include:
  *    - insert_dbxref: Insert the dbxref if it doesn't already exist. TRUE is the default
@@ -412,7 +414,8 @@ function chado_associate_dbxref($basetable, $record_id, $dbxref, $options = arra
     );
     if (isset($dbxref['db_id'])) {
       $values['db_id'] = $dbxref['db_id'];
-    } elseif (isset($dbxref['db_name'])) {
+    } 
+    elseif (isset($dbxref['db_name'])) {
       $values['db_id'] = array(
         'name' => $dbxref['db_name']
       );

+ 11 - 6
tripal_pub/api/tripal_pub.DEPRECATED.inc

@@ -22,8 +22,8 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to
       '%new_function' => 'pub_search_remote'
     )
   );
-
-  return FALSE;
+  
+  return pub_search_remote($remote_db, $search_array, $num_to_retrieve, $page);;
 }
 
 /**
@@ -44,8 +44,8 @@ function tripal_pub_get_raw_data($dbxref) {
       '%new_function' => 'chado_get_remote_pub_record'
     )
   );
-
-  return FALSE;
+  
+  return tripal_get_remote_pub_record($dbxref);
 }
 
 /**
@@ -176,8 +176,13 @@ function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
       '%new_function' => 'chado_associate_dbxref'
     )
   );
-
-  return FALSE;
+ 
+  $dbxref = array();
+  if(preg_match('/^(.*?):(.*?)$/', trim($pub_dbxref), $matches)) {
+    $dbxref['db_name']   = $matches[1];
+    $dbxref['accession'] = $matches[2];
+  }
+  return chado_associate_dbxref('pub', $pub_id, $dbxref);
 }
 
 /**

+ 159 - 83
tripal_pub/api/tripal_pub.api.inc

@@ -55,7 +55,7 @@
  *
  * @ingroup tripal_pub_api
  */
-function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to_retrieve, $page = 0) {
+function pub_search_remote($remote_db, $search_array, $num_to_retrieve, $page = 0) {
 
   // now call the callback function to get the results
   $callback = "tripal_pub_remote_search_$remote_db";
@@ -70,6 +70,154 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to
   return $pubs;
 }
 
+/**
+ * Builds the SQL statement need to search Chado for the publications
+ * that match the user supplied criteria.  Tpyically, this function is
+ * called by the search form generated by the tripal_pub_search_form() function
+ * but this function is included in the API for calling by anyone. 
+ * 
+ * @param $search_array
+ *   An array of search criteria provided by the user. The search array is
+ *   an associative array with the following keys:
+ *     'num_criteria': an integer indicating the number of search criteria supplied
+ *     'from_year':    filters records by a start year
+ *     'to_year':      filters records by an end year
+ *     'criteria':     an array of criteria. Each criteria is an associative
+ *                     array with the following keys:
+ *                     'search_terms':   The text used for searching
+ *                     'scope':          The cvterm_id of the property used for filtering
+ *                     'mode':           The operation (e.g. AND, OR or NOT)
+ * @param $offset
+ *   The offset for paging records.  The first record returned will be 
+ *   at the offset indicated here, and the next $limit number of records
+ *   will be returned.
+ *   
+ * @param $limit
+ *   The number of records to retrieve
+ *   
+ * @param total_records
+ *   A value passed by reference. This value will get set to the total
+ *   number of matching records
+ *   
+ * @return 
+ *   a PDO database object of the query results.
+ *
+ * @ingroup tripal_pub
+ */
+function pub_search($search_array, $offset, $limit, &$total_records) {
+
+  // build the SQL based on the criteria provided by the user
+  $select = "SELECT DISTINCT P.*, CP.nid ";
+  $from   = "FROM {pub} P
+               LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
+               INNER JOIN {cvterm} CVT on CVT.cvterm_id = P.type_id
+            ";
+  $where  = "WHERE (NOT P.title = 'null') "; // always exclude the dummy pub
+  $order  = "ORDER BY P.pyear DESC, P.title ASC";
+  $args = array();  // arguments for where clause
+  $join = 0;
+
+  $num_criteria = $search_array['num_criteria'];
+  $from_year    = $search_array['from_year'];
+  $to_year      = $search_array['to_year'];
+
+  for ($i = 1; $i <= $num_criteria; $i++) {
+    $value = $search_array['criteria'][$i]['search_terms'];
+    $type_id = $search_array['criteria'][$i]['scope'];
+    $mode = $search_array['criteria'][$i]['mode'];
+    $op = $search_array['criteria'][$i]['operation'];
+
+    // skip criteria with no values
+    if(!$value) {
+      continue;
+    }
+
+    // to prevent SQL injection make sure our operator is
+    // what we expect
+    if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
+      $op = 'AND';
+    }
+    if ($op == 'NOT') {
+      $op = 'AND NOT';
+    }
+    if (!$op) {
+      $op = 'AND';
+    }
+
+    // get the scope type
+    $values = array('cvterm_id' => $type_id);
+    $cvterm = chado_select_record('cvterm', array('name'), $values);
+    $type_name = '';
+    if (count($cvterm) > 0) {
+      $type_name = $cvterm[0]->name;
+    }
+    if ($type_name == 'Title') {
+      $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+    }
+    elseif ($type_name == 'Year') {
+      $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+    }
+    elseif ($type_name == 'Volume') {
+      $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+    }
+    elseif ($type_name == 'Issue') {
+      $where .= " $op (lower(P.issue) = lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+    }
+    elseif ($type_name == 'Journal Name') {
+      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
+      $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Journal Article') OR
+      (lower(PP$i.value) = lower(:crit$i))) ";
+      $args[":crit$i"] = $type_id;
+    }
+    elseif ($type_name == 'Conference Name') {
+      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
+      $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Conference Proceedings') OR
+      (lower(PP$i.value) = lower(:crit$i))) ";
+      $args[":crit$i"] = $type_id;
+    }
+    elseif ($type_name == 'Publication Type') {
+      $where .= " $op (lower(CVT.name) = lower(:crit$i))";
+      $args[":crit$i"] = $value;
+    }
+    elseif ($type_id == 0) { //'Any Field'
+      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
+      $where .= " $op (lower(PP$i.value)  LIKE lower(:crit$i) OR
+      lower(P.title) LIKE lower(:crit$i) OR
+      lower(P.volumetitle) LIKE lower(:crit$i) OR
+      lower(P.publisher) LIKE lower(:crit$i) OR
+      lower(P.uniquename) LIKE lower(:crit$i) OR
+      lower(P.pubplace) LIKE lower(:crit$i) OR
+      lower(P.miniref) LIKE lower(:crit$i) OR
+      lower(P.series_name) LIKE lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+    }
+    // for all other properties
+    else {
+      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :type_id$i ";
+      $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+      $args[":type_id$i"] = $type_id;
+    }
+  }
+  if($from_year and $to_year) {
+    $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= :from$i AND to_number(P.pyear,'9999') <= :to$i) ";
+    $args[":from$i"] = $from_year;
+    $args[":to$i"] = $to_year;
+  }
+  $sql = "$select $from $where $order  LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset;
+  $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
+  
+  // first get the total number of matches
+  $total_records = chado_query($count, $args)->fetchField();
+  $results = chado_query($sql, $args);
+  
+  return $results;
+}
+
 /**
  * This function is used to perfom a query using one of the supported databases
  * and return the raw query results. This may be XML or some other format 
@@ -82,11 +230,13 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to
  *   for the record in the database.
  *   
  * @return
- *   Returns the raw output wrapped in an HTML textarea element
+ *   Returns the raw output wrapped in an HTML textarea element or an
+ *   error message indicating if the database type is unsupported or the 
+ *   dbxref is invalid
  *
  * @ingroup tripal_pub_api
  */
-function tripal_pub_get_raw_data($dbxref) {
+function tripal_get_remote_pub_record($dbxref) {
 
   if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
     $remote_db = $matches[1];
@@ -110,9 +260,9 @@ function tripal_pub_get_raw_data($dbxref) {
        ),
       ),
     );
-    $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
+    $pubs = pub_search_remote($remote_db, $search, 1, 0);
 
-    return '<textarea cols=80 rows=20>' . $pubs[0]['raw'] . '</textarea>';
+    return $pubs[0]['raw'];
   }
   return 'Invalid DB xref';
 }
@@ -196,7 +346,7 @@ function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db
           ),
         ),
       );
-      $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
+      $pubs = pub_search_remote($remote_db, $search, 1, 0);
       tripal_pub_add_publications($pubs, $do_contact, TRUE);
 
       $i++;
@@ -257,7 +407,7 @@ function tripal_pub_import_publications_by_import_id($import_id, $job_id = NULL)
     $total_pubs = 0;
     do {
       // retrieve the pubs for this page. We'll retreive 100 at a time
-      $results  = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
+      $results  = pub_search_remote($remote_db, $criteria, $num_to_retrieve, $page);
       $pubs     = $results['pubs'];
       $num_pubs = $rseults['total_records'];
       $total_pubs += $num_pubs;
@@ -332,7 +482,7 @@ function tripal_pub_import_publications($report_email = FALSE, $do_update = FALS
       $remote_db = $criteria['remote_db'];
       do {
         // retrieve the pubs for this page. We'll retreive 100 at a time
-        $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
+        $results = pub_search_remote($remote_db, $criteria, $num_to_retrieve, $page);
         $pubs = $results['pubs'];
         $reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
         $page++;
@@ -435,7 +585,7 @@ function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_updat
         ),
       );
       $remote_db = $criteria['remote_db'];
-      $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
+      $results = pub_search_remote($remote_db, $criteria, $num_to_retrieve, $page);
       $pubs          = $results['pubs'];
       $search_str    = $results['search_str'];
       $total_records = $results['total_records'];
@@ -527,80 +677,6 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
   return $report;
 }
 
-/**
- * Adds a database cross-reference to a publication
- *
- * @param $pub_id
- *   The ID of the publication   
- * @param $pub_dbxref
- *   The cross reference.  This value must be of the format DB_NAME:ACCESSION 
- *   where DB_NAME is the name of the database and the 
- *   ACCESSION is the unique identifier for the record in the database.
- *
- * @return
- *
- * @ingroup tripal_pub_api
- */
-function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
-
-  // break apart the dbxref
-  $dbname = '';
-  $accession = '';
-  if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
-    $dbname = $matches[1];
-    $accession = $matches[2];
-  }
-  else {
-    return FALSE;
-  }
-
-  // check to see if the pub_dbxref record already exist
-  $values = array(
-    'dbxref_id' => array(
-      'accession' => $accession,
-      'db_id' => array(
-        'name' => $dbname,
-      ),
-    ),
-    'pub_id' => $pub_id,
-  );
-  $options = array('statement_name' => 'sel_pubdbxref_dbpu');
-  $results = chado_select_record('pub_dbxref', array('*'), $values, $options);
-
-  // if the pub_dbxref record  exist then we don't need to re-add it.
-  if(count($results) > 0) {
-    return $results[0];
-  }
-
-  // make sure our database already exists
-  $db = tripal_db_add_db($dbname);
-
-  // get the database cross-reference
-  $dbxvalues = array(
-    'accession' => $accession,
-    'db_id' => $db->db_id,
-  );
-  $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
-  $results = chado_select_record('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
-  // if the accession doesn't exist then add it
-  if(count($results) == 0){
-    $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
-  }
-  else {
-    $dbxref = $results[0];
-  }
-
-  // now add the record
-  $options = array('statement_name' => 'ins_pubdbxref_dbpu');
-  $results = chado_insert_record('pub_dbxref', $values, $options);
-  if (!$results) {
-    tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot add publication dbxref: %db:%accession.",
-    array('%db' => $dbname, '%accession' => $accession));
-    return FALSE;
-  }
-  return $results;
-}
-
 /**
  * Returns the list of publications that are assigned the database
  * cross-reference provided

+ 27 - 11
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -241,9 +241,14 @@ function chado_pub_form($node, $form_state) {
   $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') {
-      $select_options[$prop->cvterm_id] = $prop->name;
+    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(
@@ -279,6 +284,7 @@ function chado_pub_form($node, $form_state) {
   // Adds the form elements to your current form
   chado_add_node_form_relationships($form, $form_state, $details);
 
+  
   // ADDITIONAL DBXREFS FORM
   //---------------------------------------------
   $details = array(
@@ -719,9 +725,14 @@ function chado_pub_insert($node) {
     chado_update_node_form_relationships($node, $details);
 
     // add in any database cross-references
-    foreach ($cross_refs as $index => $ref) {
-      $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
-      if (!$pub_dbxref) {
+    foreach ($cross_refs as $index => $ref) { 
+      $dbxref = array();
+      if(preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
+        $dbxref['db_name']   = $matches[1];
+        $dbxref['accession'] = $matches[2];
+      }
+      $success = chado_associate_dbxref('pub', $pub['pub_id'], $dbxref);
+      if (!$success) {
         drupal_set_message("Error cannot add publication cross reference: $ref", "error");
         watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
         array('%ref' => $ref), WATCHDOG_ERROR);
@@ -905,25 +916,30 @@ function chado_pub_update($node) {
   // now add in the properties by first removing any the publication
   // already has and adding the ones we have
   $details = array(
-    'property_table' => 'pubprop',
-    'base_table' => 'pub',
+    'property_table'  => 'pubprop',
+    'base_table'      => 'pub',
     'foreignkey_name' => 'pub_id',
-    'foreignkey_value' => $pub_id
+    'foreignkey_value'=> $pub_id
   );
   chado_update_node_form_properties($node, $details, $properties);
 
   // * Relationships Form *
   $details = array(
     'relationship_table' => 'pub_relationship',  // name of the _relationship table
-    'foreignkey_value' => $pub_id                // value of the pub_id key
+    'foreignkey_value'   => $pub_id                // value of the pub_id key
   );
   chado_update_node_form_relationships($node, $details);
 
   // add in any database cross-references after first removing
   chado_delete_record('pub_dbxref', array('pub_id' => $pub_id));
   foreach ($cross_refs as $index => $ref) {
-    $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
-    if (!$pub_dbxref) {
+    $dbxref = array();
+    if(preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
+      $dbxref['db_name']   = $matches[1];
+      $dbxref['accession'] = $matches[2];
+    }
+    $success = chado_associate_dbxref('pub', $pub_id, $dbxref);
+    if (!$success) {
       drupal_set_message("Error cannot add publication cross reference: $ref", "error");
       watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
       array('%ref' => $ref), WATCHDOG_ERROR);

+ 1 - 1
tripal_pub/includes/tripal_pub.pub_importers.inc

@@ -132,7 +132,7 @@ function tripal_pub_importer_setup_page($action = 'new', $pub_import_id = NULL)
 
       // get the list of publications from the remote database using the search criteria.
       $page = isset($_GET['page']) ? $_GET['page'] : '0';
-      $results = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $page);
+      $results = pub_search_remote($remote_db, $search_array, $limit, $page);
       $total_records = $results['total_records'];
       $search_str    = $results['search_str'];
       $pubs          = $results['pubs'];

+ 6 - 125
tripal_pub/includes/tripal_pub.pub_search.inc

@@ -39,11 +39,12 @@ function tripal_pub_search_page() {
     }
 
     // get the list of publications from the remote database using the search criteria.
-    $pubs = tripal_pub_get_search_results($search_array, $limit);
     $page = isset($_GET['page']) ? $_GET['page'] : '0';
-    $total_records = chado_pager_get_count(0);
-    $total_pages = (int) ($total_records / $limit) + 1;
-
+    $offset = $page * $limit;
+    $total_records = 0;
+    $pubs = pub_search($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;
@@ -98,7 +99,6 @@ function tripal_pub_search_page() {
     $results = theme_table($table);
 
     // generate the pager
-    pager_default_initialize($total_records, $limit);
     $pager = array(
       'tags' => array(),
       'element' => 0,
@@ -108,8 +108,7 @@ function tripal_pub_search_page() {
     $pager = theme_pager($pager);
 
     // join all to form the results
-    $output .= "<br><p><b>Found " . number_format($total_records) .
-      ". Page " . ($page + 1) . " of $total_pages. " .
+    $output .= "<p><b>Found " . number_format($total_records) .
       " Results</b></br>" . $results . $pager;
   }
   return $output;
@@ -452,125 +451,7 @@ function tripal_pub_search_form_submit($form, &$form_state) {
     unset($_SESSION['tripal_pub_search_form']);
   }
 }
-/**
- * Builds the SQL statement need to search Chado for the publications
- * that match the user supplied criteria
- *
- * @param $search_array
- *   An array of search criteria provided by the user
- * @param $limit
- *   The numbe of records to retrieve
- *
- * @ingroup tripal_pub
- */
-function tripal_pub_get_search_results($search_array, $limit) {
-
-  // build the SQL based on the criteria provided by the user
-  $select = "SELECT DISTINCT P.*, CP.nid ";
-  $from   = "FROM {pub} P
-               LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
-               INNER JOIN {cvterm} CVT on CVT.cvterm_id = P.type_id
-            ";
-  $where  = "WHERE (NOT P.title = 'null') "; // always exclude the dummy pub
-  $order  = "ORDER BY P.pyear DESC, P.title ASC";
-  $args = array();  // arguments for where clause
-  $join = 0;
-
-  $num_criteria = $search_array['num_criteria'];
-  $from_year    = $search_array['from_year'];
-  $to_year      = $search_array['to_year'];
-
-  for ($i = 1; $i <= $num_criteria; $i++) {
-    $value = $search_array['criteria'][$i]['search_terms'];
-    $type_id = $search_array['criteria'][$i]['scope'];
-    $mode = $search_array['criteria'][$i]['mode'];
-    $op = $search_array['criteria'][$i]['operation'];
-
-    // skip criteria with no values
-    if(!$value) {
-      continue;
-    }
-
-    // to prevent SQL injection make sure our operator is
-    // what we expect
-    if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
-      $op = 'AND';
-    }
-    if ($op == 'NOT') {
-      $op = 'AND NOT';
-    }
-    if (!$op) {
-      $op = 'AND';
-    }
 
-    // get the scope type
-    $values = array('cvterm_id' => $type_id);
-    $cvterm = chado_select_record('cvterm', array('name'), $values);
-    $type_name = '';
-    if (count($cvterm) > 0) {
-      $type_name = $cvterm[0]->name;
-    }
-    if ($type_name == 'Title') {
-      $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
-      $args[":crit$i"] = '%' . $value . '%';
-    }
-    elseif ($type_name == 'Year') {
-      $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
-      $args[":crit$i"] = '%' . $value . '%';
-    }
-    elseif ($type_name == 'Volume') {
-      $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
-      $args[":crit$i"] = '%' . $value . '%';
-    }
-    elseif ($type_name == 'Issue') {
-      $where .= " $op (lower(P.issue) = lower(:crit$i)) ";
-      $args[":crit$i"] = '%' . $value . '%';
-    }
-    elseif ($type_name == 'Journal Name') {
-      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
-      $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Journal Article') OR
-                       (lower(PP$i.value) = lower(:crit$i))) ";
-      $args[":crit$i"] = $type_id;
-    }
-    elseif ($type_name == 'Conference Name') {
-      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
-      $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Conference Proceedings') OR
-                       (lower(PP$i.value) = lower(:crit$i))) ";
-      $args[":crit$i"] = $type_id;
-    }
-    elseif ($type_name == 'Publication Type') {
-      $where .= " $op (lower(CVT.name) = lower(:crit$i))";
-      $args[":crit$i"] = $value;
-    }
-    elseif ($type_id == 0) { //'Any Field'
-      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
-      $where .= " $op (lower(PP$i.value)  LIKE lower(:crit$i) OR
-                       lower(P.title) LIKE lower(:crit$i) OR
-                       lower(P.volumetitle) LIKE lower(:crit$i) OR
-                       lower(P.publisher) LIKE lower(:crit$i) OR
-                       lower(P.uniquename) LIKE lower(:crit$i) OR
-                       lower(P.pubplace) LIKE lower(:crit$i) OR
-                       lower(P.miniref) LIKE lower(:crit$i) OR
-                       lower(P.series_name) LIKE lower(:crit$i)) ";
-      $args[":crit$i"] = '%' . $value . '%';
-    }
-    // for all other properties
-    else {
-      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :type_id$i ";
-      $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i)) ";
-      $args[":crit$i"] = '%' . $value . '%';
-      $args[":type_id$i"] = $type_id;
-    }
-  }
-  if($from_year and $to_year) {
-    $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= :from$i AND to_number(P.pyear,'9999') <= :to$i) ";
-    $args[":from$i"] = $from_year;
-    $args[":to$i"] = $to_year;
-  }
-  $sql = "$select $from $where $order";
-  $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
-  return chado_pager_query($sql, $args, $limit, 0, $count);
-}
 
 /**
  * Ajax callback to update the form