Browse Source

Added autocomplete for accession

Chun-Huai Cheng 9 years ago
parent
commit
ee73f4f5bc
2 changed files with 57 additions and 1 deletions
  1. 41 0
      tripal_db/tripal_db.module
  2. 16 1
      tripal_entities/includes/tripal_entities.fields.inc

+ 41 - 0
tripal_db/tripal_db.module

@@ -81,6 +81,12 @@ function tripal_db_menu() {
     'access arguments' => array('administer db cross-references'),
     'type' => MENU_CALLBACK,
   );
+  $items['admin/tripal/chado/tripal_db/dbxref/auto_name/%/%'] = array(
+    'page callback' => 'tripal_db_dbxref_accession_autocomplete',
+    'page arguments' => array(6, 7),
+    'access arguments' => array('administer db cross-references'),
+    'type' => MENU_CALLBACK,
+  );
 
   return $items;
 }
@@ -147,3 +153,38 @@ function tripal_db_theme($existing, $type, $theme, $path) {
   );
   return $items;
 }
+
+
+/**
+ * This function is intended to be used in autocomplete forms
+ * for searching for accession that begin with the provided string
+ *
+ * @param $db_id
+ * The DB ID in which to search for the term
+ * @param $string
+ * The string to search for
+ *
+ * @return
+ * A json array of terms that begin with the provided string
+ *
+ * @ingroup tripal_db_api
+ */
+function tripal_db_dbxref_accession_autocomplete ($db_id, $string = '') {
+  if (!$db_id) {
+    return drupal_json_output(array());
+  }
+  $sql = "
+    SELECT dbxref_id, accession
+    FROM {dbxref}
+    WHERE db_id = :db_id and lower(accession) like lower(:accession)
+    ORDER by accession
+    LIMIT 25 OFFSET 0
+  ";
+  $results = chado_query($sql, array(':db_id' => $db_id, ':accession' => $string . '%'));
+  $items = array();
+  foreach ($results as $ref) {
+    $items[$ref->accession] = $ref->accession;
+  }
+  
+  drupal_json_output($items);
+}

+ 16 - 1
tripal_entities/includes/tripal_entities.fields.inc

@@ -217,7 +217,7 @@ function tripal_entities_field_widget_form(&$form, &$form_state, $field,
         $version = $dbxref->version;
         $description = $dbxref->description;
       }
-
+      
       $schema = chado_get_schema('dbxref');
       $options = tripal_get_db_select_options();
       $widget += array(
@@ -239,6 +239,12 @@ function tripal_entities_field_widget_form(&$form, &$form_state, $field,
             '#options' => $options,
             '#required' => $element['#required'],
             '#default_value' => $db_id,
+            '#ajax' => array(
+              'callback' => "tripal_entities_field_widget_form_ajax_callback",
+              'wrapper' => 'tripal_entities_field_widget_form',
+              'effect' => 'fade',
+              'method' => 'replace'
+            )
           ),
           'dbxref__accession' => array(
             '#type' => 'textfield',
@@ -247,6 +253,7 @@ function tripal_entities_field_widget_form(&$form, &$form_state, $field,
             '#required' => $element['#required'],
             '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
             '#size' => 15,
+            '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
           ),
           'dbxref__version' => array(
             '#type' => 'textfield',
@@ -455,4 +462,12 @@ function tripal_entities_set_field_form_values($field_name, &$form_state, $newva
     }
   }
   return $values;
+}
+
+/**
+ * An Ajax callback for the tripal_entities_admin_publish_form..
+ */
+function tripal_entities_field_widget_form_ajax_callback($form, $form_state) {
+  // return the form so Drupal can update the content on the page
+  return $form;
 }