Browse Source

Make tripal_stock views work when chado is internal -allow joining to node

laceysanderson 14 years ago
parent
commit
d5b2eed13d

+ 1 - 1
tripal_stock/tripal_stock.info

@@ -1,7 +1,7 @@
 ; $Id$
 package = Tripal
 name = Tripal Chado Stock
-version = 0.1a
+version = 3.0a
 description = A module for interfacing the GMOD chado database with Drupal, providing viewing, inserting and editing of stocks (including stock relationships and properties).
 core = 6.x
 

+ 2 - 0
tripal_stock/tripal_stock.views.inc

@@ -21,10 +21,12 @@
  *   relationships between that table and others (joins)
  */
 require_once('views/stock.views.inc');
+require_once('views/chado_stock.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());
   
   return $data;
 }

+ 50 - 0
tripal_stock/views/chado_stock.views.inc

@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the chado_stock drupal table, it's fields and any joins between it and other tables
+ * @see tripal_stock_views_data() --in tripal_stock.views.inc
+ *
+ * The main need for description of this table to views is to join chado data with drupal nodes
+ *
+ */
+function retrieve_chado_stock_views_data () {
+	global $db_url;
+  $data = array();
+  
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+     // return empty data array b/c if chado is external then no join to the nodetable can be made
+     return $data;
+  }
+
+  // Basic table definition
+  $data['chado_stock']['table'] = array(
+    'field' => 'nid',
+  );
+  
+  // Note: No joins need to be made from $data['stock']['table']
+  
+  // Join the chado stock table to stock
+  $data['chado_stock']['table']['join']['stock'] = array(
+  	'left_field' => 'stock_id',
+  	'field' => 'stock_id',
+  );
+  
+  // Join the node table to chado stock
+  $data['node']['table']['join']['chado_stock'] = array(
+  	'left_field' => 'nid',
+  	'field' => 'nid',
+  );
+  
+  // Join the node table to stock
+  $data['node']['table']['join']['stock'] = array(
+  	'left_table' => 'chado_stock',
+  	'left_field' => 'nid',
+  	'field' => 'nid',
+  );  
+
+	return $data;
+}

+ 19 - 8
tripal_stock/views/stock.views.inc

@@ -29,16 +29,27 @@
  * @endcode
  */
 function retrieve_stock_views_data() {
+  global $db_url;
   $data = array();
 
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+     $database = 'chado';
+  }
+
+
 	// Basic table definition
   $data['stock']['table']['group'] = t('Chado Stock');
   $data['stock']['table']['base'] = array(
     'field' => 'stock_id',
     'title' => t('Chado Stock'),
     'help' => t('Chado Stocks are a record of any material upon which an experiment can be done. For example, a DNA sample, an individual or a population.'),
-    'database' => 'chado'
   );
+  if($database){
+     $data['stock']['table']['database'] = $database;
+  }
 
 	// Define relationships between this table and others
   $data['stock']['table']['join'] = array(
@@ -47,8 +58,6 @@ function retrieve_stock_views_data() {
       'field' => 'organism_id',
     ),
   ); 
-  
-  
 
 	// Table Field Definitions----------------------
   // Field: feature_id (primary key)
@@ -70,13 +79,15 @@ function retrieve_stock_views_data() {
   // Calculated Field: Node ID
   //  use custom field handler to query drupal for the node ID
   //  this is only needed if chado is in a separate database from drupal
-  $data['stock']['stock_nid'] = array(
-    'title' => 'Node ID',
-    'help' => 'This is the node ID of this feature. It can be used as a link to the node.',
-    'field' => array(
+	if ($database){
+  	$data['stock']['stock_nid'] = array(
+    	'title' => 'Node ID',
+    	'help' => 'This is the node ID of this feature. It can be used as a link to the node.',
+    	'field' => array(
        'handler' => 'views_handler_field_computed_stock_nid',
       ),
-  );
+  	);
+  }
 
   //Field: unique name (text)
   $data['stock']['uniquename'] = array(