Procházet zdrojové kódy

added views chado field date filter support.

Lacey Sanderson před 9 roky
rodič
revize
be57a9217b

+ 51 - 0
tripal_chado/includes/chado_views_handler_filter.inc

@@ -38,6 +38,57 @@ class chado_views_handler_filter_boolean extends views_handler_filter_boolean_op
   }
 }
 
+/**
+ * Adds support for chado datetime filters.
+ */
+class chado_views_handler_filter_date extends views_handler_filter_date {
+
+  /**
+   * {@inheritdoc}
+   */
+  function query() {
+
+    // Adds joins to chado_entity and the chado table this field is from.
+    $alias = _chado_views_add_table_joins($this);
+   
+    // Then allow the parent handler to add the where.
+    $field = $alias .'.'. $this->definition['chado_field'];
+    $info = $this->operators();
+    if (!empty($info[$this->operator]['method'])) {
+      $this->{$info[$this->operator]['method']}($field);
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function op_between($field) {
+    // Use the substitutions to ensure a consistent timestamp.
+    $query_substitutions = views_views_query_substitutions($this->view);
+    $a = intval(strtotime($this->value['min'], $query_substitutions['***CURRENT_TIME***']));
+    $b = intval(strtotime($this->value['max'], $query_substitutions['***CURRENT_TIME***']));
+
+    // This is safe because we are manually scrubbing the values.
+    // It is necessary to do it this way because $a and $b are formulas when using an offset.
+    $operator = strtoupper($this->operator);
+    $this->query->add_where_expression($this->options['group'], "$field $operator to_timestamp($a) AND to_timestamp($b)");
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function op_simple($field) {
+    // Use the substitutions to ensure a consistent timestamp.
+    $query_substitutions = views_views_query_substitutions($this->view);
+    $value = intval(strtotime($this->value['value'], $query_substitutions['***CURRENT_TIME***']));
+
+    // This is safe because we are manually scrubbing the value.
+    // It is necessary to do it this way because $value is a formula when using an offset.
+    $this->query->add_where_expression($this->options['group'], "$field $this->operator to_timestamp($value)");
+  }
+
+}
+
 /**
  * Adds support for chado foreign key filters.
  */

+ 5 - 2
tripal_chado/tripal_chado.views.inc

@@ -118,8 +118,7 @@ function tripal_chado_add_field_views_data(&$data) {
       $data['tripal_entity'][ $field['field_name'] ]['filter']['bundles'] = $field['bundles']['TripalEntity'];
       $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_string';
       
-      // If this is a foreign key field like organism then we want a drop-down.
-      // This requires a special handler.
+      // Specify special handlers.
       if ($fk_defn) {
         $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_fk';
         $data['tripal_entity'][ $field['field_name'] ]['filter']['foreign_key'] = $fk_defn;
@@ -129,6 +128,10 @@ function tripal_chado_add_field_views_data(&$data) {
         $data['tripal_entity'][ $field['field_name'] ]['filter']['label'] = $field['settings']['chado_column'];
         $data['tripal_entity'][ $field['field_name'] ]['filter']['type'] = 'yes-no'; 
       }
+      elseif ($field_defn['type'] == 'datetime') {
+        $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_date';
+      }
+
     }    
   }
 }