--- /dev/null
+<?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';
+}
<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>
</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>
<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>