Add city range search
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 23 Apr 2019 18:10:31 +0000 (14:10 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 23 Apr 2019 18:10:31 +0000 (14:10 -0400)
Using 40 miles. This is taken from uptravel.com.
Only being used when a city is being searched.

classes/data/dataCities.php
models/front/members/list.php
views/front/members/listTypeList.html

index 53e74d2..4b8bccc 100644 (file)
@@ -268,7 +268,7 @@ class GlmDataCities extends GlmDataAbstract
         }
 
         // Get a list of all cities (optionally for active members only)
-        $cities = $this->getList($where);
+        $cities = $this->getList( $where, 'name' );
 
         return $cities;
 
index bd6b8c4..81dfde4 100755 (executable)
@@ -210,6 +210,8 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
         static $cityData             = false;
         static $citySearchSelect     = false;
         static $citySearchSelected   = false;
+        static $citySearch           = false;
+        static $cityName             = false;
         static $featuredMembers      = false;
         static $filterArchived       = false;
         static $filterFeatured       = false;
@@ -415,9 +417,9 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
                 //     SELECT id FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
                 //      WHERE name SOUNDS LIKE '%$textSearch%' OR name LIKE '%$textSearch%'
                 //  )
-                $appendSelect = " match(member_name) against('$textSearch') as rel_name ";
-                $whereSep = ' AND ';
-                $textSearch = stripslashes($textSearch);
+                $appendSelect   = " match(member_name) against('$textSearch') as rel_name ";
+                $whereSep       = ' AND ';
+                $textSearch     = stripslashes($textSearch);
                 $fullTextSearch = true;
             }
 
@@ -551,20 +553,21 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
             $categoryCityList = '';
             $categoryCitySep = '';
             if (isset($_REQUEST) && is_array($cityData) && (isset($_REQUEST['cityUserSearch']) && count($_REQUEST['cityUserSearch']) > 0) || (isset($_REQUEST['citySearch']) && $_REQUEST['citySearch'] != "")) {
+                $citySearch = true;
                 foreach ($cityData as $r) {
                     $id = $r['id'];
                     $cityData[$id]['default'] = false;
                     if ( (isset($_REQUEST['cityUserSearch']) && in_array($id, $_REQUEST['cityUserSearch'])) || (isset($_REQUEST['citySearch']) && $id == $_REQUEST['citySearch']) ) {
                         $cityData[$id]['default'] = true;
                         $citySearchSelected = $id;
-                        $where .= $whereSep." T.city = $id";
-                        $whereSep = ' AND ';
+                        // $where .= $whereSep." T.city = $id";
+                        // $whereSep = ' AND ';
                         $categoryCityList .= $categoryCitySep."$id";
                         $categoryCitySep = ', ';
                     }
                 }
             } else {
-                // Double check that we have an array from $regionData
+                // Double check that we have an array from $cityData
                 if ( isset( $cityData ) && is_array( $cityData ) ) {
                     reset($cityData);
                     foreach ($cityData as $r) {
@@ -988,22 +991,21 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
             // Get a simplified full list of member data for the map. Limit by selected Alpha is used.
     //        require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/memberDataByLatLon.php';
     //        $MapItems = new GlmMembersFront_members_memberDataByLatLon($this->wpdb, $this->config);
-            $mapItems = $this->getSimpleMemberInfoList($where.$alphaWhere, true);
 
             /*
              *  Get a current list of members - unless this is a blank start
              */
             if (!$blankStart) {
 
-                // Get stats for the current selection
-                $membersFound = $this->getStats(str_replace('T.', '', $where));
-
-                // Get stats for number of members found matching current selection criteria (includes alpha selection)
-                $filteredMembersFound = $this->getStats(str_replace('T.', '', $where.$alphaWhere));
-
                 // If there's a full text search, handle separately
                 if ( $fullTextSearch ) {
 
+                    // Get stats for the current selection
+                    $membersFound = $this->getStats(str_replace('T.', '', $where));
+
+                    // Get stats for number of members found matching current selection criteria (includes alpha selection)
+                    $filteredMembersFound = $this->getStats(str_replace('T.', '', $where.$alphaWhere));
+
                     // echo '<pre>$fullTextSearch: ' . print_r( $fullTextSearch, true ) . '</pre>';
                     if ( $appendSelect ) {
                         $order = " rel_name DESC, member_name ASC ";
@@ -1021,10 +1023,57 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
                         '',                 // prohibitListOptions
                         $appendSelect
                     );
+                } elseif ( $citySearchSelected ) {
+                    // Setup city search for distance
+
+                    $cityCoordinates = $this->getCityCoordinates( $citySearchSelected );
+                    if ( $cityCoordinates ) {
+                        $cityName = $cityCoordinates['name'];
+                        // Distance query
+                        $distanceQuery = $this->getDistanceQuery( $cityCoordinates['lat'], $cityCoordinates['lon'] );
+                        $where .= $whereSep." ( $distanceQuery < 40 )";
+                        $whereSep = ' AND ';
+                        $appendSelect = "
+                        CASE
+                            WHEN T.city = {$citySearchSelected} THEN 0
+                        ELSE " . $distanceQuery . "
+                        END AS distance";
+
+                        $order = " distance ASC, city ASC, member_name ASC ";
+                    } else {
+                        $appendSelect = "";
+                        $order = " member_name ASC ";
+                    }
+
+                    // Get stats for the current selection
+                    $membersFound = $this->getStats(str_replace('T.', '', $where));
+
+                    // Get stats for number of members found matching current selection criteria (includes alpha selection)
+                    $filteredMembersFound = $this->getStats(str_replace('T.', '', $where.$alphaWhere));
+
+                    // $where .= "  ";
+                    // echo '<pre>$where: ' . print_r( $where, true ) . '</pre>';
+
+                    ${$resultParam} = $this->getList(
+                        $where.$alphaWhere, // where
+                        $order,             // order
+                        true,               // fieldVals
+                        'id',               // idField
+                        $start,             // start
+                        $limit,             // limit
+                        '',                 // prohibitListOptions
+                        $appendSelect
+                    );
 
                 // When not full text search sort according to settings
                 } else {
 
+                    // Get stats for the current selection
+                    $membersFound = $this->getStats(str_replace('T.', '', $where));
+
+                    // Get stats for number of members found matching current selection criteria (includes alpha selection)
+                    $filteredMembersFound = $this->getStats(str_replace('T.', '', $where.$alphaWhere));
+
                     // Get member list sorted as specified
                     switch ($this->config['settings']['list_order_list']) {
 
@@ -1048,6 +1097,11 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
                     }
                 }
 
+                $mapItems = $this->getSimpleMemberInfoList($where.$alphaWhere, true);
+
+            } else {
+
+                $mapItems = $this->getSimpleMemberInfoList($where.$alphaWhere, true);
             }
 
             if ($paging) {
@@ -1239,6 +1293,7 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
             'view'                 => $view,
             'haveFeaturedMembers'  => $haveFeaturedMembers,
             'featuredMembers'      => $featuredMembers,
+            'cityName'             => $cityName,
 
             // Paging parameters
             'filteredMembersFound' => $filteredMembersFound,
@@ -1292,5 +1347,29 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
     }
 
 
-}
+    public function getDistanceQuery( $lat, $lng )
+    {
+        $foo = "
+        (pow(sin(((T.lat * pi()/180.0) - ($lat * pi()/180.0)) / 2.0), 2) + cos(($lat * pi() / 180.0)) * cos((T.lat * pi() / 180.0)) * pow(sin(((T.lon * pi() / 180.0) - ($lng * pi() / 180.0)) / 2.0), 2))";
+
+        return "ceil(3956 * 2 *
+                atan2(sqrt($foo),
+                sqrt(1 - ($foo))))";
+    }
+
+    public function getCityCoordinates( $cityId )
+    {
+        return $this->wpdb->get_row(
+            $this->wpdb->prepare(
+                "SELECT name, lat, lon
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+                  WHERE id = %d",
+                $cityId
+            ),
+            ARRAY_A
+        );
+
+    }
 
+
+}
index bd1efe5..14c3bd0 100644 (file)
                     {apply_filters('glm-member-db-front-members-list-linksBottom', '', $member.member_pointer)}
 
                 </div>
+                {if isset( $member.distance ) && $member.distance > 0 }
+                <div>
+                    Distance: {$member.distance} miles from {$cityName}
+                </div>
+                {/if}
                 <div class="glm-member-db-socials">
 
                    {apply_filters('glm-member-db-front-members-social-list', '', $member.member_pointer)}