Browse Source

imperfect solution for organism building list

bradfordcondon 6 years ago
parent
commit
f754f19672

+ 31 - 7
tripal_chado/api/modules/tripal_chado.organism.api.inc

@@ -189,7 +189,8 @@ function chado_get_organism_select_options($syncd_only = TRUE) {
   $org_list = array();
   $org_list[] = 'Select an organism';
 
-  if ($syncd_only) {
+  // Tripal 2.
+  if ($syncd_only && chado_table_exists('chado_organism')) {
     $sql = "
       SELECT *
       FROM [chado_organism] CO
@@ -202,16 +203,39 @@ function chado_get_organism_select_options($syncd_only = TRUE) {
     foreach ($orgs as $org) {
       $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
     }
+    return $org_list;
   }
-  else {
-    // use this SQL statement for getting the organisms
-    $csql =  "SELECT * FROM {organism} ORDER BY genus, species";
-    $orgs = chado_query($csql);
 
-    // Iterate through the organisms and build an array of those that are synced.
-    foreach ($orgs as $org) {
+  if ($syncd_only) {
+    $bundle_tables = chado_get_bundles_by_base_table('organism');
+
+    if (empty($bundle_tables)) {
+      return $org_list;
+    }
+    $all_orgs = [];
+
+    $query = db_select('chado.organism', 'o')
+      ->fields('o', ['genus', 'species', 'organism_id']);
+
+    foreach ($bundle_tables as $table) {
+      $query_copy = $query;
+      $query_copy->join('public.' . $table, $table, $table . '.record_id = o.organism_id');
+      $orgs = $query_copy->execute()->fetchAll();
+      array_merge($all_orgs, $orgs);
+    }
+    foreach ($all_orgs as $org) {
       $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
     }
+    return $org_list;
+  }
+  // Case: ignore published status.
+  // Use this SQL statement for getting the organisms.
+  $csql = "SELECT * FROM {organism} ORDER BY genus, species";
+  $orgs = chado_query($csql);
+
+  // Iterate through the organisms and build an array of those that are synced.
+  foreach ($orgs as $org) {
+    $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
   }
   return $org_list;
 }

+ 22 - 0
tripal_chado/api/tripal_chado.entity.api.inc

@@ -120,4 +120,26 @@ function chado_get_bundle_entity_table($bundle) {
     return '';
   }
   return 'chado_' . $bundle->name;
+}
+
+/**
+ * Fetch all chado bundles that use a specific data table.
+ *
+ * @param $chado_table
+ *   The chado table name, ie, organism, contact.
+ *
+ * @return array
+ *   An array of bundles using that base table in the form - chado_bio_data_n.
+ */
+function chado_get_bundles_by_base_table($chado_table) {
+  $return = [];
+  $query = db_select('public.chado_bundle', 'cb');
+  $query->condition('cb.data_table', $chado_table);
+  $query->fields('cb', ['bundle_id']);
+  $bundle_tables = $query->execute()->fetchAll();
+
+  foreach ($bundle_tables as $table){
+    $return[] = 'chado_bui_data_' . $table->bundle_id;
+  }
+  return $return;
 }