From 140cdcd2b2d93aacf7183b75ad128922e33e20b2 Mon Sep 17 00:00:00 2001
From: Peter Lieverdink <me@cafuego.net>
Date: Tue, 17 May 2011 10:02:40 +1000
Subject: [PATCH] Handle module requirements at install time via hook_requirements and cope with $db_url being an array.

---
 webform_mysql_views.install |   46 ++++++++++++++++++++++++++++++++++++++++++-
 webform_mysql_views.module  |   46 -------------------------------------------
 2 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/webform_mysql_views.install b/webform_mysql_views.install
index 73ad938..e2d6e55 100644
--- a/webform_mysql_views.install
+++ b/webform_mysql_views.install
@@ -5,6 +5,9 @@
  * Webform MySQL Views module install/schema hooks.
  */
 
+// Define the minimum required MySQL version needed by this module.
+define('WEBFORM_MYSQL_VIEWS_MINIMUM_MYSQL', '5.0');
+
 /**
  * Implementation of hook_uninstall().
  */
@@ -18,4 +21,45 @@ function webform_mysql_views_uninstall() {
   //Delete module variables
   variable_del('webform_mysql_views_meets_reqs');
   variable_del('webform_mysql_views_views');
-}
\ No newline at end of file
+}
+
+/**
+ * Implementation of hook_requirements().
+ *
+ * Check Drupal environment for module requirements
+ */
+function webform_mysql_views_requirements($phase) {
+  global $db_url;
+
+  $t = get_t();
+  $requirements = array();
+
+  // Cope with a site that uses multiple db_urls.
+  if (!is_array($db_url)) {
+    $_dburl = $db_url;
+  }
+  else {
+    $_dburl = $db_url['default'];
+  }
+
+  $requirements['webform_mysql_views'] = array(
+    'title' => $t('Webform MySQL Views'),
+  );
+
+  if (substr($_dburl, 0, 5) != 'mysql') {
+    $requirements['webform_mysql_views']['severity'] = REQUIREMENT_ERROR;
+    $requirements['webform_mysql_views']['value'] = $t('Unsupported.');
+    $requirements['webform_mysql_views']['description'] = $t('This module is only compatible with the MySQL backend.');
+  }
+  else if (version_compare(db_version(), WEBFORM_MYSQL_VIEWS_MINIMUM_MYSQL) < 0) {
+    $requirements['webform_mysql_views']['severity'] = REQUIREMENT_ERROR;
+    $requirements['webform_mysql_views']['value'] = $t('Unsupported.');
+    $requirements['webform_mysql_views']['description'] = $t('Your MySQL Server is too old. Webform MySQL Views requires at least MySQL %minimum but you are using %version.', array('%minimum' => WEBFORM_MYSQL_VIEWS_MINIMUM_MYSQL, '%version' => db_version()));
+  }
+  else {
+    $requirements['webform_mysql_views']['severity'] = REQUIREMENT_OK;
+    $requirements['webform_mysql_views']['value'] = $t('You are using MySQL version %version.', array('%version' => db_version()));
+  }
+
+  return $requirements;
+}
diff --git a/webform_mysql_views.module b/webform_mysql_views.module
index fa2bb89..1ab2535 100644
--- a/webform_mysql_views.module
+++ b/webform_mysql_views.module
@@ -170,16 +170,6 @@ function webform_mysql_views_help($path) {
  */
 function webform_mysql_views_admin_form() {
 
-
-  $meets_reqs = _webform_mysql_views_check_requirements();
-  if ($meets_reqs !== TRUE) {
-    $output = '<strong>'. t("This module will not work on this site.") .'</strong>';
-    $output .= theme('item_list', $meets_reqs, t('Details'));
-
-    return array("error" => array('#value' => $output));
-  }
-
-
   // Get list of webform-enabled content types
   $types = webform_variable_get('webform_node_types', array());
 
@@ -400,42 +390,6 @@ function webform_mysql_views_get_view_name($title, $nid) {
 }
 
 /**
- * Check Drupal environment for module requirements
- */
-function _webform_mysql_views_check_requirements() {
-
-  global $db_url;
-
-  $meets_reqs = variable_set('webform_mysql_views_meets_reqs', FALSE);
-
-  if ($meets_reqs) {
-    return TRUE;
-  }
-  else {
-    $errors = array();
-    //Make sure site is using MySQL backend
-    if (substr($db_url, 0, 5) != 'mysql') {
-      $errors[] = t("This module is only compatible with the MySQL backend.");
-    }
-
-    //Make sure server major version is 5
-    $version = db_result(db_query("SELECT version() AS version"));
-    if ((int)substr($version, 0, 1) < 5) {
-      $errors[] = t("This module requires MySQL server version 5.0 or later.  Your server is running version @version.", array('@version' => $version));
-    }
-
-    if (count($errors) == 0) {
-      variable_set('webform_mysql_views_meets_reqs', TRUE);
-      return TRUE;
-    }
-    else {
-      return $errors;
-    }
-  }
-
-}
-
-/**
  * Designate a column of a mysql view as primary key.
  * This is required to expose a view via views (and data.module).
  *
-- 
1.7.1

