Browse Source

Fixed bug where non-sync'd content had a broken link in views by adding link to node checkbox back to the chado_views_handler_field handler. Also added an API function tripal_core_is_tripal_node_type(chado_table) to ensure this checkbox is only shown for fields from a base table that links to a drupal node. Finally updated all the default views to use this functionality.

Lacey Sanderson 12 years ago
parent
commit
966435e323

+ 1 - 2
tripal_analysis/tripal_analysis.views.inc

@@ -526,8 +526,7 @@ function tripal_analysis_views_default_views() {
       ),
     ));
     // Change analysis.name to have a link to the node
-    $fields['name']['alter']['make_link'] = 1;
-    $fields['name']['alter']['path'] = 'node/[nid]';
+    $fields['name']['link_to_node'] = 1;
     $default_handler->override_option('fields', $fields);
     // Only show records with published nodes
     /**

+ 54 - 36
tripal_core/api/tripal_core.api.inc

@@ -121,18 +121,18 @@ require_once "tripal_core.schema_v1.11.api.inc";
  * @ingroup tripal_chado_api
  */
 function tripal_core_chado_insert($table, $values, $options = array()) {
-  
+
   if (!is_array($values)) {
-    watchdog('tripal_core', 'Cannot pass non array as values for inserting.', array(), 
+    watchdog('tripal_core', 'Cannot pass non array as values for inserting.', array(),
       WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   if (count($values)==0) {
-    watchdog('tripal_core', 'Cannot pass an empty array as values for inserting.', array(), 
+    watchdog('tripal_core', 'Cannot pass an empty array as values for inserting.', array(),
       WATCHDOG_ERROR);
     return FALSE;
   }
-  
+
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -475,27 +475,27 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
 function tripal_core_chado_update($table, $match, $values, $options = NULL) {
 
   if (!is_array($values)) {
-    watchdog('tripal_core', 'Cannot pass non array as values for updating.', array(), 
+    watchdog('tripal_core', 'Cannot pass non array as values for updating.', array(),
       WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   if (count($values)==0) {
-    watchdog('tripal_core', 'Cannot pass an empty array as values for updating.', array(), 
+    watchdog('tripal_core', 'Cannot pass an empty array as values for updating.', array(),
       WATCHDOG_ERROR);
     return FALSE;
   }
-  
+
   if (!is_array($match)) {
-    watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(), 
+    watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(),
       WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   if (count($match)==0) {
-    watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(), 
+    watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(),
       WATCHDOG_ERROR);
     return FALSE;
   }
-  
+
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -874,18 +874,18 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
  * @ingroup tripal_chado_api
  */
 function tripal_core_chado_delete($table, $match, $options = NULL) {
-  
+
   if (!is_array($match)) {
-    watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(), 
+    watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(),
       WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   if (count($match)==0) {
-    watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(), 
+    watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(),
       WATCHDOG_ERROR);
     return FALSE;
   }
-  
+
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -1170,27 +1170,27 @@ function tripal_core_chado_delete($table, $match, $options = NULL) {
 function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
 
   if (!is_array($values)) {
-    watchdog('tripal_core', 'Cannot pass non array as values for selecting.', array(), 
+    watchdog('tripal_core', 'Cannot pass non array as values for selecting.', array(),
       WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   if (count($values)==0) {
-    watchdog('tripal_core', 'Cannot pass an empty array as values for selecting.', array(), 
+    watchdog('tripal_core', 'Cannot pass an empty array as values for selecting.', array(),
       WATCHDOG_ERROR);
     return FALSE;
   }
-  
+
   if (!is_array($columns)) {
-    watchdog('tripal_core', 'Cannot pass non array as columns for selecting.', array(), 
+    watchdog('tripal_core', 'Cannot pass non array as columns for selecting.', array(),
       WATCHDOG_ERROR);
-    return FALSE;  
+    return FALSE;
   }
   if (count($columns)==0) {
-    watchdog('tripal_core', 'Cannot pass an empty array as columns for selecting.', array(), 
+    watchdog('tripal_core', 'Cannot pass an empty array as columns for selecting.', array(),
       WATCHDOG_ERROR);
     return FALSE;
   }
-  
+
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -1719,17 +1719,17 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
  *     fields to include.  For example, if expanding a property table (e.g. featureprop)
  *     and you want the CV and accession but do not want the DB the following
  *     array would work:
- *     
+ *
  *        $table_options =  array(
  *          'include_fk' => array(
  *            'type_id' => array(
- *              'cv_id' => 1, 
+ *              'cv_id' => 1,
  *              'dbxref_id' => 1,
  *            )
  *          )
  *        );
- *        
- *     The above array will expand the 'type_id' of the property table but only 
+ *
+ *     The above array will expand the 'type_id' of the property table but only
  *     further expand the cv_id and the dbxref_id and will go no further.
  * @return
  *   Either an object (if only one record was selected from the base table)
@@ -2019,7 +2019,7 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
  *     option of 'order_by' may be used to sort results in the base table
  *     if more than one are returned.  The options must be compatible with
  *     the options accepted by the tripal_core_chado_select() function.
- *   - return_array: 
+ *   - return_array:
  *     Additionally,  The option 'return_array' can be provided to force
  *     the function to expand tables as an array. Default behavior is to expand
  *     a table as single record if only one record exists or to expand as an array if
@@ -2035,13 +2035,13 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
  *        $table_options =  array(
  *          'include_fk' => array(
  *            'type_id' => array(
- *              'cv_id' => 1, 
+ *              'cv_id' => 1,
  *              'dbxref_id' => 1,
  *            )
  *          )
  *        );
- *        
- *     The above array will expand the 'type_id' of the property table but only 
+ *
+ *     The above array will expand the 'type_id' of the property table but only
  *     further expand the cv_id and the dbxref_id and will go no further.
  * @return
  *   A chado object supplemented with the field/table/node requested to be expanded.
@@ -2939,10 +2939,10 @@ function tripal_db_set_default_search_path() {
  */
 function tripal_core_is_sql_prepared($statement_name) {
   global $prepared_statements;
-  
+
   if (!is_array($prepared_statements)) {
     watchdog('tripal_core', "tripal_core_is_sql_prepared: argument must be an array", array(), WATCHDOG_ERROR);
-      return FALSE; 
+      return FALSE;
   }
 
   // check to see if the statement is prepared already
@@ -3706,3 +3706,21 @@ function tripal_core_is_chado_local() {
   }
   return FALSE;
 }
+
+/**
+ * Determine whether a given chado table is directly linked to a node
+ *
+ * @param $chado_table
+ *   The name of a chado table to check (ie: feature)
+ * @return
+ *   TRUE if it is linked to a node and FALSE otherwise
+ */
+function tripal_core_is_tripal_node_type($chado_table) {
+  $linking_table = 'chado_' . $chado_table;
+  if (db_table_exists($linking_table)) {
+    return TRUE;
+  }
+  else {
+    return FALSE;
+  }
+}

+ 1 - 2
tripal_feature/tripal_feature.views.inc

@@ -664,8 +664,7 @@ function tripal_feature_views_default_views() {
       ),
     ));
     // Change analysis.name to have a link to the node
-    $fields['name']['alter']['make_link'] = 1;
-    $fields['name']['alter']['path'] = 'node/[nid]';
+    $fields['name']['alter']['link_to_node'] = 1;
     $default_handler->override_option('fields', $fields);
     // Only show records with published nodes
     /**

+ 1 - 2
tripal_library/tripal_library.views.inc

@@ -480,8 +480,7 @@ function tripal_library_views_default_views() {
       ),
     ));
     // Change analysis.name to have a link to the node
-    $fields['name_1']['alter']['make_link'] = 1;
-    $fields['name_1']['alter']['path'] = 'node/[nid]';
+    $fields['name_1']['alter']['link_to_node'] = 1;
     $default_handler->override_option('fields', $fields);
     // Only show records with published nodes
     /**

+ 1 - 2
tripal_organism/tripal_organism.views.inc

@@ -401,8 +401,7 @@ function tripal_organism_views_default_views() {
       ),
     ));
     // Change analysis.name to have a link to the node
-    $fields['common_name']['alter']['make_link'] = 1;
-    $fields['common_name']['alter']['path'] = 'node/[nid]';
+    $fields['common_name']['alter']['link_to_node'] = 1;
     $default_handler->override_option('fields', $fields);
     // Only show records with published nodes
     /**

+ 1 - 2
tripal_project/tripal_project.views.inc

@@ -323,8 +323,7 @@ function tripal_project_views_default_views() {
       ),
     ));
     // Change project.name to have a link to the node
-    $fields['name']['alter']['make_link'] = 1;
-    $fields['name']['alter']['path'] = 'node/[nid]';
+    $fields['name']['alter']['link_to_node'] = 1;
     $default_handler->override_option('fields', $fields);
     // Only show records with published nodes
     /**

+ 1 - 2
tripal_stock/tripal_stock.views.inc

@@ -625,8 +625,7 @@ function tripal_stock_views_default_views() {
     );
     $fields = $new_fields + $fields;
     // Change analysis.name to have a link to the node
-    $fields['name_2']['alter']['make_link'] = 1;
-    $fields['name_2']['alter']['path'] = 'node/[nid]';
+    $fields['name_2']['alter']['link_to_node'] = 1;
     $default_handler->override_option('fields', $fields);
     // Adds stock => Node relationship
     $default_handler->override_option('relationships', array(

+ 56 - 1
tripal_views/views/handlers/chado_views_handler_field.inc

@@ -33,6 +33,33 @@ class chado_views_handler_field extends views_handler_field {
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
 
+    // Add a link to node checkbox
+    // but only if this base table is linked to a node
+    if (tripal_core_is_tripal_node_type($this->table)) {
+      // If there is a Node: NID field then show a link to node checkbox
+      if (isset($this->view->display['default']->display_options['fields']['nid'])) {
+        $form['link_to_node'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('Link to Node'),
+          '#description' => t('If a given row is associated with a drupal node then '
+            .'this field will appear as a link, linking the user to that node. Otherwise,'
+            .' no link will be displayed.'),
+          '#default_value' => $this->options['link_to_node'],
+        );
+      }
+      // Otherwise inform the user that they need to add a Node:Nid field
+      // to get this functionality
+      else {
+        $form['link_to_node'] = array(
+          '#type' => 'item',
+          '#value' => "This field has the ability to link to it's corresponding node. "
+            . "However, you first need to add the NID field associated with the node. "
+            . "Simple set the NID field to hidden when adding it to ensure it's not "
+            . "shown in the resulting view."
+        );
+      }
+    }
+
     $form['type'] = array(
       '#type' => 'radios',
       '#title' => t('Display type'),
@@ -85,7 +112,35 @@ class chado_views_handler_field extends views_handler_field {
    *   The values retrieved from the database.
    */
   function render($values) {
-    return chado_wrapper_render_items($this, $values);
+    if ($this->options['link_to_node']) {
+      $link_text = chado_wrapper_render_items($this, $values);
+      return $this->render_node_link($link_text, $values);
+    }
+    else {
+      return chado_wrapper_render_items($this, $values);
+    }
+  }
+
+  /**
+   * Will render the supplied text as a link to the node
+   *
+   * @param $link_text
+   *  The text to render as a link (ie; the text that would normally become underlined
+   *
+   * @return
+   *  A rendered link to the node based on the nid field
+   */
+  function render_node_link($link_text, $values) {
+
+    $node_field = $this->view->field['nid'];
+    $nid = $values->{$node_field->aliases['nid']};
+
+    if ($nid) {
+      return l($link_text, 'node/' . $nid);
+    }
+    else {
+      return $link_text;
+    }
   }
 
   function parent_render($val) {