Procházet zdrojové kódy

Got pager working with Drupal 7 & Chado API. The feature browser on the organism page now works

Stephen Ficklin před 11 roky
rodič
revize
c0649fd593

+ 23 - 71
tripal_core/api/tripal_core_chado.api.inc

@@ -942,32 +942,20 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
   $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
 
   if (!is_array($values)) {
-    tripal_core_report_error(
-      'tripal_core',
-      TRIPAL_ERROR,
-      'Cannot pass non array as values for selecting.',
-      array(),
-      array('print' => $print_errors)
+    tripal_core_report_error('tripal_core', TRIPAL_ERROR, 'Cannot pass non array as values for selecting.',
+      array(), array('print' => $print_errors)
     );
     return FALSE;
   }
   if (!is_array($columns)) {
-    tripal_core_report_error(
-      'tripal_core',
-      TRIPAL_ERROR,
-      'Cannot pass non array as columns for selecting.',
-      array(),
-      array('print' => $print_errors)
+    tripal_core_report_error('tripal_core', TRIPAL_ERROR, 'Cannot pass non array as columns for selecting.',
+      array(), array('print' => $print_errors)
     );
     return FALSE;
   }
   if (count($columns)==0) {
-    tripal_core_report_error(
-      'tripal_core',
-      TRIPAL_ERROR,
-      'Cannot pass an empty array as columns for selecting.',
-      array(),
-      array('print' => $print_errors)
+    tripal_core_report_error('tripal_core', TRIPAL_ERROR, 'Cannot pass an empty array as columns for selecting.',
+      array(), array('print' => $print_errors)
     );
     return FALSE;
   }
@@ -1230,13 +1218,12 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
     $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing ', '
   }
 
-  // if the caller has requested the SQL rather than the results...
-  // which happens in the case of wanting to use the Drupal pager, then do so
+  // if the caller has requested the SQL rather than the results then do so
   if ($options['return_sql'] == TRUE) {
     return array('sql' => $sql, 'args' => $args);
   }
   if (array_key_exists('limit', $pager)) {
-    $resource = chado_pager_query($sql, $pager['limit'], $pager['element'], NULL, $args);
+    $resource = chado_pager_query($sql, $args, $pager['limit'], $pager['element']);
   }
   else {
     $resource = chado_query($sql, $args);
@@ -2036,77 +2023,42 @@ function tripal_core_exclude_field_from_feature_by_default() {
  * @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 $args
+ *   The array of arguments for the query. They keys are the placeholders
  * @param $limit
  *   The number of query results to display per page.
  * @param $element
  *   An optional integer to distinguish between multiple pagers on one page.
  * @param $count_query
  *   An SQL query used to count matching records.
- *
+ *   
  * @returns
  *   A database query result resource or FALSE if the query was not
  *   executed correctly
  *
  * @ingroup tripal_chado_api
  */
-function chado_pager_query($query, $limit, $element, $count_query) {
-
-  // 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
+function chado_pager_query($query, $args, $limit, $element, $count_query = NULL) {
 
-  global $pager_page_array, $pager_total, $pager_total_items;
-  $page = isset($_GET['page']) ? $_GET['page'] : '';
-
-  // get the SQL query arguments that get substituted into modifiers later.
-  $args = func_get_args();
-  $args = array_slice($args, 4);
-  // Alternative syntax for '...'
-  if (isset($args[0]) && is_array($args[0])) {
-    $args = $args[0];
-  }
+  $page = pager_find_page();
+  $offset = $limit * $page;
 
   // 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);
+    $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);
+  $pager_total_items = chado_query($count_query, $args)->fetchField();
+  
+  pager_default_initialize($pager_total_items, $limit, $element);
+  
+  $query .= ' LIMIT ' . (int) $limit . ' OFFSET ' . (int) $offset;
+  $results = chado_query($query, $args);
+  return $results;
 }
 
-/**
- * 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 $args
- *   The SQL arguments
- * @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
- *
- * @ingroup tripal_chado_api
- */
-function chado_query_range($query, $args, $from, $count) {
-
-  $query .= ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from;
-
-  return chado_query($query, $args);
-}
 /**
  * Use this function instead of db_query() to avoid switching databases
  * when making query to the chado database

+ 0 - 1
tripal_core/theme/css/tripal.css

@@ -237,7 +237,6 @@ table.tripal-table-horz .tripal-table-odd-row {
 }
 .tripal-teaser-img {
   width: 100px; 
-  height: 100px;
   float: left; 
   padding-right: 10px; 
   padding-bottom: 5px;

+ 125 - 44
tripal_feature/theme/tripal_organism/tripal_organism_feature_browser.tpl.php

@@ -1,13 +1,57 @@
 <?php
 
 $organism = $variables['node']->organism;
+
+// don't show the browser if the settings in the admin page is turned off
+// instead return the array indicating the status of the browser
 $enabled = 1;
-$features = array();
+$show_browser = variable_get('tripal_feature_browse_setting', 'show_feature_browser');
+if (strcmp($show_browser, 'show_feature_browser') !=0 ) {
+  $enabled = 0;
+}
+
+// get the list of available sequence ontology terms for which
+// we will build drupal pages from features in chado.  If a feature
+// is not one of the specified typse we won't build a node for it.
+$allowed_types = variable_get('chado_browser_feature_types', 'gene mRNA');
+$allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
+$so_terms = explode(' ', $allowed_types);
 
-if(property_exists($organism, 'feature_browser')) {
-  $features = $organism->feature_browser['features'];
-  $pager    = $organism->feature_browser['pager'];
-  $enabled  = $organism->feature_browser['enabled'];
+// get the feature_id's of the feature that belong to this organism.  But we only
+// want 25 and we want a pager to let the user cycle between pages of features.
+// so we, use the tripal_core_chado_select API function to get the results and
+// generate the pager.  The function is smart enough to know which page the user is
+// on and retrieves the proper set of features
+$element = 0;        // an index to specify the pager if more than one is on the page
+$num_per_page = 25;  // the number of features to show per page
+$values = array(
+  'organism_id' => $organism->organism_id,
+  'type_id' => array(
+    'name' => $so_terms
+  ),
+);
+$columns = array('feature_id');
+$options = array(
+  'pager' => array(
+    'limit' => $num_per_page, 
+    'element' => $element
+   ),
+  'order_by' => array('name' => 'ASC'),
+);
+$results = tripal_core_chado_select('feature', $columns, $values, $options);
+
+// now that we have all of the feature IDs, we want to expand each one so that we
+// have all of the neccessary values, including the node ID, if one exists, and the
+// cvterm type name.
+$features = array();
+foreach ($results as $result) {
+  $values = array('feature_id' => $result->feature_id);
+  $options = array(
+    'include_fk' => array(
+      'type_id' => 1
+    )
+  );
+  $features[] = tripal_core_generate_chado_var('feature', $values, $options);
 }
 
 // only show this block if it is enabled
@@ -16,40 +60,71 @@ if ($enabled) {
     <div id="tripal_organism-feature_browser-box" class="tripal_organism-info-box tripal-info-box">
       <div class="tripal_organism-info-box-title tripal-info-box-title">Feature Browser</div>
       <div class="tripal_organism-info-box-desc tripal-info-box-desc">The following browser provides a quick view for new visitors.  Use the searching mechanism to find specific features.</div> <?php
+      
+      // the $headers array is an array of fields to use as the colum headers.
+      // additional documentation can be found here
+      // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+      $headers = array('Feature Name' ,'Unique Name', 'Type');
+      
+      // the $rows array contains an array of rows where each row is an array
+      // of values for each column of the table in that row.  Additional documentation
+      // can be found here:
+      // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+      $rows = array();
+      
       // let admins know they can customize the terms that appear in the list
       if (user_access('access administration pages')) { ?>
-         <div class="tripal-no-results">Administrators, to disable this browser and 
-         remove it from the list of resources, navigate to the <a href="<?php print url('admin/tripal/tripal_feature/configuration') ?>">Tripal feature configuration page</a> 
-         and hide the "Feature Browser".
+         <div class="tripal-no-results">Administrators, you can specify the feature types
+         that should appear in this browser or remove it from the list of resources by navigating to the 
+         <?php print l("Tripal feature settings page", "admin/tripal/chado/tripal_feature/configuration", array('attributes' => array('target' => '_blank'))) ?>.  
          </div><?php 
-      }?>
-      <table id="tripal_organism-table-feature_browser" class="tripal_organism-table tripal-table tripal-table-horz">     
-        <tr class="tripal_organism-table-odd-row tripal-table-even-row">
-          <th>Feature Name</th>
-          <th>Unique Name</th>
-          <th>Type</th>
-        </tr> <?php
-        foreach ($features as $feature){ 
-          $class = 'tripal_organism-table-odd-row tripal-table-odd-row';
-          if ($i % 2 == 0 ) {
-            $class = 'tripal_organism-table-odd-row tripal-table-even-row';
-          } ?>
-          <tr class="<?php print $class ?>">
-            <td><?php 
-              if ($feature->nid) {    
-                $link =   url("node/$feature->nid");        
-                print "<a href=\"$link\">$feature->name</a>";
-              } else {
-                print $feature->name;
-              }?>
-            </td>
-            <td><?php print $feature->uniquename?></td>
-            <td><?php print $feature->type_name?></td>
-          </tr><?php
-          $i++;  
-        } ?>
-      </table>
-      <?php print $pager ?>
+      }
+      
+      foreach ($features as $feature){
+        $fname =  $feature->name;
+        if (property_exists($feature, 'nid')) {
+          $fname =   l($fname, "node/$feature->nid", array('attributes' => array('target' => '_blank')));
+        }
+        $rows[] = array(
+          $fname,
+          $feature->uniquename,
+          $feature->type_id->name
+        );
+      } 
+      // the $table array contains the headers and rows array as well as other
+      // options for controlling the display of the table.  Additional
+      // documentation can be found here:
+      // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+      $table = array(
+        'header' => $headers,
+        'rows' => $rows,
+        'attributes' => array(
+          'id' => 'tripal_feature-table-analyses',
+        ),
+        'sticky' => FALSE,
+        'caption' => '',
+        'colgroups' => array(),
+        'empty' => '',
+      );
+      // once we have our table array structure defined, we call Drupal's theme_table()
+      // function to generate the table.
+      print theme_table($table);
+      
+      // the $pager array values that control the behavior of the pager.  For 
+      // documentation on the values allows in this array see:
+      // https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_pager/7
+      // here we add the paramter 'block' => 'feature_browser'. This is because
+      // the pager is not on the default block that appears. When the user clicks a
+      // page number we want the browser to re-appear with the page is loaded.
+      $pager = array(
+        'tags' => array(),
+        'element' => $element,
+        'parameters' => array(
+          'block' => 'feature_browser'
+        ),
+        'quantity' => $num_per_page,
+      );
+      print theme_pager($pager); ?>
     </div> <?php
   } 
   else {
@@ -62,17 +137,23 @@ if ($enabled) {
           There are no features available for browsing
           <p><br>Administrators, perform the following to show features in this browser:
           <ul>
-            <li>Load features for this organism using the <a href="<?php print url('admin/tripal/tripal_feature/fasta_loader');?>">FASTA loader</a>, <a href="<?php print url('admin/tripal/tripal_feature/gff3_load');?>">GFF loader</a> or <a href="<?php print url('admin/tripal/tripal_bulk_loader_template');?>">Bulk Loader</a>.</li>
-            <li>Sync the features that should have pages using the <a href="<?php print url('admin/tripal/tripal_feature/sync');?>">Sync Features</a> tool.</li>
+            <li>Load features for this organism using the 
+            <?php print l("FASTA loader", 'admin/tripal/loaders/fasta_loader'); ?>, 
+            <?php print l("GFF Loader",   'admin/tripal/loaders/gff3_load'); ?> or 
+            <?php print l("Bulk Loader",  'admin/tripal/loaders/bulk'); ?></li>
+            <li>Sync the features that should have pages using the 
+            <?php print l("Sync features page", 'admin/tripal/chado/tripal_feature/sync');?></li>
             <li>Return to this page to browse features.</li>
-            <li>Ensure the user <a href="<?php print url('admin/user/permissions'); ?>"> has permission</a> to view the feature content</li>
+            <li>Ensure the user 
+            <?php print l("has permission", 'admin/people/permissions'); ?> to view the feature content</li>
           </ul> 
-          <br><br>To disable this browser and remove it from the list of resources:
-          <ul>
-            <li>Navigate to the <a href="<?php print url('admin/tripal/tripal_feature/configuration') ?>">Tripal feature configuration page</a> and hide the "Feature Browser"</li>
-          </ul>
+          <br>
+          <br>
+          You can specify the feature types
+          that should appear in this browser or remove it from the list of resources by navigating to the 
+          <?php print l("Tripal feature settings page", "admin/tripal/chado/tripal_feature/configuration", array('attributes' => array('target' => '_blank'))) ?>.
           </p>
-          This page will not appear to site visitors unless features are present.
+          The feature browser will not appear to site visitors unless features are present.  These instructions only appear to site administrators.
         </div>         
       </div><?php
     }

+ 0 - 1
tripal_feature/theme/tripal_organism/tripal_organism_feature_counts.tpl.php

@@ -9,7 +9,6 @@ if(property_exists($organism, 'feature_counts')) {
   $names    = $organism->feature_counts['names'];
   $enabled  = $organism->feature_counts['enabled'];
 }
-dpm($organism);
 
 // only show this block if it is enabled
 if ($enabled) { 

+ 0 - 82
tripal_feature/tripal_feature.module

@@ -1350,58 +1350,6 @@ function tripal_feature_load_organism_feature_counts($organism) {
   }
   return array( 'types' => $ordered_types, 'names' => $names, 'enabled' => TRUE );
 }
-/**
- *
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_load_organism_feature_browser($organism) {
-
-  if (!$organism) {
-    return array();
-  }
-
-  // don't show the browser if the settings in the admin page is turned off
-  // instead return the array indicating the status of the browser
-  $show_browser = variable_get('tripal_feature_browse_setting', 'show_feature_browser');
-  if (strcmp($show_browser, 'show_feature_browser')!=0) {
-    return array('enabled' => FALSE);
-  }
-
-  // get the list of available sequence ontology terms for which
-  // we will build drupal pages from features in chado.  If a feature
-  // is not one of the specified typse we won't build a node for it.
-  $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
-  $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
-  $so_terms = split(' ', $allowed_types);
-
-  // 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');
-
-  // add the node ids and types
-  $nsql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
-  $tsql = "SELECT name FROM {cvterm} WHERE cvterm_id = :cvterm_id";
-  foreach ($features as $feature) {
-    $node = db_query($nsql, array(':feature_id' => $feature->feature_id))->fetchObject();
-    $type = chado_query($tsql, array(':cvterm_id' => $feature->type_id))->fetchObject();
-    $feature->nid = $node->nid;
-    $feature->type_name = $type->name;
-  }
-
-  return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
-}
 
 /**
  * This generates the Feature Browser which can optionally be included on library pages
@@ -1967,36 +1915,6 @@ function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables, $
   $organism->feature_counts = tripal_feature_load_organism_feature_counts($organism);
 }
 
-/**
- *
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_preprocess_tripal_organism_feature_browser(&$variables, $hook) {
-  $organism = $variables['node']->organism;
-  $organism->feature_browser = tripal_feature_load_organism_feature_browser($organism);
-}
-
-/**
- * Preprocessor function for the Library Feature Browser
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_preprocess_tripal_library_feature_browser(&$variables) {
-  $library = $variables['node']->library;
-  $library->feature_browser = tripal_feature_load_library_feature_browser($library);
-}
-
-/**
- * Preprocessor function for the Analysis Feature Browser
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_preprocess_tripal_analysis_feature_browser(&$variables) {
-  $analysis = $variables['node']->analysis;
-  $analysis->feature_browser = tripal_feature_load_analysis_feature_browser($analysis);
-}
-
 /**
  *
  *