瀏覽代碼

Converted tripal_stock

spficklin 11 年之前
父節點
當前提交
c55cdb9d29

+ 29 - 29
tripal_library/tripal_library.module

@@ -299,35 +299,35 @@ function tripal_library_block_view($delta = '') {
 
     $block = array();
     switch ($delta) {
-    	case 'libreferences':
-    	  $block['subject'] = t('Cross References');
-    	  $block['content'] = theme('tripal_library_references', $node);
-    	  break;
-    	case 'libbase':
-    	  $block['subject'] = t('Library Details');
-    	  $block['content'] = theme('tripal_library_base', $node);
-    	  break;
-    	case 'libsynonyms':
-    	  $block['subject'] = t('Synonyms');
-    	  $block['content'] = theme('tripal_library_synonyms', $node);
-    	  break;
-    	case 'libproperties':
-    	  $block['subject'] = t('Properties');
-    	  $block['content'] = theme('tripal_library_properties', $node);
-    	  break;
-    	case 'libterms':
-    	  $block['subject'] = t('Library Terms');
-    	  $block['content'] = theme('tripal_library_terms', $node);
-    	  break;
-    	case 'featurelibs':
-    	  $block['subject'] = t('Libraries');
-    	  $block['content'] = theme('tripal_feature_libraries', $node);
-    	  break;
-    	case 'orglibs':
-    	  $block['subject'] = t('Libraries');
-    	  $block['content'] = theme('tripal_organism_libraries', $node);
-    	  break;
-    	default :
+      case 'libreferences':
+        $block['subject'] = t('Cross References');
+        $block['content'] = theme('tripal_library_references', $node);
+        break;
+      case 'libbase':
+        $block['subject'] = t('Library Details');
+        $block['content'] = theme('tripal_library_base', $node);
+        break;
+      case 'libsynonyms':
+        $block['subject'] = t('Synonyms');
+        $block['content'] = theme('tripal_library_synonyms', $node);
+        break;
+      case 'libproperties':
+        $block['subject'] = t('Properties');
+        $block['content'] = theme('tripal_library_properties', $node);
+        break;
+      case 'libterms':
+        $block['subject'] = t('Library Terms');
+        $block['content'] = theme('tripal_library_terms', $node);
+        break;
+      case 'featurelibs':
+        $block['subject'] = t('Libraries');
+        $block['content'] = theme('tripal_feature_libraries', $node);
+        break;
+      case 'orglibs':
+        $block['subject'] = t('Libraries');
+        $block['content'] = theme('tripal_organism_libraries', $node);
+        break;
+      default :
     }
     return $block;
   }

+ 3 - 3
tripal_stock/api/tripal_stock.api.inc

@@ -36,8 +36,8 @@ function tripal_stock_get_stock_by_nid($nid) {
  */
 function tripal_stock_get_stock_by_stock_id($stock_id) {
 
-  $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
-  $r = db_fetch_object(db_query($sql, $stock_id));
+  $sql = "SELECT nid FROM {chado_stock} WHERE stock_id = :stock_id";
+  $r = db_query($sql, array(':stock_id' => $stock_id))->fetchObject();
   if (!empty($r->nid)) {
     return node_load($r->nid);
   }
@@ -61,7 +61,7 @@ function tripal_stock_get_all_stocks() {
   $sql = "SELECT stock_id, nid from {chado_stock}";
   $resource = db_query($sql);
   $stocks = array();
-  while ($r = db_fetch_object($resource)) {
+  while ($r = $resource->fetchObject()) {
     $node = node_load($r->nid);
     if ($node) {
       $stocks[$r->stock_id] = $node;

+ 21 - 15
tripal_stock/includes/other_module_api_functions.inc

@@ -4,10 +4,6 @@
  * @todo Add file header description
  */
 
-///////////////////////////////////////////////////////////////////////////
-// Module: tripal_core
-///////////////////////////////////////////////////////////////////////////
-
 /*************************************************************************
  * Purpose: Get max rank for a given set of criteria
  *   This function was developed with the many property tables in chado in mind
@@ -28,26 +24,36 @@
  */
 function get_max_chado_rank($tablename, $where_options) {
 
-  $where= array();
+  $where = array();
   //generate the where clause from supplied options
   // the key is the column name
+  $args = array();
+  $i = 0;
+  $sql = "
+    SELECT max(rank) as max_rank, count(rank) as count 
+    FROM {:table} 
+    WHERE 1=1
+  ";
+  $args[":table"] = $tablename;
   foreach ($where_options as $key => $val_array) {
     if (preg_match('/INT/', $val_array['type'])) {
-      $where[] = $key . "=" . $val_array['value'];
+      $sql .= " AND $key = :$key$i ";
+      $args[":$key$i"] = $val_array['value'];
     }
     else {
       if ($val_array['exact']) {
-        $operator='='; }
-      else { $operator='~'; }
-        $where[] = $key . $operator . "'" . $val_array['value'] . "'";
+        $operator = '='; 
+      }
+      else { 
+        $operator = '~'; 
+      }
+      $sql .= " AND $key $operator :$key$i ";
+      $args[":$key$i"] = $val_array['value'];
     }
+    $i++;
   }
-
-  $result = db_fetch_object(chado_query(
-    "SELECT max(rank) as max_rank, count(rank) as count FROM {%s} WHERE %s",
-    $tablename,
-    implode(' AND ', $where)
-  ));
+   
+  $result = chado_query($sql, $tablename, $args)->fetchObject();
   //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
   if ($result->count > 0) {
     return $result->max_rank;

+ 19 - 32
tripal_stock/includes/tripal_stock-db_references.inc

@@ -115,7 +115,8 @@ function tripal_stock_add_ONE_dbreference_form_validate($form, &$form_state) {
 
     // Check database is valid db_id in chado
     if ( $form_state['values']['database'] > 0) {
-      $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $form_state['values']['database']));
+      $sql = "SELECT count(*) as count FROM {db} WHERE db_id = :db_id";
+      $tmp_obj = chado_query($sql, array(':db_id' => $form_state['values']['database']))->fetchObject();
 
       if ($tmp_obj->count != 1) {
         form_set_error('database', 'The database you selected is not valid. Please choose another one.');
@@ -126,7 +127,8 @@ function tripal_stock_add_ONE_dbreference_form_validate($form, &$form_state) {
     }
 
     // Check Accession is unique for database
-    $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {dbxref} WHERE accession='%s'", $form_state['values']['accession']));
+    $sql =  "SELECT count(*) as count FROM {dbxref} WHERE accession = :accession";
+    $tmp_obj = chado_query($sql, array(':accession' => $form_state['values']['accession']))->fetchObject();
 
     if ($tmp_obj->count > 0) {
       form_set_error('accession', 'This accession has already been assigned to another stock.');
@@ -158,21 +160,21 @@ function tripal_stock_add_ONE_dbreference_form_submit($form, &$form_state) {
 
 
     // create dbxref
-    chado_query(
-      "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')",
-      $form_state['values']['database'],
-      $form_state['values']['accession'],
-      $form_state['values']['db_description']
-    );
+    $sql = "
+      INSERT INTO {dbxref} (db_id, accession, description) 
+      VALUES (:db_id, :accession, :description)
+    ";
+    chado_query($sql, array(':db_id' => $form_state['values']['database'], 
+      ':accession' => $form_state['values']['accession'], 
+      ':description' => $form_state['values']['db_description']
+    ));
 
     //create stock_dbxref
     $dbxref = tripal_db_get_dbxref_by_accession($form_state['values']['accession'], $form_state['values']['database']);
     if (!empty($dbxref->dbxref_id)) {
-      chado_query(
-        "INSERT INTO {stock_dbxref} (stock_id, dbxref_id) VALUES (%d, %d)",
-        $form_state['values']['db_stock_id'],
-        $dbxref->dbxref_id
-      );
+      $sl = "INSERT INTO {stock_dbxref} (stock_id, dbxref_id) VALUES (:stock_id, :dbxref_id)";
+      chado_query($sql, array(':stock_id' => $form_state['values']['db_stock_id'],
+        ':dbxref_id' => $dbxref->dbxref_id));
 
       drupal_set_message(t('Successfully Added Database Reference'));
     }
@@ -347,14 +349,8 @@ function tripal_stock_edit_ALL_db_references_form_submit($form, &$form_state) {
  * @ingroup tripal_stock
  */
 function tripal_stock_update_db_reference($dbxref_id, $database_id, $accession) {
-
-  chado_query(
-    "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d",
-    $database_id,
-    $accession,
-    $dbxref_id
-  );
-
+  $sql = "UPDATE {dbxref} SET db_id = :db_id, accession = :accession WHERE dbxref_id = :dbxref_id";
+  chado_query($sql, array(':db_id' => $database_id, ':accession' => $accession, ':dbxref_id' =>  $dbxref_id));
 }
 
 /**
@@ -369,17 +365,8 @@ function tripal_stock_update_db_reference($dbxref_id, $database_id, $accession)
  * @ingroup tripal_stock
  */
 function tripal_stock_delete_db_reference($dbxref_id) {
-
-  chado_query(
-    "DELETE FROM {dbxref} WHERE dbxref_id=%d",
-    $dbxref_id
-  );
-
-  chado_query(
-    "DELETE FROM {stock_dbxref} WHERE dbxref_id=%d",
-    $dbxref_id
-  );
-
+  chado_query("DELETE FROM {dbxref} WHERE dbxref_id = :dbxref_id", array(':dbxref_id' => $dbxref_id));
+  chado_query("DELETE FROM {stock_dbxref} WHERE dbxref_id = :dbxref_id", array(':dbxref_id' => $dbxref_id));
 }
 
 /**

+ 34 - 38
tripal_stock/includes/tripal_stock-properties.inc

@@ -99,7 +99,7 @@ function tripal_stock_add_ONE_property_form_validate($form, &$form_state) {
     }
     else {
       // Check that type is in chado
-      $num_rows = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['prop_type_id']));
+      $num_rows = chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['prop_type_id'])->fetchObject();
       if ( $num_rows->count != 1) {
         form_set_error('prop_type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)");
       } // end of if more or less than 1 row
@@ -134,14 +134,15 @@ function tripal_stock_add_ONE_property_form_submit($form, &$form_state) {
     $rank = 0;
     }
     else { $rank = $max_rank+1; }
-
-    chado_query(
-      "INSERT INTO {stockprop} (stock_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)",
-      $form_state['values']['prop_stock_id'],
-      $form_state['values']['prop_type_id'],
-      $form_state['values']['prop_value'],
-      $rank
-    );
+    
+    $sql = "
+      INSERT INTO {stockprop} (stock_id, type_id, value, rank) 
+      VALUES (:stock_id, :type_id, :value, :rank)
+    ";
+    chado_query($sql, array(':stock_id' => $form_state['values']['prop_stock_id'],
+      ':type_id' => $form_state['values']['prop_type_id'],
+      ':value' => $form_state['values']['prop_value'],
+      ':rank' => $rank));
 
     drupal_set_message(t("Successfully Added Property"));
 
@@ -158,7 +159,7 @@ function tripal_stock_add_ONE_property_form_submit($form, &$form_state) {
         )
       );
       module_load_include('inc', 'node', 'node.pages');
-      drupal_execute('chado_stock_node_form', $node_form_state, $node);
+      drupal_form_submit('chado_stock_node_form', $node_form_state, $node);
 
     }
 
@@ -287,34 +288,34 @@ function tripal_stock_edit_ALL_properties_form_submit($form, &$form_state) {
  */
 function tripal_stock_update_property($stockprop_id, $cvterm_id, $value, $preferred, $nid) {
 
-  $old_obj = db_fetch_object(chado_query("SELECT * FROM {stockprop} WHERE stockprop_id=%d", $stockprop_id));
+  $old_obj = chado_query("SELECT * FROM {stockprop} WHERE stockprop_id=%d", $stockprop_id)->fetchObject();
 
   // if they changed the type need to check rank
   //   (if there is another property of the same type then rank needs to be increased to prevent collisions)
   if ($cvterm_id == $old_obj->type_id) {
-    chado_query(
-      "UPDATE {stockprop} SET type_id=%d, value='%s' WHERE stockprop_id=%d",
-      $cvterm_id,
-      $value,
-      $stockprop_id
-    );
+    $sql = "
+      UPDATE {stockprop} SET type_id = :type_id, value = :value
+      WHERE stockprop_id = :stockprop_id
+    ";
+    chado_query($sql, array(':type_id' => $cvterm_id,
+      ':value' => $value, ':stockprop_id' => $stockprop_id));
   }
   else {
       //determine the rank for this property
-    $max_rank = get_max_chado_rank('stockprop',
-                                  array('stock_id' => array('type' => 'INT', 'value' => $old_obj->stock_id),
-                                        'type_id' => array('type' => 'INT', 'value' => $cvterm_id ) ));
+    $max_rank = get_max_chado_rank('stockprop', 
+      array('stock_id' => array('type' => 'INT', 'value' => $old_obj->stock_id),
+      'type_id' => array('type' => 'INT', 'value' => $cvterm_id))
+    );
     if ($max_rank == -1) {
       $rank = 0;
     }
     else { $rank = $max_rank+1; }
-      chado_query(
-        "UPDATE {stockprop} SET type_id=%d, value='%s', rank=%d WHERE stockprop_id=%d",
-        $cvterm_id,
-        $value,
-        $rank,
-        $stockprop_id
-     );
+      $sql = "
+        UPDATE {stockprop} SET type_id = :type_id, value = :value, rank = :rank 
+        WHERE stockprop_id = :stockprop_id
+      ";
+      chado_query($sql, array(':type_id' => $cvterm_id, ':value' => $value,
+        ':rank' => $rank, ':stockprop_id' => $stockprop_id));
   }
 
   // Set Preferred Synonym
@@ -329,7 +330,7 @@ function tripal_stock_update_property($stockprop_id, $cvterm_id, $value, $prefer
       )
     );
     module_load_include('inc', 'node', 'node.pages');
-    drupal_execute('chado_stock_node_form', $node_form_state, $node);
+    drupal_form_submit('chado_stock_node_form', $node_form_state, $node);
   }
 }
 
@@ -339,12 +340,7 @@ function tripal_stock_update_property($stockprop_id, $cvterm_id, $value, $prefer
  * @ingroup tripal_stock
  */
 function tripal_stock_delete_property($stockprop_id) {
-
-  chado_query(
-    "DELETE FROM {stockprop} WHERE stockprop_id=%d",
-    $stockprop_id
-  );
-
+  chado_query("DELETE FROM {stockprop} WHERE stockprop_id = :stockprop_id", array(':stockprop_id' => $stockprop_id));
 }
 
 /**
@@ -357,10 +353,10 @@ function theme_tripal_stock_edit_ALL_properties_form($form) {
 
   $output .= '<br /><fieldset>';
   $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
-  $output .= '<p>Below is a list of already existing properties for this stock, one property per line. The type refers to the type of '
-         .'property and the value is the value for that property. For example, if this stock has a seed coat colour of green then '
-       .'the property type=sead coat colour and the value=green. When the type of property is synonym, there is an extra checkbox '
-       .'allowing you to specify which is the <b>Preferred Synonym</b>. This will change the current name of the stock.</p>';
+  $output .= '<p>Below is a list of already existing properties for this stock, one property per line. The type refers to the type of ' .
+             'property and the value is the value for that property. For example, if this stock has a seed coat colour of green then ' .
+             'the property type=sead coat colour and the value=green. When the type of property is synonym, there is an extra checkbox ' .
+             'allowing you to specify which is the <b>Preferred Synonym</b>. This will change the current name of the stock.</p>';
   $output .= '<table>';
   $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
 

+ 23 - 26
tripal_stock/includes/tripal_stock-relationships.inc

@@ -154,7 +154,8 @@ function tripal_stock_add_ONE_relationship_form_validate($form, &$form_state) {
       form_set_error('type_id', 'Please select a type of relationship.');
     }
     else {
-      $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['type_id']));
+      $sql = "SELECT count(*) as count FROM {cvterm} WHERE cvterm_id = :cvterm_id";
+      $tmp_obj = chado_query($sql, array(':cvterm_id' => $form_state['values']['type_id']))->fetchObject();
 
       if ($tmp_obj->count != 1) {
         form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
@@ -178,13 +179,14 @@ function tripal_stock_add_ONE_relationship_form_validate($form, &$form_state) {
 function tripal_stock_add_ONE_relationship_form_submit($form, &$form_state) {
 
   if ($form_state['values']['subject_id'] > 0) {
-    chado_query(
-      "INSERT INTO {stock_relationship} (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
-      $form_state['values']['subject_id'],
-      $form_state['values']['type_id'],
-      $form_state['values']['object_id'],
-      $form_state['values']['r_description']
-    );
+    $sql = "
+      INSERT INTO {stock_relationship} (subject_id, type_id, object_id, value) 
+      VALUES (:subject_id, :type_id, :object_id, :value)
+    ";
+    chado_query($sql, array(':subject_id' => $form_state['values']['subject_id'],
+      ':type_id' => $form_state['values']['type_id'],
+      ':object_id' => $form_state['values']['object_id'],
+      ':value' => $form_state['values']['r_description']));
 
     drupal_set_message(t('Successfully Added Relationship.'));
   } //end of insert relationship
@@ -381,7 +383,8 @@ function tripal_stock_edit_ALL_relationships_form_validate($form, &$form_state)
         form_set_error('type_id', 'Please select a type of relationship.');
       }
       else {
-        $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']["type_id-$i"]));
+        $sql = "SELECT count(*) as count FROM {cvterm} WHERE cvterm_id= :cvterm_id";
+        $tmp_obj = chado_query($sql, array(':cvterm_id' => $form_state['values']["type_id-$i"]))->fetchObject();
 
         if ($tmp_obj->count != 1) {
           form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
@@ -446,14 +449,12 @@ function tripal_stock_edit_ALL_relationships_form_submit($form, &$form_state) {
  */
 function tripal_stock_update_relationship($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
 
-  chado_query(
-    "UPDATE {stock_relationship} SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
-    $subject_id,
-    $cvterm_id,
-    $object_id,
-    $stock_relationship_id
-  );
-
+  $sql = "
+    UPDATE {stock_relationship} SET subject_id = :subject_id, type_id = :type_id, object_id = :object_id 
+    WHERE stock_relationship_id = :stock_relationship_id
+  ";
+  chado_query($sql, array(':subject_id' => $subject_id, ':type_id' => $cvterm_id,
+    ':object_id' => $object_id, ':stock_relationship_id' => $stock_relationship_id));
 }
 
 /**
@@ -462,12 +463,8 @@ function tripal_stock_update_relationship($stock_relationship_id, $subject_id, $
  * @ingroup tripal_stock
  */
 function tripal_stock_delete_relationship($stock_relationship_id) {
-
-  chado_query(
-    "DELETE FROM {stock_relationship} WHERE stock_relationship_id=%d",
-    $stock_relationship_id
-  );
-
+  $sql = "DELETE FROM {stock_relationship} WHERE stock_relationship_id = :stock_relationship_id";
+  chado_query($sql, array(':stock_relationship_id' => $stock_relationship_id));
 }
 
 /**
@@ -480,9 +477,9 @@ function theme_tripal_stock_edit_ALL_relationships_form($form) {
 
   $output .= '<br /><fieldset>';
   $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
-  $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
-        . 'the subject and object of the relationship can contain the uniquename, name, database '
-      . 'reference or synonym of a stock of the same organism.</p>';
+  $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating ' .
+             'the subject and object of the relationship can contain the uniquename, name, database ' .
+             'reference or synonym of a stock of the same organism.</p>';
   $output .= '<table>';
   $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
 

+ 2 - 6
tripal_stock/includes/tripal_stock-secondary_tables.inc

@@ -222,11 +222,7 @@ function tripal_stock_is_obsolete_form($node, $stock_id) {
  * @ingroup tripal_stock
  */
 function tripal_stock_is_obsolete_form_submit($form, &$form_state) {
-
-  chado_query(
-    "UPDATE {stock} SET is_obsolete='t' WHERE stock_id=%d",
-    $form_state['values']['make_obsolete_stock_id']
-  );
-
+  $sql = "UPDATE {stock} SET is_obsolete='t' WHERE stock_id= :stock_id";
+  chado_query($sql, array(':stock_id' => $form_state['values']['make_obsolete_stock_id']));
 }
 

+ 47 - 56
tripal_stock/includes/tripal_stock.sync_stocks.inc

@@ -132,7 +132,7 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
   
   // get the number of records we need to set URLs for
   $csql = "SELECT count(*) FROM {chado_stock}";
-  $num_nodes = db_result(db_query($csql));
+  $num_nodes = db_query($csql)->fetchField();
     
   // calculate the interval at which we will print an update on the screen
   $num_set = 0;
@@ -162,7 +162,7 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
   // get the list of stocks that have been synced
   $sql = "SELECT * FROM {chado_stock}";
   $nodes = db_query($sql);  
-  while ($node = db_fetch_object($nodes)) {
+  while ($node = $nodes->fetchObject()) {
    
     // get the URL alias
     $src = "node/$node->nid";
@@ -181,7 +181,7 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
     }
     
     // remove any previous alias and then add the new one
-    $success = db_query("EXECUTE del_url_alias_by_src('%s')", $src);    
+    $success = db_query("EXECUTE del_url_alias_by_src(':src')", array(':src' => $src));    
     if (!$success) {
       db_query('DEALLOCATE "del_url_alias_by_src"');
       db_query('DEALLOCATE "ins_url_alias_nisrds"');
@@ -189,7 +189,7 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
       watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
       return;
     }
-    $success = db_query("EXECUTE ins_url_alias_nisrds('%s', '%s')", $src, $dst);
+    $success = db_query("EXECUTE ins_url_alias_nisrds(:src, :dst)", array(':src' => $src, ':dst' => $dst));
     if (!$success) {
       db_query('DEALLOCATE "del_url_alias_by_src"');
       db_query('DEALLOCATE "ins_url_alias_nisrds"');
@@ -309,7 +309,8 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
   else {
     $allowed_types = $stock_types;
   }
-  
+  $args = array();
+  $i = 0;
   if ($allowed_types) {
     $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
     print "Looking for stocks of type: $allowed_types\n";
@@ -317,9 +318,11 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
     $so_terms = split(' ', $allowed_types);
     $where_cvt = "";
     foreach ($so_terms as $term) {
-      $where_cvt .= "CVT.name = '$term' OR ";
+      $where_cvt .= "CVT.name = :cvterm$i OR ";
+      $args[":cvterm$i"] = $term;
     }
     $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3);  # strip trailing 'OR'
+    $i++;
   }
   else {
     $where_cvt = '1=1';
@@ -329,34 +332,40 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
   // those organisms
   $orgs = tripal_organism_get_synced();
   $where_org = "";
+  $i = 0;
   foreach ($orgs as $org) {
     if ($organism_id) {
       if ($org->organism_id and $org->organism_id == $organism_id) {
-        $where_org .= "S.organism_id = $org->organism_id OR ";
+        $where_org .= "S.organism_id = :organism_id$i OR ";
+        $args[":organism_id$i"] = $org->organism_id;
       }
     }
     else {
       if ($org->organism_id) {
-        $where_org .= "S.organism_id = $org->organism_id OR ";
+        $where_org .= "S.organism_id = :organism_id$i OR ";
+        $args[":organism_id$i"] = $org->organism_id;
       }
     }
+    $i++;
   }
   $where_org = drupal_substr($where_org, 0, drupal_strlen($where_org)-3);  # strip trailing 'OR'
 
   // use this SQL statement to get the stocks that we're going to upload
-  $sql = "SELECT stock_id ".
-        "FROM {stock} S ".
-        "  INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id ".
-        "WHERE ($where_cvt) AND ($where_org) ".
-        "ORDER BY stock_id";
+  $sql = "
+    SELECT stock_id
+    FROM {stock} S 
+      INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id 
+    WHERE ($where_cvt) AND ($where_org) 
+    ORDER BY stock_id
+  ";
 
   // get the list of stocks
-  $results = chado_query($sql);
+  $results = chado_query($sql, $args);
 
   // load into ids array
   $count = 0;
   $ids = array();
-  while ($id = db_fetch_object($results)) {
+  while ($id = $results->fetchObject()) {
     $ids[$count] = $id->stock_id;
     $count++;
   }
@@ -367,7 +376,7 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
   // pre-create the SQL statement that will be used to check
   // if a stock has already been synced.  We skip stocks
   // that have been synced
-  $sql = "SELECT * FROM {chado_stock} WHERE stock_id = %d";
+  $sql = "SELECT * FROM {chado_stock} WHERE stock_id = :stock_id";
 
   // Iterate through stocks that need to be synced
   $interval = intval($count * 0.01);
@@ -386,7 +395,7 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
     if ($max_sync and $i == $max_sync) {
       return '';
     }
-    if (!db_fetch_object(db_query($sql, $stock_id))) {
+    if (!db_query($sql, array(':stock_id' => $stock_id))->fetchObject()) {
 
       # parsing all the stocks can cause memory overruns
       # we are not sure why PHP does not clean up the memory as it goes
@@ -426,41 +435,25 @@ global $user;
   }
 
   // get information about this stock
-  $fsql = "SELECT S.*, O.genus, O.species,CVT.name as cvname ".
-          "FROM {stock} S ".
-          "  INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id ".
-          "  INNER JOIN {organism} O ON S.organism_id = O.organism_ID ".
-          "WHERE S.stock_id = %d";
-  $stock = db_fetch_object(chado_query($fsql, $stock_id));
+  $fsql = "
+     SELECT S.*, O.genus, O.species,CVT.name as cvname 
+     FROM {stock} S 
+       INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id 
+       INNER JOIN {organism} O ON S.organism_id = O.organism_ID 
+    WHERE S.stock_id = :stock_id
+   ";
+  $stock = chado_query($fsql, array(':stock_id' => $stock_id))->fetchObject();
   
-  /*
-  // get the synonyms for this stock
-  $synsql = "SELECT S.name ".
-            "FROM {stock_synonym} SS ".
-            "  INNER JOIN {synonym} S on SS.synonym_id = S.synonym_id ".
-            "WHERE SS.stock_id = %d";
-  $synonyms = chado_query($synsql, $stock_id);  
-    
-  // now add these synonyms to the stock object as a single string
-  $synstring = '';
-  while ($synonym = db_fetch_object($synonyms)) {
-    $synstring .= "$synonym->name\n";
-  }
-  $stock->synonyms = $synstring;
-  */
-
   // check to make sure that we don't have any nodes with this stock name as a title
   // but without a corresponding entry in the chado_stock table if so then we want to
   // clean up that node.  (If a node is found we don't know if it belongs to our stock or
   // not since stocks can have the same name/title.)
-  $tsql =  "SELECT * FROM {node} N ".
-           "WHERE title = '%s'";
-  $cnsql = "SELECT * FROM {chado_stock} ".
-           "WHERE nid = %d";
-  $nodes = db_query($tsql, $stock->name);
+  $tsql =  "SELECT * FROM {node} N WHERE title = :title";
+  $cnsql = "SELECT * FROM {chado_stock} WHERE nid =:nid";
+  $nodes = db_query($tsql, array(':title' => $stock->name));
   // cycle through all nodes that may have this title
-  while ($node = db_fetch_object($nodes)) {
-    $stock_nid = db_fetch_object(db_query($cnsql, $node->nid));
+  while ($node = $nodes->fetchObject()) {
+    $stock_nid = db_query($cnsql, array(':nid' => $node->nid))->fetchObject();
     if (!$stock_nid) {
       drupal_set_message(t("%stock_id: A node is present but the chado_stock entry is missing... correcting", array('%stock_id' => $stock_id)));
       node_delete($node->nid);
@@ -469,20 +462,18 @@ global $user;
 
   // check if this stock already exists in the chado_stock table.
   // if we have a chado stock, we want to check to see if we have a node
-  $cfsql = "SELECT * FROM {chado_stock} ".
-           "WHERE stock_id = %d";
-  $nsql =  "SELECT * FROM {node} N ".
-           "WHERE nid = %d";
-  $chado_stock = db_fetch_object(db_query($cfsql, $stock->stock_id));
+  $cfsql = "SELECT * FROM {chado_stock} WHERE stock_id = :stock_id";
+  $nsql =  "SELECT * FROM {node} N WHERE nid = :nid";
+  $chado_stock = db_query($cfsql, array(':stock_id' => $stock->stock_id))->fetchObject();
   if ($chado_stock) {
     drupal_set_message(t("%stock_id: A chado_stock entry exists", array('%stock_id' => $stock_id)));
-    $node = db_fetch_object(db_query($nsql, $chado_stock->nid));
+    $node = db_query($nsql, array(':nid' => $chado_stock->nid))->fetchObject();
     if (!$node) {
       // if we have a chado_stock but not a node then we have a problem and
       // need to cleanup
       drupal_set_message(t("%stock_id: The node is missing, but has a chado_stock entry... correcting", array('%stock_id' => $stock_id)));
-      $df_sql = "DELETE FROM {chado_stock} WHERE stock_id = %d";
-      db_query($df_sql, $stock_id);
+      $df_sql = "DELETE FROM {chado_stock} WHERE stock_id = :stock_id";
+      db_query($df_sql, array(':stock_id' => $stock_id));
     }
     else {
       drupal_set_message(t("%stock_id: A corresponding node exists", array('%stock_id' => $stock_id)));
@@ -501,8 +492,8 @@ global $user;
   // will call the hook_submit function which
   if ($create_node) {
     // get the organism for this stock
-    $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
-    $organism = db_fetch_object(chado_query($sql, $stock->organism_id));
+    $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
+    $organism = chado_query($sql, array(':organism_id' => $stock->organism_id))->fetchObject();
 
     drupal_set_message(t("%stock_id: Creating node $stock->name", array('%stock_id' => $stock_id)));
     $new_node = new stdClass();

+ 2 - 2
tripal_stock/tripal_stock.info

@@ -1,8 +1,8 @@
 name = Tripal Stock
 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
+core = 7.x
 project = tripal_stock
 package = Tripal
-version = 6.x-1.1
+version = 7.x-2.0-beta1
 dependencies[] = tripal_core
 dependencies[] = tripal_organism

+ 27 - 41
tripal_stock/tripal_stock.install

@@ -6,15 +6,31 @@
  * @file
  */
 
+/**
+ * Implementation of hook_requirements().
+ *
+ */
+function tripal_stock_requirements($phase) {
+  $requirements = array();
+  if ($phase == 'install') {
+    // make sure chado is installed
+    if (!tripal_core_is_chado_installed()) {
+      $requirements ['tripal_stock'] = array(
+        'title' => "tripal_stock",
+        'value' => "ERROR: Chado most be installed before this module can be enabled",
+        'severity' => REQUIREMENT_ERROR,
+      );
+    }
+  }
+  return $requirements;
+}
+
 /**
  * Implementation of hook_install().
  */
 function tripal_stock_install() {
   // create the module's data directory
   tripal_create_moddir('tripal_stock');
-  
-  // create the stock tables
-  drupal_install_schema('tripal_stock');
 }
 
 /**
@@ -22,18 +38,6 @@ function tripal_stock_install() {
  */
 
 function tripal_stock_uninstall() {
-  drupal_uninstall_schema('tripal_stock');
-
-  // Get the list of nodes to remove
-  $sql_lib_id = "SELECT nid, vid ".
-                 "FROM {node} ".
-                 "WHERE type='chado_stock'";
-  $result = db_query($sql_lib_id);
-
-  //delete all nodes
-  while ($node = db_fetch_object($result)) {
-  node_delete($node->nid);
-  }
 
 }
 
@@ -47,16 +51,16 @@ function tripal_stock_schema() {
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
-        ),
+      ),
       'nid' => array(
-          'type' => 'int',
-          'unsigned' => TRUE,
-          'not null' => TRUE,
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
       ),
-        'stock_id' => array(
-          'type' => 'int',
-          'unsigned' => TRUE,
-          'not null' => TRUE,
+      'stock_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
       ),
     ),
     'indexes' => array(
@@ -71,21 +75,3 @@ function tripal_stock_schema() {
 
   return $schema;
 }
-/**
- * Implementation of hook_requirements(). 
- *
- */
-function tripal_stock_requirements($phase) {
-  $requirements = array();
-  if ($phase == 'install') {
-    // make sure chado is installed
-    if (!tripal_core_is_chado_installed()) {
-      $requirements ['tripal_stock'] = array(
-            'title' => "tripal_stock",
-            'value' => "ERROR: Chado most be installed before this module can be enabled",
-            'severity' => REQUIREMENT_ERROR,
-      );
-    }
-  }
-  return $requirements;
-}

+ 209 - 228
tripal_stock/tripal_stock.module

@@ -174,13 +174,28 @@ function cs_node_load($nid) {
  *
  * @ingroup tripal_stock
  */
-function tripal_stock_perm() {
+function tripal_stock_permissions() {
   return array(
-    'access chado_stock content',
-    'create chado_stock content',
-    'edit chado_stock content',
-    'delete chado_stock content',
-    'administer tripal stocks',
+    'access chado_stock content' => array(
+      'title' => t('View Stocks'),
+      'description' => t('Allow users to view stock pages.'),
+    ),
+    'create chado_stock content' => array(
+      'title' => t('Create Stocks'),
+      'description' => t('Allow users to create new stock pages.'),
+    ),
+    'delete chado_stock content' => array(
+      'title' => t('Delete Stocks'),
+      'description' => t('Allow users to delete stock pages.'),
+    ),
+    'edit chado_stock content' => array(
+      'title' => t('Edit Stocks'),
+      'description' => t('Allow users to edit stock pages.'),
+    ),
+    'adminster tripal stock' => array(
+      'title' => t('Administer Stocks'),
+      'description' => t('Allow users to administer all stocks.'),
+    ),
   );
 }
 
@@ -357,7 +372,7 @@ function tripal_stock_node_info() {
   return array(
     'chado_stock' => array(
       'name' => t('Stock'),
-      'module' => 'chado_stock',
+      'base' => 'chado_stock',
       'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
       'has_title' => TRUE,
       'has_body' => FALSE,
@@ -522,7 +537,7 @@ function chado_stock_form($node, $form_state) {
   $org_rset = chado_query($sql);
   $organisms = array();
   $organisms[''] = '';
-  while ($organism = db_fetch_object($org_rset)) {
+  while ($organism = $org_rset->fetchObject()) {
     $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
   }
   $form['details']['organism_id'] = array(
@@ -586,19 +601,24 @@ function chado_stock_form($node, $form_state) {
  */
 function chado_stock_validate($node, &$form) {
 
-  $int_in_chado_sql = "SELECT count(*) as count FROM {%s} WHERE %s=%d";
-  $string_in_chado_sql = "SELECT count(*) as count FROM {%s} WHERE %s='%s'";
+  $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
+  $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
 
   // if this is an update, we want to make sure that a different stock for
   // the organism doesn't already have this uniquename. We don't want to give
   // two sequences the same uniquename
   if ($node->stock_id) {
-    $sql = "SELECT *
-            FROM {stock} S
-              INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
-            WHERE uniquename = '%s'
-             AND organism_id = %d AND CVT.name = '%s' AND NOT stock_id = %d";
-    $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->stock_type, $node->stock_id));
+    $sql = "
+      SELECT *
+      FROM {stock} S
+        INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
+      WHERE 
+        uniquename = :uname AND organism_id = :organism_id AND 
+        CVT.name = :cvtname AND NOT stock_id = :stock_id
+    ";
+    $result = chado_query($sql, array(':uname' => $node->uniquename, 
+      ':organism_id' => $node->organism_id, ':cvtname' => $node->stock_type, 
+      ':stock_id' => $node->stock_id))->fetchObject();
     if ($result) {
       form_set_error('uniquename', t("Stock update cannot proceed. The stock name '$node->uniquename' is not unique for this organism. Please provide a unique name for this stock."));
     }
@@ -607,12 +627,13 @@ function chado_stock_validate($node, &$form) {
   // if this is an insert then we just need to make sure this name doesn't
   // already exist for this organism if it does then we need to throw an error
   else {
-    $sql = "SELECT *
-            FROM {Stock} S
-              INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
-            WHERE uniquename = '%s'
-             AND organism_id = %d AND CVT.name = '%s'";
-    $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->stock_type));
+    $sql = "
+      SELECT *
+      FROM {Stock} S
+        INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
+      WHERE uniquename = :uname'AND organism_id = :organism_id AND CVT.name = :cvtname";
+    $result = chado_query($sql, array(':uname' => $node->uniquename, 
+      ':organism_id' => $node->organism_id, ':cvtname' => $node->stock_type))->fetchObject();
     if ($result) {
       form_set_error('uniquename', t("Stock insert cannot proceed. The stock name '$node->uniquename' already exists for this organism. Please provide a unique name for this stock."));
     }
@@ -624,7 +645,7 @@ function chado_stock_validate($node, &$form) {
     form_set_error('type_id', 'Please select a type of stock.');
   }
   else {
-    $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
+    $num_rows = chado_query($int_in_chado_sql, array(':table' => 'cvterm', ':column' => 'cvterm_id', ':value' => $node->type_id))->fetchObject();
     if ( $num_rows->count != 1) {
       form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
   }
@@ -634,7 +655,7 @@ function chado_stock_validate($node, &$form) {
     form_set_error('organism_id', 'Please select a source organism for this stock');
   }
   else {
-    $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
+    $num_rows = chado_query($int_in_chado_sql, array(':table' => 'organism', ':column' => 'organism_id', ':value' => $node->organism_id))->fetchObject();
     if ( $num_rows->count != 1 ) {
       form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
   }
@@ -655,7 +676,7 @@ function chado_stock_validate($node, &$form) {
 
   // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
   if ( $node->database > 0) {
-    $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'db', 'db_id', $node->database));
+    $num_rows = chado_query($int_in_chado_sql, array(':table' => 'db', ':column' => 'db_id', ':value' => $node->database))->fetchObject();
     if ($num_rows->count != 1) {
       form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
   }
@@ -679,13 +700,8 @@ function chado_stock_insert($node) {
   // then don't create but simply link to node
   if ($node->chado_stock_exists) {
     if (!empty($node->stock_id)) {
-      db_query(
-        "INSERT INTO {chado_stock} (nid, vid, stock_id) "
-        ."VALUES (%d, %d, %d)",
-        $node->nid,
-        $node->vid,
-        $node->stock_id
-      );
+      $sql = "INSERT INTO {chado_stock} (nid, vid, stock_id) VALUES (:nid, :vid, :stock_id)";
+      db_query($sql, array(':nid' => $node->nid, ':vid' => $node->vid, ':stock_id' => $node->stock_id));
     }
     
     return $node;
@@ -755,8 +771,8 @@ function chado_stock_insert($node) {
     $stock = (object) $stock;
 
     // add the entry to the chado_stock table
-    $sql = "INSERT INTO {chado_stock} (nid, vid, stock_id) VALUES (%d, %d, %d)";
-    db_query($sql, $node->nid, $node->vid, $stock->stock_id);
+    $sql = "INSERT INTO {chado_stock} (nid, vid, stock_id) VALUES :nid, :vid, :stock_id)";
+    db_query($sql, array(':nid' => $node->nid, ':vid' => $node->vid, ':stock_id' => $stock->stock_id));
 
     // Move on to next stage of Stock Creation based on next_stage_path field
     if ($node->simulate_multipart) {
@@ -906,133 +922,112 @@ function chado_stock_update($node) {
 function chado_stock_delete($node) {
 
   // Set stock in chado: is_obsolete = TRUE
-  chado_query(
-    "DELETE FROM {stock} WHERE stock_id=%d",
-    $node->stock->stock_id
-  );
+  chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
 
   //remove drupal node and all revisions
-  db_query(
-    "DELETE FROM {chado_stock} WHERE nid=%d",
-    $node->nid
-  );
+  db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
 }
 
 /**
- * Purpose: Implement Blocks relating to stock content
- *
- * @param $op
- *   What kind of information to retrieve about the block or blocks.
- *   Possible values include list, configure, save, view.
- * @param $delta
- *   Which block to return (not applicable if $op is 'list').
- * @param $edit
- *   If $op is 'save', the submitted form data from the configuration form.
- *
- * @return
- *   One of the following depending on $op: An array of block descriptions (list), the configuration
- *   form (configure), nothing (save), an array defining subject and content for the block indexed
- *   by $delta (view)
- *
  * @ingroup tripal_stock
  */
-function tripal_stock_block($op = 'list', $delta = 0, $edit=array()) {
-  switch ($op) {
-    case 'list':
-      $blocks['base']['info'] = t('Tripal Stock Details');
-      $blocks['base']['cache'] = BLOCK_NO_CACHE;
+function tripal_stock_block_info() {
+  $blocks['base']['info'] = t('Tripal Stock Details');
+  $blocks['base']['cache'] = BLOCK_NO_CACHE;
 
-      $blocks['properties']['info'] = t('Tripal Stock Properties');
-      $blocks['properties']['cache'] = BLOCK_NO_CACHE;
+  $blocks['properties']['info'] = t('Tripal Stock Properties');
+  $blocks['properties']['cache'] = BLOCK_NO_CACHE;
 
-      $blocks['references']['info'] = t('Tripal Stock References');
-      $blocks['references']['cache'] = BLOCK_NO_CACHE;
+  $blocks['references']['info'] = t('Tripal Stock References');
+  $blocks['references']['cache'] = BLOCK_NO_CACHE;
 
-      $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
-      $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
+  $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
+  $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
 
-      $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
-      $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
-      
-      $blocks['collections']['info'] = t('Tripal Stock Collections');
-      $blocks['collections']['cache'] = BLOCK_NO_CACHE;
-      
-      $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
-      $blocks['phenotypes']['cache'] = BLOCK_NO_CACHE;
-      
-      $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
-      $blocks['genotypes']['cache'] = BLOCK_NO_CACHE;
-      
-      $blocks['locations']['info'] = t('Tripal Stock Locations');
-      $blocks['locations']['cache'] = BLOCK_NO_CACHE;
-      
-      $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
-      $blocks['orgstocks']['cache'] = BLOCK_NO_CACHE;
-      
-      
+  $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
+  $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
+  
+  $blocks['collections']['info'] = t('Tripal Stock Collections');
+  $blocks['collections']['cache'] = BLOCK_NO_CACHE;
+  
+  $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
+  $blocks['phenotypes']['cache'] = BLOCK_NO_CACHE;
+  
+  $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
+  $blocks['genotypes']['cache'] = BLOCK_NO_CACHE;
+  
+  $blocks['locations']['info'] = t('Tripal Stock Locations');
+  $blocks['locations']['cache'] = BLOCK_NO_CACHE;
+  
+  $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
+  $blocks['orgstocks']['cache'] = BLOCK_NO_CACHE;
 
-      return $blocks;
-
-    case 'view':
-      if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
-      $nid = arg(1);
-      $node = node_load($nid);
-
-      $block = array();
-      switch ($delta) {
-        case 'base':
-          $block['subject'] = t('Stock Details');
-          $block['content'] = theme('tripal_stock_base', $node);
-          break;
-
-        case 'properties':
-          $block['subject'] = t('Properties');
-          $block['content'] = theme('tripal_stock_properties', $node);
-          break;
-
-        case 'references':
-          $block['subject'] = t('References');
-          $block['content'] = theme('tripal_stock_references', $node);
-          break;
-
-        case 'relationships':
-          $block['subject'] = t('Relationships');
-          $block['content'] = theme('tripal_stock_relationships', $node);
-          break;
-
-        case 'synonyms':
-          $block['subject'] = t('Synonyms');
-          $block['content'] = theme('tripal_stock_synonyms', $node);
-          break;
-          
-        case 'collections':
-          $block['subject'] = t('Stock Collections');
-          $block['content'] = theme('tripal_stock_collections', $node);
-          break;
-          
-        case 'phenotypes':
-          $block['subject'] = t('Stock Phenotypes');
-          $block['content'] = theme('tripal_stock_phenotypes', $node);
-          break;
-          
-        case 'genotypes':
-          $block['subject'] = t('Stock Genotypes');
-          $block['content'] = theme('tripal_stock_genotypes', $node);
-          break;
-          
-        case 'locations':
-          $block['subject'] = t('Stock Locations');
-          $block['content'] = theme('tripal_stock_locations', $node);
-          break;
-          
-        case 'orgstocks':
-          $block['subject'] = t('Organism Stocks');
-          $block['content'] = theme('tripal_organism_stocks', $node);
-          break;
+  return $blocks;
+}
+/**
+ * 
+ * @ingroup tripal_stock
+ */
+function tripal_stock_block_view($delta = '') {
 
-      }
-      return $block;
+  if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
+    $nid = arg(1);
+    $node = node_load($nid);
+  
+    $block = array();
+    switch ($delta) {
+      case 'base':
+        $block['subject'] = t('Stock Details');
+        $block['content'] = theme('tripal_stock_base', $node);
+        break;
+  
+      case 'properties':
+        $block['subject'] = t('Properties');
+        $block['content'] = theme('tripal_stock_properties', $node);
+        break;
+  
+      case 'references':
+        $block['subject'] = t('References');
+        $block['content'] = theme('tripal_stock_references', $node);
+        break;
+  
+      case 'relationships':
+        $block['subject'] = t('Relationships');
+        $block['content'] = theme('tripal_stock_relationships', $node);
+        break;
+  
+      case 'synonyms':
+        $block['subject'] = t('Synonyms');
+        $block['content'] = theme('tripal_stock_synonyms', $node);
+        break;
+        
+      case 'collections':
+        $block['subject'] = t('Stock Collections');
+        $block['content'] = theme('tripal_stock_collections', $node);
+        break;
+        
+      case 'phenotypes':
+        $block['subject'] = t('Stock Phenotypes');
+        $block['content'] = theme('tripal_stock_phenotypes', $node);
+        break;
+        
+      case 'genotypes':
+        $block['subject'] = t('Stock Genotypes');
+        $block['content'] = theme('tripal_stock_genotypes', $node);
+        break;
+        
+      case 'locations':
+        $block['subject'] = t('Stock Locations');
+        $block['content'] = theme('tripal_stock_locations', $node);
+        break;
+        
+      case 'orgstocks':
+        $block['subject'] = t('Organism Stocks');
+        $block['content'] = theme('tripal_organism_stocks', $node);
+        break;
+  
     }
+    return $block;
   }
 }
 
@@ -1059,9 +1054,9 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
       INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
       INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
       LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
-    WHERE SR.subject_id = %d      
+    WHERE SR.subject_id = :stock_id
   ";
-  $as_subject = chado_query($sql, $stock->stock_id);
+  $as_subject = chado_query($sql, array(':stock_id' => $stock->stock_id));
   $sql = "
     SELECT 
       S.name, S.uniquename,  S.stock_id, CS.nid, 
@@ -1072,9 +1067,9 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
       INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
       INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
       LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
-    WHERE SR.object_id = %d      
+    WHERE SR.object_id = :stock_id
   ";
-  $as_object = chado_query($sql, $stock->stock_id);   
+  $as_object = chado_query($sql, array(':stock_id' => $stock->stock_id));
   
   // combine both object and subject relationshisp into a single array
   $relationships = array();
@@ -1082,7 +1077,7 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
   $relationships['subject'] = array();
   
   // iterate through the object relationships
-  while ($relationship = db_fetch_object($as_object)) {
+  while ($relationship = $as_object->fetchObject()) {
      
      // get the relationship and child types
      $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
@@ -1098,7 +1093,7 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
   }
   
   // now add in the subject relationships
-  while ($relationship = db_fetch_object($as_subject)) {
+  while ($relationship = $as_subject->fetchObject()) {
      
      // get the relationship and child types
      $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
@@ -1118,88 +1113,74 @@ function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
 
 }
 /**
- * Implementation of hook_nodeapi().
- * Display stock information for associated organisms
- * This function also provides contents for indexing
- *
  * @ingroup tripal_stock
  */
-function tripal_stock_nodeapi(&$node, $op, $teaser, $page) {
-
-  switch ($op) {
-    
-    // set the title to ensure it is always unique
-    case 'presave':
-      switch ($node->type) {
-        case 'chado_stock':
-          
-          $values = array('organism_id' => $node->organism_id);
-          $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
-          $values = array('cvterm_id' => $node->type_id);
-          $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
-          $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
-          break;
-      }
+function tripal_stock_node_presave($node) {
+
+  switch ($node->type) {
+    case 'chado_stock':
+      $values = array('organism_id' => $node->organism_id);
+      $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
+      $values = array('cvterm_id' => $node->type_id);
+      $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
+      $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
       break;
-      
-    // set the URL path after inserting.  We do it here because we do not 
-    // know the stock_id in the presave  
-    case 'insert':
-      switch ($node->type) {
-        case 'chado_stock':
-          if (!$node->stock_id) {
-            $sql = "SELECT * FROM {chado_stock} WHERE nid = %d";
-            $chado_stock = db_fetch_object(db_query($sql, $node->nid));
-            $node->stock_id = $chado_stock->stock_id;
-          }
-          
-          // remove any previous alias
-          db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
-          
-          // set the URL for this stock page
-          $url_alias = tripal_stock_get_stock_url($node);
-          path_set_alias("node/$node->nid", $url_alias);
-          break;
+  }
+}
+/**
+ * @ingroup tripal_stock
+ */
+function tripal_stock_node_insert($node) {
+  switch ($node->type) {
+    case 'chado_stock':
+      if (!$node->stock_id) {
+        $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
+        $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
+        $node->stock_id = $chado_stock->stock_id;
       }
-      break;
       
-    // set the URL path after inserting.  We do it here because we do not 
-    // know the stock_id in the presave  
-    case 'update':
-      switch ($node->type) {
-        case 'chado_stock':
-          
-          // remove any previous alias
-          db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
-          
-          // set the URL for this stock page
-          $url_alias = tripal_stock_get_stock_url($node);
-          path_set_alias("node/$node->nid", $url_alias);
-          break;
-      }
-      break;
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE src = :src", array(':src' => "node/$node->nid"));
       
-    // add items to other nodes, build index and search results
-    case 'view':
-      // add the stock to the organism/feature search indexing
-      if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
-
-      }
-      elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
+      // set the URL for this stock page
+      $url_alias = tripal_stock_get_stock_url($node);
+      path_set_alias("node/$node->nid", $url_alias);
+      break;
+  }
+}
+/**
+ * @ingroup tripal_stock
+ */
+function tripal_stock_node_update($node) {
 
-      }
-      else {
-        switch ($node->type) {
-          case 'chado_organism':
-            // Show stock if the organism/feature is not at teaser view
-            $node->content['tripal_organism_stocks'] = array(
-              '#value' => theme('tripal_organism_stocks', $node),
-            );
-            break;          
-        }
+  switch ($node->type) {
+    case 'chado_stock':
+      
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE src = :src", array(':src' => "node/$node->nid"));
+      
+      // set the URL for this stock page
+      $url_alias = tripal_stock_get_stock_url($node);
+      path_set_alias("node/$node->nid", $url_alias);
+      break;
+  }
+}
+/**
+ * @ingroup tripal_stock
+ */
+function tripal_stock_node_view($node, $view_mode, $langcode) {
+
+  switch ($node->type) {
+    case 'chado_organism':
+      if ($view_mode == 'full') {
+        // Show stock if the organism/feature is not at teaser view
+        $node->content['tripal_organism_stocks'] = array(
+          '#value' => theme('tripal_organism_stocks', $node),
+        );
       }
       break;
   }
+
 }
 
 /*
@@ -1234,9 +1215,9 @@ function tripal_stock_match_stocks_page($id) {
       INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
       INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
     WHERE
-      S.uniquename = '%s' or S.name = '%s'
+      S.uniquename = :uname or S.name = :name
   ";
-  $results = chado_query($sql, $id, $id);
+  $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
 
   $num_matches = 0;
 
@@ -1244,7 +1225,7 @@ function tripal_stock_match_stocks_page($id) {
   $header = array('Uniquename', 'Name', 'Type', 'Species');
   $rows = array();
   $curr_match;
-  while ($match = db_fetch_object($results)) {
+  while ($match = $results->fetchObject()) {
     $curr_match = $match;
     $rows[] = array(
        $match->uniquename,