Browse Source

Fix to chado filter handlers to set default options and made chado aggregator safe views_handler_filter_chado_select_cvterm_name

Lacey Sanderson 12 years ago
parent
commit
6926718007

+ 2 - 2
tripal_views/tripal_views.api.inc

@@ -443,8 +443,8 @@ function tripal_views_get_integration_array_for_chado_table($table_name, $base_t
       }
 
       // Specify specialty handlers
-      if ($field_name == 'type_id') {
-        $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'views_handler_filter_chado_select_cvterm_name';
+      if ($field_name == 'type_id' OR $field_name == 'cvterm_id') {
+        $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'chado_views_handler_filter_select_cvterm';
       }
     }
   }

+ 6 - 3
tripal_views/tripal_views.views.inc

@@ -72,6 +72,9 @@ function tripal_views_views_handlers() {
       'views_handler_filter_chado_select_cvterm_name' => array(
         'parent' => 'views_handler_filter_string',
       ),
+      'chado_views_handler_filter_select_cvterm' => array(
+        'parent' => 'views_handler_filter_chado_select_cvterm_name',
+      ),
 
       // Join Handlers
       'views_handler_join_chado_aggregator' => array(
@@ -721,12 +724,12 @@ function tripal_views_data_export_download_form(&$form_state, $view, $display_id
   $current_display = $view->current_display;
   foreach ($displays as $name => $display) {
     if (preg_match("/^views_data_export/", $name)) {
-    
-      // only add this display to the form if it is attached 
+
+      // only add this display to the form if it is attached
       $display_options = $display->display_options;
       if(strcmp($display_options['displays'][$current_display],$current_display)!=0){
          continue;
-      } 
+      }
 
       // set the first item as default
       if (!$default) {

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_boolean_operator.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_boolean_operator extends views_handler_filter_b
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_boolean_operator_string.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_boolean_operator_string extends views_handler_f
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %table to only those with the value in the aggregate array.', array('%table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %table records.', array('%table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_date.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_date extends views_handler_filter_date {
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_equality.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_equality extends views_handler_filter_equality
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_float.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_float extends views_handler_filter_float {
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_numeric.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_numeric extends views_handler_filter_numeric {
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 105 - 0
tripal_views/views/handlers/chado_views_handler_filter_select_cvterm.inc

@@ -0,0 +1,105 @@
+<?php
+
+/**
+ * @file
+ * Purpose: This Handler provides a select list for the type field
+ *
+ *  NOTE: This handler only works when applied to the type_id field in the base_table of
+ *  this view.
+ *
+ * @ingroup views_filter_handlers
+ * @ingroup tripal_core
+ */
+class chado_views_handler_filter_select_cvterm extends views_handler_filter_chado_select_cvterm_name {
+
+ /**
+  * Executed when the field is added
+  * Determine which cv to limit the cvterms to
+  */
+  function init(&$view, $options) {
+
+    include_once('chado_wrapper_functions.inc');
+    parent::init($view, $options);
+
+  }
+
+  /**
+   * Defines the options form (form available to admin when they add a field to a view)
+   */
+  function options_form(&$form, &$form_state) {
+    $form['msg'] = array(
+      '#type' => 'item',
+      '#value' => '<b>If this filter applies to a table that is aggregated, additionally options may be ignored.</b>'
+    );
+
+    parent::options_form($form, $form_state);
+
+    $form['agg'] = array(
+      '#type' => 'fieldset',
+      '#title' => 'Apply to fields that are aggregated'
+    );
+
+    $form['agg']['records_with'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Filter base table records'),
+      '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
+    );
+
+    $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['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() {
+
+    // make optional
+    // if it is not set or empty then don't restrict the query
+    if (!$this->value) {
+      return;
+    }
+
+    $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['agg']['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['agg']['aggregates_with']) {
+        if (sizeof($this->value) == 1) {
+          $table['join']->filter[] = $field . " = '" . array_pop($this->value) . "'";
+        }
+      }
+    }
+
+  }
+}

+ 2 - 2
tripal_views/views/handlers/chado_views_handler_filter_string.inc

@@ -30,14 +30,14 @@ class chado_views_handler_filter_string extends views_handler_filter_string {
       '#type' => 'checkbox',
       '#title' => t('Filter base table records'),
       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['records_with'])) ? $this->options['records_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
     );
 
     $form['agg']['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 %base_table records.', array('%base_table' => $this->view->base_table)),
-      '#default_value' => (isset($this->options['aggregates_with'])) ? $this->options['aggregates_with'] : TRUE,
+      '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
     );
 
   }

+ 7 - 1
tripal_views/views/handlers/chado_wrapper_functions.inc

@@ -65,7 +65,13 @@ function chado_wrapper_is_aggregated_by_join($handler) {
 function chado_wrapper_is_aggregated_by_result($handler, $values) {
   $aggregated = FALSE;
 
-  if (preg_match('/^{.*}$/', $values[0]->{$handler->field_alias})) {
+
+  $i = 0;
+  while (empty($values[$i]->{$handler->field_alias}) AND $i <= sizeof($values)) {
+    $i++;
+  }
+
+  if (preg_match('/^{.*}$/', $values[$i]->{$handler->field_alias})) {
     $aggregated = TRUE;
   }
 

+ 1 - 1
tripal_views/views/handlers/views_handler_filter_chado_select_cvterm_name.inc

@@ -61,7 +61,7 @@ class views_handler_filter_chado_select_cvterm_name extends views_handler_filter
       // @coder-ignore: non-drupal schema therefore table prefixing does not apply
       $sql = "SELECT cvterm_id, name FROM cvterm WHERE cvterm_id IN (SELECT distinct(type_id) FROM %s)";
       $previous_db = tripal_db_set_active('chado');
-      $resource = db_query($sql, $this->view->base_table);
+      $resource = db_query($sql, $this->table);
       tripal_db_set_active($previous_db);
       $cvterms = array();
       while ( $r = db_fetch_object($resource) ) {