Browse Source

Correcting a check of too many stock relationships

spficklin 12 years ago
parent
commit
c97f3a560e
1 changed files with 64 additions and 75 deletions
  1. 64 75
      tripal_stock/tripal_stock.module

+ 64 - 75
tripal_stock/tripal_stock.module

@@ -961,99 +961,88 @@ function tripal_stock_block($op = 'list', $delta = 0, $edit=array()) {
 function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
   // we want to provide a new variable that contains the matched stocks.
   $stock = $variables['node']->stock;
-
-  /*
-  // first check the size of the relationships. If there are too many we don't want to
-  // expand the stock object, it will take too long
-  $values = array('object_id' => $stock->stock_id);
-  $columns = array('count(*) as cnt');
-  $options = array('statement_name' => 'cnt_stockrelationship_ob');
-  $num_objs_rels = tripal_core_chado_select('stock_relationship', $columns, $values, $options);
-  
-  if($num_objs_rels[0]->cnt 25){
-  	
-  }*/
    
   // expand the stock object to include the stock relationships.
   $options = array(
     'order_by'=> array('rank' => 'ASC'),
-    'return_array' => 1
+    'return_array' => 1,
+    'statement_name' => 'exp_stock_rels'
   );
   $stock = tripal_core_expand_chado_vars($stock,'table', 'stock_relationship', $options);
 
-  // get the subject and object relationships
+  // get the subject relationships
   $srelationships = $stock->stock_relationship->subject_id;
+  if (!$srelationships) {
+     $srelationships = array();
+  } 
+  elseif (!is_array($srelationships)) { 
+     $srelationships = array($srelationships); 
+  }
+  
+  // get the object relationships
   $orelationships = $stock->stock_relationship->object_id;
+  if (!$orelationships) {
+     $orelationships = array();
+  } 
+  elseif (!is_array($orelationships)) { 
+     $orelationships = array($orelationships); 
+  }
   
   // combine both object and subject relationshisp into a single array
   $relationships = array();
   $relationships['object'] = array();
   $relationships['subject'] = array();
   
-  // if we have more than 25 it may make the page too slow to load
-  // in which case, we will return a number
-  if(count($orelationships) > 25) {
-     $relationships['object'] = count($relationships); 
-     $stock->stock_relationship->object_id = array();
-  } 
-  else {
-	  // iterate through the object relationships
-	  foreach ($orelationships as $relationship) {
-	     $rel = new stdClass();      
-	     $rel->record = $relationship;    
-	     
-	     // get the relationship and child types
-	     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
-	     $child_type = $relationship->subject_id->type_id->name;
-	     
-	     // get the node id of the subject
-	     $sql = "SELECT nid FROM chado_stock WHERE stock_id = %d";
-	     $n = db_fetch_object(db_query($sql,$relationship->subject_id->stock_id));
-	     if($n){
-	        $rel->record->nid = $n->nid;
-	     }
-	
-	     if (!array_key_exists($rel_type, $relationships['object'])) {
-	       $relationships['object'][$rel_type] = array();   
-	     }
-	     if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
-	       $relationships['object'][$rel_type][$child_type] = array();   
-	     }
-	     $relationships['object'][$rel_type][$child_type][] = $rel;     
-	  }
+   // iterate through the object relationships
+   foreach ($orelationships as $relationship) {
+     $rel = new stdClass();      
+     $rel->record = $relationship;    
+     
+     // get the relationship and child types
+     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
+     $child_type = $relationship->subject_id->type_id->name;
+     
+     // get the node id of the subject
+     $sql = "SELECT nid FROM chado_stock WHERE stock_id = %d";
+     $n = db_fetch_object(db_query($sql,$relationship->subject_id->stock_id));
+     if($n){
+        $rel->record->nid = $n->nid;
+     }
+
+     if (!array_key_exists($rel_type, $relationships['object'])) {
+       $relationships['object'][$rel_type] = array();   
+     }
+     if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
+       $relationships['object'][$rel_type][$child_type] = array();   
+     }
+     $relationships['object'][$rel_type][$child_type][] = $rel;     
   }
 
-  // if we have more than 25 it may make the page too slow to load
-  // in which case, we will return a number
-  if(count($srelationships) > 25) {
-     $relationships['subject'] = count($srelationships);
-     $stock->stock_relationship->subject_id = array();
-  }
-  else {
-	  // now add in the subject relationships
-	  foreach ($srelationships as $relationship) {
-	     $rel = new stdClass(); 
-	     
-	     $rel->record = $relationship;
-	     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
-	     $parent_type = $relationship->object_id->type_id->name;
-	     
-	     // get the node id of the subject
-	     $sql = "SELECT nid FROM chado_stock WHERE stock_id = %d";
-	     $n = db_fetch_object(db_query($sql,$relationship->object_id->stock_id));
-	     if($n){
-	        $rel->record->nid = $n->nid;
-	     }
-	     
-	     if (!array_key_exists($rel_type, $relationships['subject'])) {
-	       $relationships['subject'][$rel_type] = array();   
-	     }
-	     if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
-	       $relationships['subject'][$rel_type][$parent_type] = array();   
-	     }
-	     $relationships['subject'][$rel_type][$parent_type][] = $rel;
-	  }
+  
+  // now add in the subject relationships
+  foreach ($srelationships as $relationship) {
+     $rel = new stdClass(); 
+     
+     $rel->record = $relationship;
+     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
+     $parent_type = $relationship->object_id->type_id->name;
+     
+     // get the node id of the subject
+     $sql = "SELECT nid FROM chado_stock WHERE stock_id = %d";
+     $n = db_fetch_object(db_query($sql,$relationship->object_id->stock_id));
+     if($n){
+        $rel->record->nid = $n->nid;
+     }
+     
+     if (!array_key_exists($rel_type, $relationships['subject'])) {
+       $relationships['subject'][$rel_type] = array();   
+     }
+     if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
+       $relationships['subject'][$rel_type][$parent_type] = array();   
+     }
+     $relationships['subject'][$rel_type][$parent_type][] = $rel;
   }
+  
   $stock->all_relationships = $relationships;
 
 }