--- /dev/null
+<?php
+// Setup the brewbakers image url
+$dbImageURL = 'http://is0.gaslightmedia.com/brewbakers/CKImage/';
+
+// Setup the database connection to postgres
+$dbServer = 'ds4';
+$dbName = 'michigantrailmaps';
+$dbUser = 'postgres';
+$trails = array();
+$failure = false;
+
+// Create connection
+$connString = "host=$dbServer 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;
+}
+// Reset the custom field data table.
+$this->wpdb->query("DELETE FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data");
+$this->wpdb->query("DELETE FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "counties");
+
+function getMemberInfoIdFromOldMemberId( $wpdb, $oldMemberId )
+{
+ return $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info
+ WHERE member IN (
+ SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE old_member_id = %d)",
+ $oldMemberId
+ )
+ );
+}
+function getCustomFieldId( $wpdb, $name )
+{
+ return $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields
+ WHERE field_name = %s",
+ $name
+ )
+ );
+}
+$customFieldIds = array(
+ 'reviewed' => getCustomFieldId( $this->wpdb, 'Reviewed' ),
+ 'distance' => getCustomFieldId( $this->wpdb, 'Distance' ),
+ 'trailtype' => getCustomFieldId( $this->wpdb, 'Type' ),
+ 'terrain' => getCustomFieldId( $this->wpdb, 'Terrain' ),
+ 'difficulty' => getCustomFieldId( $this->wpdb, 'Difficulty' ),
+ 'nearest_city' => getCustomFieldId( $this->wpdb, 'Nearest City or Town' ),
+ 'guide' => getCustomFieldId( $this->wpdb, 'Trail Guide' ),
+ 'facilities' => getCustomFieldId( $this->wpdb, 'Facilities' ),
+ 'hours' => getCustomFieldId( $this->wpdb, 'Hours & Fees' ),
+ 'directions' => getCustomFieldId( $this->wpdb, 'Directions' ),
+ 'information' => getCustomFieldId( $this->wpdb, 'Information' ),
+);
+//echo '<pre>$customFieldIds: ' . print_r( $customFieldIds, true ) . '</pre>';
+function addCustomFieldData( $wpdb, $customFieldIds, $ref_dest, $fieldName, $fieldValue )
+{
+ // check the $fieldValue for t or f
+ if ( $fieldValue ) {
+ if ( $fieldValue == 't' ) {
+ $fieldValue = 'Yes';
+ } elseif ( $fieldValue == 'f' ) {
+ $fieldValue = 'No';
+ }
+ }
+ $wpdb->insert(
+ GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_data',
+ array(
+ 'field_id' => $customFieldIds[$fieldName],
+ 'ref_dest' => $ref_dest,
+ 'field_data' => ($fieldValue) ? $fieldValue: '',
+ ),
+ array( '%d', '%d', '%s' )
+ );
+}
+
+// Read in all counties
+if (!$failure) {
+ $sql = "
+ SELECT *
+ FROM members.county
+ ;";
+ $res = pg_query($db, $sql);
+ $rows = pg_num_rows($res);
+ if ($rows == 0) {
+ $templateData['genError'] = 'There does not appear to be any counties listed in this database!';
+ $failure = true;
+ } else {
+ $tmp = pg_fetch_all($res);
+ if (count($tmp) != $rows) {
+ $notice = pg_last_notice($res);
+ $templateData['genError'] = 'While reading county data, we did not receive the expected number of counties! ';
+ if ($notice) {
+ $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+ }
+ $failure = true;
+ } else {
+
+ // Since there was no problem, we'll post-process the cities into an array indexed by city_id
+ $county = array();
+
+ // Reprocess into array indexed by state_id
+ foreach ($tmp as $x) {
+ $county[$x['county_id']] = $x;
+ }
+ }
+ }
+}
+
+if (!$failure) {
+ // Import Counties
+ while (list ($key, $val) = each ($county) ) {
+
+ $res = $this->wpdb->insert(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX.'counties',
+ array(
+ 'name' => $val['county_name'],
+ 'descr' => '',
+ 'short_descr' => ''
+ ),
+ array(
+ '%s',
+ '%s',
+ '%s'
+ )
+ );
+ $county[$key]['new_id'] = $this->wpdb->insert_id;
+
+ }
+}
+
+if ( !$failure ) {
+ $sql = "
+ SELECT member_id,county,reviewed,distance,trailtype,terrain,difficulty,
+ nearest_city,guide,facilities,hours,directions,information
+ FROM members.member
+ 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 member files listed in this database!';
+ $failure = true;
+ } else {
+ $trails = pg_fetch_all( $res );
+ if ( count( $trails ) != $rows ) {
+ $notice = pg_last_notice( $res );
+ $templateData['genError'] = 'While reading base trails data, we did not receive the expected number of trails! ';
+ if ( $notice ) {
+ $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+ }
+ $failure = true;
+ }
+ }
+}
+//echo '<pre>$trails: ' . print_r( $trails, true ) . '</pre>';
+//echo '<pre>$customFieldIds: ' . print_r( $customFieldIds, true ) . '</pre>';
+//exit;
+$test = false;//true;
+if (!$failure) {
+ // Import Members
+ while (list ($key, $val) = each ($trails) ) {
+ // Grab the new member info id from the old member_id field.
+ $infoID = getMemberInfoIdFromOldMemberId( $this->wpdb, $val['member_id'] );
+ if ( !$infoID ) {
+ echo '<pre>$infoID: ' . print_r( $infoID, true ) . '</pre>';
+ echo '<pre>$val[member_id]: ' . print_r( $val['member_id'], true ) . '</pre>';
+ }
+
+ if ( $infoID && !$test ) {
+ // Update the county field for the member info record
+ $res = $this->wpdb->update(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
+ array(
+ 'county' => (isset($county[$val['county']]) ? $county[$val['county']]['new_id']: 0)
+ ),
+ array('id' => $infoID ),
+ array( '%d' ),
+ array( '%d' )
+ );
+ // Add Custom Fields
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'reviewed', $val['reviewed'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'distance', $val['distance'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'trailtype', $val['trailtype'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'terrain', $val['terrain'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'difficulty', $val['difficulty'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'nearest_city', $val['nearest_city'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'guide', $val['guide'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'facilities', $val['facilities'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'hours', $val['hours'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'directions', $val['directions'] );
+ addCustomFieldData( $this->wpdb, $customFieldIds, $infoID, 'information', $val['information'] );
+ }
+
+ }
+ //echo '<pre>$member: ' . print_r( $member, true ) . '</pre>';
+}
+
+$haveCatImportIssues = false;
+$catImportIssues = '';
+// If everything is OK, make data available to the template
+if (!$failure) {
+
+ $templateData['numbMembers'] = count($trails);
+}
+
+
+if ($failure) {
+ $requestedView = 'import.html';
+} else {
+ $requestedView = 'import/trailmapsImport.html';
+}