Browse Source

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

Lacey Sanderson 12 years ago
parent
commit
a60ffe8836

+ 128 - 16
tripal_core/api/tripal_core.api.inc

@@ -1121,7 +1121,14 @@ function tripal_core_chado_delete($table, $match, $options = NULL) {
  *  - is_duplicate: TRUE or FALSE.  Checks the values submited to see if
  *     they violate any of the unique constraints. If so, the record
  *     is returned, if not, FALSE is returned.
- *
+ *  - pager:  Use this option if it is desired to return only a subset of results 
+ *     so that they may be shown with in a Drupal-style pager. This should be
+ *     an array with two keys: 'limit' and 'element'.  The value of 'limit' 
+ *     should specify the number of records to return and 'element' is a 
+ *     unique integer to differentiate between pagers when more than one
+ *     appear on a page.  The 'element' should start with zero and increment by
+ *     one for each pager.  The pager currently does not work with prepared queries
+ *     (when using the -statement_name option).
  *
  * @return
  *  A database query result resource, FALSE if the query was not executed
@@ -1174,7 +1181,6 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
       WATCHDOG_ERROR);
       return FALSE;
   }
-
   if (!is_array($columns)) {
     watchdog('tripal_core', 'Cannot pass non array as columns for selecting.', array(),
       WATCHDOG_ERROR);
@@ -1215,6 +1221,10 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
   if (!array_key_exists('is_duplicate', $options)) {
     $options['is_duplicate'] = FALSE;
   }
+  $pager = array();
+  if (array_key_exists('pager', $options)) {
+    $pager = $options['pager'];
+  }
 
   // if this is a prepared statement check to see if it has already been prepared
   $prepared = FALSE;
@@ -1548,12 +1558,16 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
       }
     }
     $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $itypes) . ")";
-    // WARNING: This call creates a memory leak:  if you remove the $pvalues it doesn't
-    // do this. Got to find out what's causing this.
+    // TODO: make the pager option work with prepared queries.
     $resource = tripal_core_chado_execute_prepared($options['statement_name'], $sql, $pvalues);
   }
   else {
-    $resource = chado_query($sql, $args);
+    if (array_key_exists('limit', $pager)) {
+      $resource = chado_pager_query($sql, $pager['limit'], $pager['element'], NULL, $args);
+    } 
+    else {
+      $resource = chado_query($sql, $args);
+    }     
   }
 
   // format results into an array
@@ -1730,6 +1744,14 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
  *
  *     The above array will expand the 'type_id' of the property table but only
  *     further expand the cv_id and the dbxref_id and will go no further.
+ *   - pager:  
+ *     Use this option if it is desired to return only a subset of results 
+ *     so that they may be shown within a Drupal-style pager. This should be
+ *     an array with two keys: 'limit' and 'element'.  The value of 'limit' 
+ *     should specify the number of records to return and 'element' is a 
+ *     unique integer to differentiate between pagers when more than one
+ *     appear on a page.  The 'element' should start with zero and increment by
+ *     one for each pager.  This only works when type is a 'table'.
  * @return
  *   Either an object (if only one record was selected from the base table)
  *   or an array of objects (if more than one record was selected from the base table).
@@ -1737,10 +1759,10 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
  *
  * Example Usage:
  * @code
- $values = array(
-   'name' => 'Medtr4g030710'
- );
- $features = tripal_core_generate_chado_var('feature', $values);
+   $values = array(
+     'name' => 'Medtr4g030710'
+   );
+   $features = tripal_core_generate_chado_var('feature', $values);
  * @endcode
  * This will return an object if there is only one feature with the name Medtr4g030710 or it will
  * return an array of feature objects if more than one feature has that name.
@@ -1751,9 +1773,9 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
  *      This hook allows you to add fields to be excluded on a per table basis. Simply implement
  *      this hook to return an array of fields to be excluded. For example:
  * @code
- mymodule_exclude_field_from_feature_by_default() {
-   return array('residues' => TRUE);
- }
+   mymodule_exclude_field_from_feature_by_default() {
+     return array('residues' => TRUE);
+   }
  * @endcode
  *      will ensure that feature.residues is ecluded from a feature object by default.
  *  - hook_exclude_type_by_default:
@@ -1762,9 +1784,9 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
  *      Then all fields of that type where the criteria supplied returns TRUE will be excluded from
  *      any table. Tokens available in criteria are >field_value<  and >field_name< . For example:
  * @code
- mymodule_exclude_type_by_default() {
-   return array('text' => 'length(>field_value< ) > 50');
- }
+   mymodule_exclude_type_by_default() {
+     return array('text' => 'length(>field_value< ) > 50');
+   }
  * @endcode
  *      will exclude all text fields with a length > 50. Thus if $feature.residues is longer than 50 *      it will be excluded, otherwise it will be added.
  *
@@ -1781,7 +1803,10 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
   if (array_key_exists('include_fk', $base_options)) {
     $include_fk = $base_options['include_fk'];
   }
-
+  $pager = array();
+  if (array_key_exists('pager', $base_options)) {
+    $pager = $base_options['pager'];
+  }
   // get description for the current table----------------------------------------------------------
   $table_desc = tripal_core_get_chado_table_schema($table);
   if (!$table_desc or count($table_desc) == 0) {
@@ -2042,6 +2067,14 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
  *
  *     The above array will expand the 'type_id' of the property table but only
  *     further expand the cv_id and the dbxref_id and will go no further.
+ *   - pager:  
+ *     Use this option if it is desired to return only a subset of results 
+ *     so that they may be shown within a Drupal-style pager. This should be
+ *     an array with two keys: 'limit' and 'element'.  The value of 'limit' 
+ *     should specify the number of records to return and 'element' is a 
+ *     unique integer to differentiate between pagers when more than one
+ *     appear on a page.  The 'element' should start with zero and increment by
+ *     one for each pager.  This only works when type is a 'table'.
  * @return
  *   A chado object supplemented with the field/table/node requested to be expanded.
  *   If the type is a table and it has already been expanded no changes is made to the
@@ -2313,6 +2346,85 @@ function tripal_core_exclude_field_from_feature_by_default() {
   return array();
 }
 
+/**
+ * Use this function instead of pager_query() when selecting a
+ * subset of records from a Chado table.  
+ *
+ * @param $query
+ *   The SQL statement to execute, this is followed by a variable number of args
+ *   used as substitution values in the SQL statement.
+ * @param $limit
+ *   The number of query results to display per page.
+ * @param $element
+ *   An optional integer to distinguish between multiple pagers on one page.
+ *
+ * @returns
+ *   A database query result resource or FALSE if the query was not
+ *   executed correctly
+ */
+function chado_pager_query($query, $limit, $element, $count) {
+  
+  // The following code is almost an exact duplicate of the 
+  // Drupal pager_query function. However, substitions have
+  // been made to call chado_query rather than db_query
+  
+  global $pager_page_array, $pager_total, $pager_total_items;
+  $page = isset($_GET['page']) ? $_GET['page'] : '';
+
+  // Substitute in query arguments.
+  $args = func_get_args();
+  $args = array_slice($args, 4);
+  // Alternative syntax for '...'
+  if (isset($args[0]) && is_array($args[0])) {
+    $args = $args[0];
+  }
+
+  // Construct a count query if none was given.
+  if (!isset($count_query)) {
+    $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
+  }
+
+  // Convert comma-separated $page to an array, used by other functions.
+  $pager_page_array = explode(',', $page);
+
+  // We calculate the total of pages as ceil(items / limit).
+  $pager_total_items[$element] = db_result(chado_query($count_query, $args));
+  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
+  $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], ((int) $pager_total[$element]) - 1));  
+  
+  return chado_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
+}
+
+/**
+ * Use this function instead of db_query_range().
+ *
+ * @param $sql
+ *   The SQL statement to execute, this is followed by a variable number of args
+ *   used as substitution values in the SQL statement.
+ * @param $from
+ *   The first result row to return..
+ * @param $count
+ *   The maximum number of result rows to return.
+ *
+ * @returns
+ *   A database query result resource or FALSE if the query was not
+ *   executed correctly
+ */
+function chado_query_range($query) {
+  $args = func_get_args();
+  $count = array_pop($args);
+  $from = array_pop($args);
+  array_shift($args);
+
+  $query = db_prefix_tables($query);
+  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
+    $args = $args[0];
+  }
+  _db_query_callback($args, TRUE);
+  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+  $query .= ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from;  
+  return chado_query($query);
+}
 /**
  * Use this function instead of db_query() to avoid switching databases
  * when making query to the chado database

+ 92 - 101
tripal_feature/tripal_feature.module

@@ -298,6 +298,76 @@ function tf_node_load($nid) {
   }
   return FALSE;
 }
+/**
+ *  We need to let drupal know about our theme functions and their arguments.
+ *  We create theme functions to allow users of the module to customize the
+ *  look and feel of the output generated in this module
+ *
+ * @ingroup tripal_feature
+ */
+function tripal_feature_theme() {
+  return array(
+    'tripal_feature_search_index' => array(
+      'arguments' => array('node'),
+    ),
+    'tripal_feature_search_results' => array(
+       'arguments' => array('node'),
+    ),
+    'tripal_organism_feature_browser' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_organism_feature_browser',
+    ),
+    'tripal_organism_feature_counts' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_organism_feature_counts',
+    ),
+    'tripal_feature_base' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_base',
+    ),
+    'tripal_feature_sequence' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_sequence',
+    ),
+    'tripal_feature_synonyms' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_synonyms',
+    ),
+    'tripal_feature_featureloc_sequences' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_featureloc_sequences',
+    ),
+    'tripal_feature_references' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_references',
+    ),
+    'tripal_feature_properties' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_properties',
+    ),
+    'tripal_feature_terms' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_terms',
+    ),
+    'tripal_feature_alignments' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_alignments',
+    ),
+    'tripal_feature_relationships' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_feature_relationships',
+    ),
+    'tripal_feature_edit_ALL_properties_form' => array(
+      'arguments' => array('form' => NULL),
+      'function' => 'theme_tripal_feature_edit_ALL_properties_form',
+    ),
+    'tripal_feature_admin' => array(
+      'template' => 'tripal_feature_admin',  
+      'arguments' =>  array(NULL),  
+      'path' => drupal_get_path('module', 'tripal_feature') . '/theme' 
+    ),
+  );
+}
 /**
  *
  *
@@ -329,7 +399,7 @@ function tripal_feature_block($op = 'list', $delta = 0, $edit=array()) {
 
       $blocks['alignments']['info'] = t('Tripal Feature Alignments');
       $blocks['alignments']['cache'] = BLOCK_NO_CACHE;
-
+      
       $blocks['relationships']['info'] = t('Tripal Feature Relationships');
       $blocks['relationships']['cache'] = BLOCK_NO_CACHE;
 
@@ -1494,40 +1564,30 @@ function tripal_feature_load_organism_feature_browser($organism) {
   $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
   $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
   $so_terms = split(' ', $allowed_types);
-  $where_cvt = "";
-  foreach ($so_terms as $term) {
-    $where_cvt .= "CVT.name = '$term' OR ";
-  }
-  $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3);  # strip trailing 'OR'
-
-  // get the features for this organism
-  $sql  = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
-         "FROM {feature} F ".
-            "  INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
-          "WHERE organism_id = %s and ($where_cvt) ".
-          "ORDER BY feature_id ASC";
-
-  // the counting SQL
-  $csql  = "SELECT count(*) ".
-          "FROM {feature} F".
-          "  INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
-          "WHERE organism_id = %s and ($where_cvt) ".
-          "GROUP BY organism_id ";
 
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $org_features = pager_query($sql, 10, 0, $csql, $organism->organism_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  // perform the query
+  $values = array(
+    'organism_id' => $organism->organism_id,
+    'type_id' => array(
+      'name' => $so_terms
+    ),
+  );
+  $columns = array('feature_id', 'name', 'uniquename', 'type_id');
+  $options = array(
+    'pager' => array('limit' => 10, 'element' => 0),
+    'order_by' => array('name' => 'ASC'),
+  );  
+  $features = tripal_core_chado_select('feature', $columns, $values, $options);
   $pager = theme('pager');
 
-  // prepare the query that will lookup node ids
-  $sql = "SELECT nid FROM {chado_feature} ".
-         "WHERE feature_id = %d";
-  $i=0;
-  $features = array();
-  while ($feature = db_fetch_object($org_features)) {
-    $node = db_fetch_object(db_query($sql, $feature->feature_id));
+  // add the node ids and types
+  $nsql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
+  $tsql = "SELECT name FROM {cvterm} WHERE cvterm_id = %d";
+  foreach ($features as $feature) {
+    $node = db_fetch_object(db_query($nsql, $feature->feature_id));
+    $type = db_fetch_object(chado_query($tsql, $feature->type_id));
     $feature->nid = $node->nid;
-    $features[$i++] = $feature;
+    $feature->type_name = $type->name;
   }
 
   return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
@@ -1850,76 +1910,7 @@ function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
   }
 }
 
-/**
- *  We need to let drupal know about our theme functions and their arguments.
- *  We create theme functions to allow users of the module to customize the
- *  look and feel of the output generated in this module
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_theme() {
-  return array(
-    'tripal_feature_search_index' => array(
-      'arguments' => array('node'),
-    ),
-    'tripal_feature_search_results' => array(
-       'arguments' => array('node'),
-    ),
-    'tripal_organism_feature_browser' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_organism_feature_browser',
-    ),
-    'tripal_organism_feature_counts' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_organism_feature_counts',
-    ),
-    'tripal_feature_base' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_base',
-    ),
-    'tripal_feature_sequence' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_sequence',
-    ),
-    'tripal_feature_synonyms' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_synonyms',
-    ),
-    'tripal_feature_featureloc_sequences' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_featureloc_sequences',
-    ),
-    'tripal_feature_references' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_references',
-    ),
-    'tripal_feature_properties' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_properties',
-    ),
-    'tripal_feature_terms' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_terms',
-    ),
-    'tripal_feature_alignments' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_alignments',
-    ),
-    'tripal_feature_relationships' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_relationships',
-    ),
-    'tripal_feature_edit_ALL_properties_form' => array(
-      'arguments' => array('form' => NULL),
-      'function' => 'theme_tripal_feature_edit_ALL_properties_form',
-    ),
-    'tripal_feature_admin' => array(
-      'template' => 'tripal_feature_admin',  
-      'arguments' =>  array(NULL),  
-      'path' => drupal_get_path('module', 'tripal_feature') . '/theme' 
-    ),
-  );
-}
+
 /**
  *
  *

+ 13 - 2
tripal_library/tripal_library.module

@@ -235,6 +235,10 @@ function tripal_library_theme() {
        'arguments' => array('node' => NULL),
        'template' => 'tripal_library_properties',
     ),
+    'tripal_library_terms' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_library_terms',
+    ),
     'tripal_library_admin' => array(
       'template' => 'tripal_library_admin',  
       'arguments' =>  array(NULL),  
@@ -696,11 +700,14 @@ function tripal_library_block($op = 'list', $delta = '0', $edit = array()) {
   switch ($op) {
     case 'list':
 
-    $blocks['libreferences']['info'] = t('Tripal Library References');
+    $blocks['libreferences']['info'] = t('Tripal Library Cross References');
     $blocks['libreferences']['cache'] = BLOCK_NO_CACHE;
 
     $blocks['libbase']['info'] = t('Tripal Library Details');
     $blocks['libbase']['cache'] = BLOCK_NO_CACHE;
+    
+    $blocks['libterms']['info'] = t('Tripal Library Terms');
+    $blocks['libterms']['cache'] = BLOCK_NO_CACHE;
 
     $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
     $blocks['libsynonyms']['cache'] = BLOCK_NO_CACHE;
@@ -724,7 +731,7 @@ function tripal_library_block($op = 'list', $delta = '0', $edit = array()) {
         $block = array();
         switch ($delta) {
           case 'libreferences':
-            $block['subject'] = t('References');
+            $block['subject'] = t('Cross References');
             $block['content'] = theme('tripal_library_references', $node);
             break;
           case 'libbase':
@@ -739,6 +746,10 @@ function tripal_library_block($op = 'list', $delta = '0', $edit = array()) {
             $block['subject'] = t('Properties');
             $block['content'] = theme('tripal_library_properties', $node);
             break;
+          case 'libterms':
+            $block['subject'] = t('Library Terms');
+            $block['content'] = theme('tripal_library_terms', $node);
+            break;
           case 'featurelibs':
             $block['subject'] = t('Libraries');
             $block['content'] = theme('tripal_feature_libraries', $node);

+ 11 - 9
tripal_natural_diversity/tripal_natural_diversity.module

@@ -33,17 +33,17 @@ function tripal_natural_diversity_theme() {
       'arguments' => array('node' => NULL),
       'template' => 'tripal_feature-genotype_experiments',
     ),
-    'tripal_stock_genotype_experiments' => array(
+    'tripal_stock_nd_genotypes' => array(
       'arguments' => array('node' => NULL),
-      'template' => 'tripal_feature-stock_experiments',
+      'template' => 'tripal_stock_nd_genotypes',
     ),
   );
 }
 
-/**
+/*
  *
  */
-function tripal_natural_diversity_nodeapi(&$node, $op, $teaser, $page) {
+function tripal_natural_diversity_nodeapi(&$node, $op, $teaser, $page){
   switch ($op) {
     case 'view':
       if ($node->type == 'chado_feature') {
@@ -51,10 +51,12 @@ function tripal_natural_diversity_nodeapi(&$node, $op, $teaser, $page) {
            '#value' => theme('tripal_feature_genotype_experiments', $node),
         );
       }
-      elseif ($node->type == 'chado_stock') {
-        $node->content['tripal_stock_genotype_experiments'] = array(
-           '#value' => theme('tripal_stock_genotype_experiments', $node),
-        );
-      }
   }
 }
+
+/*
+ * 
+ */
+function tripal_natural_diversity_preprocess_tripal_stock_nd_genotypes(&$variables){
+
+}

+ 1 - 1
tripal_project/includes/tripal_project.admin.inc

@@ -95,7 +95,7 @@ function get_tripal_project_admin_form_sync_set(&$form) {
       // if so, then skip it.
       $sql = "SELECT * FROM {chado_project} WHERE project_id = %d";
       if (!db_fetch_object(db_query($sql, $project->project_id))) {
-        $proj_boxes[$project->project_id] = "$project->genus $project->species ($project->common_name)";
+        $proj_boxes[$project->project_id] = $project->name;
         $added++;
       }
     }

+ 8 - 2
tripal_stock/tripal_stock.module

@@ -977,7 +977,10 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
   // number of relationships are present this significantly slows the
   // query, therefore we will manually perform the query
   $sql = "
-    SELECT S.name, S.uniquename, S.stock_id, CS.nid, CVT.name as rel_type, CVTs.name as obj_type
+    SELECT 
+      S.name, S.uniquename, S.stock_id, CS.nid, 
+      CVT.name as rel_type, CVTs.name as obj_type,
+      SR.value
     FROM stock_relationship SR
       INNER JOIN stock S on SR.object_id = S.stock_id
       INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
@@ -987,7 +990,10 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
   ";
   $as_subject = chado_query($sql, $stock->stock_id);
   $sql = "
-    SELECT S.name, S.uniquename,  S.stock_id, CS.nid, CVT.name as rel_type, CVTs.name as sub_type
+    SELECT 
+      S.name, S.uniquename,  S.stock_id, CS.nid, 
+      CVT.name as rel_type, CVTs.name as sub_type,
+      SR.value
     FROM stock_relationship SR
       INNER JOIN stock S on SR.subject_id = S.stock_id
       INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id