Better handling of database table issues
authorChuck Scott <cscott@gaslightmedia.com>
Wed, 24 Feb 2016 19:30:25 +0000 (14:30 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Wed, 24 Feb 2016 19:30:25 +0000 (14:30 -0500)
classes/glmPluginSupport.php
glm-member-db.php
views/admin/management/addons.html

index 545d8f6..b138c20 100644 (file)
@@ -169,8 +169,9 @@ class GlmPluginSupport
      * @return boolean False if some failure
      * @access private
      */
-    public function checkDatabase ($option = false)
+    public function checkDatabase ()
     {
+        global $startupNotices;
 
         $all_db_setup_status = true;
 
@@ -178,6 +179,7 @@ class GlmPluginSupport
         foreach ($this->config['addOns'] as $a) {
 
             $db_setup_status = false;
+            $db_error = false;
 
             // Has this plug-in registered any database versions
             if ($a['database']) {
@@ -206,48 +208,42 @@ class GlmPluginSupport
 
                     // Check if the database version set for this plug-in is invalid - not in the dbVersions array
                     if (!isset($a['database']['dbVersions'][$dbVersion])) {
-                        update_option(
-                            'glmMembersInstallErrors',
-                            "The last database version set for the ".$a['name']." (V$dbVersion) isn't valid.");
-                        return false;
-                    }
+                        $startupNotices .= "<p>The last database version set for the ".$a['name']." (V$dbVersion) isn't valid.<p>";
+                        $db_error = true;
+                    } else {
 
-                    // Get the number of tables for this plugin database version that should exist.
-                    $tables = $a['database']['dbVersions'][$dbVersion]['tables'];
+                        // Get the number of tables for this plugin database version that should exist.
+                        $tables = $a['database']['dbVersions'][$dbVersion]['tables'];
 
-                    // Since MYSQL considers an underscore (_) to be a single character wild-card, we need to escape them.
-                    $prefixEscaped = str_replace('_', '\_', $a['database']['dbPrefix']);
+                        // Since MYSQL considers an underscore (_) to be a single character wild-card, we need to escape them.
+                        $prefixEscaped = str_replace('_', '\_', $a['database']['dbPrefix']);
 
-                    // Get the number of tables for this plugin that match the
-                    $existingTables = $this->wpdb->get_var("
-                        SELECT COUNT(*)
-                          FROM information_schema.tables
-                         WHERE table_schema = '".DB_NAME."'
-                           AND table_name like '$prefixEscaped%';
-                    ");
+                        // Get the number of tables for this plugin that match the
+                        $existingTables = $this->wpdb->get_var("
+                            SELECT COUNT(*)
+                              FROM information_schema.tables
+                             WHERE table_schema = '".DB_NAME."'
+                               AND table_name like '$prefixEscaped%';
+                        ");
 
-                    // If there's no tables, just assume we need to install all new ones.
-                    if ($existingTables == 0) {
+                        // If there's no tables, just assume we need to install all new ones.
+                        if ($existingTables == 0) {
 
-                        $dbVersion = false;
+                            $dbVersion = false;
+
+                            // Otherwise check if the number of tables is correct
+                        } elseif ($tables != $existingTables) {
+                            $startupNotices .= '<p>We do not have the correct number of tables for the currently set database version (V'.$dbVersion.') for '.$a['name'].'.'
+                                    ."<br>There should be $tables but there are currently $existingTables. Please call for support.</p>";
+                            $db_error = true;
+                        }
 
-                        // Otherwise check if the number of tables is correct
-                    } elseif ($tables != $existingTables) {
-                        update_option('glmMembersInstallErrors', 'We do not have the correct number of tables for the currently set database version (V'.$dbVersion.') for '.$a['name'].'.'
-                                ."<br>There should be $tables but there are currently $existingTables. Please call for support."
-                        );
-                        return false;
                     }
 
                 }
 
                 // If glmMembersDatabaseDbVersion is not set, install current version
-                if (!$dbVersion) {
-
-                    // If $option is not 'install', then don't do it;
-                    if ($option != 'install') {
-                        return false;
-                    }
+                if (!$dbVersion && !$db_error) {
 
                     // Get current database version
                     $dbVersion = $a['database']['dbCurrentVersion'];
@@ -273,7 +269,7 @@ class GlmPluginSupport
                     if (trim($queryError) == '') {
 
                         // Notify the user that the database has been installed
-                        update_option('glmMembersInstallErrors', 'New database tables installed for the '.$a['name'].' plugin.');
+                        $startupNotices .= '<P>New database tables installed for the '.$a['name'].' plugin.</P>';
 
                         // Save the version of the installed database
                         update_option($a['database']['dbActiveVersionOption'], $dbVersion);
@@ -282,10 +278,9 @@ class GlmPluginSupport
                         $db_setup_status = true;
 
                     } else {
-                        update_option('glmMembersInstallErrors', 'Failure installing database tables for the '.$a['name']
+                        $startupNotices .= '<P>Failure installing database tables for the '.$a['name']
                                 .'<br><b>Database Installation Error:</b> '.print_r($queryError,1)
-                                .'<br><pre>'.$q.'</pre>'
-                        );
+                                .'<br><pre>'.$q.'</pre></P>';
                     }
 
                 // Otherwise, check if we need to update the database
@@ -377,9 +372,6 @@ class GlmPluginSupport
                     }
 
                 } else {
-                    if ($option == 'install') {
-// was always displaying since adding 'install' to the controllers call                       $this->addNotice('The '.$a['name'].' plugin/add-on has been reactivated using the existing database tables.');
-                    }
                     $db_setup_status = true;
                 }
 
@@ -392,6 +384,15 @@ class GlmPluginSupport
             if (!$db_setup_status) {
                 $all_db_setup_status = false;
             }
+
+            // If there was any serious error with the database for this add-on
+            if ($db_error) {
+                // Deactivate this add-on
+                deactivate_plugins($a['slug'].'/index.php');
+                global $startupNotices;
+                $startupNotices .= '<p>Plugin '.$a['name'].' Deactivated.<p>';
+
+            }
         } // For each plugin
 
         return $all_db_setup_status;
@@ -519,3 +520,14 @@ function glmMembersAdminNotices($windowed = true)
 
 }
 
+/**
+ * Display Git Branch as an admin warning
+ *
+ * @return void
+ * @access public
+ */
+function glmMembersInstallErrorsNotice() {
+    $installErrors = get_option('glmMembersInstallErrors');
+    echo '<div class="error"><h3 class="glm-error">Plugin Git Branch Warning:</h3><br>'.$installErrors.'</div>';
+    delete_option('glmMembersInstallErrors');
+}
index 3f00020..bf20849 100644 (file)
@@ -425,7 +425,7 @@ if (isset($_REQUEST['glmDebugWindow']) && $_REQUEST['glmDebugWindow'] == true) {
 // Display any notices that need to be displayed at the top of the admin content area
 function glmMembersWordpressAdminNotices() {
     global $startupNotices;
-    echo '<div class="updated"><h3>'.GLM_MEMBERS_PLUGIN_NAME.' Plugin Warning</h3><p>'.$startupNotices.'</p></div>';
+    echo '<div class="updated">'.GLM_MEMBERS_PLUGIN_NAME.' Plugin Notice<br>'.$startupNotices.'</div>';
 }
 if ($startupNotices != '') {
     add_action('admin_notices','glmMembersWordpressAdminNotices');
index 0154d61..69da074 100644 (file)
@@ -1,17 +1,39 @@
 {include file='admin/management/header.html'}
 
     <h2>Currently Installed and Active GLM Members DB Plugins</h2>
-    
+<style>
+a.glm-anchor {
+    display: block;
+    position: relative;
+    top: -50px;
+    visibility: hidden;
+}
+a.glm-top {
+    display: block;
+    position: relative;
+    top: -1000px;
+    visibility: hidden;
+}
+</style>    
+
 {if $addOns}
-    
-    <ul class="glm-li">
-    {foreach $addOns as $a}
-        <li>
-            <b>{$a.name}</b><br>
-            <pre style="font-size: .9em; line-height: .9em; border: 1px solid black; padding: 5px;">{$a.print_r}</pre>
-        </li>
-    {/foreach}
-    </ul>
+    <a class="glm-top" id="top"></a>
+    <div>    
+        {foreach $addOns as $a}
+                <a href="#{$a.slug}"><b>{$a.name}</b></a><br>
+        {/foreach}
+    </div>
+    <div>
+        <ul class="glm-li">
+        {foreach $addOns as $a}
+            <li>
+                <a class="glm-anchor" id="{$a.slug}"></a>
+                <a href="#top" class="glm-right">Top</a><b>{$a.name}</b><br>
+                <pre style="font-size: .9em; line-height: .9em; border: 1px solid black; padding: 5px;">{$a.print_r}</pre>
+            </li>
+        {/foreach}
+        </ul>
+    </div>
 {/if}
         
 {include file='admin/footer.html'}