--- sites/all/modules/webform_mysql_views/webform_mysql_views.module.old	2011-01-14 10:10:32.000000000 -0500
+++ sites/all/modules/webform_mysql_views/webform_mysql_views.module	2011-01-14 10:10:55.000000000 -0500
@@ -1,4 +1,4 @@
-<?php // $Id: webform_mysql_views.module,v 1.1.2.3 2011/01/14 15:10:32 usonian Exp $
+<?php // $Id: webform_mysql_views.module,v 1.1.2.2 2011/01/11 20:35:58 usonian Exp $
 
 /**
  * @file
@@ -237,7 +237,7 @@ function webform_mysql_views_admin_form(
 function webform_mysql_views_admin_form_submit($form, &$form_state) {
 
   //Load existing view info
-  $webform_views = variable_get('webform_mysql_views_views', array());
+  $webform_views = $previous_webform_views = variable_get('webform_mysql_views_views', array());
   $webform_view_nids = array_keys($webform_views);
 
   //Parse submitted settings;
@@ -262,7 +262,7 @@ function webform_mysql_views_admin_form_
       'Dropped @count views.',
       array('@count' => count($drop_nids))));
   }
-  
+
   //If there are any nids in the new array not in the saved one, create new views
   $new_nids = array_diff($submitted_nids, $webform_view_nids);
 
@@ -282,8 +282,38 @@ function webform_mysql_views_admin_form_
   //Save the webform_views variable
   variable_set('webform_mysql_views_views', $webform_views);
 
+  //Data module integration
+  if ( module_exists('data') ) {
+    data_include('DataTable');
+    // adopt new mysql views
+    foreach ($new_nids as $nid) {
+      $webform_mysql_view = $webform_views[$nid];
+      $table = DataTable::instance($webform_mysql_view);
+      if ($table->adopt()) {
+        drupal_set_message( t('Data module has adopted view @webform_mysql_view.', array('@webform_mysql_view'=>$webform_mysql_view)) );
+      }
+      else {
+        drupal_set_message( t('Data module was unable to adopt view @webform_mysql_view.', array('@webform_mysql_view'=>$webform_mysql_view)), 'error');
+      }
+      unset($table);
+    }
+    // drop old mysql views from data.module
+    foreach ($drop_nids as $nid) {
+      $webform_mysql_view = $previous_webform_views[$nid];
+      $table = DataTable::instance($webform_mysql_view);
+      if ($table->drop()) {
+        drupal_set_message( t('Data module has dropped view @webform_mysql_view.', array('@webform_mysql_view'=>$webform_mysql_view)) );
+      }
+      else {
+        drupal_set_message( t('Data module was unable to drop view @webform_mysql_view.', array('@webform_mysql_view'=>$webform_mysql_view)), 'error' );
       }
-      
+
+      unset($table);
+    }
+    DataTable::clearCaches();
+  }
+}
+
 /**
  * Given a webform node ID, build an SQL query that will create a flat MySQL view
  * of subnissions for that webform
@@ -304,7 +334,7 @@ function webform_mysql_views_build_query
   $result = db_query('SELECT CONCAT("(SELECT GROUP_CONCAT(data) FROM {webform_submitted_data} AS child WHERE child.sid = parent.sid AND cid = ", c.cid,") AS ", c.form_key, ", ") AS component FROM {webform_component} c WHERE c.nid = %d AND c.type != "fieldset" ORDER BY c.weight ASC, c.cid ASC', $nid);
 
   $components = '';
-  
+
   while ($row = db_fetch_array($result)) {
     $components .= $row['component'];
   }
@@ -328,7 +358,7 @@ function webform_mysql_views_build_query
 function webform_mysql_views_get_view_name($title, $nid) {
 
   global $db_prefix;
-  
+
   //Discard non-alphanumeric chars
   $title = strtolower(str_replace(' ', '_', $title));
   $title = 'webform_views_'. preg_replace('/[^a-z0-9_]/', '', $title);
@@ -381,3 +411,49 @@ function _webform_mysql_views_check_requ
   }
 
 }
+
+/**
+ * Designate a column of a mysql view as primary key.
+ * This is required to expose a view via views (and data.module).
+ *
+ * @param string $dbview_name 	name of the database view
+ * @param string $pseudo_pk			name of the column acting as primary key
+ * @param array $views_data 		array passed into hook_views_data_alter
+ * @return bool	false on error
+ */
+function _webform_mysql_views_designate_primarykey($dbview_name, $pseudo_pk, &$views_data) {
+  $tables = data_get_all_tables();
+  // Sanity checking
+  if (!isset($tables[$dbview_name]) || !isset($views_data[$dbview_name])) {
+    return false;
+  }
+  if (!isset($views_data[$dbview_name][$pseudo_pk])) {
+    return false;
+  }
+  $table = $tables[$dbview_name];
+  // Expose the database view as a base table for views.
+  $views_data[$dbview_name]['table']['base'] = array(
+    'field' => $pseudo_pk,
+    'title' => $table->get('title'),
+    'help' => t('Data table for webform_mysql_view @dbview_name', array('@dbview_name'=>$dbview_name)),
+    'weight' => 10,
+  );
+  // In order to avoid schema.module getting confused, we do not actually set the primary key.
+  // data_ui_views_default_views() will though not pick be able up on the view.
+  // You will have to create your own "default" drupal view.
+  return true;
+}
+
+/**
+ * Implementation of hook_views_data_alter().
+ */
+function webform_mysql_views_views_data_alter(&$views) {
+  if (module_exists('data')) {
+    // expose the webform_mysql_views by designating a "pseudo"-primary key
+    $webform_mysql_views = variable_get('webform_mysql_views_views', array());
+    foreach ( $webform_mysql_views as $nid => $dbview_name ) {
+      // sid (submission id) is unique
+      _webform_mysql_views_designate_primarykey($dbview_name, 'sid', $views);
+    }
+  }
+}
