Browse Source

Merge branch '7.x-2.x' of git.drupal.org:sandbox/spficklin/1337878 into 7.x-2.x

Stephen Ficklin 11 years ago
parent
commit
8c1bad722e

+ 53 - 6
tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -356,6 +356,7 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
 
   $form['relationships']['relationship_table']['new']['object_name'] = array(
     '#type' => 'textfield',
+    '#autocomplete_path' => 'tripal_ajax/relationship_nodeform/'.$details['base_table'].'/'.$details['base_name_field'].'/name_to_id'
   );
 
   $form['relationships']['relationship_table']['new']['object_is_current'] = array(
@@ -370,6 +371,7 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
 
   $form['relationships']['relationship_table']['new']['subject_name'] = array(
     '#type' => 'textfield',
+    '#autocomplete_path' => 'tripal_ajax/relationship_nodeform/'.$details['base_table'].'/'.$details['base_name_field'].'/name_to_id'
   );
 
   $form['relationships']['relationship_table']['new']['subject_is_current'] = array(
@@ -422,6 +424,25 @@ function chado_node_relationships_form_add_button_validate($form, &$form_state)
 
   $details = unserialize($form_state['values']['relationship_table']['details']);
 
+  // First deal with autocomplete fields
+  // extract the base_id assuming '(###) NAME FIELD'
+  if (!empty($form_state['values']['relationship_table']['new']['subject_name'])) {
+    if (preg_match('/\((\d+)\) .*/', $form_state['values']['relationship_table']['new']['subject_name'], $matches)) {
+      $form_state['values']['relationship_table']['new']['subject_id'] = $matches[1];
+    }
+    else {
+      form_set_error('subject_name', 'You need to select the subject from the autocomplete drop-down');
+    }
+  }
+  if (!empty($form_state['values']['relationship_table']['new']['object_name'])) {
+    if (preg_match('/\((\d+)\) .*/', $form_state['values']['relationship_table']['new']['object_name'], $matches)) {
+      $form_state['values']['relationship_table']['new']['object_id'] = $matches[1];
+    }
+    else {
+      form_set_error('object_name', 'You need to select the subject from the autocomplete drop-down');
+    }
+  }
+
   // At least one of the participants must be the current node
   if (!($form_state['values']['relationship_table']['new']['subject_is_current'] OR $form_state['values']['relationship_table']['new']['object_is_current'])) {
     // If the checkbox isn't set then check to see if either has the same uniquename as the node
@@ -448,15 +469,15 @@ function chado_node_relationships_form_add_button_validate($form, &$form_state)
   if (!($form_state['values']['relationship_table']['new']['subject_is_current'])) {
     $result = tripal_core_chado_select(
       $details['base_table'],
-      array($details['base_foreign_key']),
-      array($details['base_name_field'] => $form_state['values']['relationship_table']['new']['subject_name'])
+      array($details['base_name_field']),
+      array($details['base_foreign_key'] => $form_state['values']['relationship_table']['new']['subject_id'])
     );
     if (!isset($result[0])) {
       form_set_error('subject_name', 'The subject must be the unique name of an
         existing '.$details['nodetype'].' unless the "Current '.$details['nodetype'].'" checkbox is selected');
     }
     else {
-      $form_state['values']['relationship_table']['new']['subject_id'] = $result[0]->{$details['base_foreign_key']};
+      $form_state['values']['relationship_table']['new']['subject_name'] = $result[0]->{$details['base_name_field']};
     }
   }
 
@@ -464,15 +485,15 @@ function chado_node_relationships_form_add_button_validate($form, &$form_state)
   if (!($form_state['values']['relationship_table']['new']['object_is_current'])) {
     $result = tripal_core_chado_select(
       $details['base_table'],
-      array($details['base_foreign_key']),
-      array($details['base_name_field'] => $form_state['values']['relationship_table']['new']['object_name'])
+      array($details['base_name_field']),
+      array($details['base_foreign_key'] => $form_state['values']['relationship_table']['new']['object_id'])
     );
     if (!isset($result[0])) {
       form_set_error('object_name', 'The object must be the unique name of an
         existing '.$details['nodetype'].' unless the "Current '.$details['nodetype'].'" checkbox is selected');
     }
     else {
-      $form_state['values']['relationship_table']['new']['object_id'] = $result[0]->{$details['base_foreign_key']};
+      $form_state['values']['relationship_table']['new']['object_name'] = $result[0]->{$details['base_name_field']};
     }
   }
 
@@ -810,4 +831,30 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
       }
     }
   }
+}
+
+/**
+ * Handles autocomplete for subject & object id
+ *
+ * @param $string
+ *    The part of the string already typed in the textfield
+ */
+function chado_node_relationships_name_to_id_callback($base_table, $name_field, $string) {
+  $matches = array();
+
+  $base_key = $base_table.'_id';
+
+  $result = db_select('chado.'.$base_table, 'b')
+    ->fields('b', array($base_key, $name_field))
+    ->condition($name_field, '%' . db_like($string) . '%', 'LIKE')
+    ->execute();
+
+  // save the query to matches
+  foreach ($result as $row) {
+    $key = '('.$row->{$base_key}.') '.substr($row->{$name_field},0,50) . '...';
+    $matches[$key] = check_plain($row->{$name_field});
+  }
+
+  // return for JS
+  drupal_json_output($matches);
 }

+ 19 - 11
tripal_core/tripal_core.module

@@ -383,6 +383,14 @@ function tripal_core_menu() {
     'type' => MENU_CALLBACK,
   );
 
+  // Relationshi API autocomplete callback
+  $items['tripal_ajax/relationship_nodeform/%/%/name_to_id'] = array(
+    'page callback' => 'chado_node_relationships_name_to_id_callback',
+    'page arguments' => array(2,3),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK
+  );
+
   return $items;
 }
 
@@ -453,7 +461,7 @@ function tripal_core_theme($existing, $type, $theme, $path) {
       'function' => 'theme_chado_node_relationships_form_table',
       'render element' => 'element',
     ),
-    
+
     // Admin messages theme
     // --------------------------------
     'tripal_admin_message' => array(
@@ -539,7 +547,7 @@ function tripal_core_views_api() {
  * After the node is built, we want to add instructions to each
  * content section letting the administrator know which template
  * they can customize
- * 
+ *
  * @param unknown $build
  */
 function tripal_core_node_view_alter(&$build) {
@@ -547,23 +555,23 @@ function tripal_core_node_view_alter(&$build) {
 
   $cache = cache_get("theme_registry:$theme", 'cache');
   $node = $build['#node'];
-  
+
   // if we are looking at a Tripal node template then we want to
   // make some changes to each block of content so that we can associate
   // a table of contents and add administrator and curator messages
   if (preg_match('/chado_/', $node->type)) {
-  
+
     // iterate through all the elements of the $build array and for those
     // with a '#value' add some extra <div> tags
     foreach ($build as $key => $value) {
-      
+
       // only examine elements without a '#' prefix
       if (!preg_match('/^#/', $key)) {
 
         // get the template path
         $path = '';
         if (array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
-          
+
           $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
           $path = theme('tripal_admin_message', array('message' => "Administrators, you can
             customize the way the content above is presented.  Tripal provides a template
@@ -573,15 +581,15 @@ function tripal_core_node_view_alter(&$build) {
             <br><br>$path
           "));
         }
-        
-        // if this element has a #value child then add our div box        
+
+        // if this element has a #value child then add our div box
         if (array_key_exists('#value', $build[$key])) {
           $build[$key]['#value'] = "
-            <div id=\"$key-tripal-data-block\" class=\"tripal-data-block\">" . 
-              $build[$key]['#value'] . 
+            <div id=\"$key-tripal-data-block\" class=\"tripal-data-block\">" .
+              $build[$key]['#value'] .
               $path .
             "</div>";
-  
+
 
         }
       }

+ 1 - 1
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -275,7 +275,7 @@ function chado_pub_form($node, $form_state) {
     'base_key_value' => $pub_id,                // the value of example_id for this record
     'nodetype' => 'pub',                        // the human-readable name of your node type
     'cv_name' => 'pub_relationship_types',      // the cv.name of the cv governing example_relationship.type_id
-    'base_name_field' => 'title',               // the base table field you want to be used as the name
+    'base_name_field' => 'uniquename',          // the base table field you want to be used as the name
     'select_options' => $select_options
   );
   // Adds the form elements to your current form