Browse Source

Chado Filter String Wrapper

Lacey Sanderson 13 years ago
parent
commit
468ff6c351

+ 6 - 6
base/tripal_analysis/views/analysis.views.inc

@@ -87,7 +87,7 @@ function retrieve_analysis_views_data() {
       'handler' => 'chado_views_handler_sort',
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter_string',
+      'handler' => 'chado_views_handler_filter_string',
     ),
     'argument' => array(
       'handler' => 'views_handler_argument_string',
@@ -105,7 +105,7 @@ function retrieve_analysis_views_data() {
       'handler' => 'chado_views_handler_sort',
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter_string',
+      'handler' => 'chado_views_handler_filter_string',
     ),
     'argument' => array(
       'handler' => 'views_handler_argument_string',
@@ -194,7 +194,7 @@ function retrieve_analysis_views_data() {
       'handler' => 'chado_views_handler_sort',
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter_string',
+      'handler' => 'chado_views_handler_filter_string',
     ),
     'argument' => array(
       'handler' => 'views_handler_argument_string',
@@ -269,7 +269,7 @@ function retrieve_analysis_views_data() {
       'handler' => 'chado_views_handler_sort',
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter_string',
+      'handler' => 'chado_views_handler_filter_string',
     ),
     'argument' => array(
       'handler' => 'views_handler_argument_string',
@@ -288,7 +288,7 @@ function retrieve_analysis_views_data() {
       'handler' => 'chado_views_handler_sort',
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter_string',
+      'handler' => 'chado_views_handler_filter_string',
     ),
     'argument' => array(
       'handler' => 'views_handler_argument_string',
@@ -307,7 +307,7 @@ function retrieve_analysis_views_data() {
       'handler' => 'chado_views_handler_sort',
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter_string',
+      'handler' => 'chado_views_handler_filter_string',
     ),
     'argument' => array(
       'handler' => 'views_handler_argument_string',

+ 5 - 3
base/tripal_core/tripal_core.views.inc

@@ -55,6 +55,7 @@ include('views/handlers/views_handler_join_chado_aggregator.inc');
  * @ingroup tripal_core
  */
 function tripal_core_views_handlers() {
+
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'tripal_core') . '/views/handlers',
@@ -119,9 +120,10 @@ function tripal_core_views_handlers() {
      'chado_views_handler_field_numeric' => array(
       'parent' => 'views_handler_field_numeric'
      ),
-     'chado_views_handler_sort' => array(
-      'parent' => 'views_handler_sort',
-     ),
+     // Filter Handlers
+     'chado_views_handler_filter_string' => array(
+      'parent' => 'views_handler_filter_string',
+     ),     
      // Sort Handlers
      'chado_views_handler_sort' => array(
       'parent' => 'views_handler_sort'

+ 65 - 0
base/tripal_core/views/handlers/chado_views_handler_filter_string.inc

@@ -0,0 +1,65 @@
+<?php
+
+class chado_views_handler_filter_string extends views_handler_filter_string {
+
+  /**
+   * Defines the options form (form available to admin when they add a field to a view)
+   */
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    
+    $form['records_with'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Filter base table records'),
+      '#description' => t('Filters '.$this->view->base_table.' to only those with the value in the aggregate array.'),
+      '#default_value' => ($this->options['records_with']) ? $this->options['records_with'] : TRUE,
+    );
+    
+    $form['aggregates_with'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Filter aggregates displayed'),
+      '#description' => t('Filters the aggregates shown based on the value. Doesn\'t affect the number of '.$this->view->base_table.' records.'),
+      '#default_value' => ($this->options['aggregates_with']) ? $this->options['aggregates_with'] : TRUE,    
+    );
+    
+  }
+  
+  /**
+   * If the table to be filtered is not aggregated uses the parent::query()
+   * However, if it is uses postgresql any() function to compare
+   */
+  function query () {
+    $this->ensure_my_table();
+    
+    
+    $table = $this->query->get_table_info($this->table);
+    if (preg_match('/aggregator/',$table['join']->definition['handler'])) {
+      $this->aggregated = TRUE;
+    } else {
+      $this->aggregated = FALSE;
+    }
+
+    if (!$this->aggregated) {
+      parent::query();
+    } else {
+
+      // Only base records with value in the aggregated field
+      // This doesn't restrict the items in the aggregate field
+      $this->ensure_my_table();
+      $field = "$this->table_alias.$this->real_field";
+      if ($this->options['records_with']) {
+        $where = "'%s' = ANY($field)";
+        $this->query->add_where($this->options['group'], $where, $this->value);
+      }
+
+      // To restrict the items in the aggregate...
+      // Tell the join handler about the filter 
+      // so it can be done in the join query
+      if ($this->options['aggregates_with']) {
+        $table['join']->filter[] = $field ." = '". $this->value."'";
+      }    
+    }
+    
+  }
+  
+}

+ 8 - 3
base/tripal_core/views/handlers/views_handler_join_chado_aggregator.inc

@@ -105,9 +105,14 @@ class views_handler_join_chado_aggregator extends views_join {
     // SQL to use in the join
     $sql = 'SELECT '.implode(', ',$fields)
       .' FROM '.$this->definition['table']
-      .' '.implode(' ',$joins)
-      .' GROUP BY '.$this->definition['field'];
-   
+      .' '.implode(' ',$joins);
+ 
+    if (!empty($this->filter)) {
+      $sql .= ' WHERE '.implode(', ', $this->filter);
+    }
+    
+    $sql .= ' GROUP BY '.$this->definition['field'];
+    
     // Create the join (full SQL) ----------------
     $output[] = $this->create_single_join(
       $query,