Browse Source

Bug fix to boolean field handler to work correctly with t/f aggregated fields and updated boolean filter to work with t/f booleans

Lacey Sanderson 12 years ago
parent
commit
5a591a4ea6

+ 3 - 7
tripal_views/tripal_views.views.inc

@@ -86,22 +86,21 @@ function tripal_views_views_handlers() {
         'parent' => 'views_handler_filter'
       ),
 
-      // Old Handlers
+      // Old Handlers (deprecated)
       'views_handler_field_node_optional' => array(
         'parent' => 'views_handler_field_node',
       ),
       'views_handler_field_chado_count' => array(
         'parent' => 'views_handler_field',
       ),
-      'views_handler_filter_chado_boolean' => array(
-        'parent' => 'views_handler_filter_boolean_operator',
-      ),
       'views_handler_field_dbxref_accession_link' => array(
         'parent' => 'views_handler_field',
       ),
       'views_handler_field_readable_date' => array(
         'parent' => 'views_handler_field',
       ),
+
+      // Old handlers with new counterparts (deprecated)
       'views_handler_filter_chado_select_string' => array(
         'parent' => 'views_handler_filter_string',
       ),
@@ -139,9 +138,6 @@ function tripal_views_views_handlers() {
       'chado_views_handler_filter_string' => array(
         'parent' => 'views_handler_filter_string',
       ),
-      'chado_views_handler_filter_boolean_operator_string' => array(
-        'parent' => 'views_handler_filter_boolean_operator_string',
-      ),
       'chado_views_handler_filter_boolean_operator' => array(
         'parent' => 'views_handler_filter_boolean_operator',
       ),

+ 15 - 2
tripal_views/views/handlers/chado_views_handler_field_boolean.inc

@@ -89,14 +89,27 @@ class chado_views_handler_field_boolean extends views_handler_field_boolean {
     // check to see if this is a t/f boolean field or a 1/0 boolean field
     // parent render expects 1/0 so need to translate to that form before rendering
     if (!is_array($values->{$this->field_alias})) {
-      if (!preg_match('/^[01]$/',$values->{$this->field_alias})) {
+      if (!preg_match('/[01]/',$values->{$this->field_alias})) {
         if (preg_match('/^[tT]/',$values->{$this->field_alias})) {
           $values->{$this->field_alias} = 1;
-        }  elseif (preg_match('/^[fF]/',$values->{$this->field_alias})) {
+        }
+        elseif (preg_match('/^[fF]/',$values->{$this->field_alias})) {
           $values->{$this->field_alias} = 0;
         }
       }
     }
+    else {
+      if (!preg_match('/[01]/',$values->{$this->field_alias}[0])) {
+        foreach ($values->{$this->field_alias} as $k => $v) {
+          if (preg_match('/^[tT]/',$v)) {
+            $values->{$this->field_alias}[$k] = 1;
+          }
+          elseif (preg_match('/^[fF]/',$v)) {
+            $values->{$this->field_alias}[$k] = 0;
+          }
+        }
+      }
+    }
 
     return chado_wrapper_render_items($this, $values);
   }

+ 53 - 4
tripal_views/views/handlers/chado_views_handler_filter_boolean_operator.inc

@@ -50,12 +50,13 @@ class chado_views_handler_filter_boolean_operator extends views_handler_filter_b
 
     // make optional
     // if it is not set or empty then don't restrict the query
-    if (!$this->value) {
+    if ($this->value == 'All') {
       return;
     }
 
     $this->ensure_my_table();
 
+    // determine whether it is aggregated or not
     $table = $this->query->get_table_info($this->table);
     if (preg_match('/aggregator/', $table['join']->definition['handler'])) {
       $this->aggregated = TRUE;
@@ -64,8 +65,46 @@ class chado_views_handler_filter_boolean_operator extends views_handler_filter_b
       $this->aggregated = FALSE;
     }
 
+    // check if its a t/f or 1/0 boolean
+    $check = db_fetch_object(db_query("SELECT %s as val FROM %s LIMIT 1", $this->real_field, $this->table));
+    if (preg_match('/^[tTfF]/',$check->val)) {
+      $true = 't';
+      $false = 'f';
+    }
+    else {
+      $true = 1;
+      $false = 0;
+    }
+
     if (!$this->aggregated) {
-      parent::query();
+
+      // Only base records with value in the aggregated field
+      // This doesn't restrict the items in the aggregate field
+      if ($this->options['agg']['records_with']) {
+        $where = "$this->table_alias.$this->real_field ";
+        if (empty($this->value)) {
+          $where .= "= '".$false."'";
+          if ($this->accept_null) {
+            $where = '(' . $where . " OR $this->table_alias.$this->real_field IS NULL)";
+          }
+        }
+        else {
+          if (!empty($this->definition['use equal'])) {
+            $where .= "= '".$true."'";
+          }
+          else {
+            $where .= "<> '".$false."'";
+          }
+        }
+        $this->query->add_where($this->options['group'], $where);
+      }
+
+      // 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']) {
+        //Do nothing b/c it's not aggregated!
+      }
     }
     else {
 
@@ -75,14 +114,24 @@ class chado_views_handler_filter_boolean_operator extends views_handler_filter_b
       $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);
+        if ($this->value) {
+          $this->query->add_where($this->options['group'], $where, $true);
+        }
+        else {
+          $this->query->add_where($this->options['group'], $where, $false);
+        }
       }
 
       // 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']) {
-        $table['join']->filter[] = $field . " = '" . $this->value . "'";
+        if ($this->value) {
+          $table['join']->filter[] = $field . " = '" . $true . "'";
+        }
+        else {
+          $table['join']->filter[] = $field . " = '" . $false . "'";
+        }
       }
     }
 

+ 0 - 91
tripal_views/views/handlers/chado_views_handler_filter_boolean_operator_string.inc

@@ -1,91 +0,0 @@
-<?php
-
-/**
- * @file
- * A chado wrapper for the views_handler_filter_boolean_operator_string.
- *
- * Handles fields which may be aggregated during the chado join process. There are options
- * to filter the base table based on an aggregated table or just filter the aggregated
- * table (showing blank for that field if there are no records in the aggregated table).
- */
-class chado_views_handler_filter_boolean_operator_string extends views_handler_filter_boolean_operator_string {
-
-  /**
-   * 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 %table to only those with the value in the aggregate array.', array('%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 %table records.', array('%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']) {
-        $table['join']->filter[] = $field . " = '" . $this->value . "'";
-      }
-    }
-
-  }
-
-}