Browse Source

Priority is now in the views integration UI and taken into account during integration allowing you to enter more then one integration definition per table/mview.

Lacey Sanderson 13 years ago
parent
commit
3aa1a4e8e1

+ 68 - 8
base/tripal_views/tripal_views.api.inc

@@ -1,5 +1,41 @@
 <?php
 
+/**
+ * Retrieve the views integration setup with the lightest priority for a given table
+ *
+ * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
+ * and -10 is of highest priority.
+ *
+ * @param $table_name
+ *   The name of the table to retrieve the setup ID for. This can be either a materialized 
+ *   view or a chado table
+ *
+ * @return
+ *   On success, the setup_id to use for integration of this table; otherwise FALSE
+ */
+function tripal_views_get_lightest_priority_setup ($table_name) {
+
+  $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
+  $setup = db_fetch_object(db_query($sql, $table_name));
+  if ($setup) {
+    return $setup->setup_id;  
+  } else {
+    return FALSE;
+  }
+}
+
+/**
+ * Checks if you are dealing with the lightest priority setup for a given table
+ */
+function tripal_views_is_lightest_priority_setup ($setup_id, $table_name) {
+  $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup ($table_name);
+  if ($lightest_priority_setup_id == $setup_id) {
+    return TRUE;
+  } else {
+    return FALSE;
+  }
+}
+
 /**
  * Add views integration records into the tripal_views* tables
  *
@@ -38,10 +74,10 @@
       )
     ),
   );
-  tripal_core_views_integration_add_entry($defn_array);
+  tripal_views_integration_add_entry($defn_array);
  * @endcode
  */
-function tripal_core_views_integration_add_entry($defn_array) {
+function tripal_views_integration_add_entry($defn_array) {
   $no_errors = TRUE;
   
   // First insert into tripal_views
@@ -132,15 +168,39 @@ function tripal_core_views_integration_add_entry($defn_array) {
 }
 
 /**
+ * Removes a View Integration Entry
+ *
+ * @param $table_name
+ *   The name of the table to remove a views integration entry for
+ * @param $priority
+ *   The priority of the of views integration entry
  *
+ * @return
+ *   TRUE on Success; FALSE otherwise
  */
-function tripal_core_views_integration_remove_entry($tablename, $priority) {
+function tripal_views_integration_remove_entry_by_table_name ($table_name, $priority) {
 
-  $views = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s'",$tablename));
+  $views = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",$table_name,$priority));
   if ($views->setup_id) {
-    db_query('DELETE FROM {tripal_views} WHERE setup_id=%d',$views->setup_id);
-    db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d',$views->setup_id);
-    db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d',$views->setup_id);
-    db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d',$views->setup_id);
+    tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
+    return TRUE;  
+  } else {
+    return FALSE;
   }
+  
+}
+
+/**
+ * Removes a View Integration Entry
+ *
+ * @param $setup_id
+ *   The setup ID of the views integration entry to remove
+ */
+function tripal_views_integration_remove_entry_by_setup_id ($setup_id) {
+
+    db_query('DELETE FROM {tripal_views} WHERE setup_id=%d',$setup_id);
+    db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d',$setup_id);
+    db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d',$setup_id);
+    db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d',$setup_id);
+
 }

+ 16 - 1
base/tripal_views/tripal_views.views.inc

@@ -74,6 +74,14 @@ function tripal_views_views_handlers() {
       'parent' => 'views_handler_filter_string',
      ),
      
+     // Join Handlers
+     'views_handler_join_chado_aggregator' => array(
+      'parent' => 'views_join',
+     ),
+     'views_handler_join_chado_through_linking' => array(
+      'parent' => 'views_join',
+     ),
+     
      // Other Custom Handlers
      'views_handler_filter_no_results' => array(
       'parent' => 'views_handler_filter'
@@ -389,7 +397,14 @@ function tripal_views_views_data(){
     $tvi_query = db_query('SELECT * FROM {tripal_views}');
 
     while($tvi_row = db_fetch_object($tvi_query)){
-
+      
+      // check to see if this is the lightest (drupal-style) priority setup for this table
+      // if not then don't use this definition
+      $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
+      if (!$lightest_priority_setup) {
+        continue;
+      }
+      
       // ids we'll use for queries
       $setup_id = $tvi_row->setup_id;
       $mview_id = $tvi_row->mview_id;

+ 31 - 21
base/tripal_views/tripal_views_integration.inc

@@ -59,40 +59,27 @@ function tripal_views_description_page() {
  */
 function tripal_views_integration_setup_list(){
 
-   $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Mview', 'Comment','');
+   $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Mview', 'Priority', 'Comment','');
    $rows = array();
 
    // get the list of materialized views
-   $tviews = db_query('SELECT * FROM {tripal_views}');
+   $tviews = db_query('SELECT * FROM {tripal_views} ORDER BY table_name, priority');
    while($tview = db_fetch_object($tviews)){
-      if($tview->mview_id){
-         // get the materialized view
-         $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
-         $mview = db_fetch_object(db_query($sql,$tview->mview_id));
-         $rows[] = array(
-            l('Edit',"admin/tripal/views/integration/edit/".$tview->setup_id) ,
-            $tview->name, 
-            $mview->mv_table, 
-            'Yes', 
-            $tview->comment,
-            l('Delete',"admin/tripal/views/integration/delete/".$tview->setup_id),
-         );
-      } else {
          $rows[] = array(
             l('Edit',"admin/tripal/views/integration/edit/".$tview->setup_id) ,
             $tview->name, 
             $tview->table_name, 
-            'No', 
+            ($tview->mview_id) ? 'Yes' : 'No', 
+            $tview->priority,
             $tview->comment,
             l('Delete',"admin/tripal/views/integration/delete/".$tview->setup_id),
          );         
-      }
    }
    $rows[] = array(
       'data' => array( 
          array('data' => l('Add a new entry',"admin/tripal/views/integration/new") . " | " .
                          l("Create View",'admin/build/views/add'), 
-               'colspan' => 6),
+               'colspan' => 7),
          )
    );
    return theme('table', $header, $rows);
@@ -103,12 +90,11 @@ function tripal_views_integration_setup_list(){
  * @ingroup tripal_views_integration
  */
 function tripal_views_integration_delete($setup_id){
-  db_query("DELETE FROM {tripal_views} WHERE setup_id = %d",$setup_id);
-  db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = %d",$setup_id);
-  db_query("DELETE FROM {tripal_views_join} WHERE setup_id = %d",$setup_id);
+  tripal_views_integration_remove_entry_by_setup_id ($setup_id);
   drupal_set_message("Record Deleted");
   drupal_goto('admin/tripal/views/integration');
 }
+
 /**
  *
  * @ingroup tripal_views_integration
@@ -230,6 +216,22 @@ function tripal_views_integration_form(&$form_state, $setup_id = NULL){
     $form['row_name']['#attributes'] = array('readonly' => 'readonly');
   }
 
+  $priorities = array();
+  foreach (range(-10,10) as $v) {
+    $priorities[$v] = $v;
+  }
+  $form['views_type']['row_priority'] = array(
+    '#type' => 'select',
+    '#title' => ('Priority'),
+    '#description' => 'The level of priority your Views integration has in relation to the '
+      .'default core and module definitions. The views integration definition with the '
+      .'lightest priority will be used. For example, if there is a definition created by '
+      .'core with a priority of 10 and another by a custom module of 5 and yours is -1 then '
+      .'you definition will be used for that table because -1 is lighter then both 5 and 10.',
+    '#options' => $priorities,
+    '#default_value' => (isset($setup_obj->priority)) ? $setup_obj->priotiy : -1,
+  );
+  
   $form['views_type']['row_description'] = array(
     '#title' => t('Comment'),
     '#type' => 'textarea',
@@ -589,8 +591,11 @@ function tripal_views_integration_form_submit($form, &$form_state){
       // build the record for insert/update
       $tripal_views_record = array(
        'mview_id' => $mview_id,
+       'table_name' => $mview->mv_table,
        'name' => $name,
+       'priority' => $form_state['values']['row_priority'],
        'comment' => $form_state['values']['row_description'],
+       
       );
    } 
    // if a chado table then...
@@ -599,6 +604,7 @@ function tripal_views_integration_form_submit($form, &$form_state){
       $tripal_views_record = array(
        'table_name' => $table_name,
        'name' => $name,
+       'priority' => $form_state['values']['row_priority'],
        'comment' => $form_state['values']['row_description'],
       );
    }
@@ -742,5 +748,9 @@ function tripal_views_integration_discover_handlers() {
    $handlers[] = 'views_handler_sort_date';
    $handlers[] = 'views_handler_sort_menu_hierarchy';
    $handlers[] = 'views_handler_sort_random';
+   
+   // join handler
+   $handlers[] = 'views_join';
+   
    return $handlers;
  }