Browse Source

Fixed bug in Title/Path API: insert and update hooks didn't have the table object which required that the form field ids matched table names. This can't always be guaranteed

Stephen Ficklin 11 years ago
parent
commit
e86f0491d8

+ 13 - 1
tripal_analysis/includes/tripal_analysis.chado_node.inc

@@ -724,7 +724,13 @@ function tripal_analysis_node_insert($node) {
 
   switch ($node->type) {
     case 'chado_analysis':
-
+      
+      // build the analysis variable
+      $analysis_id = chado_get_id_from_nid('analysis', $node->nid);      
+      $values = array('analysis_id' => $analysis_id);
+      $analysis = chado_generate_var('analysis', $values);
+      $node->analysis = $analysis;
+      
       // Now get the title
       $node->title = chado_get_node_title($node);
 
@@ -742,6 +748,12 @@ function tripal_analysis_node_update($node) {
 
   switch ($node->type) {
     case 'chado_analysis':
+      
+      // build the analysis variable
+      $analysis_id = chado_get_id_from_nid('analysis', $node->nid);
+      $values = array('analysis_id' => $analysis_id);
+      $analysis = chado_generate_var('analysis', $values);
+      $node->analysis = $analysis;
 
       // Now get the title
       $node->title = chado_get_node_title($node);

+ 16 - 0
tripal_contact/includes/tripal_contact.chado_node.inc

@@ -647,6 +647,14 @@ function tripal_contact_node_insert($node) {
   switch ($node->type) {
     case 'chado_contact':
 
+      // find the contact and add in the details
+      $contact_id = chado_get_id_from_nid('contact', $node->nid);
+     
+      // get the contact
+      $values = array('contact_id' => $contact_id);
+      $contact = chado_generate_var('contact', $values);
+      $node->contact = $contact;
+      
       // Now get the title
       $node->title = chado_get_node_title($node);
 
@@ -665,6 +673,14 @@ function tripal_contact_node_update($node) {
   switch ($node->type) {
     case 'chado_contact':
 
+      // find the contact and add in the details
+      $contact_id = chado_get_id_from_nid('contact', $node->nid);
+       
+      // get the contact
+      $values = array('contact_id' => $contact_id);
+      $contact = chado_generate_var('contact', $values);
+      $node->contact = $contact;
+      
       // Now get the title
       $node->title = chado_get_node_title($node);
 

+ 3 - 1
tripal_core/api/tripal_core.chado_nodes.properties.api.inc

@@ -541,7 +541,9 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
   if (isset($form_state['input']['property_table']['new']['type'])) {
     $new_type_id = $form_state['input']['property_table']['new']['type'];
     $new_term = tripal_get_cvterm(array('cvterm_id' => $new_type_id));
-    $type_desc = $new_term->definition;
+    if ($new_term) {
+      $type_desc = $new_term->definition;
+    }
   }
   $form['properties']['property_table']['new']['type'] = array(
     '#type' => 'select',

+ 30 - 18
tripal_core/api/tripal_core.chado_nodes.title_and_path.inc

@@ -105,7 +105,7 @@
  * a default format
  *
  * @param $node
- *   The node whose title you want
+ *   The node object
  *
  * @ingroup tripal_chado_node_api
  */
@@ -124,8 +124,7 @@ function chado_get_node_title($node) {
       $token_info = $tokens[$token];
       if (!empty($token_info)) {
         $value = chado_get_token_value($token_info, $node);
-
-        $title = str_replace($token,$value,$title);
+        $title = str_replace($token, $value, $title);
       }
     }
   }
@@ -320,6 +319,16 @@ function chado_add_admin_form_set_title_form_submit($form, $form_state) {
  *
  * @param $content_type
  *   The name of the content (node) type you are interested in (ie: chado_feature)
+ * @param $tokens
+ *   An array, passed by reference that is filled to include the tokens for this
+ *   node type.  Each token is an array with the following keys:
+ *    -table: the name of the chado table
+ *    -field: the name of the field in the above table
+ *    -token: the token string (ie: [stock.stock_id])
+ *    -description: a very short description of the token (displayed when tokens are listed)
+ *    -location: the location of the value in a chado node variable with each level
+ *     separated by an arrow (->) symbol. For example, the location for $node->feature->type_id->name
+ *     is feature>type_id>name
  *
  * @return
  *   A string containing tokens describing the default format for the title of nodes
@@ -329,7 +338,7 @@ function chado_node_get_title_format($content_type, &$tokens, $base_table = NULL
   $format = '';
 
   // Is there a title format set?
-  $format_record = chado_node_get_token_format('title',$content_type, array('return_record' => TRUE));
+  $format_record = chado_node_get_token_format('title', $content_type, array('return_record' => TRUE));
   if (!empty($format_record)) {
     $format = $format_record->format;
     $tokens = $format_record->tokens;
@@ -364,7 +373,7 @@ function chado_node_get_title_format($content_type, &$tokens, $base_table = NULL
     $format = chado_node_get_unique_constraint_format($base_table);
   }
 
-  // Add the format to the new system so we can use it later
+  // Add the format to table so we can use it later
   chado_node_add_token_format('title', $content_type, $format, $tokens);
 
   return $format;
@@ -638,42 +647,45 @@ function chado_node_generate_tokens($base_table, $token_prefix = FALSE, $locatio
  *     separated by an arrow (>) symbol. For example, the location for $node->feature->type_id->name
  *     is feature>type_id>name
  *  @param $node
- *   The node to get the value of the token from
+ *   The node to get the value of the token
  *
  * @return
  *   The value of the token
  */
 function chado_get_token_value($token_info, $node) {
 
-  $location = explode('>',$token_info['location']);
+  $token = $token_info['token'];
+  $table = $token_info['table'];
+  $location = explode('>', $token_info['location']);
 
   $var = $node;
   foreach ($location as $index) {
     $index = trim($index);
+
+    // if $var is an object then it is the $node object or a table
+    // that has been expanded. On a node_insert then the fields are 
     if (is_object($var)) {
-      if (isset($var->{$index})) {
-        $var = $var->{$index};
+      // check to see if the index is a member of the object. If so,
+      // then reset the $var to this value.
+      if (property_exists($var, $index)) {
+        $var = $var->$index;
       }
       else {
         // @TODO: Error Handling
       }
     }
+    // if the $var is an array then there are multiple instances of the same
+    // table in a FK relationship (e.g. relationship tables)
     elseif (is_array($var)) {
       $var = $var[$index];
     }
     else {
-      tripal_report_error(
-        'chado_node_api',
-        TRIPAL_WARNING,
+      tripal_report_error('chado_node_api', TRIPAL_WARNING,
         'Tokens: Unable to determine the value of %token. Things went awry when trying
         to access %index for the following %var',
-        array(
-          '%token' => $token,
-          '%index' => $index,
-          '%var' => print_r($var,TRUE)
-        )
+        array('%token' => $token, '%index' => $index, '%var' => print_r($var,TRUE))
       );
-      return;
+      return '';
     }
   }
   return $var;

+ 11 - 7
tripal_example/includes/tripal_example.chado_node.inc

@@ -574,6 +574,7 @@ function chado_example_load($nodes) {
     // cases it is probably best to let the end-user decide if text fields should
     // be included by using this function in the templates.
     $example = chado_expand_var($example, 'field', 'example.description');
+    
 
     // If your module is using the Chado Node: Title & Path API to allow custom titles
     // for your node type. Every time you want the title of the node, you need to use the
@@ -652,13 +653,16 @@ function tripal_example_node_insert($node) {
   // it in the hook_insert()
   switch ($node->type) {
     case 'chado_example':
-
-      // if we do not have an record ID for this node then get it
-      if (!property_exists($node, 'example_id')) {
-        $node->example_id = chado_get_id_from_nid('example', $node->nid);
-      }
-
-      // If your module is using the Chado Node: Title & Path API to allow custom titles
+     
+      // find the example and add in the details
+      $example_id = chado_get_id_from_nid('example', $nid);
+      
+      // build the example variable by using the chado_generate_var() function
+      $values = array('example_id' => $example_id);
+      $example = chado_generate_var('example', $values);
+      $node->example = $example;
+
+      // If your module is using the 'Chado Node: Title & Path API' to allow custom titles
       // for your node type. Every time you want the title of the node, you need to use the
       // following API function:
       $example->title = chado_get_node_title($node);

+ 1 - 1
tripal_feature/includes/tripal_feature.admin.inc

@@ -96,7 +96,7 @@ function tripal_feature_admin() {
       'simply save this setup. <b>Important</b>: be sure that whatever you choose will always be unique even considering '.
       'future data that may be added.  If you include the Chado table name, genus, species, type '.
       'and uniquename you are guaranteed to have a unique URL. For example feature/[genus]/[species]/[type]/[uniquename]'),
-    '#size' => 150,
+    '#size' => 75,
     '#default_value' => variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]'),
   );
 

+ 15 - 0
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -774,6 +774,13 @@ function tripal_feature_node_insert($node) {
   switch ($node->type) {
     case 'chado_feature':
 
+      $feature_id = chado_get_id_from_nid('feature', $node->nid);
+      
+      // build the feature variable
+      $values = array('feature_id' => $feature_id);
+      $feature = chado_generate_var('feature', $values);
+      $node->feature = $feature;
+      
       // on an insert we need to add the feature_id to the node object
       // so that the tripal_feature_get_feature_url function can set the URL properly
       $node->feature_id = chado_get_id_from_nid('feature', $node->nid);
@@ -804,6 +811,14 @@ function tripal_feature_node_update($node) {
   // add items to other nodes, build index and search results
   switch ($node->type) {
     case 'chado_feature':
+      
+      $feature_id = chado_get_id_from_nid('feature', $node->nid);
+      
+      // build the feature variable
+      $values = array('feature_id' => $feature_id);
+      $feature = chado_generate_var('feature', $values);
+      $node->feature = $feature;
+      
       // remove any previous alias
       db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
 

+ 12 - 0
tripal_featuremap/includes/tripal_featuremap.chado_node.inc

@@ -551,6 +551,12 @@ function tripal_featuremap_node_insert($node) {
 
   switch ($node->type) {
     case 'chado_featuremap':
+      
+      // get the feature details from chado
+      $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);      
+      $values = array('featuremap_id' => $featuremap_id);
+      $featuremap = chado_generate_var('featuremap', $values);
+      $node->featuremap = $featuremap;
 
       // Now get the title
       $node->title = chado_get_node_title($node);
@@ -570,6 +576,12 @@ function tripal_featuremap_node_update($node) {
   switch ($node->type) {
     case 'chado_featuremap':
 
+      // get the feature details from chado
+      $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
+      $values = array('featuremap_id' => $featuremap_id);
+      $featuremap = chado_generate_var('featuremap', $values);
+      $node->featuremap = $featuremap;
+      
       // Now get the title
       $node->title = chado_get_node_title($node);
 

+ 12 - 0
tripal_library/includes/tripal_library.chado_node.inc

@@ -592,6 +592,12 @@ function tripal_library_node_insert($node) {
 
   switch ($node->type) {
     case 'chado_library':
+      
+      $library_id = chado_get_id_from_nid('library', $node->nid);
+      $values = array('library_id' => $library_id);
+      $library = chado_generate_var('library', $values);
+      $library = chado_expand_var($library, 'field', 'library.uniquename');
+      $node->library = $library;
 
       // Now get the title
       $node->title = chado_get_node_title($node);
@@ -610,6 +616,12 @@ function tripal_library_node_update($node) {
 
   switch ($node->type) {
     case 'chado_library':
+      
+      $library_id = chado_get_id_from_nid('library', $node->nid);
+      $values = array('library_id' => $library_id);
+      $library = chado_generate_var('library', $values);
+      $library = chado_expand_var($library, 'field', 'library.uniquename');
+      $node->library = $library;
 
       // Now get the title
       $node->title = chado_get_node_title($node);

+ 12 - 0
tripal_organism/includes/tripal_organism.chado_node.inc

@@ -579,6 +579,12 @@ function tripal_organism_node_insert($node) {
 
   switch ($node->type) {
     case 'chado_organism':
+      
+      // find the organism and add in the details
+      $organism_id = chado_get_id_from_nid('organism', $node->nid);
+      $values = array('organism_id' => $organism_id);
+      $organism = chado_generate_var('organism', $values);
+      $node->organism = $organism;
 
       // Now get the title
       $node->title = chado_get_node_title($node);
@@ -597,6 +603,12 @@ function tripal_organism_node_update($node) {
 
   switch ($node->type) {
     case 'chado_organism':
+      
+      // find the organism and add in the details
+      $organism_id = chado_get_id_from_nid('organism', $node->nid);
+      $values = array('organism_id' => $organism_id);
+      $organism = chado_generate_var('organism', $values);
+      $node->organism = $organism;
 
       // Now get the title
       $node->title = chado_get_node_title($node);

+ 1 - 1
tripal_project/includes/tripal_project.admin.inc

@@ -95,7 +95,7 @@ function tripal_project_admin($form, $form_state) {
       'simply save this setup. <b>Important</b>: be sure that whatever you choose will '.
       'always be unique even considering future data that may be added.  If you include '.
       'the Chado table name and id you are guaranteed to have a unique URL. For example project/[id]'),
-    '#size' => 150,
+    '#size' => 75,
     '#default_value' => variable_get('chado_project_url_string', '/project/[id]'),
   );
 

+ 13 - 1
tripal_project/includes/tripal_project.chado_node.inc

@@ -524,13 +524,19 @@ function tripal_project_node_insert($node) {
   // know the project_id in the presave
   switch ($node->type) {
     case 'chado_project':
+      
+      // get the feature details from chado
+      $project_id = chado_get_id_from_nid('project', $node->nid);
+      $values = array('project_id' => $project_id);
+      $project = chado_generate_var('project', $values);
+      $nodes->project = $project;
 
       // Now get the title
       $node->title = chado_get_node_title($node);
       
       // on an insert we need to add the project_id to the node object
       // so that the tripal_project_get_project_url function can set the URL properly
-      $node->project_id = chado_get_id_from_nid('project', $node->nid);
+      $node->project_id = $project_id;
 
       // remove any previous alias
       db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
@@ -555,6 +561,12 @@ function tripal_project_node_update($node) {
   switch ($node->type) {
     case 'chado_project':
 
+      // get the feature details from chado
+      $project_id = chado_get_id_from_nid('project', $node->nid);
+      $values = array('project_id' => $project_id);
+      $project = chado_generate_var('project', $values);
+      $nodes->project = $project;
+      
       // Now get the title
       $node->title = chado_get_node_title($node);
       

+ 1 - 0
tripal_project/tripal_project.info

@@ -9,3 +9,4 @@ configure = admin/tripal/chado/tripal_project
 dependencies[] = tripal_core
 dependencies[] = tripal_views
 dependencies[] = tripal_cv
+dependencies[] = tripal_contact

+ 28 - 6
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -148,7 +148,7 @@ function chado_pub_form($node, $form_state) {
     $title        = $form_state['input']['pubtitle'];
     $uniquename   = $form_state['input']['uniquename'];
     $type_id      = $form_state['input']['type_id'];
-    $is_obsolete  = $form_state['input']['is_obsolete'];
+    $is_obsolete  = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : '';
   }
 
   $form['pubtitle'] = array(
@@ -586,7 +586,7 @@ function chado_pub_insert($node) {
       if ($prop->name == 'Citation') {
         $citation_id = $prop->cvterm_id;
         if (!empty($node->uniquename)) {
-          $properties[$citation_id] = $node->uniquename;
+          $properties[$citation_id][0] = $node->uniquename;
         }
       }
     }
@@ -1091,11 +1091,22 @@ function tripal_pub_node_view($node, $view_mode, $langcode) {
 function tripal_pub_node_insert($node) {
 
   if ($node->type == 'chado_pub') {
+    
+    // get the pub
+    $pub_id = chado_get_id_from_nid('pub', $node->nid);
+    $values = array('pub_id' => $pub_id);
+    $pub = chado_generate_var('pub', $values);
 
+    // expand the 'text' fields as those aren't included by default
+    // and they really shouldn't be so large to cause problems
+    $pub = chado_expand_var($pub, 'field', 'pub.title');
+    $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
+    $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
+    $node->pub = $pub;
+    
     // Now get the title
     $node->title = chado_get_node_title($node);
-      
-    $pub_id = chado_get_id_from_nid('pub', $node->nid);
+
     tripal_pub_set_pub_url($node, $pub_id);
   }
 }
@@ -1140,10 +1151,21 @@ function tripal_pub_node_update($node) {
 
   if ($node->type == 'chado_pub') {
 
+    // get the pub
+    $pub_id = chado_get_id_from_nid('pub', $node->nid);
+    $values = array('pub_id' => $pub_id);
+    $pub = chado_generate_var('pub', $values);
+    
+    // expand the 'text' fields as those aren't included by default
+    // and they really shouldn't be so large to cause problems
+    $pub = chado_expand_var($pub, 'field', 'pub.title');
+    $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
+    $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
+    $node->pub = $pub;
+            
     // Now get the title
     $node->title = chado_get_node_title($node);
-      
-    $pub_id = chado_get_id_from_nid('pub', $node->nid);
+
     tripal_pub_set_pub_url($node, $pub_id);
   }
 }

+ 1 - 1
tripal_stock/includes/tripal_stock.admin.inc

@@ -95,7 +95,7 @@ function tripal_stock_admin() {
       simply save this setup. <b>Important</b>: be sure that whatever you choose will always be unique even considering
       future data that may be added.  If you include the Chado table name, genus, species, type
       and uniquename you are guaranteed to have a unique URL. For example stock/[genus]/[species]/[type]/[uniquename]'),
-    '#size' => 150,
+    '#size' => 75,
     '#default_value' => variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]'),
   );
 

+ 16 - 0
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -888,6 +888,14 @@ function tripal_stock_node_insert($node) {
   // know the stock_id in the presave
   switch ($node->type) {
     case 'chado_stock':
+      
+      // get the feature details from chado
+      $stock_id = chado_get_id_from_nid('stock', $node->nid);
+      $values = array('stock_id' => $stock_id);
+      $stock = chado_generate_var('stock', $values);
+      $stock = chado_expand_var($stock, 'field', 'stock.uniquename');
+      $stock = chado_expand_var($stock, 'field', 'stock.description');
+      $node->stock = $stock;
 
       // If your module is using the Chado Node: Title & Path API to allow custom titles
       // for your node type. Every time you want the title of the node, you need to use the
@@ -920,6 +928,14 @@ function tripal_stock_node_update($node) {
   // add items to other nodes, build index and search results
   switch ($node->type) {
     case 'chado_stock':
+      
+      // get the feature details from chado
+      $stock_id = chado_get_id_from_nid('stock', $node->nid);
+      $values = array('stock_id' => $stock_id);
+      $stock = chado_generate_var('stock', $values);
+      $stock = chado_expand_var($stock, 'field', 'stock.uniquename');
+      $stock = chado_expand_var($stock, 'field', 'stock.description');
+      $node->stock = $stock;
 
       // If your module is using the Chado Node: Title & Path API to allow custom titles
       // for your node type. Every time you want the title of the node, you need to use the