Browse Source

Stocks: Fixed basic node add/update/load/view functionality

Lacey Sanderson 11 years ago
parent
commit
8aae7286e6

+ 68 - 67
tripal_stock/includes/tripal_stock.sync_stocks.inc

@@ -49,7 +49,7 @@ function tripal_stock_sync_form() {
   $form['stock_types'] = array(
     '#title'       => t('Stock Types'),
     '#type'        => 'textarea',
-    '#description' => t("Enter the names of the stock types to sync. " . 
+    '#description' => t("Enter the names of the stock types to sync. " .
        "Leave blank to sync all stocks. Pages for these stock ".
        "types will be created automatically for stocks that exist in the ".
        "chado database.  The names listed here should be spearated by ".
@@ -108,38 +108,38 @@ function tripal_stock_sync_form_submit($form, &$form_state) {
   }
   else {
     $title = 'Sync stocks';
-  }  
+  }
 
   variable_set('chado_sync_stock_types', $stock_types);
 
   tripal_add_job($title, 'tripal_stock', 'tripal_stock_sync_stocks', $job_args, $user->uid);
 }
 /**
- *  
- * @param $na 
+ *
+ * @param $na
  *   Tripal expects all jobs to have at least one argument. For this function
  *   we don't need any, so we have this dummy argument as a filler
  * @param $job_id
  */
 function tripal_stock_set_urls($na = NULL, $job = NULL) {
-  
+
   // begin the transaction
   db_query("BEGIN");
-      
+
   print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
         "If the load fails or is terminated prematurely then the entire set of \n" .
         "new URLs will be rolled back and no changes will be made\n\n";
-  
+
   // get the number of records we need to set URLs for
   $csql = "SELECT count(*) FROM {chado_stock}";
   $num_nodes = db_query($csql)->fetchField();
-    
+
   // calculate the interval at which we will print an update on the screen
   $num_set = 0;
   $num_per_interval = 100;
-  
+
   // prepate the statements which will quickly add url alias. Because these
-  // are not Chado tables we must manually prepare them 
+  // are not Chado tables we must manually prepare them
   $psql = "
     PREPARE del_url_alias_by_src (text) AS
     DELETE FROM {url_alias} WHERE src = \$1
@@ -150,38 +150,38 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
     INSERT INTO url_alias (src, dst) VALUES (\$1, \$2)
   ";
   db_query($psql);
-  
+
   // get the URL alias syntax string
-  $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]'); 
+  $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
   if (!$url_alias) {
     $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
-  } 
+  }
   $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
-  
-  
+
+
   // get the list of stocks that have been synced
   $sql = "SELECT * FROM {chado_stock}";
-  $nodes = db_query($sql);  
+  $nodes = db_query($sql);
   while ($node = $nodes->fetchObject()) {
-   
+
     // get the URL alias
     $src = "node/$node->nid";
     $dst = tripal_stock_get_stock_url($node, $url_alias);
     if (!$dst) {
       db_query('DEALLOCATE "del_url_alias_by_src"');
       db_query('DEALLOCATE "ins_url_alias_nisrds"');
-      db_query("ROLLBACK"); 
-      return; 
+      db_query("ROLLBACK");
+      return;
     }
-    
+
     // if the src and dst is the same (the URL alias couldn't be set)
     // then skip to the next one. There's nothing we can do about this one.
     if($src == $dst) {
       continue;
     }
-    
+
     // remove any previous alias and then add the new one
-    $success = db_query("EXECUTE del_url_alias_by_src(':src')", array(':src' => $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"');
@@ -204,7 +204,7 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
       tripal_job_set_progress($job, intval($percent));
       $percent = sprintf("%.2f", $percent);
       print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
-      
+
     }
     $num_set++;
   }
@@ -213,55 +213,55 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
   $percent = sprintf("%.2f", $percent);
   print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
   print "\nDone. Set " . number_format($num_set) . " URLs\n";
-  
+
   // unprepare the statements
   db_query('DEALLOCATE "del_url_alias_by_src"');
   db_query('DEALLOCATE "ins_url_alias_nisrds"');
-  
+
   db_query("COMMIT");
 }
 /**
- * 
+ *
  * @param $node
  *   A node object containing at least the stock_id and nid
  * @param $url_alias
  *   Optional.  This should be the URL alias syntax string that contains
  *   placeholders such as [id], [genus], [species], [name], [uniquename],
  *   and [type].  These placeholders will be substituted for actual values.
- *   If this parameter is not provided then the value of the 
+ *   If this parameter is not provided then the value of the
  *   chado_stock_url_string Drupal variable will be used.
  */
 function tripal_stock_get_stock_url($node, $url_alias = NULL) {
 
   // get the starting URL alias
   if(!$url_alias) {
-    $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]'); 
+    $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
     if (!$url_alias) {
       $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
-    } 
+    }
     $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
   }
 
-  // get the stock 
-  $values = array('stock_id' => $node->stock_id); 
-  $options = array('statement_name' => 'sel_stock_id');       
+  // get the stock
+  $values = array('stock_id' => $node->stock_id);
+  $options = array('statement_name' => 'sel_stock_id');
   $stock = tripal_core_chado_select('stock', array('*'), $values, $options);
   if (!$stock) {
     watchdog('trp-seturl', "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id), WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   $stock = (object) $stock[0];
-  
+
   // get the organism
   $values = array('organism_id' => $stock->organism_id);
   $options = array('statement_name' => 'sel_organism_id');
-  $organism  = tripal_core_chado_select('organism', array('*'), $values, $options);  
+  $organism  = tripal_core_chado_select('organism', array('*'), $values, $options);
   if (!$organism) {
     watchdog('trp-seturl', "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id), WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
-  $species = preg_replace('/\s/', '_', strtolower($organism[0]->species)); 
+  $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
 
   // get the type
   $values = array('cvterm_id' => $stock->type_id);
@@ -269,10 +269,10 @@ function tripal_stock_get_stock_url($node, $url_alias = NULL) {
   $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
   if (!$cvterm) {
     watchdog('trp-seturl', "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id), WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   $type = preg_replace('/\s/', '_', $cvterm[0]->name);
-  
+
   // now substitute in the values
   $url_alias = preg_replace('/\[id\]/', $stock->stock_id, $url_alias);
   $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
@@ -280,14 +280,14 @@ function tripal_stock_get_stock_url($node, $url_alias = NULL) {
   $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
   $url_alias = preg_replace('/\[name\]/', $stock->name, $url_alias);
   $url_alias = preg_replace('/\[uniquename\]/', $stock->uniquename, $url_alias);
- 
-  // the dst field of the url_alias table is only 128 characters long. 
+
+  // the dst field of the url_alias table is only 128 characters long.
   // if this is the case then simply return the node URL, we can't set this one
   if (strlen($url_alias) > 128) {
     watchdog('trp-seturl', "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias), WATCHDOG_ERROR);
     return "node/" . $node->nid;
   }
-  
+
   return $url_alias;
 }
 /**
@@ -314,7 +314,7 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
   if ($allowed_types) {
     $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
     print "Looking for stocks of type: $allowed_types\n";
-  
+
     $so_terms = split(' ', $allowed_types);
     $where_cvt = "";
     foreach ($so_terms as $term) {
@@ -353,9 +353,9 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
   // 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) 
+    FROM {stock} S
+      INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
+    WHERE ($where_cvt) AND ($where_org)
     ORDER BY stock_id
   ";
 
@@ -397,14 +397,15 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
     }
     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
-      # to avoid this problem we will call this script through an
-      # independent system call
+      // parsing all the stocks can cause memory overruns
+      // we are not sure why PHP does not clean up the memory as it goes
+      // to avoid this problem we will call this script through an
+      // independent system call
       print ($i + 1) . " of $num_ids Syncing stock id: $stock_id\n";
-      $cmd = "php " . drupal_get_path('module', 'tripal_stock') . "/includes/tripal_stock.sync_stocks.inc -f $stock_id -t chado_stock";
-      print "$cmd\n";
-      system($cmd);
+      // $cmd = "php " . drupal_get_path('module', 'tripal_stock') . "/includes/tripal_stock.sync_stocks.inc -f $stock_id -t chado_stock";
+      // print "$cmd\n";
+      // system($cmd);
+      tripal_stock_sync_stock($stock_id);
 
     }
     $i++;
@@ -419,15 +420,15 @@ function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL,
  * @ingroup tripal_stock
  */
 function tripal_stock_sync_stock($stock_id) {
-  
+
 print "\tSyncing stock $stock_id\n";
-  
+
 global $user;
   $create_node = 1;   // set to 0 if the node exists and we just sync and not create
 
   // get the accession prefix
   $aprefix = variable_get('chado_stock_accession_prefix', 'SID');
-  
+
   // if we don't have a stock_id then return
   if (!$stock_id) {
     drupal_set_message(t("Please provide a stock_id to sync"));
@@ -436,14 +437,14 @@ 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 
+     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();
-  
+
   // 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
@@ -482,10 +483,10 @@ global $user;
   }
 
   // if we've encountered an error then just return.
-  if ($error_msg = db_error()) {
+  //if ($error_msg = db_error()) {
     //print "$error_msg\n";
-    return '';
-  }
+    //return '';
+  //}
 
   // if a drupal node does not exist for this stock then we want to
   // create one.  Note that the node_save call in this block
@@ -504,14 +505,14 @@ global $user;
     $new_node->uniquename = "$stock->uniquename";
     $new_node->type_id = $stock->type_id;
     $new_node->organism_id = $stock->organism_id;
-    $new_node->stock_id = $stock->stock_id;        
+    $new_node->stock_id = $stock->stock_id;
     $new_node->chado_stock_exists = TRUE;
-    
+
     // validate the node and if okay then submit
     node_validate($new_node);
     if ($errors = form_get_errors()) {
       print "Error encountered validating new node. Cannot sync: $msg\n";
-      foreach ($errors as $key => $msg) {        
+      foreach ($errors as $key => $msg) {
         watchdog('trp-fsync', "%msg", array('%msg' => $msg), WATCHDOG_ERROR);
       }
       exit;

+ 21 - 72
tripal_stock/theme/node--chado-stock.tpl.php

@@ -7,7 +7,7 @@
 //   - Using Panels: Override the node page using Panels3 and place the blocks
 //       of content as you please. This method requires no programming. See
 //       the Tripal User Guide for more details
-//   - Block Templates: customize the content/layout of each block of stock 
+//   - Block Templates: customize the content/layout of each block of stock
 //       content. These templates are found in the tripal_stock subdirectory
 //
 // Variables Available:
@@ -17,7 +17,7 @@
 //       properties, db_references, object_relationships, subject_relationships,
 //       organism, etc.
 //   NOTE: For a full listing of fields available in the node object the
-//       print_r $node line below or install the Drupal Devel module which 
+//       print_r $node line below or install the Drupal Devel module which
 //       provides an extra tab at the top of the node page labelled Devel
 
 $stock = $variables['node']->stock;
@@ -27,93 +27,41 @@ $template_settings = theme_get_setting('tripal');
 
 // toggle the sidebar if desired
 $no_sidebar = 0;
-if (is_array($template_settings['tripal_no_sidebar']) and 
+if (is_array($template_settings['tripal_no_sidebar']) and
    $template_settings['tripal_no_sidebar']['stock']) {
   $no_sidebar = 1;
 }
 
-if ($teaser) { 
-  print theme('tripal_stock_teaser',$node); 
-} 
+if ($teaser) {
+  //print theme('tripal_stock_teaser',$node);
+}
 else { ?>
 
-<script type="text/javascript">
-(function ($) {
-  Drupal.behaviors.stockBehavior = {
-    attach: function (context, settings){ <?php 
-      if ($no_sidebar) { ?>    
-        // hide the resource side bar and strech the details section    
-        $(".tripal_toc").hide();
-        $(".tripal_details").addClass("tripal_details_full");
-        $(".tripal_details_full").removeClass("tripal_details"); <?php
-      } else { ?>
-        // use default resource sidebar
-        $(".tripal-info-box").hide(); <?php
-      } ?>
- 
-      // iterate through all of the info boxes and add their titles
-      // to the table of contents
-      $(".tripal-info-box-title").each(function(){
-        var parent = $(this).parent();
-        var id = $(parent).attr('id');
-        var title = $(this).text();
-        $('#tripal_stock_toc_list').append('<li><a href="#'+id+'" class="tripal_stock_toc_item">'+title+'</a></li>');
-      });
-
-      // when a title in the table of contents is clicked, then
-      // show the corresponding item in the details box
-      $(".tripal_stock_toc_item").click(function(){
-         $(".tripal-info-box").hide();
-         href = $(this).attr('href');
-         $(href).fadeIn('slow');
-         // we want to make sure our table of contents and the details
-         // box stay the same height
-         $("#tripal_stock_toc").height($(href).parent().height());
-         return false;
-      }); 
-
-      // we want the base details to show up when the page is first shown 
-      // unless the user specified a specific block
-      var block = window.location.href.match(/[\?|\&]block=(.+?)\&/)
-      if(block == null){
-         block = window.location.href.match(/[\?|\&]block=(.+)/)
-      }
-      if(block != null){
-         $("#tripal_stock-"+block[1]+"-box").show();
-      } else {
-         $("#tripal_stock-base-box").show();
-      }
-
-      $("#tripal_stock_toc").height($("#tripal_stock-base-box").parent().height());
-    }     
-  };
-})(jQuery);
-</script>
-
 <div id="tripal_stock_details" class="tripal_details">
 
   <!-- Base Theme -->
-  <?php print theme('tripal_stock_base',$node); ?>
+  <?php //print theme('tripal_stock_base',$node); ?>
 
   <!-- Cross References -->
-  <?php print theme('tripal_stock_references',$node); ?>
+  <?php //print theme('tripal_stock_references',$node); ?>
 
   <!-- Properties -->
-  <?php print theme('tripal_stock_properties',$node); ?>
+  <?php //print theme('tripal_stock_properties',$node); ?>
 
   <!-- Synonyms -->
-  <?php print theme('tripal_stock_synonyms',$node); ?>
+  <?php //print theme('tripal_stock_synonyms',$node); ?>
 
   <!-- Relationships -->
-  <?php print theme('tripal_stock_relationships',$node); ?>
-  
+  <?php //print theme('tripal_stock_relationships',$node); ?>
+
   <!-- Stock Collections -->
-  <?php print theme('tripal_stock_collections',$node); ?>
+  <?php //print theme('tripal_stock_collections',$node); ?>
 
   <!-- Stock Genotypes -->
-  <?php print theme('tripal_stock_genotypes',$node); ?>
+  <?php //print theme('tripal_stock_genotypes',$node); ?>
 
   <!-- Resource Blocks CCK elements --><?php
+  /**
   for($i = 0; $i < count($node->field_resource_titles); $i++){
     if($node->field_resource_titles[$i]['value']){ ?>
       <div id="tripal_stock-resource_<?php print $i?>-box" class="tripal_stock-info-box tripal-info-box">
@@ -121,8 +69,8 @@ else { ?>
         <?php print $node->field_resource_blocks[$i]['value']; ?>
       </div><?php
     }
-  }?>
-   
+  }*/?>
+
   <!-- Let modules add more content -->
 	<?php print $content; ?>
 </div>
@@ -132,15 +80,16 @@ else { ?>
    <div id="tripal_stock_toc_title" class="tripal_toc_title">Resources</div>
    <span id="tripal_stock_toc_desc" class="tripal_toc_desc"></span>
    <ul id="tripal_stock_toc_list" class="tripal_toc_list">
-   
+
      <!-- Resource Links CCK elements --><?php
+     /**
      for($i = 0; $i < count($node->field_resource_links); $i++){
        if($node->field_resource_links[$i]['value']){
          $matches = preg_split("/\|/",$node->field_resource_links[$i]['value']);?>
          <li><a href="<?php print $matches[1] ?>" target="_blank"><?php print $matches[0] ?></a></li><?php
        }
-     }?>
-     
+     }*/?>
+
      <?php // ADD CUSTOMIZED <li> LINKS HERE ?>
    </ul>
 </div>

+ 10 - 11
tripal_stock/theme/tripal_stock/tripal_stock_base.tpl.php

@@ -1,12 +1,11 @@
 <?php
 $stock = $node->stock;
-$organism = $node->stock->organism_id; 
+$organism = $node->stock->organism_id;
 $main_db_reference = $stock->dbxref_id;
 
 // expand the text fields
 $stock = tripal_core_expand_chado_vars($stock, 'field', 'stock.description');
 $stock = tripal_core_expand_chado_vars($stock, 'field', 'stock.uniquename');
-
 ?>
 <div id="tripal_stock-base-box" class="tripal_stock-info-box tripal-info-box">
   <div class="tripal_stock-info-box-title tripal-info-box-title">Details</div>
@@ -30,23 +29,23 @@ $stock = tripal_core_expand_chado_vars($stock, 'field', 'stock.uniquename');
       </tr>
       <tr class="tripal_stock-table-odd-row tripal-table-odd-row">
         <th>Organism</th>
-        <td><?php 
+        <td><?php
           if ($organism->nid) { ?>
-      	   <a href="<?php print url("node/$organism->nid") ?>"><?php print "<i>" . $organism->genus . 
-            " " . $organism->species . "</i> (" . $organism->common_name . " )"?></a><?php 
-          } else { 
+      	   <a href="<?php print url("node/$organism->nid") ?>"><?php print "<i>" . $organism->genus .
+            " " . $organism->species . "</i> (" . $organism->common_name . " )"?></a><?php
+          } else {
             print "<i>" . $organism->genus . " " . $organism->species . "</i> (" . $organism->common_name . ")";
           } ?>
         </td>
-     	</tr>   
+     	</tr>
       <tr class="tripal_stock-table-even-row tripal-table-even-row">
         <th>Description</th>
-        <td><?php print $stock->description ?></td>      
-      </tr>   
-      <!--  
+        <td><?php print $stock->description ?></td>
+      </tr>
+      <!--
       <tr class="tripal_stock-table-odd-row tripal-table-odd-row">
         <th>Internal ID</th>
         <td><?php print $stock->stock_id ?></td>
-      </tr>     -->	                                
+      </tr>     -->
    </table>
 </div>

+ 16 - 16
tripal_stock/theme/tripal_stock/tripal_stock_references.tpl.php

@@ -4,7 +4,7 @@ $references = array();
 
 // First, get the dbxref record from stock recrod itself if one exists
 if ($stock->dbxref_id) {
-  $stock->dbxref_id->is_primary = 1;  // add this new property so we know it's the primary reference
+  $stock->dbxref_id->is_primary = TRUE;  // add this new property so we know it's the primary reference
   $references[] = $stock->dbxref_id;
 }
 
@@ -27,35 +27,35 @@ if(count($references) > 0){ ?>
 	      <th>Dababase</th>
 	      <th>Accession</th>
 	    </tr> <?php
-	    $i = 0; 
-	    foreach ($references as $dbxref){ 
+	    $i = 0;
+	    foreach ($references as $dbxref){
 	      $class = 'tripal_stock-table-odd-row tripal-table-odd-row';
 	      if($i % 2 == 0 ){
 	         $class = 'tripal_stock-table-odd-row tripal-table-even-row';
 	      } ?>
 	      <tr class="<?php print $class ?>">
-	        <td> <?php 
-	          if ($dbxref->db_id->url) { 
+	        <td> <?php
+	          if ($dbxref->db_id->url) {
               print l($dbxref->db_id->name, $dbxref->db_id->url);
-            } 
-            else { 
-              print $dbxref->db_id->name; 
+            }
+            else {
+              print $dbxref->db_id->name;
             } ?>
 	        </td>
-	        <td> <?php 
-	          if ($dbxref->db_id->urlprefix) { 
+	        <td> <?php
+	          if ($dbxref->db_id->urlprefix) {
 	           	print l($dbxref->accession, $dbxref->db_id->urlprefix.$dbxref->accession);
-	          } 
-	          else { 
-	            print $dbxref->accession; 
+	          }
+	          else {
+	            print $dbxref->accession;
 	          }
 	          if ($dbxref->is_primary) {
 	            print " <i>(primary cross-reference)</i>";
 	          } ?>
 	        </td>
 	      </tr> <?php
-	      $i++;  
+	      $i++;
 	    } ?>
-	  </table> 
-	</div><?php 
+	  </table>
+	</div><?php
 }

+ 150 - 69
tripal_stock/tripal_stock.module

@@ -53,7 +53,7 @@ function tripal_stock_menu() {
     'title' => 'Settings',
     'description' => 'Settings for Chado Stocks',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_stock_help'),
+    'page arguments' => array('tripal_stock_admin'),
     'access arguments' => array('administer tripal stocks'),
     'type' => MENU_LOCAL_TASK,
     'weight' => 5
@@ -302,6 +302,12 @@ function tripal_stock_theme() {
       'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
     ),
     // tripal_stock templates
+    'node__chado_stock' => array(
+      'template' => 'node--chado-generic',
+      'path' => drupal_get_path('module', 'tripal_core') . '/theme',
+      'render element' => 'node',
+      'base hook' => 'node',
+    ),
     'tripal_stock_base' => array(
       'arguments' => array('node' => NULL),
       'template' => 'tripal_stock_base',
@@ -400,44 +406,63 @@ function tripal_stock_node_info() {
  * @return
  *   A stock node containing all the variables from the basic node and all stock specific variables
  *
+ * D7 @todo: Make optimizations to take advantage of $nodes
+ *
  * @ingroup tripal_stock
  */
-function chado_stock_load($node) {
+function chado_stock_load($nodes) {
+
+  $new_nodes = array();
+  foreach ($nodes as $nid => $node) {
+    // get the stock details from chado
+    $stock_id = chado_get_id_for_node('stock', $node->nid);
+    if (empty($stock_id)) {
+      tripal_core_report_error(
+        'tripal_stock',
+        TRIPAL_ERROR,
+        'Unable to retrieve stock_id for %title (NID = %nid).',
+        array('%title' => $node->title, '%nid' => $node->nid)
+      );
+    }
+    else {
 
-  // get the stock details from chado
-  $stock_id = chado_get_id_for_node('stock', $node->nid);
+      // build the variable with all the stock details
+      $values = array('stock_id' => $stock_id);
+      $stock = tripal_core_generate_chado_var('stock', $values);
+
+      // by default, the titles are saved using the unique constraint.  We will
+      // keep it the same, but remove the duplicate name if the unique name and name
+      // are identical
+      $title_type = variable_get('chado_stock_title', 'unique_constraint');
+      if($title_type == 'unique_constraint') {
+        if (strcmp($stock->name, $stock->uniquename)==0) {
+          $node->title = $stock->name . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
+        }
+        // in previous version of Tripal, the stock title was simply the unique name.
+        // so, we recreate the title just to be sure all of our stock pages are consistent
+        else {
+          $node->title = $stock->name . ", " . $stock->uniquename . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
+        }
+      }
+      // set the title to be the stock name or uniquename as configured
+      if($title_type == 'stock_name') {
+        $node->title = $stock->name;
+      }
+      if($title_type == 'stock_unique_name') {
+        $node->title = $stock->uniquename;
+      }
 
-  // build the variable with all the stock details
-  $values = array('stock_id' => $stock_id);
-  $stock = tripal_core_generate_chado_var('stock', $values);
+      // add this to the node
+      $node->stock = $stock;
+    }
 
+    $new_nodes[$nid] = $node;
 
-  // by default, the titles are saved using the unique constraint.  We will
-  // keep it the same, but remove the duplicate name if the unique name and name
-  // are identical
-  $title_type = variable_get('chado_stock_title', 'unique_constraint');
-  if($title_type == 'unique_constraint') {
-    if (strcmp($stock->name, $stock->uniquename)==0) {
-      $node->title = $stock->name . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
-    }
-    // in previous version of Tripal, the stock title was simply the unique name.
-    // so, we recreate the title just to be sure all of our stock pages are consistent
-    else {
-      $node->title = $stock->name . ", " . $stock->uniquename . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
-    }
-  }
-  // set the title to be the stock name or uniquename as configured
-  if($title_type == 'stock_name') {
-    $node->title = $stock->name;
-  }
-  if($title_type == 'stock_unique_name') {
-    $node->title = $stock->uniquename;
   }
 
-  // add this to the node
-  $additions = new stdClass();
-  $additions->stock = $stock;
-  return $additions;
+  ddl($new_nodes, 'nodes at end of load');
+
+  return $new_nodes;
 }
 
 
@@ -464,13 +489,16 @@ function chado_stock_load($node) {
  */
 function chado_stock_form($node, $form_state) {
 
-  // Expand all fields needed
-  $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
-  foreach ($fields_needed as $field_name) {
-    // Check to see if it's excluded and expand it if so
-    if ($node->expandable_fields) {
-      if (in_array($field_name, $node->expandable_fields)) {
-        $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
+  ddl($node, 'node');
+  if (isset($node->nid)) {
+    // Expand all fields needed
+    $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
+    foreach ($fields_needed as $field_name) {
+      // Check to see if it's excluded and expand it if so
+      if (isset($node->expandable_fields)) {
+        if (in_array($field_name, $node->expandable_fields)) {
+          $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
+        }
       }
     }
   }
@@ -505,20 +533,20 @@ function chado_stock_form($node, $form_state) {
   $form['names']['sname'] = array(
     '#type' => 'textfield',
     '#title' => t('Name'),
-    '#default_value' => $node->stock->name,
+    '#default_value' => (isset($node->stock->name)) ? $node->stock->name : '',
     '#required'       => TRUE
   );
 
   $form['names']['uniquename'] = array(
     '#type' => 'textfield',
     '#title' => t('Unique Name'),
-    '#default_value' => $node->stock->uniquename,
+    '#default_value' => (isset($node->stock->uniquename)) ? $node->stock->uniquename : '',
     '#required'       => TRUE
   );
 
   $form['names']['stock_id'] = array(
     '#type' => 'hidden',
-    '#value' => $node->stock->stock_id
+    '#value' => (isset($node->stock->stock_id)) ? $node->stock->stock_id : NULL
   );
 
   $form['details'] = array(
@@ -528,7 +556,7 @@ function chado_stock_form($node, $form_state) {
 
   $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
   $type_options[0] = 'Select a Type';
-  if ($node->nid == '') {
+  if (!isset($node->nid)) {
     $type_default = 0;
   }
   else {
@@ -554,7 +582,7 @@ function chado_stock_form($node, $form_state) {
   $form['details']['organism_id'] = array(
     '#type' => 'select',
     '#title' => t('Source Organism for stock'),
-    '#default_value' => $node->stock->organism_id->organism_id,
+    '#default_value' => (isset($node->stock->organism_id->organism_id)) ? $node->stock->organism_id->organism_id : '',
     '#options' => $organisms,
     '#required'    => TRUE
   );
@@ -563,7 +591,7 @@ function chado_stock_form($node, $form_state) {
   $form['details']['stock_description'] = array(
     '#type' => 'textarea',
     '#title' => t('Notes'),
-    '#default_value' => $node->stock->description,
+    '#default_value' => (isset($node->stock->description)) ? $node->stock->description : '',
     '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
   );
 
@@ -575,21 +603,24 @@ function chado_stock_form($node, $form_state) {
   $form['database_reference']['accession'] = array(
     '#type' => 'textfield',
     '#title' => t('Accession'),
-    '#default_value' => $node->stock->dbxref_id->accession
+    '#default_value' => (isset($node->stock->dbxref_id->accession)) ? $node->stock->dbxref_id->accession : ''
   );
 
   $form['database_reference']['db_description'] = array(
     '#type' => 'textarea',
     '#title' => t('Description of Database Reference'),
-    '#default_value' => $node->stock->dbxref_id->description,
+    '#default_value' => (isset($node->stock->dbxref_id->description)) ? $node->stock->dbxref_id->description : '',
     '#description' => t('Optionally enter a description about the database accession.')
   );
 
   $db_options = tripal_db_get_db_options();
   $db_options[0] = 'Select a Database';
-  if ($node->nid == '') {
-  $db_default = 0; }
-  else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
+  if (!isset($node->nid)) {
+    $db_default = 0;
+  }
+  else {
+    $db_default = $node->stock->dbxref_id->db_id->db_id;
+  }
   $form['database_reference']['database'] = array(
     '#type' => 'select',
     '#title' => t('Database'),
@@ -628,7 +659,7 @@ function chado_stock_validate($node, &$form) {
         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,
+      ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
       ':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."));
@@ -642,9 +673,9 @@ function chado_stock_validate($node, &$form) {
       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";
+      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();
+      ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->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."));
     }
@@ -656,7 +687,9 @@ function chado_stock_validate($node, &$form) {
     form_set_error('type_id', 'Please select a type of stock.');
   }
   else {
-    $num_rows = chado_query($int_in_chado_sql, array(':table' => 'cvterm', ':column' => 'cvterm_id', ':value' => $node->type_id))->fetchObject();
+    $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
+    $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
+    $num_rows = chado_query($new_sql, array(':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)"); }
   }
@@ -666,7 +699,9 @@ function chado_stock_validate($node, &$form) {
     form_set_error('organism_id', 'Please select a source organism for this stock');
   }
   else {
-    $num_rows = chado_query($int_in_chado_sql, array(':table' => 'organism', ':column' => 'organism_id', ':value' => $node->organism_id))->fetchObject();
+    $replace = array(':table' => 'organism', ':column' => 'organism_id');
+    $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
+    $num_rows = chado_query($new_sql, array(':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)"); }
   }
@@ -687,7 +722,9 @@ 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 = chado_query($int_in_chado_sql, array(':table' => 'db', ':column' => 'db_id', ':value' => $node->database))->fetchObject();
+    $replace = array(':table' => 'db', ':column' => 'db_id');
+    $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
+    $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
     if ($num_rows->count != 1) {
       form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
   }
@@ -709,13 +746,18 @@ function chado_stock_insert($node) {
 
   // If the chado stock exists (e.g. this is only a syncing operation)
   // then don't create but simply link to node
-  if ($node->chado_stock_exists) {
-    if (!empty($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));
-    }
+  if (isset($node->chado_stock_exists)) {
+    if ($node->chado_stock_exists) {
+      if (!empty($node->stock_id)) {
+        db_insert('chado_stock')->fields(array(
+          'nid' => $node->nid,
+          'vid' => $node->vid,
+          'stock_id' => $node->stock_id
+        ));
+      }
 
-    return $node;
+      return $node;
+    }
   }
 
   // before we can add the stock, we must add the dbxref if one has been
@@ -727,7 +769,7 @@ function chado_stock_insert($node) {
         'db_id' => $node->database,
         'accession' => $node->accession,
       );
-      if (!tripal_core_chado_select('dbxref', array(dbxref_id), $values)) {
+      if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
         $values['description'] = $node->db_description;
         $values['version'] = '1';
         $dbxref_status = tripal_core_chado_insert('dbxref', $values);
@@ -774,16 +816,19 @@ function chado_stock_insert($node) {
     $stock = tripal_core_chado_insert('stock', $values);
   }
 
+  ddl($stock, 'stock in insert');
+  ddl($node, 'node in insert');
+
   // if the stock creation was succesful then add the URL and the entry in the
   // chado_stock table
   if (is_array($stock)) {
 
-    // convert the stock into an object
-    $stock = (object) $stock;
-
     // add the entry to the chado_stock table
-    $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));
+    db_insert('chado_stock')->fields(array(
+      'nid' => (int) $node->nid,
+      'vid' => (int) $node->vid,
+      'stock_id' => (int) $stock['stock_id']
+    ))->execute();
 
     // Move on to next stage of Stock Creation based on next_stage_path field
     if ($node->simulate_multipart) {
@@ -1151,7 +1196,7 @@ function tripal_stock_node_insert($node) {
       }
 
       // remove any previous alias
-      db_query("DELETE FROM {url_alias} WHERE src = :src", array(':src' => "node/$node->nid"));
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
 
       // set the URL for this stock page
       $url_alias = tripal_stock_get_stock_url($node);
@@ -1169,7 +1214,7 @@ function tripal_stock_node_update($node) {
     case 'chado_stock':
 
       // remove any previous alias
-      db_query("DELETE FROM {url_alias} WHERE src = :src", array(':src' => "node/$node->nid"));
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
 
       // set the URL for this stock page
       $url_alias = tripal_stock_get_stock_url($node);
@@ -1192,6 +1237,42 @@ function tripal_stock_node_view($node, $view_mode, $langcode) {
         );
       }
       break;
+    case 'chado_stock':
+      if ($view_mode == 'full') {
+        $node->content['tripal_stock_base'] = array(
+          '#value' => theme('tripal_stock_base', array('node' => $node)),
+        );
+        // Cross References
+        $node->content['tripal_stock_references'] = array(
+          '#value' => theme('tripal_stock_references', array('node' => $node)),
+        );
+        // Properties
+        $node->content['tripal_stock_properties'] = array(
+          '#value' => theme('tripal_stock_properties', array('node' => $node)),
+        );
+        // Synonyms
+        $node->content['tripal_stock_synonyms'] = array(
+          '#value' => theme('tripal_stock_synonyms', array('node' => $node)),
+        );
+        // Relationships
+        $node->content['tripal_stock_relationships'] = array(
+          '#value' => theme('tripal_stock_relationships', array('node' => $node)),
+        );
+        // Stock Collections
+        $node->content['tripal_stock_collections'] = array(
+          '#value' => theme('tripal_stock_collections', array('node' => $node)),
+        );
+        // Stock Genotypes
+        $node->content['tripal_stock_genotypes'] = array(
+          '#value' => theme('tripal_stock_genotypes', array('node' => $node)),
+        );
+      }
+      if ($view_mode == 'teaser') {
+        $node->content['tripal_stock_teaser'] = array(
+          '#value' => theme('tripal_stock_teaser', array('node' => $node)),
+        );
+      }
+      break;
   }
 
 }