Переглянути джерело

Chado Node API: cleaned up the set title code and added the ability for modules to set a default for their content type

Lacey Sanderson 11 роки тому
батько
коміт
ba48682fb9

+ 89 - 29
tripal_core/api/tripal_core.chado_nodes.title_and_path.inc

@@ -13,7 +13,7 @@
     // for your node type then you need to add the configuration form for this functionality.
     $details = array(
       'module' => 'tripal_example',       // the name of the MODULE implementing the content type
-      'content_type' => 'chado_example'   // the name of the content type
+      'content_type' => 'chado_example',   // the name of the content type
         // An array of options to use under "Page Titles"
         // the key should be the token and the value should be the human-readable option
       'options' => array(
@@ -54,6 +54,17 @@
     return $new_nodes;
   }
  * @endcode
+ *
+ * Optionally define a default for a specific content type by implementing a function of the name
+ * [content type]_chado_node_default_title_format() that returns a string describing the
+ * default format.
+ * @code
+  function chado_example_chado_node_default_title_format() {
+    return '[example.example_id]';
+  }
+ * @endcode
+ * If you don't implement this then a default format based on the unique constraint for
+ * the base table of the content type will be generated.
  */
 
 /**
@@ -75,28 +86,12 @@
 function chado_get_node_title($node) {
   $content_type = $node->type;
 
-  $format_record = chado_node_get_token_format('title',$content_type, array('return_record' => TRUE));
-
-  if (empty($format_record)) {
-    $base_table = chado_node_get_base_table($content_type);
-    $tokens = chado_node_generate_tokens($base_table);
-    
-    // Check for a legacy option
-    $title = chado_node_get_legacy_title_default($content_type);
-    
-    // Otherwise get the unique constraint option
-    if (empty($title)) {
-      $title = chado_node_get_unique_constraint_format($base_table);
-    }
-    chado_node_add_token_format('title',$content_type,$title,$tokens);
-  }
-  else {
-    $title = $format_record->format;
-    $tokens = unserialize($format_record->tokens);
-  }
+  // Get the tokens and format
+  $tokens = array(); // this will be set by chado_node_get_title_format
+  $title = chado_node_get_title_format($content_type, $tokens);
 
   // Determine which tokens were used in the format string
-  if (preg_match_all('/\[[^]]+\]/',$title,$used_tokens)) {
+  if (preg_match_all('/\[[^]]+\]/', $title, $used_tokens)) {
 
     // Get the value for each token used
     foreach ($used_tokens[0] as $token) {
@@ -158,13 +153,12 @@ function chado_add_admin_form_set_title(&$form, &$form_state, $details) {
   $details['additional_instructions'] = (isset($details['additional_instructions'])) ? $details['additional_instructions'] : '';
   $details['custom_tokens'] = (isset($details['custom_tokens'])) ? $details['custom_tokens'] : array();
   $details['content_type'] = (isset($details['content_type'])) ? $details['content_type'] : $details['module'];
-  $details['default_option'] = (isset($details['default_option'])) ? $details['default_option'] : FALSE;
-  $details['default_option'] = ($details['default_option'] === FALSE) ? chado_node_get_token_format('title', $node_info[ $details['content_type'] ]['base']) : $details['default_option'];
-  $details['default_option'] = ($details['default_option'] === FALSE) ? $details['unique_option'] : $details['default_option'];
 
-
-  // Determine what the tokens for the custom option should be using the chado schema api
-  $tokens = chado_node_generate_tokens($chado_node_api['base_table']);
+  $tokens = array();
+  $details['default_option'] = (isset($details['default_option'])) ? $details['default_option'] : chado_node_get_title_format($details['content_type'], $tokens);
+  if (empty($tokens)) {
+    $tokens = chado_node_generate_tokens($chado_node_api['base_table']);
+  }
   $tokens = array_merge($tokens, $details['custom_tokens']);
   $token_list = chado_node_format_tokens($tokens);
 
@@ -285,6 +279,71 @@ function chado_add_admin_form_set_title_form_submit($form, $form_state) {
   chado_node_add_token_format('title', $form_state['values']['content_type'], $format, $form_state['values']['tokens']);
 }
 
+/**
+ * Get the title format for a specific content type
+ *
+ * If the title format has not yet been set then the following will be done
+ *  1) Check to see if there is a legacy title format set (features & stocks)
+ *  2) Check if there is a defined default for this content type
+ *  3) Create a format using any name fields and the unique constraint for the
+ *     base table associated with this content type
+ *
+ * Define a default for a specific content type by implementing a function of the name
+ * [content type]_chado_node_default_title_format() that returns a string describing the
+ * default format.
+ *
+ * @param $content_type
+ *   The name of the content (node) type you are interested in (ie: chado_feature)
+ *
+ * @return
+ *   A string containing tokens describing the default format for the title of nodes
+ *   of the specified content type.
+ */
+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));
+  if (!empty($format_record)) {
+    $format = $format_record->format;
+    $tokens = $format_record->tokens;
+  }
+
+  // All three options below need the tokens to be generated so do that now
+  if (empty($format)) {
+    if (empty($base_table)) {
+      $base_table = chado_node_get_base_table($content_type);
+    }
+    $tokens = chado_node_generate_tokens($base_table);
+  }
+
+  // 1) Check for legacy format
+  if (empty($format)) {
+    $format = chado_node_get_legacy_title_default($content_type);
+  }
+
+  // 2) Module-defined default format
+  if (empty($format)) {
+    $hook = $content_type . '_chado_node_default_title_format';
+    if (function_exists($hook)) {
+      $format = call_user_func($hook);
+    }
+  }
+
+  // 3) Create unique constraint format
+  if (empty($format)) {
+    if (empty($base_table)) {
+      $base_table = chado_node_get_base_table($content_type);
+    }
+    $format = chado_node_get_unique_constraint_format($base_table);
+  }
+
+  // Add the format to the new system so we can use it later
+  chado_node_add_token_format('title', $content_type, $format, $tokens);
+
+  return $format;
+}
+
 /**
  * Handles legacy title options
  *
@@ -403,6 +462,7 @@ function chado_node_get_token_format($application, $content_type, $options = arr
 
   if (is_object($format_record)) {
     if (isset($options['return_record'])) {
+      $format_record->tokens = unserialize($format_record->tokens);
       return $format_record;
     }
     else {
@@ -427,7 +487,7 @@ function chado_node_get_token_format($application, $content_type, $options = arr
 function chado_node_get_unique_constraint_format($base_table) {
 
   $table_descrip = chado_get_schema($base_table);
-  
+
   // Find the name/uniquename from the base table
   $names = array();
   foreach($table_descrip['fields'] as $field_name => $field) {
@@ -457,7 +517,7 @@ function chado_node_get_unique_constraint_format($base_table) {
       }
     }
   }
-  
+
   $format = implode(', ',$names) . ' (' . implode(', ',$tokens) . ')';
   return $format;
 }

+ 10 - 43
tripal_feature/includes/tripal_feature.admin.inc

@@ -65,40 +65,7 @@ function tripal_feature_admin() {
   // This call adds the configuration form to your current form
   // This sub-form handles it's own validation & submit
   chado_add_admin_form_set_title($form, $form_state, $details);
-    
-/**
-  $form['title'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Feature Page Titles'),
-    '#collapsible' => FALSE,
-    '#collapsed' => FALSE,
-  );
-  $form['title']['desc'] = array(
-    '#markup' => t(
-      'Each synced feature must have a unique page title, however, features '.
-      'may have the same name if they are of different types or from '.
-      'different organisms.  Therefore, we must be sure that the '.
-      'page titles can uniquely identify the feature being viewed.  Select '.
-      'an option below that will uniquely identify all features on your site.'),
-  );
-  $options = array(
-    'feature_unique_name'  => 'Feature unique name',
-    'feature_name'         => 'Feature name',
-    'unique_constraint'    => 'Feature Name, uniquename, type and species',
-  );
-  $form['title']['chado_feature_title'] = array(
-    '#title'         => t('Feature Page Titles'),
-    '#type'          => 'radios',
-    '#description'   => t('Choose a title type  from the list above that is '.
-      'guaranteed to be unique for all features.  If in doubt it is safest to '.
-      'choose the last option as that guarantees uniqueness. Click the '.
-      '\'Save Configuration\' button at the bottom to save your selection.'),
-    '#required'      => FALSE,
-    '#options'       => $options,
-    '#default_value' => variable_get('chado_feature_title', 'unique_constraint'),
-  );
-  */
-    
+
   // FEATURE URL PATHS
   $form['url'] = array(
     '#type' => 'fieldset',
@@ -106,7 +73,7 @@ function tripal_feature_admin() {
     '#collapsible' => FALSE,
     '#collapsed' => FALSE,
   );
-  
+
   $options = array(
     'SID[id]'      => '[id]:' . t('The Chado feature_id'),
     'feature'      => 'feature:' . t('Chado table name'),
@@ -117,7 +84,7 @@ function tripal_feature_admin() {
     '[name]'       => '[name]:' . t('The feature name'),
     'reset'        => t('Reset'),
   );
-  
+
   $form['url']['chado_feature_url_string'] = array(
     '#title' => 'URL Syntax',
     '#type' => 'textfield',
@@ -128,11 +95,11 @@ function tripal_feature_admin() {
       'reset the URLs for all feature pages.  Click the "Save Configuration" button to '.
       '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]'), 
+      'and uniquename you are guaranteed to have a unique URL. For example feature/[genus]/[species]/[type]/[uniquename]'),
     '#size' => 150,
     '#default_value' => variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]'),
   );
-  
+
   $form['url']['chado_feature_url'] = array(
     '#title'         => t('URL components'),
     '#type'          => 'checkboxes',
@@ -152,14 +119,14 @@ function tripal_feature_admin() {
     ',
     ),
   );
-  
+
   $form['url']['button'] = array(
     '#type' => 'submit',
     '#value' => t('Set Feature URLs'),
   );
-    
-    
-    
+
+
+
   // FEATURE BROWSER
   $form['browser'] = array(
      '#type' => 'fieldset',
@@ -212,7 +179,7 @@ function tripal_feature_admin() {
      '#value' => t('Set Summary'),
      '#weight' => 2,
   );
-  
+
   return system_settings_form($form);
 }
 

+ 24 - 16
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -139,7 +139,7 @@ function chado_feature_form($node, &$form_state) {
     '#description' => t('Enter a unique name for this feature.  This name must be unique for the organism and feature type.'),
     '#maxlength' => 255
   );
-  
+
   $type_options = tripal_get_cvterm_default_select_options('feature', 'type_id', 'feature types');
   $type_options[0] = 'Select a Type';
   $type_cv = tripal_get_default_cv('feature', 'type_id');
@@ -230,10 +230,10 @@ function chado_feature_form($node, &$form_state) {
     'base_key_value' => $feature_id       // the value of feature_id for this record
   );
   chado_add_node_form_dbxrefs($form, $form_state, $details);
-  
+
   // TODO: For some reason adding a relationship to the form breaks AJAX
   // for features (works for other node type)... need to debug
-  
+
   // RELATIONSHIPS FORM
   //---------------------------------------------
   $relationship_cv = tripal_get_default_cv('feature_relationship', 'type_id');
@@ -247,7 +247,7 @@ function chado_feature_form($node, &$form_state) {
     'cv_id' => $cv_id
   );
   chado_add_node_form_relationships($form, $form_state, $details);
-  
+
 
   return $form;
 }
@@ -280,7 +280,7 @@ function chado_feature_validate($node, $form, &$form_state) {
   $node->fname        = trim($node->fname);
   $node->feature_type = trim($node->feature_type);
   $node->residues     = trim($node->residues);
-  
+
   // Validating for an update
   if (property_exists($node, 'nid')) {
 
@@ -368,7 +368,7 @@ function chado_feature_node_access($node, $op, $account) {
   if (is_object($node)) {
     $node_type = $node->type;
   }
-  
+
   if($node_type == 'chado_feature') {
     if ($op == 'create') {
       if (!user_access('create chado_feature content', $account)) {
@@ -376,7 +376,7 @@ function chado_feature_node_access($node, $op, $account) {
       }
       return NODE_ACCESS_ALLOW;
     }
-  
+
     if ($op == 'update') {
       if (!user_access('edit chado_feature content', $account)) {
         return NODE_ACCESS_DENY;
@@ -606,7 +606,7 @@ function chado_feature_delete($node) {
   db_query("DELETE FROM frange.featuregroup WHERE group_id = :feature_id", array(':feature_id' => $feature_id));
   db_query("DELETE FROM frange.featuregroup WHERE srcfeature_id = :feature_id", array(':feature_id' => $feature_id));
   chado_set_active($previous_db);
-  
+
   chado_query("DELETE FROM {featureloc} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
   chado_query("DELETE FROM {featureloc} WHERE srcfeature_id = :feature_id", array(':feature_id' => $feature_id));
   chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
@@ -700,7 +700,7 @@ function chado_feature_load($nodes) {
   foreach ($nodes as $nid => $node) {
     // find the feature and add in the details
     $feature_id = chado_get_id_from_nid('feature', $nid);
-    
+
     // if the nid does not have a matching record then skip this node.
     // this can happen with orphaned nodes.
     if (!$feature_id) {
@@ -773,7 +773,7 @@ function tripal_feature_node_insert($node) {
   // know the feature_id in the presave
   switch ($node->type) {
     case 'chado_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);
@@ -788,7 +788,7 @@ function tripal_feature_node_insert($node) {
 
       // Now get the title
       $node->title = chado_get_node_title($node);
-      
+
       break;
   }
 }
@@ -814,7 +814,7 @@ function tripal_feature_node_update($node) {
 
       // Now get the title
       $node->title = chado_get_node_title($node);
-      
+
       break;
   }
 }
@@ -892,12 +892,12 @@ function tripal_feature_get_feature_url($node, $url_alias = NULL) {
 /**
  * Resets all of the URL alias for all features.  This function is meant to
  * be run using Tripal's job managmenet interface
- *  
+ *
  * @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
- * 
+ *
  * @ingroup tripal_feature
  */
 function tripal_feature_set_urls($na = NULL, $job = NULL) {
@@ -930,7 +930,7 @@ function tripal_feature_set_urls($na = NULL, $job = NULL) {
     $sql = "SELECT * FROM {chado_feature}";
     $nodes = db_query($sql);
     foreach ($nodes as $node) {
-       
+
       // get the URL alias
       $src = "node/$node->nid";
       $dst = tripal_feature_get_feature_url($node, $url_alias);
@@ -1064,4 +1064,12 @@ function tripal_feature_node_view($node, $view_mode, $langcode) {
   }
 }
 
-
+/**
+ * Implements [content_type]_chado_node_default_title_format().
+ *
+ * Defines a default title format for the Chado Node API to set the titles on
+ * Chado Feature nodes based on chado fields.
+ */
+function chado_feature_chado_node_default_title_format() {
+  return '[feature.name], [feature.uniquename] ([feature.type_id>cvterm.name]) [feature.organism_id>organism.genus] [feature.organism_id>organism.species]';
+}

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

@@ -52,7 +52,6 @@ function tripal_stock_admin() {
 
 
   // STOCK PAGE TITLES CONFIGURATION
-
   $details = array(
     'module' => 'tripal_stock',
     'content_type' => 'chado_stock',

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

@@ -1085,4 +1085,14 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
     watchdog_exception('tripal_stock', $e);
     watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
   }
+}
+
+/**
+ * Implements [content_type]_chado_node_default_title_format().
+ *
+ * Defines a default title format for the Chado Node API to set the titles on
+ * Chado Stock nodes based on chado fields.
+ */
+function chado_stock_chado_node_default_title_format() {
+  return '[stock.name], [stock.uniquename] ([stock.type_id>cvterm.name]) [stock.organism_id>organism.genus] [stock.organism_id>organism.species]';
 }