فهرست منبع

Tripal Views: Added count (counts the number of records in another table) and t/f field handlers

laceysanderson 14 سال پیش
والد
کامیت
7e1bc55016

+ 2 - 0
tripal_analysis/tripal_analysis.views.inc

@@ -21,11 +21,13 @@
  */
 require('views/analysis.views.inc');
 require('views/chado_analysis.views.inc');
+require('views/misc_tables.views.inc');
 function tripal_analysis_views_data()  {
   $data = array();
 
   $data = array_merge($data, retrieve_analysis_views_data());
   $data = array_merge($data, retrieve_chado_analysis_views_data());
+  $data = array_merge($data, retrieve_analysis_misc_tables_views_data());
 
   return $data;
 }

+ 35 - 0
tripal_analysis/views/misc_tables.views.inc

@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * Purpose: Allows the analysis module to add fields to other module views
+ *
+ *   For example, a field counting the number of analysis' associted with a given feature would be
+ *   added to the feature view as follows:
+ *   @code
+      $data['feature']['num_analysis''] = array(
+        'title' => "Number of analysis'",
+        'help' => "Provides a count of the number of analysis' associated with a given feature",
+        'field' => array(
+          'handler' => 'views_handler_field_chado_count',
+          'table_to_query' => 'analysis_feature',
+        ),
+      ); 
+ *   @endcode
+ */
+function retrieve_analysis_misc_tables_views_data() {
+  $data = array();
+ 
+  // Table: Feature---------------------------------------------------------------------------------
+  // Calculated Field: Number of analysis' (Count -Int)
+  // Provides the number of analysis' for a given feature
+  $data['feature']['num_analysis'] = array(
+    'title' => "Number of analysis'",
+    'help' => "Provides a count of the number of analysis' associated with a given feature",
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'analysisfeature',
+    ),
+  );  
+  
+  return $data;
+}

+ 6 - 0
tripal_core/tripal_core.views.inc

@@ -20,6 +20,12 @@ function tripal_core_views_handlers() {
      'views_handler_field_cvterm_name' => array(
        'parent' => 'views_handler_field',
      ),
+     'views_handler_field_chado_tf_boolean' => array(
+      'parent' => 'views_handler_field_boolean',
+     ),
+     'views_handler_field_chado_count' => array(
+      'parent' => 'views_handler_field',
+     ),
      'views_handler_filter_chado_select_string' => array(
       'parent' => 'views_handler_filter_string',
      ),

+ 61 - 0
tripal_core/views/handlers/views_handler_field_chado_count.inc

@@ -0,0 +1,61 @@
+<?php
+
+// @file
+// Purpose: Provide a field that counts the number of records in the current table
+//   are connected to the base table. For example, this field could be used to count
+//   the number of features connected to a given organism -base table=organism, 
+//   current table=feature: for each record in the organism view count feature records where 
+//   feature.organism_id=organism_id of current organism record
+class views_handler_field_chado_count extends views_handler_field {
+
+  function init(&$view, $options) {
+    parent::init($view, $options);
+    
+    // the table to query is required
+    // check that it was provided in the field definition and if not warn
+    if ($this->definition['table_to_query']) {
+      $this->aliases['current_table'] = $this->definition['table_to_query'];
+    } else {
+      drupal_set_message("The field definition ( in hook_views_data() ) needs to specify the 'table_to_query' in order for this fields to work. Field:".$this->field." in the ".$this->table." table definition", 'error');
+    }
+    
+    // set aliases
+    $this->aliases['primary_id'] = $this->table . '_id';
+    $this->aliases['foreign_key'] = $this->table . '_id';
+  }
+  
+  //Needed to ensure that the name of this field is not added to the query
+  function query() {
+    $this->add_additional_fields();
+  }
+  
+  function pre_render(&$values) {
+    // Render nothing if the current table wasn't set in the field definition
+    if(!$this->aliases['current_table']) {
+      return '';
+    }
+
+    foreach ($values as $key => $record) {
+      $primary_id = $record->{$this->aliases['primary_id']};
+      
+      //Select count from database
+      $sql = 'SELECT count(*) as count FROM %s WHERE %s=%d';
+      $previous_db = tripal_db_set_active('chado');
+      $result = db_fetch_object(db_query(
+        $sql, 
+        $this->aliases['current_table'], 
+        $this->aliases['foreign_key'],
+        $primary_id
+      ));
+      tripal_db_set_active($previous_db);
+      
+      //Add to view results
+      $this->view->result[$key]->{$this->field} = $result->count;
+    }
+  }
+  
+  function render($values) {
+    return $values->{$this->field};
+  }
+
+}

+ 37 - 0
tripal_core/views/handlers/views_handler_field_chado_tf_boolean.inc

@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * A handler to provide proper displays for booleans.
+ *
+ * Allows for display of true/false, yes/no, on/off.
+ *
+ * Definition terms:
+ *   - output formats: An array where the first entry is displayed on boolean false
+ *      and the second is displayed on boolean true. An example for sticky is:
+ *      @code
+ *      'output formats' => array(
+ *        'sticky' => array('', t('Sticky')),
+ *      ),
+ *      @endcode
+ *
+ * @ingroup views_field_handlers
+ */
+class views_handler_field_chado_tf_boolean extends views_handler_field_boolean {
+
+  // Changes the rendered value: t='Yes' & f='No'
+  // Rendered value depends on type of value chosen in options
+  function render($values) {
+    $value = $values->{$this->field_alias};
+    if (!empty($this->options['not'])) {
+      $value = !$value;
+    }
+
+    if (isset($this->formats[$this->options['type']])) {
+      return preg_match('/t/',$value) ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
+    }
+    else {
+      return preg_match('/t/', $value) ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
+    }
+  }
+  
+}

+ 0 - 31
tripal_core/views/handlers/views_handler_field_tf_boolean.inc

@@ -1,31 +0,0 @@
-<?php
-// $Id: views_handler_field_boolean.inc,v 1.3.2.1 2010/03/25 01:14:22 merlinofchaos Exp $
-
-/**
- * A handler to provide proper displays for booleans.
- *
- * Allows for display of true/false, yes/no, on/off.
- *
- * Definition terms:
- *   - output formats: An array where the first entry is displayed on boolean false
- *      and the second is displayed on boolean true. An example for sticky is:
- *      @code
- *      'output formats' => array(
- *        'sticky' => array('', t('Sticky')),
- *      ),
- *      @endcode
- *
- * @ingroup views_field_handlers
- */
-class views_handler_field_tf extends views_handler_field {
-
-  function render($values) {
-    $value = $values->{$this->field_alias};
-    
-    if (preg_match('/^t$/', $value)) {
-      return 'Yes';
-    } else {
-      return 'No';
-    }
-  }
-}

+ 2 - 1
tripal_feature/tripal_feature.views.inc

@@ -22,12 +22,13 @@
  */
 require_once('views/feature.views.inc');
 require_once('views/chado_feature.views.inc');
-
+require_once('views/misc_tables.views.inc');
 function tripal_feature_views_data()  {
   $data = array();
   
   $data = array_merge($data, retrieve_feature_views_data());
   $data = array_merge($data, retrieve_chado_feature_views_data());
+  $data = array_merge($data, retrieve_feature_misc_tables_views_data());
   
   return $data;
 }

+ 16 - 4
tripal_feature/views/feature.views.inc

@@ -189,8 +189,10 @@
     'title' => 'Is Analysis',
     'help' => 'A boolean indicating whether this feature was annotated by means of automated analysis.',
     'field' => array(
-      'handler' => 'views_handler_field',
-      'click sortable' => TRUE,
+       'handler' => 'views_handler_field_chado_tf_boolean',
+       'click sortable' => TRUE,
+       'label' => t('Is Analysis?'),
+       'type' => 'yes-no',
     ),
     'filter' => array(
        'handler' => 'views_handler_filter_chado_boolean',
@@ -207,8 +209,10 @@
     'title' => 'Is Obsolete',
     'help' => 'A boolean indicating whether this feature is obsolete.',
     'field' => array(
-      'handler' => 'views_handler_field',
-      'click sortable' => TRUE,
+       'handler' => 'views_handler_field_chado_tf_boolean',
+       'click sortable' => TRUE,
+       'label' => t('Is Obsolete?'),
+       'type' => 'yes-no',
     ),
     'filter' => array(
        'handler' => 'views_handler_filter_chado_boolean',
@@ -245,6 +249,14 @@
       'handler' => 'views_handler_sort_date',
     ),
   );
+
+  // Calculated Field: Number of Analysis' (Count -Int)
+  // Provides the number of analysis' for a given feature
+  // @see tripal_analysis/views/misc_tables.views.inc
   
+  //Calculated Field: Number of Libraries (Count -Int)
+  // Provides the number of libraries for a given feature
+  // @see tripal_library/views/misc_tables.views.inc 
+
   return $data;
  }

+ 58 - 0
tripal_feature/views/misc_tables.views.inc

@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * Purpose: Allows the feature module to add fields to other module views
+ *
+ *   For example, a field counting the number of features associted with a given organism would be
+ *   added to the organism view as follows:
+ *   @code
+      $data['organism']['num_features'] = array(
+        'title' => 'Number of features',
+        'help' => 'Provides a count of the number of features associated with a given organism',
+        'field' => array(
+          'handler' => 'views_handler_field_chado_count',
+          'table_to_query' => 'feature',
+        ),
+      ); 
+ *   @endcode
+ */
+function retrieve_feature_misc_tables_views_data() {
+  $data = array();
+ 
+  // Table: Organism--------------------------------------------------------------------------------
+  // Calculated Field: Number of features (Count -Int)
+  // Provides the number of features for a given organism
+  $data['organism']['num_features'] = array(
+    'title' => 'Number of Features',
+    'help' => 'Provides a count of the number of features associated with a given organism',
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'feature',
+    ),
+  );  
+
+  // Table: Library---------------------------------------------------------------------------------
+  // Calculated Field: Number of features (Count -Int)
+  // Provides the number of features for a given organism
+  $data['library']['num_features'] = array(
+    'title' => 'Number of Features',
+    'help' => 'Provides a count of the number of features associated with a given library',
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'library_feature',
+    ),
+  );   
+
+  // Table: Analysis--------------------------------------------------------------------------------
+  // Calculated Field: Number of features (Count -Int)
+  // Provides the number of features for a given organism
+  $data['analysis']['num_features'] = array(
+    'title' => 'Number of Features',
+    'help' => 'Provides a count of the number of features associated with a given analysis',
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'analysisfeature',
+    ),
+  ); 
+  return $data;
+}

+ 2 - 1
tripal_library/tripal_library.views.inc

@@ -21,11 +21,12 @@
  */
 require_once('views/library.views.inc');
 require_once('views/chado_library.views.inc');
+require_once('views/misc_tables.views.inc');
 function tripal_library_views_data()  {
   $data = array();
   
   $data = array_merge($data, retrieve_library_views_data());
-  $data = array_merge($data, retrieve_chado_library_views_data());
+  $data = array_merge($data, retrieve_library_misc_tables_views_data());
   
   return $data;
 }

+ 47 - 0
tripal_library/views/misc_tables.views.inc

@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * Purpose: Allows the library module to add fields to other module views
+ *
+ *   For example, a field counting the number of libraries associted with a given feature would be
+ *   added to the feature view as follows:
+ *   @code
+      $data['feature']['num_libraries'] = array(
+        'title' => "Number of Libraries",
+        'help' => "Provides a count of the number of libraries associated with a given feature",
+        'field' => array(
+          'handler' => 'views_handler_field_chado_count',
+          'table_to_query' => 'library_feature',
+        ),
+      ); 
+ *   @endcode
+ */
+function retrieve_library_misc_tables_views_data() {
+  $data = array();
+ 
+  // Table: Feature---------------------------------------------------------------------------------
+  // Calculated Field: Number of Libraries (Count -Int)
+  // Provides the number of libraries for a given feature
+  $data['feature']['num_libraries'] = array(
+    'title' => "Number of Libraries",
+    'help' => "Provides a count of the number of libraries associated with a given feature",
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'library_feature',
+    ),
+  ); 
+
+  // Table: Organism--------------------------------------------------------------------------------
+  // Calculated Field: Number of Libraries (Count -Int)
+  // Provides the number of libraries for a given organism
+  $data['organism']['num_libraries'] = array(
+    'title' => 'Number of Libraries',
+    'help' => 'Provides a count of the number of libraries associated with a given organism',
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'library',
+    ),
+  );  
+  
+  return $data;
+}

+ 12 - 0
tripal_organism/views/organism.views.inc

@@ -193,6 +193,18 @@ function retrieve_organism_views_data() {
        'handler' => 'views_handler_argument_string',
      ),
   );
+  
+  //Calculated Field: Count (Int)
+  // Provides the number of features for a given organism
+  // @see tripal_feature/views/misc_tables.views.inc
+
+  //Calculated Field: Count (Int)
+  // Provides the number of stocks for a given organism
+  // @see tripal_stock/views/misc_tables.views.inc
+
+  //Calculated Field: Number of Libraries (Count -Int)
+  // Provides the number of libraries for a given organism
+  // @see tripal_library/views/misc_tables.views.inc
 
   return $data;
 

+ 2 - 0
tripal_stock/tripal_stock.views.inc

@@ -22,11 +22,13 @@
  */
 require_once('views/stock.views.inc');
 require_once('views/chado_stock.views.inc');
+require_once('views/misc_tables.views.inc');
 function tripal_stock_views_data()  {
   $data = array();
 
   $data = array_merge($data, retrieve_stock_views_data());
   $data = array_merge($data, retrieve_chado_stock_views_data());
+  $data = array_merge($data, retrieve_stock_misc_tables_views_data());
   
   return $data;
 }

+ 35 - 0
tripal_stock/views/misc_tables.views.inc

@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * Purpose: Allows the stock module to add fields to other module views
+ *
+ *   For example, a field counting the number of stocks associted with a given organism would be
+ *   added to the organism view as follows:
+ *   @code
+      $data['organism']['num_stocks'] = array(
+        'title' => 'Number of stocks',
+        'help' => 'Provides a count of the number of stocks associated with a given organism',
+        'field' => array(
+          'handler' => 'views_handler_field_chado_count',
+          'table_to_query' => 'stock',
+        ),
+      ); 
+ *   @endcode
+ */
+function retrieve_stock_misc_tables_views_data() {
+  $data = array();
+ 
+  // Table: Organism--------------------------------------------------------------------------------
+  // Calculated Field: Number of stocks (Count -Int)
+  // Provides the number of stocks for a given organism
+  $data['organism']['num_stocks'] = array(
+    'title' => 'Number of Stocks',
+    'help' => 'Provides a count of the number of stocks associated with a given organism',
+    'field' => array(
+      'handler' => 'views_handler_field_chado_count',
+      'table_to_query' => 'stock',
+    ),
+  );  
+  
+  return $data;
+}