Browse Source

Added organism autocomplete in API

Stephen Ficklin 10 years ago
parent
commit
1e9614f029

+ 1 - 1
tripal_feature/includes/tripal_feature.admin.inc

@@ -56,7 +56,7 @@ function tripal_feature_admin() {
     'options' => array(
       '[feature.name]' => 'Feature Name Only',
       '[feature.uniquename]' => 'Feature Unique Name Only',
-        // there should always be one options matching the unique constraint.
+      // there should always be one options matching the unique constraint.
       '[feature.name], [feature.uniquename] ([feature.type_id>cvterm.name]) [feature.organism_id>organism.genus] [feature.organism_id>organism.species]' => 'Unique Contraint: Includes the name, uniquename, type and scientific name'
     ),
     // the token indicating the unique constraint in the options array

+ 38 - 1
tripal_organism/api/tripal_organism.api.inc

@@ -182,7 +182,7 @@ function tripal_get_organism_select_options($syncd_only = TRUE) {
  */
 function tripal_get_organism_image_url($organism) {
   $url = '';
-  
+
   if (!is_object($organism)) {
     return NULL;
   }
@@ -230,3 +230,40 @@ function tripal_get_organism_image_url($organism) {
   return NULL;
 }
 
+/**
+ * This function is intended to be used in autocomplete forms
+ * for searching for organisms that begin with the provided string
+ *
+ * @param $text
+ *   The string to search for
+ *
+ * @return
+ *   A json array of terms that begin with the provided string
+ *
+ * @ingroup tripal_organism_api
+ */
+function tripal_autocomplete_organism($text) {
+  $matches = array();
+  $genus = $text;
+  $species = '';
+  if (preg_match('/^(.*?) (.*)$/', $text, $matches)) {
+    $genus = $matches[1];
+    $species = $matches[2];
+  }
+  $sql = "SELECT * FROM {organism} WHERE lower(genus) like lower(:genus) ";
+  $args = array();
+  $args[':genus'] = $genus . '%';
+  if ($species) {
+    $sql .= "AND lower(species) like lower(:species) ";
+    $args[':species'] =  $species . '%';
+  }
+  $sql .= "ORDER BY genus, species ";
+  $sql .= "LIMIT 25 OFFSET 0 ";
+  $results = chado_query($sql, $args);
+  $items = array();
+  foreach ($results as $organism) {
+    $name = $organism->genus . ' ' .$organism->species;
+    $items[$name] = $name;
+  }
+  drupal_json_output($items);
+}

+ 6 - 0
tripal_organism/tripal_organism.module

@@ -96,6 +96,12 @@ function tripal_organism_menu() {
     'access arguments' => array('administer tripal organism'),
     'type' => MENU_CALLBACK,
   );
+  $items['admin/tripal/chado/tripal_organism/organism/auto_name/%'] = array(
+    'page callback' => 'tripal_autocomplete_organism',
+    'page arguments' => array(6),
+    'access arguments' => array('administer tripal phylotree'),
+    'type' => MENU_CALLBACK,
+  );
 
 
   return $items;