Selaa lähdekoodia

Added a Filter Handler for Chado Booleans: Supports both 0/1 (ie: cvterm:is_obsolete) and f/t (ie: feature.is_obsolete) booleans and changed handler of all booleans

laceysanderson 14 vuotta sitten
vanhempi
commit
ba0d41a291

+ 3 - 0
tripal_core/tripal_core.views.inc

@@ -23,6 +23,9 @@ function tripal_core_views_handlers() {
      'views_handler_filter_chado_select_string' => array(
       'parent' => 'views_handler_filter_string',
      ),
+     'views_handler_filter_chado_boolean' => array(
+      'parent' => 'views_handler_filter_boolean_operator',
+     ),
    ),
  );
 }

+ 88 - 0
tripal_core/views/handlers/views_handler_filter_chado_boolean.inc

@@ -0,0 +1,88 @@
+<?php
+
+/* @file
+ * Purpose: This handler provides a TRUE/FALSE or YES/NO select for chado fields
+ *  of type boolean (includes both 0/1 and t/f booleans)
+ */
+class views_handler_filter_chado_boolean extends views_handler_filter_boolean_operator {
+ /**
+  * Checks if this field uses 0/1 or t/f
+  */
+  function init(&$view, $options) {
+    parent::init($view, $options);
+    
+    $sql = 'SELECT '.$this->field.' FROM '.$this->table.' LIMIT 1';
+    $previous_db = tripal_db_set_active('chado');
+    $result = db_fetch_object(db_query($sql));
+    tripal_db_set_active($previous_db);
+    
+    $this->boolean_type_tf = FALSE;
+    if (preg_match('/[tf]/', $result->{$this->field})) {
+      $this->boolean_type_tf = TRUE;
+    }
+
+  }
+
+ /**
+  * This function sets the options array for the select.
+  * If we are using a t/f boolean then the options need to evaluate to either t or f
+  * Otherwise, (0/1 boolean) the options evaluate to 0 or 1
+  */
+  function get_value_options() {
+
+    if ($this->boolean_type_tf) {
+      if (isset($this->definition['type'])) {
+        if ($this->definition['type'] == 'yes-no') {
+          $this->value_options = array('t' => t('Yes'), 'f' => t('No'));
+        }
+        if ($this->definition['type'] == 'on-off') {
+          $this->value_options = array('t' => t('On'), 'f' => t('Off'));
+        }
+      }
+
+      // Provide a fallback if the above didn't set anything.
+      if (!isset($this->value_options)) {
+        $this->value_options = array('t' => t('True'), 'f' => t('False'));
+      }    
+    } else { //end of t/f boolean
+      if (isset($this->definition['type'])) {
+        if ($this->definition['type'] == 'yes-no') {
+          $this->value_options = array(1 => t('Yes'), 0 => t('No'));
+        }
+        if ($this->definition['type'] == 'on-off') {
+          $this->value_options = array(1 => t('On'), 0 => t('Off'));
+        }
+      }
+
+      // Provide a fallback if the above didn't set anything.
+      if (!isset($this->value_options)) {
+        $this->value_options = array(1 => t('True'), 0 => t('False'));
+      }
+    } //end of 0/1 boolean
+  }
+
+  function query() {
+    $this->ensure_my_table();
+
+    $where = "$this->table_alias.$this->real_field ";
+
+    if ($this->boolean_type_tf) {
+      if (preg_match('/f/',$this->value)) {
+        $where .= "= 'f'";
+      } else {
+        $where .= "= 't'";
+      }
+      $this->query->add_where($this->options['group'], $where);    
+    } else {
+      if (empty($this->value)) {
+        $where .= '= 0';
+        if ($this->accept_null) {
+          $where = '(' . $where . " OR $this->table_alias.$this->real_field IS NULL)";
+        }
+      } else {
+        $where .= '<> 0';
+      }
+      $this->query->add_where($this->options['group'], $where);
+    }
+  }
+}

+ 3 - 3
tripal_cv/views/cvterm.views.inc

@@ -121,7 +121,7 @@ function retrieve_cvterm_views_data() {
        'click sortable' => TRUE,
      ),
      'filter' => array(
-       'handler' => 'views_handler_filter_boolean_operator',
+       'handler' => 'views_handler_filter_chado_boolean',
        'label' => t('Is Obsolete?'),
        'type' => 'yes-no',
      ),
@@ -139,8 +139,8 @@ function retrieve_cvterm_views_data() {
        'click sortable' => TRUE,
      ),
      'filter' => array(
-       'handler' => 'views_handler_filter_boolean_operator',
-       'label' => t('Published'),
+       'handler' => 'views_handler_filter_chado_boolean',
+       'label' => t('Is Relationship Type?'),
        'type' => 'yes-no',
      ),
      'sort' => array(

+ 6 - 2
tripal_feature/views/feature.views.inc

@@ -193,7 +193,9 @@
       'click sortable' => TRUE,
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter',
+       'handler' => 'views_handler_filter_chado_boolean',
+       'label' => t('Is Analysis?'),
+       'type' => 'yes-no',
     ),
     'sort' => array(
       'handler' => 'views_handler_sort',
@@ -209,7 +211,9 @@
       'click sortable' => TRUE,
     ),
     'filter' => array(
-      'handler' => 'views_handler_filter',
+       'handler' => 'views_handler_filter_chado_boolean',
+       'label' => t('Is Obsolete?'),
+       'type' => 'yes-no',
     ),
     'sort' => array(
       'handler' => 'views_handler_sort',

+ 3 - 3
tripal_library/views/library.views.inc

@@ -157,9 +157,9 @@ function retrieve_library_views_data() {
 	    'click sortable' => TRUE,
 	  ),
 	  'filter' => array(
-	    'handler' => 'views_handler_filter_boolean_operator',
-	    'label' => t('Is Obsolete?'),
-	    'type' => 'yes-no',
+       'handler' => 'views_handler_filter_chado_boolean',
+       'label' => t('Is Obsolete?'),
+       'type' => 'yes-no',
 	  ),
 	  'sort' => array(
 	    'handler' => 'views_handler_sort',

+ 5 - 0
tripal_stock/views/stock.views.inc

@@ -162,6 +162,11 @@ function retrieve_stock_views_data() {
       'handler' => 'views_handler_field',
       'click sortable' => TRUE,
     ),
+    'filter' => array(
+       'handler' => 'views_handler_filter_chado_boolean',
+       'label' => t('Is Obsolete?'),
+       'type' => 'yes-no',    
+    ),
     'sort' => array(
       'handler' => 'views_handler_sort',
     ),