Browse Source

Added home page for the controlled vocabulary lookup

Stephen Ficklin 7 years ago
parent
commit
75bea06943

+ 32 - 0
tripal/api/tripal.terms.api.inc

@@ -222,6 +222,18 @@ function hook_vocab_get_root_terms($vocabulary) {
 function hook_vocab_get_vocabulary($vocabulary) {
   // See the tripal_chado_vocab_get_vocabulary() function for an example.
 }
+
+/**
+ * Retrieves the list of vocabularies that are available on the site.
+ *
+ * @return
+ *   An array of vocabularies where each entry in the array is compatible
+ *   with the array returned by the tripal_get_vocabulary_details()
+ *   function.
+ */
+function hook_vocab_get_vocabularies() {
+  // See the tripal_chado_vocab_get_vocabularies() function for an example.
+}
 /**
  * Hook used by the default term storage backend to add new terms.
  *
@@ -439,6 +451,26 @@ function tripal_get_vocabulary_details($vocabulary) {
   }
 }
 
+/**
+ * Retrieves the list of vocabularies that are available on the site.
+ *
+ * @return
+ *   An array of vocabularies where each entry in the array is compatible
+ *   with the array returned by the tripal_get_vocabulary_details()
+ *   function.
+ */
+function tripal_get_vocabularies() {
+  $stores = module_invoke_all('vocab_storage_info');
+  if (is_array($stores) and count($stores) > 0) {
+    $keys = array_keys($stores);
+    $module = $stores[$keys[0]]['module'];
+    $function = $module . '_vocab_get_vocabularies';
+    if (function_exists($function)) {
+      return $function();
+    }
+  }
+}
+
 
 /**
  * Provides a term lookup form.

+ 44 - 19
tripal/includes/tripal.term_lookup.inc

@@ -1,31 +1,52 @@
 <?php
 
 /**
- *
- * @param $form
- * @param string $form_state
+ *  Provides the content for the Controlled vocabulary home page.
  */
-function tripal_vocabulary_lookup_form($form, &$form_state = NULL) {
+function tripal_vocabulary_lookup_page() {
+  // set the breadcrumb
+  $breadcrumb = array();
+  $breadcrumb[] = l('Home', '<front>');
+  drupal_set_breadcrumb($breadcrumb);
+  $vocabs = tripal_get_vocabularies();
 
-}
-/**
- *
- * @param $form
- * @param $form_state
- */
-function tripal_vocabulary_lookup_form_validate($form, $form_state) {
+  $rows = array();
+  foreach ($vocabs as $vocabulary) {
+    $rows[] = array(
+      l($vocabulary['short_name'], 'cv/lookup/' . $vocabulary['short_name']),
+      $vocabulary['name'],
+      $vocabulary['description'],
+    );
+  }
 
-}
-/**
- *
- * @param $form
- * @param $form_state
- */
-function tripal_vocabulary_lookup_form_submit($form, $form_state) {
+  $headers = array('Short Name', 'Vocabulary Name(s)', 'Description');
+  $table = array(
+    'header' => $headers,
+    'rows' => $rows,
+    'attributes' => array(),
+    'sticky' => FALSE,
+    'caption' => '',
+    'colgroups' => array(),
+    'empty' => '',
+  );
 
+  $content = array(
+    'description' => array(
+      '#type' => 'markup',
+      '#markup' => '<p>The following controlled vocabularies are used on this site. Click a vocabulary short name for more details.</p>',
+    ),
+    'vocab_table' => array(
+      '#type' => 'item',
+      '#markup' => theme_table($table),
+    ),
+  );
+  return $content;
 }
 
-function tripal_vocabulary_lookup_page($vocabulary) {
+/**
+ * Provides the content for a single controlled vocabulary.
+ */
+function tripal_vocabulary_lookup_vocab_page($vocabulary) {
 
   // set the breadcrumb
   $breadcrumb = array();
@@ -94,6 +115,10 @@ function tripal_vocabulary_lookup_page($vocabulary) {
   $root_terms = tripal_get_vocabulary_root_terms($vocabulary);
   $items = tripal_vocabulary_lookup_term_children_format($root_terms);
 
+  if (count($root_terms) == 0) {
+    $items = '<p>Sometimes a vocabulary may not be fully loaded, but individual terms are. In this case the term browser is not available.</p>';
+  }
+
   drupal_add_js(array(
     'tripal' => array(
       'cv_lookup' => array(

+ 10 - 12
tripal/tripal.module

@@ -274,23 +274,21 @@ function tripal_menu() {
   /*
    * Term Lookup
    */
-// TODO: finish this menu callback.
-//   $items['cv/lookup'] = array(
-//     'title' => 'Vocabulary Lookup',
-//     'description' => t("Provides a tool to discover controlled vocabularies and their terms used by this site."),
-//     'access arguments' => array('access content'),
-//     'page callback' => 'drupal_get_form',
-//     'page arguments' => array('tripal_vocabulary_lookup_form'),
-//     'file' => 'includes/tripal.term_lookup.inc',
-//     'file path' => drupal_get_path('module', 'tripal'),
-//     'type' => MENU_NORMAL_ITEM,
-//   );
+   $items['cv/lookup'] = array(
+     'title' => 'Controlled Vocabularies',
+     'description' => t("A tool to explore the controlled vocabularies that are used on this site."),
+     'access arguments' => array('access content'),
+     'page callback' => 'tripal_vocabulary_lookup_page',
+     'file' => 'includes/tripal.term_lookup.inc',
+     'file path' => drupal_get_path('module', 'tripal'),
+     'type' => MENU_NORMAL_ITEM,
+   );
 
   $items['cv/lookup/%'] = array(
     'title' => 'Vocabulary Details',
     'description' => t("Provides a tool to discover controlled vocabularies"),
     'access arguments' => array('access content'),
-    'page callback' => 'tripal_vocabulary_lookup_page',
+    'page callback' => 'tripal_vocabulary_lookup_vocab_page',
     'page arguments' => array(2),
     'file' => 'includes/tripal.term_lookup.inc',
     'file path' => drupal_get_path('module', 'tripal'),

+ 59 - 20
tripal_chado/includes/tripal_chado.vocab_storage.inc

@@ -15,6 +15,54 @@ function tripal_chado_vocab_storage_info() {
     ),
   );
 }
+
+/**
+ * Implements hook_vocab_get_vocabularies().
+ *
+ * This hook is created by the Tripal module and is not a Drupal hook.
+ */
+function tripal_chado_vocab_get_vocabularies() {
+  $vocabs = array();
+
+  // It's possible that Chado is not available (i.e. it gets renamed
+  // for copying) but Tripal has already been prepared and the
+  // entities exist.  If this is the case we don't want to run the
+  // commands below.
+  if (!chado_table_exists('cv')) {
+    return FALSE;
+  }
+
+  // Make sure the materiailzd view is present.
+  if (!chado_table_exists('db2cv_mview')) {
+    drupal_set_message('Please update the database using "drush updatedb" before continuing');
+    return FALSE;
+  }
+
+  $sql = "
+     SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
+       array_to_string(array_agg(DBCVM.cvname), ', ') as name
+     FROM {db} DB
+      INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
+     GROUP BY DB.name, DB.description, DB.url, DB.urlprefix
+     ORDER BY DB.name
+  ";
+
+  $results = chado_query($sql, array());
+  while ($result = $results->fetchAssoc()) {
+    if (!$result['name']) {
+      $result['name'] = $result['short_name'];
+    }
+    $sw_url = $result['urlprefix'];
+    if ($sw_url) {
+      $sw_url = preg_replace('/\{db\}/', $result['short_name'], $sw_url);
+      $sw_url = preg_replace('/\{accession\}/', '', $sw_url);
+      $sw_url = url($sw_url, array('absolute' => TRUE));
+    }
+    $result['sw_url'] = $sw_url;
+    $vocabs[] = $result;
+  }
+  return $vocabs;
+}
 /**
  * Implements hook_vocab_get_vocabulary().
  *
@@ -29,29 +77,20 @@ function tripal_chado_vocab_get_vocabulary($vocabulary) {
     return FALSE;
   }
 
-  if (chado_table_exists('db2cv_mview')) {
-    $sql = "
-       SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
-         array_to_string(array_agg(DBCVM.cvname), ', ') as name
-       FROM {db} DB
-        INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
-       WHERE DB.name = :name
-       GROUP BY DB.name, DB.description, DB.url, DB.urlprefix
-    ";
+  // Make sure the materiailzd view is present.
+  if (!chado_table_exists('db2cv_mview')) {
+    drupal_set_message('Please update the database using "drush updatedb" before continuing');
+    return FALSE;
   }
-  // For backwards compatibility.  Tripal sites above 3.0-beta2 don't need this.
-  else {
-    $sql = "
-     SELECT DB.name as short_name, CV.name as name, DB.description, DB.url, DB.urlprefix
+
+  $sql = "
+     SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
+       array_to_string(array_agg(DBCVM.cvname), ', ') as name
      FROM {db} DB
-      LEFT JOIN {dbxref} DBX on DBX.db_id = DB.db_id
-      LEFT JOIN {cvterm} CVT on CVT.dbxref_id = DBX.dbxref_id
-      LEFT JOIN {cv} CV on CV.cv_id = CVT.cv_id
-     WHERE
-      DB.name = :name
-     LIMIT 1 OFFSET 0
+      INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
+     WHERE DB.name = :name
+     GROUP BY DB.name, DB.description, DB.url, DB.urlprefix
   ";
-  }
   $result = chado_query($sql, array(':name' => $vocabulary));
   $result = $result->fetchAssoc();