Browse Source

Changed get Node ID method: Now you can access 1+ Node ID field for 1+ table without conflict

laceysanderson 14 years ago
parent
commit
9522c82fc7

+ 1 - 30
tripal_analysis/tripal_analysis.views.inc

@@ -41,7 +41,7 @@ function chado_analysis_views_views_handlers() {
       'path' => drupal_get_path('module', 'tripal_analysis') . '/views/handlers',
     ),
     'handlers' => array(
-      'views_handler_field_computed_nid' => array(
+      'views_handler_field_computed_analysis_nid' => array(
         'parent' => 'views_handler_field_numeric',
       ),
       'views_handler_field_readable_date' => array(
@@ -51,32 +51,3 @@ function chado_analysis_views_views_handlers() {
   );
 }
 
-/**
- * Implements hook_views_pre_render
- * Purpose: Intercepts the view after the query has been executed
- *   All the results are stored in $view->result
- *   Looking up the NID here ensures the query is only executed once
- *   for all analysis' in the table.
- *
- * @todo add if !<chado/drupal same db> around NID portion
- */
-function tripal_analysis_views_pre_render	(&$view) {
-	if (preg_match('/analysis/', $view->base_table)) {
-		
-		// retrieve the analysis_id for each record in the views current page
-		$analysis_ids = array();
-		foreach ($view->result as $row_num => $row) {
-			$analysis_ids[$row_num] = $row->analysis_id;
-		}
-
-		// Using the list of analysis_ids from the view
-		// lookup the NIDs from drupal
-		// and add that to the results of the view
-		$sql = "SELECT nid, analysis_id FROM chado_analysis WHERE analysis_id IN (".implode(',',$analysis_ids).")";
-		$resource = db_query($sql);
-		while ($r = db_fetch_object($resource)) {
-			$key = array_search($r->analysis_id, $analysis_ids);
-			$view->result[$key]->nid = $r->nid;
-		}
-	}
-}

+ 2 - 2
tripal_analysis/views/analysis.views.inc

@@ -78,11 +78,11 @@ function retrieve_analysis_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['analysis']['nid'] = array(
+	$data['analysis']['analysis_nid'] = array(
 	  'title' => 'Node ID',
 	  'help' => 'The node ID for the current analysis',
 	  'field' => array(
-	    'handler' => 'views_handler_field_computed_nid',
+	    'handler' => 'views_handler_field_computed_analysis_nid',
 	  ),
 	);
 	

+ 18 - 0
tripal_analysis/views/handlers/views_handler_field_computed_analysis_nid.inc

@@ -0,0 +1,18 @@
+<?php
+
+class views_handler_field_computed_analysis_nid extends views_handler_field_numeric {
+	function construct() {
+		parent::construct();
+		drupal_set_message('Computed Analysis NID Handler loaded');
+		$this->additional_fields['analysis_id'] = array('table' => 'analysis', 'field' => 'analysis_id');
+	}
+
+	function query() { 
+		$this->ensure_my_table();
+		$this->add_additional_fields(); 
+	}
+
+	function render($values) { 
+		return $values->analysis_nid;
+	}
+} 

+ 0 - 1
tripal_analysis/views/handlers/views_handler_field_computed_nid.inc

@@ -1 +0,0 @@
-../../../tripal_core/views/handlers/views_handler_field_computed_nid.inc

+ 163 - 0
tripal_core/tripal_core.views.inc

@@ -1,15 +1,178 @@
 <?php
 
+/*************************************************************************
+ * Implements hook_views_pre_render
+ * Purpose: Intercepts the view after the query has been executed
+ *   All the results are stored in $view->result
+ *   Looking up the NID here ensures the query is only executed once
+ *   for all stocks in the table.
+ *
+ * @todo add if !<chado/drupal same db> around NID portion
+ */
+function tripal_core_views_pre_render	(&$view) {
+		
+		//Add Node IDs in to every table that needs them
+		// @see file: tripal_core.views.inc
+		tripal_core_add_node_ids_to_view (&$view);
+		
+}
+
 /*************************************************************************
  * Purpose: To add basetable_nid fields to all result arrays of a view
  *   only if the basetable_nid field is added. This function will only be
  *   called if chado/drupal are not in the same database (ie: only if 
  *   a join between the base and node table isn't possible.
+ * Note: Supports adding Node IDs to analysis, feature, library, organism, stock
  *
  * @params the view object passed to hook_views_pre_render
  * @return the views object with nids added to the result array
  */
 function tripal_core_add_node_ids_to_view (&$view) {
+	
+	//-----Analysis----------------------------------------------
+	if (!empty($view->field['analysis_nid'])) {
+		// retrieve the analysis_id for each record in the views current page
+		$analysis_ids = array();
+		foreach ($view->result as $row_num => $row) {
+			if (!empty($row->analysis_id)) {
+				//we're looking at analysis id field already in table
+				$analysis_ids[$row_num] = $row->analysis_id;
+			} else {
+				//we're looking at analysis id added by field handler
+				$analysis_ids[$row_num] = $row->analysis_analysis_id;
+			}
+		}
+		$unique_analysis_ids = array_filter($analysis_ids);
+		$unique_analysis_ids = array_unique($unique_analysis_ids);
+
+		// Using the list of analysis_ids from the view
+		// lookup the NIDs from drupal
+		// and add that to the results of the view
+		$sql = "SELECT nid, analysis_id FROM {chado_analysis} WHERE analysis_id IN (".implode(',',$unique_analysis_ids).")";
+		$resource = db_query($sql);
+		while ($r = db_fetch_object($resource)) {
+			$keys = array_keys($analysis_ids, $r->analysis_id);
+			foreach ($keys as $k) {
+				$view->result[$k]->analysis_nid = $r->nid;
+			}
+		}
+	} //end of case for analysis NID
+	
+	//-----Feature-----------------------------------------------
+	if (!empty($view->field['feature_nid'])) {
+		// retrieve the feature_id for each record in the views current page
+		$feature_ids = array();
+		foreach ($view->result as $row_num => $row) {
+			if (!empty($row->feature_id)) {
+				//we're looking at feature id field already in table
+				$feature_ids[$row_num] = $row->feature_id;
+			} else {
+				//we're looking at feature id added by field handler
+				$feature_ids[$row_num] = $row->feature_feature_id;
+			}
+		}
+		$unique_feature_ids = array_filter($feature_ids);
+		$unique_feature_ids = array_unique($unique_feature_ids);
+
+		// Using the list of feature_ids from the view
+		// lookup the NIDs from drupal
+		// and add that to the results of the view
+		$sql = "SELECT nid, feature_id FROM {chado_feature} WHERE feature_id IN (".implode(',',$unique_feature_ids).")";
+		$resource = db_query($sql);
+		while ($r = db_fetch_object($resource)) {
+			$keys = array_keys($feature_ids, $r->feature_id);
+			foreach ($keys as $k) {
+				$view->result[$k]->feature_nid = $r->nid;
+			}
+		}
+	} //end of case for feature NID
+	
+	//-----Library-----------------------------------------------
+	if (!empty($view->field['library_nid'])) {
+		// retrieve the library_id for each record in the views current page
+		$library_ids = array();
+		foreach ($view->result as $row_num => $row) {
+			if (!empty($row->library_id)) {
+				//we're looking at library id field already in table
+				$library_ids[$row_num] = $row->library_id;
+			} else {
+				//we're looking at library id added by field handler
+				$library_ids[$row_num] = $row->library_library_id;
+			}
+		}
+		$unique_library_ids = array_filter($library_ids);
+		$unique_library_ids = array_unique($unique_library_ids);
+
+		// Using the list of library_ids from the view
+		// lookup the NIDs from drupal
+		// and add that to the results of the view
+		$sql = "SELECT nid, library_id FROM {chado_library} WHERE library_id IN (".implode(',',$unique_library_ids).")";
+		$resource = db_query($sql);
+		while ($r = db_fetch_object($resource)) {
+			$keys = array_keys($library_ids, $r->library_id);
+			foreach ($keys as $k) {
+				$view->result[$k]->library_nid = $r->nid;
+			}
+		}
+	} //end of case for library NID
+	
+	//-----Organism----------------------------------------------
+	if (!empty($view->field['organism_nid'])) {
+		// retrieve the organism_id for each record in the views current page
+		$organism_ids = array();
+		foreach ($view->result as $row_num => $row) {
+			if (!empty($row->organism_id)) {
+				//we're looking at organism id field already in table
+				$organism_ids[$row_num] = $row->organism_id;
+			} else {
+				//we're looking at organism id added by field handler
+				$organism_ids[$row_num] = $row->organism_organism_id;
+			}
+		}
+		$unique_organism_ids = array_filter($organism_ids);
+		$unique_organism_ids = array_unique($unique_organism_ids);
+
+		// Using the list of organism_ids from the view
+		// lookup the NIDs from drupal
+		// and add that to the results of the view
+		$sql = "SELECT nid, organism_id FROM {chado_organism} WHERE organism_id IN (".implode(',',$unique_organism_ids).")";
+		$resource = db_query($sql);
+		while ($r = db_fetch_object($resource)) {
+			$keys = array_keys($organism_ids, $r->organism_id);
+			foreach ($keys as $k) {
+				$view->result[$k]->organism_nid = $r->nid;
+			}
+		}
+	} //end of case for organism NID
+	
+	//-----Stock-------------------------------------------------
+	if (!empty($view->field['stock_nid'])) {
+		// retrieve the stock_id for each record in the views current page
+		$stock_ids = array();
+		foreach ($view->result as $row_num => $row) {
+			if (!empty($row->stock_id)) {
+				//we're looking at stock id field already in table
+				$stock_ids[$row_num] = $row->stock_id;
+			} else {
+				//we're looking at stock id added by field handler
+				$stock_ids[$row_num] = $row->stock_stock_id;
+			}
+		}
+		$unique_stock_ids = array_filter($stock_ids);
+		$unique_stock_ids = array_unique($unique_stock_ids);
 
+		// Using the list of stock_ids from the view
+		// lookup the NIDs from drupal
+		// and add that to the results of the view
+		$sql = "SELECT nid, stock_id FROM {chado_stock} WHERE stock_id IN (".implode(',',$unique_stock_ids).")";
+		$resource = db_query($sql);
+		while ($r = db_fetch_object($resource)) {
+			$keys = array_keys($stock_ids, $r->stock_id);
+			foreach ($keys as $k) {
+				$view->result[$k]->stock_nid = $r->nid;
+			}
+		}
+	} //end of case for stock NID
+		
 	return $view;
 }

+ 0 - 15
tripal_core/views/handlers/views_handler_field_computed_nid.inc

@@ -1,15 +0,0 @@
-<?php
-
-class views_handler_field_computed_nid extends views_handler_field_numeric {
-	function construct() {
-		parent::construct();
-	}
-
-	function query() { 
-		$this->add_additional_fields(); 
-	}
-
-	function render($values) { 
-		return $values->nid;
-	}
-} 

+ 1 - 30
tripal_feature/tripal_feature.views.inc

@@ -43,7 +43,7 @@ function tripal_feature_views_handlers() {
      'path' => drupal_get_path('module', 'tripal_feature') . '/views/handlers',
    ),
    'handlers' => array(
-     'views_handler_field_computed_nid' => array(
+     'views_handler_field_computed_feature_nid' => array(
        'parent' => 'views_handler_field_numeric',
      ),
      'views_handler_field_readable_date' => array(
@@ -53,32 +53,3 @@ function tripal_feature_views_handlers() {
  );
 }
 
-/**
- * Implements hook_views_pre_render
- * Purpose: Intercepts the view after the query has been executed
- *   All the results are stored in $view->result
- *   Looking up the NID here ensures the query is only executed once
- *   for all features in the table.
- *
- * @todo add if !<chado/drupal same db> around NID portion
- */
-function tripal_feature_views_pre_render	(&$view) {
-	if (preg_match('/feature/', $view->base_table)) {
-		
-		// retrieve the feature_id for each record in the views current page
-		$feature_ids = array();
-		foreach ($view->result as $row_num => $row) {
-			$feature_ids[$row_num] = $row->feature_id;
-		}
-
-		// Using the list of feature_ids from the view
-		// lookup the NIDs from drupal
-		// and add that to the results of the view
-		$sql = "SELECT nid, feature_id FROM chado_feature WHERE feature_id IN (".implode(',',$feature_ids).")";
-		$resource = db_query($sql);
-		while ($r = db_fetch_object($resource)) {
-			$key = array_search($r->feature_id, $feature_ids);
-			$view->result[$key]->nid = $r->nid;
-		}
-	}
-}

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

@@ -64,11 +64,11 @@
   // 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['feature']['nid'] = array(
+  $data['feature']['feature_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_nid',
+      'handler' => 'views_handler_field_computed_feature_nid',
       ),
   );
 

+ 17 - 0
tripal_feature/views/handlers/views_handler_field_computed_feature_nid.inc

@@ -0,0 +1,17 @@
+<?php
+
+class views_handler_field_computed_feature_nid extends views_handler_field_numeric {
+	function construct() {
+		parent::construct();
+		$this->additional_fields['feature_id'] = array('table' => 'feature', 'field' => 'feature_id');
+	}
+
+	function query() { 
+		$this->ensure_my_table();
+		$this->add_additional_fields(); 
+	}
+
+	function render($values) { 
+		return $values->feature_nid;
+	}
+} 

+ 0 - 1
tripal_feature/views/handlers/views_handler_field_computed_nid.inc

@@ -1 +0,0 @@
-../../../tripal_core/views/handlers/views_handler_field_computed_nid.inc

+ 1 - 30
tripal_library/tripal_library.views.inc

@@ -41,7 +41,7 @@ function tripal_library_views_handlers() {
      'path' => drupal_get_path('module', 'tripal_library') . '/views/handlers',
    ),
    'handlers' => array(
-     'views_handler_field_computed_nid' => array(
+     'views_handler_field_computed_library_nid' => array(
        'parent' => 'views_handler_field_numeric',
      ),
      'views_handler_field_tf_boolean' => array(
@@ -54,32 +54,3 @@ function tripal_library_views_handlers() {
  );
 }
 
-/**
- * Implements hook_views_pre_render
- * Purpose: Intercepts the view after the query has been executed
- *   All the results are stored in $view->result
- *   Looking up the NID here ensures the query is only executed once
- *   for all features in the table.
- *
- * @todo add if !<chado/drupal same db> around NID portion
- */
-function tripal_library_views_pre_render	(&$view) {
-	if (preg_match('/library/', $view->base_table)) {
-		
-		// retrieve the library_id for each record in the views current page
-		$library_ids = array();
-		foreach ($view->result as $row_num => $row) {
-			$library_ids[$row_num] = $row->library_id;
-		}
-
-		// Using the list of library_ids from the view
-		// lookup the NIDs from drupal
-		// and add that to the results of the view
-		$sql = "SELECT nid, library_id FROM chado_library WHERE library_id IN (".implode(',',$library_ids).")";
-		$resource = db_query($sql);
-		while ($r = db_fetch_object($resource)) {
-			$key = array_search($r->library_id, $library_ids);
-			$view->result[$key]->nid = $r->nid;
-		}
-	}
-}

+ 17 - 0
tripal_library/views/handlers/views_handler_field_computed_library_nid.inc

@@ -0,0 +1,17 @@
+<?php
+
+class views_handler_field_computed_library_nid extends views_handler_field_numeric {
+	function construct() {
+		parent::construct();
+		$this->additional_fields['library_id'] = array('table' => 'library', 'field' => 'library_id');
+	}
+
+	function query() { 
+		$this->ensure_my_table();
+		$this->add_additional_fields(); 
+	}
+
+	function render($values) { 
+		return $values->library_nid;
+	}
+} 

+ 0 - 1
tripal_library/views/handlers/views_handler_field_computed_nid.inc

@@ -1 +0,0 @@
-../../../tripal_core/views/handlers/views_handler_field_computed_nid.inc

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

@@ -78,11 +78,11 @@ function retrieve_library_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['library']['nid'] = array(
+	$data['library']['library_nid'] = array(
 	  'title' => 'Node ID',
 	  'help' => 'The node ID for the current library',
 	  'field' => array(
-	    'handler' => 'views_handler_field_computed_nid',
+	    'handler' => 'views_handler_field_computed_library_nid',
 	  ),
 	);
 	

+ 1 - 31
tripal_organism/tripal_organism.views.inc

@@ -41,39 +41,9 @@ function tripal_organism_views_handlers() {
      'path' => drupal_get_path('module', 'tripal_organism') . '/views/handlers',
    ),
    'handlers' => array(
-     'views_handler_field_computed_nid' => array(
+     'views_handler_field_computed_organism_nid' => array(
        'parent' => 'views_handler_field_numeric',
      ),
    ),
  );
 }
-
-/**
- * Implements hook_views_pre_render
- * Purpose: Intercepts the view after the query has been executed
- *   All the results are stored in $view->result
- *   Looking up the NID here ensures the query is only executed once
- *   for all organisms in the table.
- *
- * @todo add if !<chado/drupal same db> around NID portion
- */
-function tripal_organism_views_pre_render	(&$view) {
-	if (preg_match('/organism/', $view->base_table)) {
-		
-		// retrieve the organism_id for each record in the views current page
-		$organism_ids = array();
-		foreach ($view->result as $row_num => $row) {
-			$organism_ids[$row_num] = $row->organism_id;
-		}
-
-		// Using the list of organism_ids from the view
-		// lookup the NIDs from drupal
-		// and add that to the results of the view
-		$sql = "SELECT nid, organism_id FROM chado_organism WHERE organism_id IN (".implode(',',$organism_ids).")";
-		$resource = db_query($sql);
-		while ($r = db_fetch_object($resource)) {
-			$key = array_search($r->organism_id, $organism_ids);
-			$view->result[$key]->nid = $r->nid;
-		}
-	}
-}

+ 0 - 1
tripal_organism/views/handlers/views_handler_field_computed_nid.inc

@@ -1 +0,0 @@
-../../../tripal_core/views/handlers/views_handler_field_computed_nid.inc

+ 17 - 0
tripal_organism/views/handlers/views_handler_field_computed_organism_nid.inc

@@ -0,0 +1,17 @@
+<?php
+
+class views_handler_field_computed_organism_nid extends views_handler_field_numeric {
+	function construct() {
+		parent::construct();
+		$this->additional_fields['organism_id'] = array('table' => 'organism', 'field' => 'organism_id');
+	}
+
+	function query() { 
+		$this->ensure_my_table();
+		$this->add_additional_fields(); 
+	}
+
+	function render($values) { 
+		return $values->organism_nid;
+	}
+} 

+ 2 - 2
tripal_organism/views/organism.views.inc

@@ -69,11 +69,11 @@ function retrieve_organism_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['organism']['nid'] = array(
+  $data['organism']['organism_nid'] = array(
     'title' => 'Node ID',
     'help' => 'This is the node ID of this organism. It can be used as a link to the node.',
     'field' => array(
-      'handler' => 'views_handler_field_computed_nid',
+      'handler' => 'views_handler_field_computed_organism_nid',
     ),    
   );
 

+ 5 - 12
tripal_stock/tripal_stock.views.inc

@@ -43,7 +43,7 @@ function tripal_stock_views_handlers() {
      'path' => drupal_get_path('module', 'tripal_stock') . '/views/handlers',
    ),
    'handlers' => array(
-     'views_handler_field_computed_nid' => array(
+     'views_handler_field_computed_stock_nid' => array(
        'parent' => 'views_handler_field_numeric',
      ),
      'views_handler_field_stockprop_by_type' => array(
@@ -68,7 +68,7 @@ function tripal_stock_views_handlers() {
  );
 }
 
-/**
+/*************************************************************************
  * Implements hook_views_pre_render
  * Purpose: Intercepts the view after the query has been executed
  *   All the results are stored in $view->result
@@ -80,21 +80,14 @@ function tripal_stock_views_handlers() {
 function tripal_stock_views_pre_render	(&$view) {
 	if (preg_match('/stock/', $view->base_table)) {
 		
+		//-----Node IDs---------------------------------------------
+		// @see file: tripal_core.views.inc function: tripal_core_add_node_ids_to_view (&$view);
+		
 		// retrieve the stock_id for each record in the views current page
 		$stock_ids = array();
 		foreach ($view->result as $row_num => $row) {
 			$stock_ids[$row_num] = $row->stock_id;
 		}
-
-		// Using the list of stock_ids from the view
-		// lookup the NIDs from drupal
-		// and add that to the results of the view
-		$sql = "SELECT nid, stock_id FROM chado_stock WHERE stock_id IN (".implode(',',$stock_ids).")";
-		$resource = db_query($sql);
-		while ($r = db_fetch_object($resource)) {
-			$key = array_search($r->stock_id, $stock_ids);
-			$view->result[$key]->nid = $r->nid;
-		}
 		
 		//-----Properties------------------------------------------
 		$field_names = array_keys($view->field);

+ 0 - 1
tripal_stock/views/handlers/views_handler_field_computed_nid.inc

@@ -1 +0,0 @@
-../../../tripal_core/views/handlers/views_handler_field_computed_nid.inc

+ 17 - 0
tripal_stock/views/handlers/views_handler_field_computed_stock_nid.inc

@@ -0,0 +1,17 @@
+<?php
+
+class views_handler_field_computed_stock_nid extends views_handler_field_numeric {
+	function construct() {
+		parent::construct();
+		$this->additional_fields['stock_id'] = array('table' => 'stock', 'field' => 'stock_id');
+	}
+
+	function query() { 
+		$this->ensure_my_table();
+		$this->add_additional_fields(); 
+	}
+
+	function render($values) { 
+		return $values->stock_nid;
+	}
+} 

+ 2 - 2
tripal_stock/views/stock.views.inc

@@ -70,11 +70,11 @@ 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']['nid'] = array(
+  $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_nid',
+       'handler' => 'views_handler_field_computed_stock_nid',
       ),
   );