Updates for interactive map
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 8 Nov 2018 15:49:42 +0000 (10:49 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 8 Nov 2018 15:49:42 +0000 (10:49 -0500)
Apply bounds if in shortcode.
Import for county field from the old city table.

js/leaflet-area-map.js
models/admin/management/import.php
models/admin/management/import/countyCity.php [new file with mode: 0644]
models/front/members/interactivemap.php
views/admin/management/import.html
views/admin/management/import/countyCity.html [new file with mode: 0644]

index 2b8a430..a25ef18 100644 (file)
@@ -270,6 +270,22 @@ var GlmMap = {
             var lat = markers[i].lat;
             var lng = markers[i].lon;
 
+            // Check if there's bounds
+            if ( glm_lat_bounds ) {
+                var latbounds = glm_lat_bounds.split(',');
+                if ( parseFloat(lat) < parseFloat(latbounds[0]) || parseFloat(lat) > parseFloat(latbounds[1]) ) {
+                    // out of bounds
+                    continue;
+                }
+            }
+            if ( glm_lon_bounds ) {
+                var lonbounds = glm_lon_bounds.split(',');
+                if ( parseFloat(lng) < parseFloat(lonbounds[0]) || parseFloat(lng) > parseFloat(lonbounds[1]) ) {
+                    // out of bounds
+                    continue;
+                }
+            }
+
             var point = new google.maps.LatLng(
                 parseFloat(lat),
                 parseFloat(lng)
@@ -303,6 +319,22 @@ var GlmMap = {
         for (i = 0; i < markers.length; i++) {
             var lat = markers[i].lat;
             var lng = markers[i].lon;
+
+            // Check if there's bounds
+            if ( glm_lat_bounds ) {
+                var latbounds = glm_lat_bounds.split(',');
+                if ( parseFloat(lat) < parseFloat(latbounds[0]) || parseFloat(lat) > parseFloat(latbounds[1]) ) {
+                    // out of bounds
+                    continue;
+                }
+            }
+            if ( glm_lon_bounds ) {
+                var lonbounds = glm_lon_bounds.split(',');
+                if ( parseFloat(lng) < parseFloat(lonbounds[0]) || parseFloat(lng) > parseFloat(lonbounds[1]) ) {
+                    // out of bounds
+                    continue;
+                }
+            }
             var leafletMarker = L.marker([lat, lng])
                 .addTo(GlmMap.markerAreaGroup);
         }
index f8c4b9d..9998b55 100644 (file)
@@ -144,6 +144,10 @@ class GlmMembersAdmin_management_import
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/mailingAddresses.php';
                 break;
 
+            case 'importCountyCity':
+                require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/countyCity.php';
+                break;
+
             case 'images':
                 require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/memberImages.php';
                 break;
diff --git a/models/admin/management/import/countyCity.php b/models/admin/management/import/countyCity.php
new file mode 100644 (file)
index 0000000..3cb9908
--- /dev/null
@@ -0,0 +1,191 @@
+<?php
+/**
+ * Import Counties into the City Table
+ */
+
+$cities           = 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 C.city_id,R.region_name,C.city_name
+      FROM members.city C LEFT OUTER JOIN members.region R ON ( C.region_id = R.region_id )
+     WHERE C.region_id is not null
+    ORDER BY C.city_name";
+    $res  = pg_query($db, $sql);
+    $rows = pg_num_rows($res);
+    if ( $rows == 0 ) {
+        $templateData['genError']  = 'There does not appear to be any cities listed in this database!';
+        $failure                   = true;
+    } else {
+        $cities = pg_fetch_all($res);
+        if ( count( $cities ) != $rows ) {
+            $notice                    = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base member data, we did not receive the expected number of cities! ';
+            if ( $notice ) {
+                $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+            }
+            $failure = true;
+        }
+    }
+}
+
+/*
+ * Find cities for the Counties
+ */
+$notFoundList       = array();
+$countyNames        = array();
+$numbMembersFound   = 0;
+$numbCitiesNotFound = 0;
+
+if ( !$failure ) {
+    // Grab all of the county id's
+    $sql = "
+    SELECT *
+      FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "counties
+    ORDER BY name";
+    $counties = $this->wpdb->get_results( $sql, ARRAY_A );
+    foreach ( $counties as $county ) {
+        $countyNames[$county['name']] = $county['id'];
+    }
+    // echo '<pre>$countyNames: ' . print_r( $countyNames, true ) . '</pre>';
+    foreach ( $cities as $city ) {
+        // grab the new city id searching the name field.
+        $newCityID = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+                  WHERE name = %s",
+                $city['city_name']
+            )
+        );
+        // echo '<pre>$newCityID: ' . print_r( $newCityID, true ) . '</pre>';
+        if ( !$newCityID ) {
+            $notFoundList[] = $city['city_name'];
+            $numbCitiesNotFound++;
+        } else {
+            $countyId = $countyNames[$city['region_name']];
+            // echo '<pre>$countyId: ' . print_r( $countyId, true ) . '</pre>';
+            if ( !$countyId ) {
+                // need to add the cienty name
+
+                // Clean up city name
+                $cName = trim(filter_var($city['region_name']));
+
+                // 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) {
+                //     $countyId = $cID;
+                // }
+            }
+            if ( $countyId ) {
+                $this->wpdb->update(
+                    GLM_MEMBERS_PLUGIN_DB_PREFIX."cities",
+                    array( 'county' => $countyId ),
+                    array( 'id' => $newCityID ),
+                    array( '%d' ),
+                    array( '%d' )
+
+                );
+                $numbCitiesFound++;
+            }
+        }
+    }
+}
+
+// If everything is OK, make data available to the template
+if (!$failure) {
+
+    $templateData['numbCities']         = count($cities);
+    $templateData['notFoundList']       = $notFoundList;
+    $templateData['numbCitiesFound']    = $numbCitiesFound;
+    $templateData['numbCitiesNotFound'] = $numbCitiesNotFound;
+    $requestedView                      = 'import/countyCity.html';
+
+} else {
+    $requestedView = 'import.html';
+}
+
index e0d61a9..cd4b9ea 100644 (file)
@@ -208,7 +208,12 @@ class GlmMembersFront_members_interactivemap //extends GlmDataMemberInfo
     {
         return $this->wpdb->get_results(
             "SELECT *
-               FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "counties",
+               FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "counties
+              WHERE id IN (
+                    SELECT distinct( county )
+                      FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info
+                     WHERE status = 10
+                        AND (county IS NOT NULL OR county != 0 ) )",
             ARRAY_A
         );
     }
@@ -217,7 +222,12 @@ class GlmMembersFront_members_interactivemap //extends GlmDataMemberInfo
     {
         return $this->wpdb->get_results(
             "SELECT *
-               FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities",
+               FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+              WHERE id IN (
+                    SELECT distinct( city )
+                      FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info
+                     WHERE status = 10
+                        AND (county IS NOT NULL OR county != 0 ) )",
             ARRAY_A
         );
     }
index 5ce7b9f..3c4db07 100644 (file)
@@ -21,6 +21,7 @@
                     <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="importCountyCity" class="import-type"> Import Counties to City Table<br>
                     <input type="radio" name="option" value="displayOldNewMemberIds" class="import-type"> List old/new member IDs<br>
                 </td>
             </tr>
             // if doing member data import
             if (selected == 'members') {
                 // do nothing
+            } else if (selected == 'importCountyCity') {
+                $('.for-member-import').addClass('glm-hidden');
             } else if (selected == 'importOldMemberIds') {
                 $('.for-member-import').addClass('glm-hidden');
             } else if (selected == 'importClicksViews') {
diff --git a/views/admin/management/import/countyCity.html b/views/admin/management/import/countyCity.html
new file mode 100644 (file)
index 0000000..5934485
--- /dev/null
@@ -0,0 +1,17 @@
+{include file='admin/management/header.html'}
+
+    <h2>Import Counties into City Table - Import Results</h2>
+    <table class="glm-admin-table">
+        <tr><th>Number of Cities</th><td>{$numbCities}</td></tr>
+        <tr><th>Number of Cities Found</th><td>{$numbCitiesFound}</td></tr>
+        <tr><th>Number of Cities Not Found</th><td>{$numbCitiesNotFound}</td></tr>
+    </table>
+    <table class="wp-list-table striped glm-admin-table">
+        <tr><th>Cities not found in new database</th></tr>
+  {foreach $notFoundList as $n}
+        <tr><td>{$n}</td></tr>
+  {/foreach}
+    </table>
+
+
+{include file='admin/footer.html'}