Browse Source

Fixed bug in adding new organism

Stephen Ficklin 11 years ago
parent
commit
b002c3d5fb

+ 0 - 137
tripal_analysis/theme/node--chado-analysis.tpl.php

@@ -1,137 +0,0 @@
-<?php
-// Purpose: This template provides the layout of the organism node (page)
-//   using the same templates used for the various feature content blocks.
-//
-// To Customize the Featture Node Page:
-//   - This Template: customize basic layout and which elements are included
-//   - 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 
-//       content. These templates are found in the tripal_stock subdirectory
-//
-// Variables Available:
-//   - $node: a standard object which contains all the fields associated with
-//       nodes including nid, type, title, taxonomy. It also includes stock
-//       specific fields such as stock_name, uniquename, stock_type, synonyms,
-//       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 
-//       provides an extra tab at the top of the node page labelled Devel
-$analysis  = $variables['node']->organism;
-
-// get the template settings
-$template_settings = theme_get_setting('tripal');
-
-// toggle the sidebar if desired
-$no_sidebar = 0;
-if (is_array($template_settings['tripal_no_sidebar']) and 
-   $template_settings['tripal_no_sidebar']['analysis']) {
-  $no_sidebar = 1;
-}
- 
-if ($teaser) { 
-  print theme('tripal_analysis_teaser', $variables); 
-} 
-else { ?>
-
-<script type="text/javascript">
-(function ($) {
-  Drupal.behaviors.analysisBehavior = {
-    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
-      } ?>
-	    
-      // hide all tripal info boxes at the start
-      $(".tripal-info-box").hide();
- 
-      // 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_analysis_toc_list').append('<li><a href="#'+id+'" class="tripal_analysis_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_analysis_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_analysis_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_analysis-"+block[1]+"-box").show();
-      } else {
-         $("#tripal_analysis-base-box").show();
-      }
-      
-      // make the height of the table of contents match the height of the details box
-      $("#tripal_analysis_toc").height($("#tripal_analysis-base-box").parent().height());
-      
-    }     
-  };
-})(jQuery);
-</script>
-
-<div id="tripal_analysis_details" class="tripal_details">
-
-   <!-- Basic Details Theme -->
-   <?php print theme('tripal_analysis_base',$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_analysis-resource_<?php print $i?>-box" class="tripal_analysis-info-box tripal-info-box">
-         <div class="tripal_analysis-info-box-title tripal-info-box-title"><?php print $node->field_resource_titles[$i]['value'] ?></div>
-         <?php print $node->field_resource_blocks[$i]['value']; ?>
-       </div><?php
-     }
-   }?>
-   
-   <!-- Properties Theme -->
-   <?php print theme('tripal_analysis_properties',$node); ?>
-   
-   <!-- Let modules add more content -->
-   <?php print $content ?>
-</div>
-
-<!-- Table of contents -->
-<div id="tripal_analysis_toc" class="tripal_toc">
-   <div id="tripal_analysis_toc_title" class="tripal_toc_title">Resources</i></div>
-   <span id="tripal_analysis_toc_desc" class="tripal_toc_desc"></span>
-   <ul id="tripal_analysis_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>
-
-<?php } ?>

+ 18 - 11
tripal_analysis/tripal_analysis.module

@@ -554,31 +554,38 @@ function tripal_analysis_permission() {
  *
  * @ingroup tripal_analysis
  */
-function tripal_analysis_theme() {
+function tripal_analysis_theme($existing, $type, $theme, $path) {
+  $core_path = drupal_get_path('module', 'tripal_core');
   
   $theme_path = drupal_get_path('module', 'tripal_analysis') . '/theme';
   $items = array(
+    'node__chado_analysis' => array(
+      'template' => 'node--chado-generic',
+      'render element' => 'node',
+      'base hook' => 'node',
+      'path' => "$core_path/theme",
+    ),
     'tripal_analysis_base' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_analysis_base',
-       'path' => "$theme_path/tripal_analysis",
+      'variables' => array('node' => NULL),
+      'template' => 'tripal_analysis_base',
+      'path' => "$path/theme/tripal_analysis",
     ),
     'tripal_feature_analyses' => array(
       'template' => 'tripal_feature_analyses',
-      'arguments' =>  array('node' => NULL),
-      'path' => "$theme_path/tripal_analysis",
+      'variables' =>  array('node' => NULL),
+      'path' => "$path/theme/tripal_analysis",
     ),
    'tripal_analysis_properties' => array(
-     'arguments' => array('node' => NULL),
-     'path' => "$theme_path/tripal_analysis",
+     'variables' => array('node' => NULL),
+     'path' => "$path/theme/tripal_analysis",
     ),
     'tripal_analysis_help' => array(
       'template' => 'tripal_analysis_help',
-      'arguments' =>  array(NULL),
-      'path' => $theme_path,
+      'variables' =>  array(NULL),
+      'path' => "$path/theme",
     ),
     'chado_analysis_node_form' => array(
-      'arguments' => array('form'),
+      'variables' => array('form'),
       'path' => $theme_path,
     ),
   );

+ 3 - 6
tripal_core/api/tripal_core_chado.api.inc

@@ -391,7 +391,7 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
       foreach ($table_desc['primary key'] as $field) {
         $sql = "SELECT CURRVAL('{" . $table . "_" . $field . "_seq}')";
         $results = chado_query($sql);
-        $value = $results->fetchObject();
+        $value = $results->fetchField();
         if (!$value) {
           tripal_core_report_error(
             'tripal_core',
@@ -2185,9 +2185,7 @@ function chado_query($sql, $args = array()) {
  */
 function chado_get_id_for_node($table, $nid) {
   $sql = "SELECT " . $table . "_id as id FROM {chado_$table} WHERE nid = :nid";
-  $result = db_query($sql, array(':nid' => $nid))->fetchObject();
-
-  return $result->id;
+  return db_query($sql, array(':nid' => $nid))->fetchField();
 }
 
 /**
@@ -2202,8 +2200,7 @@ function chado_get_id_for_node($table, $nid) {
  */
 function chado_get_node_id($table, $id) {
   $sql = "SELECT nid FROM {chado_$table} WHERE " . $table . "_id = :" . $table . "_id";
-  $result = db_query($sql, array(":" . $table . "_id" => $id))->fetchObject();
-  return $results->nid;
+  return db_query($sql, array(":" . $table . "_id" => $id))->fetchField();
 }
 
 /**

+ 9 - 9
tripal_organism/theme/tripal_organism/tripal_organism_base.tpl.php

@@ -29,15 +29,6 @@ $organism = tripal_core_expand_chado_vars($organism,'field','organism.comment');
   // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7 
   $rows = array();
   
-  // common name row
-  $rows[] = array(
-    array(
-      'data' => 'Common Name', 
-      'header' => TRUE
-    ), 
-    $organism->common_name,
-  );
-  
   // genus row
   $rows[] = array(
     array(
@@ -56,6 +47,15 @@ $organism = tripal_core_expand_chado_vars($organism,'field','organism.comment');
     '<i>' . $organism->species . '</i>'
   );
   
+  // common name row
+  $rows[] = array(
+    array(
+      'data' => 'Common Name',
+      'header' => TRUE
+    ),
+    $organism->common_name,
+  );
+  
   // abbreviation row
   $rows[] = array(
     array(

+ 63 - 29
tripal_organism/tripal_organism.module

@@ -281,9 +281,16 @@ function tripal_organism_permission() {
  *  @ingroup tripal_organism
  */
 function chado_organism_validate($node, $form, &$form_state) {
+  // remove any white space around values
+  $node->genus        = trim($node->genus);
+  $node->species      = trim($node->species);
+  $node->abbreviation = trim($node->abbreviation);
+  $node->common_name  = trim($node->common_name);
+  $node->description  = trim($node->description);
+  
   // if this is an update, we want to make sure that a different organism doesn't
   // already have this genus and speces
-  if ($node->organism_id) {
+  if (property_exists($node, 'organism_id')) {
     $sql = "
       SELECT *
       FROM {organism} O
@@ -328,6 +335,12 @@ function chado_organism_validate($node, $form, &$form_state) {
  * @ingroup tripal_organism
  */
 function chado_organism_insert($node) {
+  // remove any white space around values
+  $node->genus        = trim($node->genus);
+  $node->species      = trim($node->species);
+  $node->abbreviation = trim($node->abbreviation);
+  $node->common_name  = trim($node->common_name);
+  $node->description  = trim($node->description);
 
   $values = array(
     'genus'        => $node->genus,
@@ -336,10 +349,11 @@ function chado_organism_insert($node) {
     'common_name'  => $node->common_name,
     'comment'      => $node->description
   );
+  
   // if there is an organism_id in the $node object then this must be a sync so
   // we can skip adding the organism as it is already there, although
   // we do need to proceed with the rest of the insert
-  if (!$node->organism_id) {
+  if (!property_exists($node,'organism_id')) {
     $organism = tripal_core_chado_insert('organism', $values);
     if (!$organism) {
       drupal_set_message(t('Unable to add organism.', 'warning'));
@@ -355,13 +369,13 @@ function chado_organism_insert($node) {
 
   // Make sure the entry for this organism doesn't already exist in the
   // chado_organism table if it doesn't exist then we want to add it.
-
-  if (!chado_get_id_for_node('organism', $node->nid) ) {
-    // next add the item to the drupal table
-    $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) " .
-           "VALUES (:nid, :vid, :organism_id)";
-    $args = array(':nid' => $node->nid, ':vid' => $node->vid, ':organism_id' => $organism_id);
-    db_query($sql, $args);
+  $check_org_id = chado_get_id_for_node('organism', $node->nid);  
+  if (!$check_org_id) {
+    $record = new stdClass();
+    $record->nid = $node->nid;
+    $record->vid = $node->vid;
+    $record->organism_id = $organism_id;
+    drupal_write_record('chado_organism', $record);
   }
 
   // set the title for the node
@@ -380,6 +394,13 @@ function chado_organism_insert($node) {
  * @ingroup tripal_organism
  */
 function chado_organism_update($node) {
+  // remove any white space around values
+  $node->genus        = trim($node->genus);
+  $node->species      = trim($node->species);
+  $node->abbreviation = trim($node->abbreviation);
+  $node->common_name  = trim($node->common_name);
+  $node->description  = trim($node->description);
+  
   if ($node->revision) {
     // there is no way to handle revisions in Chado but leave
     // this here just to make not we've addressed it.
@@ -497,29 +518,42 @@ function chado_organism_add_image($node) {
  *
  * @ingroup tripal_organism
  */
-function chado_organism_form($node, $param) {
-  $organism = $node->organism;
-
-  // add in the comment since it is a text field and may not be included if too big
-  $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
-
-  // get form defaults
-  $abbreviation   = property_exists($node, 'abbreviation')   ? property_exists($node, 'abbreviation')   : $organism->abbreviation;
-  $genus          = property_exists($node, 'genus')          ? property_exists($node, 'genus')          : $organism->genus;
-  $species        = property_exists($node, 'species')        ? property_exists($node, 'species')        : $organism->species;
-  $common_name    = property_exists($node, 'common_name')    ? property_exists($node, 'common_name')    : $organism->common_name;
-  $description    = property_exists($node, 'description')    ? property_exists($node, 'description')    : $organism->comment;
-  $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
-
+function chado_organism_form($node, $form_state) {
   $form = array();
+  
+  // we have a file upload element on the form soe we need the multipart encoding type
   $form['#attributes']['enctype'] = 'multipart/form-data';
+  
+  // if the organism is part of the node object then we are editing. If not we are inserting
+  if (property_exists($node, 'organism')) {
+    $organism = $node->organism;
 
-  // keep track of the organism id if we have one.  If we do have one then
-  // this would indicate an update as opposed to an insert.
-  $form['organism_id'] = array(
-    '#type' => 'value',
-    '#value' => $organism->organism_id,
-  );
+    // add in the comment since it is a text field and may not be included if too big
+    $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
+    
+    // get form defaults
+    $abbreviation   = property_exists($node, 'abbreviation')   ? property_exists($node, 'abbreviation')   : $organism->abbreviation;
+    $genus          = property_exists($node, 'genus')          ? property_exists($node, 'genus')          : $organism->genus;
+    $species        = property_exists($node, 'species')        ? property_exists($node, 'species')        : $organism->species;
+    $common_name    = property_exists($node, 'common_name')    ? property_exists($node, 'common_name')    : $organism->common_name;
+    $description    = property_exists($node, 'description')    ? property_exists($node, 'description')    : $organism->comment;
+    $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
+    
+    // set the organism_id in the form
+    $form['organism_id'] = array(
+      '#type' => 'value',
+      '#value' => $organism->organism_id,
+    );
+  }
+  else {
+    // get form defaults
+    $abbreviation   = property_exists($node, 'abbreviation')   ? property_exists($node, 'abbreviation')   : '';
+    $genus          = property_exists($node, 'genus')          ? property_exists($node, 'genus')          : '';
+    $species        = property_exists($node, 'species')        ? property_exists($node, 'species')        : '';
+    $common_name    = property_exists($node, 'common_name')    ? property_exists($node, 'common_name')    : '';
+    $description    = property_exists($node, 'description')    ? property_exists($node, 'description')    : '';
+    $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
+  }
 
   $form['abbreviation']= array(
     '#type' => 'textfield',