Browse Source

Added aggregate all field handler: allows all fields of an aggregate table to be displayed in one views field

Lacey Sanderson 13 năm trước cách đây
mục cha
commit
ce184d6881

+ 18 - 0
base/tripal_analysis/views/analysis.views.inc

@@ -94,6 +94,24 @@ function retrieve_analysis_views_data() {
     ),
  	);
  	
+ 	$data['analysisprop']['all'] = array(
+    'title' => t('All'),
+    'help' => t('An aggregate field that contains all fields for a row.'),
+    'field' => array(
+      'handler' => 'chado_views_handler_field_aggregate',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+ 	);
+ 	
 	// Table Field Definitions----------------------
 	// Field: analysis_id (primary key)
 	$data['analysis']['analysis_id'] = array(

+ 12 - 6
base/tripal_core/tripal_core.views.inc

@@ -60,7 +60,19 @@ function tripal_core_views_handlers() {
      'path' => drupal_get_path('module', 'tripal_core') . '/views/handlers',
    ),
    'handlers' => array(
+   
      // Custom Chado Handlers
+     'chado_views_handler_field_aggregate' => array(
+      'parent' => 'chado_views_handler_field',
+     ),
+     'views_handler_filter_chado_select_string' => array(
+      'parent' => 'views_handler_filter_string',
+     ),
+     'views_handler_filter_chado_select_cvterm_name' => array(
+      'parent' => 'views_handler_filter_string',
+     ),
+     
+     // Old Handlers
      'views_handler_field_node_optional' => array(
        'parent' => 'views_handler_field_node',
      ),
@@ -73,12 +85,6 @@ function tripal_core_views_handlers() {
      'views_handler_field_chado_count' => array(
       'parent' => 'views_handler_field',
      ),
-     'views_handler_filter_chado_select_string' => array(
-      'parent' => 'views_handler_filter_string',
-     ),
-     'views_handler_filter_chado_select_cvterm_name' => array(
-      'parent' => 'views_handler_filter_string',
-     ),
      'views_handler_filter_chado_boolean' => array(
       'parent' => 'views_handler_filter_boolean_operator',
      ),

+ 16 - 2
base/tripal_core/views/handlers/chado_views_handler_field.inc

@@ -19,6 +19,7 @@ class chado_views_handler_field extends views_handler_field {
    */
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
+    
     $form['type'] = array(
       '#type' => 'radios',
       '#title' => t('Display type'),
@@ -54,6 +55,19 @@ class chado_views_handler_field extends views_handler_field {
     }
   }
   
+  /**
+   * Splits the aggregated values up for use in rendering
+   */
+  function pre_render (&$values) {
+    
+    if ($this->aggregated) {
+      foreach($values as $k => $v) {
+        $values[$k]->{$this->field_alias} = $this->split_array_agg_results($v->{$this->field_alias});
+      }
+    }
+    
+  }
+  
   /**
    * Render the field.
    *
@@ -64,13 +78,13 @@ class chado_views_handler_field extends views_handler_field {
    *   The values retrieved from the database.
    */
   function render($values) {
-    
+
     // If it's aggregated (an array), then render each part 
     // using the parent render functionality
     if ($this->aggregated) {
       $items = array();
       
-      $parts = $this->split_array_agg_results($values->{$this->field_alias});
+      $parts = $values->{$this->field_alias};
       foreach ($parts as $p) {
         $v[ $this->field_alias ] = $p;
         $val = (object) $v;

+ 86 - 0
base/tripal_core/views/handlers/chado_views_handler_field_aggregate.inc

@@ -0,0 +1,86 @@
+<?php
+
+class chado_views_handler_field_aggregate extends chado_views_handler_field {
+  
+  function init(&$view, $options) {
+    parent::init($view, $options);
+    
+    if (!isset($this->chado_table_description)) {
+      $this->chado_table_description = module_invoke_all('chado_'.$this->table.'_schema');
+    }
+    
+  }
+
+  /**
+   * 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['format'] = array(
+      '#type' => 'fieldset',
+      '#title' => 'Format Output',
+      '#description' => t('The following fields specify how a single result of this field will be 
+        displayed. When there are multiple results of this field due to aggregation, each result
+        will be rendered according to the following rules and then all results will be joined
+        together based on the "Display Type" indicated.')
+    );
+
+    $this->tokens = array();
+    $value = array();
+    foreach( array_keys($this->chado_table_description['fields']) as $field ) {
+      $t = '[' . $this->options['id'] . '-' . $field . ']';
+      $this->tokens[$t] = t($field);
+      $value[] = $t . ' == ' . $field;
+    }
+    
+    $form['format']['format_string'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Format String'),
+      '#description' => 'Use any of the format tokens below to indicate what fields you want displayed.',
+      '#default_value' => ($this->options['format']['format_string']) ? $this->options['format']['format_string'] : implode(', ', array_keys($this->tokens)),
+    );
+    
+    $form['format']['tokens'] = array(
+      '#type' => 'item',
+      '#title' => 'Format Tokens',
+      '#value' => implode("<br />",$value),
+    );
+    
+  }
+  
+  function query() {
+    parent::query();
+    
+    $this->table_definition = $this->query->get_table_info($this->table);
+  }
+
+
+  function pre_render (&$values) {
+    
+    if ($this->aggregated) {
+      foreach($values as $k => $v) {
+        $values[$k]->{$this->field_alias} = $this->split_array_agg_results($v->{$this->field_alias});
+        
+        foreach($values[$k]->{$this->field_alias} as &$val) {
+          
+          // First, get the token values
+          $subparts = explode(',',$val);
+          $token_values = array();
+          foreach($subparts as $ssk => $ssv) {
+            if(preg_match('/(.*)::(.*)/',$ssv,$matches)) {
+              $token_values[ '[all-'.$matches[1].']' ] = $matches[2];
+            }
+          }
+          
+          // Now manually sub them in
+          $val = str_replace(array_keys($token_values),$token_values,$this->options['format']['format_string']);
+          
+        }
+      }
+
+    }
+    
+  }
+	
+}