--- /dev/null
+<?php
+/**
+ * Import Old Member IDs
+ */
+
+$members = array();
+$idMatchingIssues = false;
+
+/*
+ * Check all input
+ */
+
+// 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.';
+}
+
+/*
+ * 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
+ */
+
+// Attempt to get member base data
+if (!$failure) {
+ $sql = "
+ SELECT m.member_id,m.member_name,m.mailing_address,m.mailing_city,s.state_abb as mailing_state,m.mailing_zip
+ FROM members.member m, members.state s
+ WHERE m.state_id = s.state_id
+ AND (m.mailing_address IS NOT NULL OR m.mailing_address != '')
+ AND (m.mailing_city IS NOT NULL OR m.mailing_city != '')
+ ORDER BY member_id
+ ;";
+ $res = pg_query($db, $sql);
+ $rows = pg_num_rows($res);
+ if ( $rows == 0 ) {
+ $templateData['genError'] = 'There does not appear to be any members listed in this database!';
+ $failure = true;
+ } else {
+ $members = pg_fetch_all($res);
+ if ( count( $members ) != $rows ) {
+ $notice = pg_last_notice( $res );
+ $templateData['genError'] = 'While reading base member data, we did not receive the expected number of members! ';
+ if ( $notice ) {
+ $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+ }
+ $failure = true;
+ }
+ }
+}
+
+/*
+ * Find members for the mailing addresses.
+ */
+$notFoundList = array();
+$cityNames = array();
+$numbMembersFound = 0;
+$numbMembersNotFound = 0;
+
+if ( !$failure ) {
+ // Grab all of the city id's
+ $sql = "
+ SELECT *
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+ ORDER BY name";
+ $cities = $this->wpdb->get_results( $sql, ARRAY_A );
+ foreach ( $cities as $city ) {
+ $cityNames[$city['name']] = $city['id'];
+ }
+ foreach ( $members as $member ) {
+ // grab the new member id searching the old_member_id field.
+ $newMemberID = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE old_member_id = %d",
+ $member['member_id']
+ )
+ );
+ if ( !$newMemberID ) {
+ $notFoundList[] = $member['member_name'];
+ $numbMembersNotFound++;
+ } else {
+ $cityId = $cityNames[$member['mailing_city']];
+ if ( !$cityId ) {
+ // need to add the city name
+
+ // Clean up city name
+ $cName = trim(filter_var($member['mailing_city']));
+
+ // Try to add the city
+ require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataCities.php';
+ $Cities = new GlmDataCities($this->wpdb, $this->config);
+ $cID = $Cities->addCity($cName);
+
+ // If we got a city id back
+ if (is_int($cID) && $cID > 0) {
+ $cityId = $cID;
+ }
+ }
+ $this->wpdb->update(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX."member_info",
+ array(
+ 'mailing_addr1' => $member['mailing_address'],
+ 'mailing_city' => (isset( $cityId ) ? $cityId : ''),
+ 'mailing_state' => $member['mailing_state'],
+ 'mailing_zip' => $member['mailing_zip'],
+ ),
+ array( 'id' => $newMemberID ),
+ array(
+ '%s',
+ '%d',
+ '%s',
+ '%s',
+ ),
+ array( '%d' )
+
+ );
+ $numbMembersFound++;
+ }
+ }
+}
+
+// If everything is OK, make data available to the template
+if (!$failure) {
+
+ $templateData['numbOldMembers'] = count($members);
+ $templateData['notFoundList'] = $notFoundList;
+ $templateData['numbMembersFound'] = $numbMembersFound;
+ $templateData['numbMembersNotFound'] = $numbMembersNotFound;
+ $requestedView = 'import/mailingAddresses.html';
+
+} else {
+ $requestedView = 'import.html';
+}
+
{include file='admin/management/header.html'}
-
+
<h2>Data Import Step 1: Supply database access information.</h2>
-{if isset($genError)}
+{if isset($genError)}
<p>
<h3 class="glm-error">Oops!</h3>
<p class="glm-error">{$genError}</p>
</p>
-{/if}
+{/if}
<form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
<input type="hidden" name="glm_action" value="import">
-
+
<table class="glm-admin-table">
-
+
<tr>
<th>Select Action:</th>
<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="importMailingAddresses" class="import-type"> Import member Mailing Addresses<br>
<input type="radio" name="option" value="displayOldNewMemberIds" class="import-type"> List old/new member IDs
</td>
</tr>
<li>Import any images.</li>
<li>Review results</li>
</ol>
- </td>
+ </td>
</tr>
<tr><td colspan="2"> </td></tr>
</table>
</td>
</tr>
</table>
-
+
<input type="submit" value="Continue" class="button button-primary submit-import">
-
+
</form>
-
+
{if $haveImageArray}
<form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data" class="for-member-import">
<input type="hidden" name="glm_action" value="import">
<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($) {
-
+
$('.import-type').change( function() {
-
+
var selected = $(this).val();
$('.all-import-fields').removeClass('glm-hidden');
$('.for-member-import').addClass('glm-hidden');
} else if (selected == 'importClicksViews') {
$('.for-member-import').addClass('glm-hidden');
+ } else if (selected == 'importMailingAddresses') {
+ $('.for-member-import').addClass('glm-hidden');
} else {
$('.for-member-import').addClass('glm-hidden');
$('.all-import-fields').addClass('glm-hidden');
}
-
+
});
-
+
$('.submit-import').on('click', function(){
$('#pleaseWait').removeClass('glm-hidden');
});
});
</script>
-
-
-
+
+
+
{include file='admin/footer.html'}