Browse Source

Relationship API: added autocomplete

Lacey Sanderson 11 years ago
parent
commit
4595c402d4

+ 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);
 }

+ 24 - 16
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(
@@ -534,36 +542,36 @@ 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) {
   global $theme;
 
+
   $cache = cache_get("theme_registry:$theme", 'cache');
-  $node = $build['#node'];
-  
+  $paths = array();
   foreach ($build as $key => $value) {
-    // for tripal templates do some customizations
     if (preg_match('/^tripal_/', $key)) {
-      
-      // add in some default div's, id's and classes
-      // that can be consistent regardless of customizations to templates
-      //$build[$key]['#value'] = "<div id=\"$key-block\" class=\"tripal-info-box\">" . $build[$key]['#value'] . '</div>';
-
-      // get the template path
-      $path = '';
       if (array_key_exists($key, $cache->data)) {
-        $path = $cache->data[$key]['path'];
+        $paths[$key] = $cache->data[$key]['path'];
       }
     }
   }
+  if (count($paths) > 0) {
+    $message = '<p>The content displayed on this page is provided by the following templates: <ul>';
+    foreach ($paths as $template => $path) {
+      $message .= "<li>$key: $path</li>";
+    }
+    $message .= "</ul>";
+    $build['tripal_template_paths']['#value'] = theme(
+      'theme_tripal_admin_message',
+      array('message' => $message)
+    );
+  }
 
-  //dpm($build);
-  //dpm($cache);
 }

+ 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