From: Steve Sutton Date: Tue, 23 Apr 2019 18:10:31 +0000 (-0400) Subject: Add city range search X-Git-Tag: v2.12.2^2~11 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=6f4da32fbc85bfc1e3fbb509897b92f563e8ef05;p=WP-Plugins%2Fglm-member-db.git Add city range search Using 40 miles. This is taken from uptravel.com. Only being used when a city is being searched. --- diff --git a/classes/data/dataCities.php b/classes/data/dataCities.php index 53e74d2e..4b8bccc0 100644 --- a/classes/data/dataCities.php +++ b/classes/data/dataCities.php @@ -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; diff --git a/models/front/members/list.php b/models/front/members/list.php index bd6b8c41..81dfde43 100755 --- a/models/front/members/list.php +++ b/models/front/members/list.php @@ -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 '
$fullTextSearch: ' . print_r( $fullTextSearch, true ) . '
'; 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 '
$where: ' . print_r( $where, true ) . '
'; + + ${$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 + ); + + } + +} diff --git a/views/front/members/listTypeList.html b/views/front/members/listTypeList.html index bd1efe55..14c3bd00 100644 --- a/views/front/members/listTypeList.html +++ b/views/front/members/listTypeList.html @@ -97,6 +97,11 @@ {apply_filters('glm-member-db-front-members-list-linksBottom', '', $member.member_pointer)} + {if isset( $member.distance ) && $member.distance > 0 } +
+ Distance: {$member.distance} miles from {$cityName} +
+ {/if}
{apply_filters('glm-member-db-front-members-social-list', '', $member.member_pointer)}