Browse Source

Added Import/Export functionality for tripal view integrations

Lacey Sanderson 12 years ago
parent
commit
29888caa27

+ 57 - 0
tripal_views/tripal_views.api.inc

@@ -262,6 +262,63 @@ function tripal_views_integration_add_entry($defn_array) {
   return $no_errors;
 }
 
+/**
+ * Export Views integration records
+ *
+ * @param $setup_id
+ *   The unique setup id of the tripal views integration
+ *
+ * @return
+ *   A views integration definition array as used by tripal_views_integration_add_entry()
+ */
+function tripal_views_integration_export_entry($setup_id) {
+
+  // Main setup details
+  $r = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE setup_id=%d", $setup_id));
+  $defn_array = array(
+    'table' => $r->table_name,
+    'name' => $r->name,
+    'type' => ($r->mview_id) ? 'mview' : 'chado',
+    'description' => $r->comment,
+    'priority' => $r->priority,
+    'base_table' => $r->base_table,
+    'fields' => array(),
+  );
+
+  // Add fields
+  $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=%d", $setup_id);
+  while ($r = db_fetch_object($resource)) {
+    $defn_array['fields'][ $r->column_name ] = array(
+        'name' => $r->column_name,
+        'title' => $r->name,
+        'description' => $r->description,
+        'type' => $r->type,
+        'handlers' => array(),
+        'joins' => array()
+    );
+  }
+
+  // Add handlers
+  $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d", $setup_id);
+  while ($r = db_fetch_object($resource)) {
+    $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
+      'name' => $r->handler_name
+    );
+  }
+
+  // Add joins
+  $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=%d", $setup_id);
+  while ($r = db_fetch_object($resource)) {
+    $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ] = array(
+      'table' => $r->left_table,
+      'field' => $r->left_field,
+      'handler' => $r->handler,
+    );
+  }
+
+  return $defn_array;
+}
+
 /**
  * Removes a View Integration Entry
  *

+ 17 - 0
tripal_views/tripal_views.module

@@ -3,6 +3,7 @@
 require_once "tripal_views_integration.inc";
 require_once "tripal_views.views.inc";
 require_once "tripal_views_form_elements.inc";
+require_once "tripal_views_integration_port.inc";
 
 /**
  * Implements hook_menu()
@@ -54,6 +55,22 @@ function tripal_views_menu() {
     'type' => MENU_CALLBACK,
   );
 
+  $items['admin/tripal/views/import'] = array(
+    'title' => 'Import Views Integration',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_views_integration_import_form'),
+    'access arguments' => array('manage tripal_views_integration'), //TODO: figure out the proper permissions arguments
+    'type' => MENU_NORMAL_ITEM,
+  );
+
+  $items['admin/tripal/views/integration/export/%'] = array(
+    'title' => 'Import Views Integration',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_views_integration_export_form', 5),
+    'access arguments' => array('manage tripal_views_integration'), //TODO: figure out the proper permissions arguments
+    'type' => MENU_CALLBACK,
+  );
+
   return $items;
 }
 

+ 12 - 9
tripal_views/tripal_views_integration.inc

@@ -86,7 +86,7 @@ function tripal_views_integration_setup_list() {
 
   // Start with materialized views
   $output .= '<br /><h3>Materialized Views</h3>';
-  $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Legacy?', 'Priority', 'Comment', '');
+  $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Legacy?', 'Priority', 'Comment');
   $rows = array();
 
   // get the list of materialized views
@@ -97,13 +97,14 @@ function tripal_views_integration_setup_list() {
                      .'ORDER BY tv.table_name ASC, tv.priority ASC');
   while ($tview = db_fetch_object($tviews)) {
     $rows[] = array(
-      l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) ,
+      l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
+        . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
+        . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
       $tview->name,
       $tview->table_name,
       ($tview->table_id) ? 'No' : 'Yes',
       $tview->priority,
       $tview->comment,
-      l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
     );
   }
 
@@ -116,7 +117,7 @@ function tripal_views_integration_setup_list() {
 
   // Now list non-mview custom tables
   $output .= '<br /><h3>Custom Tables</h3>';
-  $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment', '');
+  $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
   $rows = array();
 
   // get the list of chado tables
@@ -127,12 +128,13 @@ function tripal_views_integration_setup_list() {
                      .'ORDER BY table_name ASC, priority ASC');
   while ($tview = db_fetch_object($tviews)) {
     $rows[] = array(
-      l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) ,
+      l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
+        . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
+        . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
       $tview->name,
       $tview->table_name,
       $tview->priority,
       $tview->comment,
-      l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
     );
   }
 
@@ -145,7 +147,7 @@ function tripal_views_integration_setup_list() {
 
   // Now list chado tables
   $output .= '<br /><h3>Chado Tables</h3>';
-  $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment', '');
+  $header = array('', 'Drupal Views Type Name', 'Table Name', 'Priority', 'Comment');
   $rows = array();
 
   // get the list of chado tables
@@ -156,12 +158,13 @@ function tripal_views_integration_setup_list() {
                      .'ORDER BY table_name ASC, priority ASC');
   while ($tview = db_fetch_object($tviews)) {
     $rows[] = array(
-      l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id),
+      l(t('Edit'), "admin/tripal/views/integration/edit/" . $tview->setup_id) . "<br />"
+        . l(t('Export'), "admin/tripal/views/integration/export/" . $tview->setup_id) . "<br />"
+        . l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
       $tview->name,
       $tview->table_name,
       $tview->priority,
       $tview->comment,
-      l(t('Delete'), "admin/tripal/views/integration/delete/" . $tview->setup_id),
     );
   }
 

+ 94 - 0
tripal_views/tripal_views_integration_port.inc

@@ -0,0 +1,94 @@
+<?php
+
+/**
+ * @file
+ * This file contains the UI to import/export tripal views integration setups
+ * between sites
+ */
+
+/**
+ * The form to export a particular tripal views integration
+ */
+function tripal_views_integration_export_form($form_state, $setup_id) {
+  $form = array();
+
+  $defn_array = tripal_views_integration_export_entry($setup_id);
+  $form['export'] = array(
+    '#type' => 'textarea',
+    '#title' => 'Export',
+    '#description' => t('Simply copy the provided export into the Import text area on '
+      . 'another tripal views enabled website to port the integration between websites.'),
+    '#rows' => 20,
+    '#value' => serialize($defn_array)
+  );
+
+  return $form;
+}
+
+/**
+ * Imports a tripal views integration
+ */
+function tripal_views_integration_import_form() {
+  $form = array();
+
+  $form['name'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Name (optional)',
+    '#description' => t('A human-readable name for your integration.')
+  );
+
+  $priorities = array();
+  foreach (range(-10, 10) as $v) {
+    $priorities[$v] = (string) $v;
+  }
+  $form['views_type']['row_priority'] = array(
+    '#type' => 'select',
+    '#title' => t('Priority (optional)'),
+    '#description' => t('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' => -1,
+  );
+
+  $form['import'] = array(
+    '#type' => 'textarea',
+    '#title' => 'Import',
+    '#description' => t('Simply copy the provided export into the text area to port the integration between websites.'),
+    '#rows' => 20,
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Submit'
+  );
+
+  return $form;
+}
+
+/**
+ * Imports a tripal views integration
+ */
+function tripal_views_integration_import_form_submit($form, &$form_state) {
+
+  $defn_array = unserialize($form_state['values']['import']);
+
+  // Add optional parameters
+  if ($form_state['values']['name']) {
+    $defn_array['name'] = $form_state['values']['name'];
+  }
+  if ($form_state['values']['row_priority']) {
+    $defn_array['priority'] = $form_state['values']['row_priority'];
+  }
+
+  // Add the views integration
+  $success = tripal_views_integration_add_entry($defn_array);
+  if ($success) {
+    drupal_set_message(t("Successfully imported %name Integration", array('%name' => $defn_array['name'])));
+  }
+  else {
+    drupal_set_message(t("Unable to import %name Integration", array('%name' => $defn_array['name'])), 'error');
+  }
+}