Initial Import Click Throughs and Detail Page views code.
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 19 Sep 2016 16:33:11 +0000 (12:33 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 19 Sep 2016 16:33:11 +0000 (12:33 -0400)
models/admin/management/import.php
models/admin/management/import/memberClicksViews.php [new file with mode: 0644]
views/admin/management/import.html
views/admin/management/import/memberClicksViews.html [new file with mode: 0644]
views/admin/member/index.html

index 06d22d8..dc5daf4 100644 (file)
@@ -127,33 +127,27 @@ class GlmMembersAdmin_management_import
         switch($option) {
 
             case 'importSelect':
-
                 $requestedView = 'import.html';
-
                 break;
 
             case 'members':
-
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/members.php';
+                break;
 
+            case 'importClicksViews':
+                require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/memberClicksViews.php';
                 break;
 
             case 'images':
-
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/memberImages.php';
-
                 break;
 
             case 'importOldMemberIds':
-
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/oldMemberIds.php';
-
                 break;
 
             case 'displayOldNewMemberIds':
-
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/displayNewOldMemberIds.php';
-
                 break;
 
             default:
diff --git a/models/admin/management/import/memberClicksViews.php b/models/admin/management/import/memberClicksViews.php
new file mode 100644 (file)
index 0000000..7848771
--- /dev/null
@@ -0,0 +1,227 @@
+<?php
+/**
+ * Import Member Clicks and Detail Views
+ */
+
+/*
+ * Check all input
+ */
+trigger_error('STARTING', E_USER_NOTICE);
+
+// Check hostname
+$dbServer = preg_replace("/[^a-zA-Z0-9\._-]+/", "", trim($_REQUEST['dbServer']));
+$templateData['dbServer'] = array('value' => $dbServer, 'problem' => false);
+if (!$dbServer || $dbServer == '' || $dbServer != trim($_REQUEST['dbServer'])) {
+    $templateData['dbServer']['problem'] = 'Server name or IP address was not provided or contained invalid characters.';
+    $failure = true;
+}
+
+// Check database port #
+$dbPort = preg_replace("/[^0-9]+/", "", trim($_REQUEST['dbPort']));
+$templateData['dbPort'] = array('value' => $dbPort, 'problem' => false);
+if (!$dbPort || $dbPort == '' || $dbPort != trim($_REQUEST['dbPort'])) {
+    $templateData['dbPort']['problem'] = 'Server port was not provided or is not a valid nummber.';
+    $failure = true;
+}
+
+// Check database name
+$dbName = preg_replace("/[^a-zA-Z0-9_]+/", "", trim($_REQUEST['dbName']));
+$templateData['dbName'] = array('value' => $dbName, 'problem' => false);
+if (!$dbName || $dbName == '' || $dbName != trim($_REQUEST['dbName'])) {
+    $templateData['dbName']['problem'] = 'Database name was not provided or is not valid for Postgres.';
+    $failure = true;
+}
+
+// Check database user
+$dbUser = preg_replace("/[^a-zA-Z0-9_]+/", "", trim($_REQUEST['dbUser']));
+$templateData['dbUser'] = array('value' => $dbUser, 'problem' => false);
+if (!$dbUser || $dbUser == '' || $dbUser != trim($_REQUEST['dbUser'])) {
+    $templateData['dbUser']['problem'] = 'Database user was not provided or is not valid for Postgres.';
+    $failure = true;
+}
+
+if ($failure) {
+    $templateData['genError'] = 'There was a problem with the database connection information you provided. See below for specific instructions.';
+}
+
+// Load Members Data Class
+require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMembers.php';
+$Members = new GlmDataMembers($this->wpdb, $this->config);
+
+
+/*
+ * Determine if source database is sane
+ */
+
+// Connect to database
+if (!$failure) {
+    $connString = "host=$dbServer port=$dbPort dbname=$dbName user=$dbUser";
+    $db = @pg_connect($connString);
+    if (!$db) {
+
+        $err = error_get_last();
+        $templateData['genError']  = 'There was a problem connecting to the database. The error message was...<br>'.$err['message'];
+        $failure = true;
+
+    }
+}
+
+// Determine if the members schema exists
+if (!$failure) {
+    $sql = "
+        SELECT EXISTS
+            (
+            SELECT 1
+              FROM information_schema.schemata AS exists
+             WHERE schema_name = 'members'
+            ) AS isMembers
+    ;";
+    $res = pg_query($db, $sql);
+    if (pg_fetch_result($res, 0, 'isMembers') == 'f') {
+        $templateData['genError']  = 'The "members" schema was not found! Is this the right database?';
+        $failure = true;
+    }
+}
+
+/*
+ * Load data from source database
+ */
+trigger_error('Memory before: '.memory_get_usage(), E_USER_NOTICE);
+// Attempt to get member base data
+if (!$failure) {
+    $sql = "
+        SELECT click, detail, edate, member_id
+          FROM members.exposure
+    ;";
+    $res = pg_query($db, $sql);
+    $rows = pg_num_rows($res);
+
+    // Check if there were no results
+    if ($rows == 0) {
+
+        $templateData['genError']  = 'There does not appear to be any Clicks and Views data listed in this database!';
+        $failure = true;
+
+    } else {
+
+        $rrows = 0;
+
+        // Get each row and process it.
+        while ($row = pg_fetch_array($res, null, PGSQL_ASSOC)) {
+
+            // Get new member ID
+            $memb = $Members->getEntry($row['member_id'], 'old_member_id');
+
+            // If we have a good member record
+            if ($memb) {
+
+                $memberId = $memb['id'];
+
+                // Set the date, first date of week, and first date of this month for this entry
+                $time = strtotime($row['edate']);
+                $today = date('Y-m-d', $time);
+                $thisWeek =  date('Y-m-d', strtotime($row['edate'].' -'.date('w', $time).' days'));
+                $thisMonth = date('Y-m-d', strtotime($row['edate'].' -'.(date('j', $time)-1).' days'));
+
+                // If there's click through counts to store
+                if ($row['click'] > 0) {
+
+                    // Create or update click entries as needed using a Transaction
+                    $this->wpdb->query('BEGIN');
+
+                    // Day count - stat_type = 1
+                    $this->wpdb->query("
+                        INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."clickthrough_stats
+                            (member, stat_type, stat_date, clicks)
+                        VALUES
+                            ($memberId, 1, '$today', ".$row['click'].")
+                        ON DUPLICATE KEY
+                            UPDATE clicks = clicks + ".$row['click'].";
+                    ");
+
+                    // Week count - stat_type = 2
+                    $this->wpdb->query("
+                        INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."clickthrough_stats
+                            (member, stat_type, stat_date, clicks)
+                        VALUES
+                            ($memberId, 2, '$thisWeek', ".$row['click'].")
+                        ON DUPLICATE KEY
+                            UPDATE clicks = clicks + ".$row['click'].";
+                    ");
+
+                    // Month count - stat_type = 3
+                    $this->wpdb->query("
+                        INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."clickthrough_stats
+                            (member, stat_type, stat_date, clicks)
+                        VALUES
+                            ($memberId, 3, '$thisMonth', ".$row['click'].")
+                        ON DUPLICATE KEY
+                            UPDATE clicks = clicks + ".$row['click'].";
+                    ");
+
+                    $this->wpdb->query('COMMIT');
+
+                }
+
+                // If there's detail view counts to store
+                if ($row['detail'] > 0) {
+
+                    // Create or update click entries as needed using a Transaction
+                    $this->wpdb->query('BEGIN');
+
+                    // Day count - stat_type = 1
+                    $this->wpdb->query("
+                        INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_detail_stats
+                            (member, stat_type, stat_date, clicks)
+                        VALUES
+                            ($memberId, 1, '$today', ".$row['detail'].")
+                        ON DUPLICATE KEY
+                            UPDATE clicks = clicks + ".$row['detail'].";
+                    ");
+
+                    // Week count - stat_type = 2
+                    $this->wpdb->query("
+                        INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_detail_stats
+                            (member, stat_type, stat_date, clicks)
+                        VALUES
+                            ($memberId, 2, '$thisWeek', ".$row['detail'].")
+                        ON DUPLICATE KEY
+                            UPDATE clicks = clicks + ".$row['detail'].";
+                    ");
+
+                    // Week count - stat_type = 3
+                    $this->wpdb->query("
+                        INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_detail_stats
+                            (member, stat_type, stat_date, clicks)
+                        VALUES
+                            ($memberId, 3, '$thisMonth', ".$row['detail'].")
+                        ON DUPLICATE KEY
+                            UPDATE clicks = clicks + ".$row['detail'].";
+                    ");
+
+                    $this->wpdb->query('COMMIT');
+
+                }
+
+            }
+
+            $rrows++;
+        }
+
+
+    }
+
+    if ($rrows != $rows) {
+        $templateData['genError']  = 'There was a problem retrieving all expected Clicks and Views data! (only '.$rrows.' of '.$rows.')';
+        $failure = true;
+    }
+
+    $templateData['rows'] = $rows;
+
+}
+
+if ($failure) {
+    $requestedView = 'import.html';
+} else {
+    $requestedView = 'import/memberClicksViews.html';
+}
index 14b69b4..8d28b74 100644 (file)
@@ -17,6 +17,7 @@
                 <td>
                     <input type="radio" name="option" value="members" checked="checked" class="import-type"> Import member data<br>
                     <input type="radio" name="option" value="importOldMemberIds" class="import-type"> Import old member IDs only<br>
+                    <input type="radio" name="option" value="importClicksViews" class="import-type"> Import member Clicks and Views<br>
                     <input type="radio" name="option" value="displayOldNewMemberIds" class="import-type"> List old/new member IDs
                 </td>
             </tr>
@@ -94,7 +95,7 @@
             </tr>
         </table>
         
-        <input type="submit" value="Continue" class="button button-primary">
+        <input type="submit" value="Continue" class="button button-primary submit-import">
         
     </form>
             
         <input type="hidden" name="glm_action" value="import">
         <input type="hidden" name="option" value="images">
         <p><span class="glm-notice">WARNING:</span> This process may take a very long time!<br>Do not interrupt or re-submit this page.</p>
-        <input type="submit" value="Import Images" class="button button-primary">
+        <input type="submit" value="Import Images" class="button button-primary submit-import">
     </form>
     {/if}
     
+    
+    <div id="pleaseWait" class="glm-hidden">
+        <h3 class="glm-error">Please Wait!</h3>
+    </div>
+    
 <script type="text/javascript">
         
     jQuery(document).ready(function($) {
             // if doing member data import
             if (selected == 'members') {
                 // do nothing
-            } else if (selected == 'importOldMemberIds'){
+            } else if (selected == 'importOldMemberIds') {
+                $('.for-member-import').addClass('glm-hidden');
+            } else if (selected == 'importClicksViews') {
                 $('.for-member-import').addClass('glm-hidden');
             } else {
                 $('.for-member-import').addClass('glm-hidden');
             }
             
         });
+        
+        $('.submit-import').on('click', function(){
+             $('#pleaseWait').removeClass('glm-hidden');
+        });
 
     });
 </script>
diff --git a/views/admin/management/import/memberClicksViews.html b/views/admin/management/import/memberClicksViews.html
new file mode 100644 (file)
index 0000000..8c117cd
--- /dev/null
@@ -0,0 +1,15 @@
+{include file='admin/management/header.html'}
+    
+    <h2>Clicks and Views Import Results</h2>
+
+{if isset($genError)}    
+    <p>
+        <h3 class="glm-error">Oops!</h3>
+        <p class="glm-error">{$genError}</p>
+    </p>
+{/if}    
+    
+    <h3>Records Imported: {$rows}</h3>
+            
+    
+{include file='admin/footer.html'}
index 963d8d0..0076bbb 100644 (file)
             <b>{$member.fieldData.name}</b>
             <p>
                 <span class="glm-right">{$thisDate}</span>
-                <span class="glm-left">URL Click-Through Counts for Past Month</span>
+                <span class="glm-left">URL Click-Through Counts for Past Month by Day</span>
             </p>
             <img src="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=memberGraphs&graphType=clicks&graphPeriod=oneMonth&memberId={$member.fieldData.id}&memberSlug={$member.fieldData.member_slug}">
             <p>
                 <span class="glm-right">{$thisDate}</span>
-                <span class="glm-left">URL Click-Through Counts for Past 2 Years</span>
+                <span class="glm-left">URL Click-Through Counts for Past 2 Years by Month</span>
             </p>
             <img src="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=memberGraphs&graphType=clicks&graphPeriod=twoYears&memberId={$member.fieldData.id}&memberSlug={$member.fieldData.member_slug}">
         </div>
     <div id="daysViews" class="graph-dialog glm-dialog-box" title="Daily Detail Page Views Graph">
         <div id="viewsPrintArea" class="PrintArea" style="padding: 10px;">
             <b>{$member.fieldData.name}</b>
-            <div>
+            <p>
                 <span class="glm-right">{$thisDate}</span>
-                <span class="glm-left">Detail Page Views for Past Month</span>
-            </div>
+                <span class="glm-left">Detail Page Views for Past Month by Day</span>
+            </p>
             <img src="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=memberGraphs&graphType=views&graphPeriod=oneMonth&memberId={$member.fieldData.id}&memberSlug={$member.fieldData.member_slug}">
-            <div>
+            <p>
                 <span class="glm-right">{$thisDate}</span>
-                <span class="glm-left">Detail Page Views for Past 2 Years</span>
-            </div>
+                <span class="glm-left">Detail Page Views for Past 2 Years by Month</span>
+            </p>
             <img src="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=memberGraphs&graphType=views&graphPeriod=twoYears&memberId={$member.fieldData.id}&memberSlug={$member.fieldData.member_slug}">
         </div>
         <div class="button button-secondary graph-print glm-right" data-areaToPrint="viewsPrintArea">Print</div>