Adding import script for trail maps.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 21 Apr 2017 16:59:05 +0000 (12:59 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 21 Apr 2017 16:59:05 +0000 (12:59 -0400)
This will import the custom fields and counties.

models/admin/management/import.php
models/admin/management/import/members.php
models/admin/management/import/trailmapsImport.php [new file with mode: 0644]
views/admin/management/import.html
views/admin/management/import/trailmapsImport.html [new file with mode: 0644]
views/admin/member/memberInfo/editProfileDescription.html

index 1a0682c..a1f15b5 100644 (file)
@@ -165,6 +165,11 @@ class GlmMembersAdmin_management_import
             case 'importBrewbakers':
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/brewbakersImport.php';
                 break;
+
+            case 'importTrailMaps':
+                require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/trailmapsImport.php';
+                break;
+
             case 'brewbakersBrands':
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/brewbakersBrands.php';
                 break;
index e6f9c86..ab7db7d 100644 (file)
@@ -224,6 +224,7 @@ if (!$failure) {
 
 // Read in member/credit card cross-reference table
 $numbCcardMembers = 0;
+$membCcard = array();
 if (!$failure) {
     $sql = "
             SELECT *
@@ -810,7 +811,7 @@ if (!$failure) {
                 $ccBits += pow(2, $b);
             }
         }
-
+        $this->wpdb->show_errors();
         // Insert Member Information Record
         $res = $this->wpdb->insert(
                 GLM_MEMBERS_PLUGIN_DB_PREFIX.'member_info',
@@ -823,7 +824,7 @@ if (!$failure) {
                     'short_descr'    => $short_descr,
                     'addr1'          => $val['street'],
                     'addr2'          => '',
-                    'city'           => $city[$val['city_id']]['new_id'],
+                    'city'           => ( (isset($city[$val['city_id']]['new_id']) && $city[$val['city_id']]['new_id']) ? $city[$val['city_id']]['new_id']: 0 ),
                     'state'          => $state[$val['state_id']]['state_abb'],
                     'country'        => 'US',
                     'zip'            => $val['zip'],
diff --git a/models/admin/management/import/trailmapsImport.php b/models/admin/management/import/trailmapsImport.php
new file mode 100644 (file)
index 0000000..82e3a1c
--- /dev/null
@@ -0,0 +1,218 @@
+<?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 &amp; 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';
+}
index d139ff5..af3b513 100644 (file)
@@ -20,7 +20,7 @@
                     <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<br>
-                    <input type="radio" name="option" value="importBrewbakers" class="import-type"> Import Brewbakers
+                    <input type="radio" name="option" value="importTrailMaps" class="import-type"> Import Trail Maps
                 </td>
             </tr>
             <tr class="for-brewbakers glm-hidden">
diff --git a/views/admin/management/import/trailmapsImport.html b/views/admin/management/import/trailmapsImport.html
new file mode 100644 (file)
index 0000000..869a3b9
--- /dev/null
@@ -0,0 +1,15 @@
+{include file='admin/management/header.html'}
+
+    <h3>Custom Field Import: Review Results.</h3>
+{if isset($genError)}
+    <p>
+        <h3 class="glm-error">Oops!</h3>
+        <p class="glm-error">{$genError}</p>
+    </p>
+{/if}
+
+<pre>
+Updated: {$numbMembers} records
+</pre>
+
+{include file='admin/footer.html'}
index 8819b1a..8c7378f 100644 (file)
@@ -3,7 +3,7 @@
                         <div class="glm-row">
                             <h4 {if $memberInfo.fieldRequired.short_descr}class="glm-required"{/if}>Short Description:</h4>
                             <div {if $memberInfo.fieldFail.short_descr}class="glm-form-bad-input" data-tabid="glm-member-info-descr"{/if}>
-                                <input type="text" name="short_descr" value="{$memberInfo.fieldData.short_descr}" class="glm-form-text-input glm-char-count" data-msgArea="short-descr-msg" maxlength="120" placeholder="A short, one line, description of the {$terms.term_member}.">
+                                <input type="text" name="short_descr" value="{$memberInfo.fieldData.short_descr}" class="glm-form-text-input glm-char-count" data-msgArea="short-descr-msg" maxlength="124" placeholder="A short, one line, description of the {$terms.term_member}.">
                                 <br><span id="short-descr-msg"></span>
                                 {if $memberInfo.fieldFail.short_descr}<p>{$memberInfo.fieldFail.short_descr}</p>{/if}
                             </div>
@@ -24,4 +24,4 @@
                                 {if $memberInfo.fieldFail.descr}<p>{$memberInfo.fieldFail.descr}</p>{/if}
                             </div>
                         </div>
-                    </div>
\ No newline at end of file
+                    </div>