From 906f36effb9de80b11332a8684f2d701ca14a691 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Mon, 19 Sep 2016 12:33:11 -0400 Subject: [PATCH] Initial Import Click Throughs and Detail Page views code. --- models/admin/management/import.php | 12 +- .../management/import/memberClicksViews.php | 227 ++++++++++++++++++ views/admin/management/import.html | 18 +- .../management/import/memberClicksViews.html | 15 ++ views/admin/member/index.html | 16 +- 5 files changed, 268 insertions(+), 20 deletions(-) create mode 100644 models/admin/management/import/memberClicksViews.php create mode 100644 views/admin/management/import/memberClicksViews.html diff --git a/models/admin/management/import.php b/models/admin/management/import.php index 06d22d8b..dc5daf40 100644 --- a/models/admin/management/import.php +++ b/models/admin/management/import.php @@ -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 index 00000000..7848771d --- /dev/null +++ b/models/admin/management/import/memberClicksViews.php @@ -0,0 +1,227 @@ + $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...
'.$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'; +} diff --git a/views/admin/management/import.html b/views/admin/management/import.html index 14b69b44..8d28b74f 100644 --- a/views/admin/management/import.html +++ b/views/admin/management/import.html @@ -17,6 +17,7 @@ Import member data
Import old member IDs only
+ Import member Clicks and Views
List old/new member IDs @@ -94,7 +95,7 @@ - + @@ -103,10 +104,15 @@

WARNING: This process may take a very long time!
Do not interrupt or re-submit this page.

- + {/if} + +
+

Please Wait!

+
+ diff --git a/views/admin/management/import/memberClicksViews.html b/views/admin/management/import/memberClicksViews.html new file mode 100644 index 00000000..8c117cd9 --- /dev/null +++ b/views/admin/management/import/memberClicksViews.html @@ -0,0 +1,15 @@ +{include file='admin/management/header.html'} + +

Clicks and Views Import Results

+ +{if isset($genError)} +

+

Oops!

+

{$genError}

+

+{/if} + +

Records Imported: {$rows}

+ + +{include file='admin/footer.html'} diff --git a/views/admin/member/index.html b/views/admin/member/index.html index 963d8d0a..0076bbb9 100644 --- a/views/admin/member/index.html +++ b/views/admin/member/index.html @@ -44,12 +44,12 @@ {$member.fieldData.name}

{$thisDate} - URL Click-Through Counts for Past Month + URL Click-Through Counts for Past Month by Day

{$thisDate} - URL Click-Through Counts for Past 2 Years + URL Click-Through Counts for Past 2 Years by Month

@@ -60,15 +60,15 @@
{$member.fieldData.name} -
+

{$thisDate} - Detail Page Views for Past Month -

+ Detail Page Views for Past Month by Day +

-
+

{$thisDate} - Detail Page Views for Past 2 Years -

+ Detail Page Views for Past 2 Years by Month +

Print
-- 2.17.1