From: Steve Sutton Date: Thu, 18 Jul 2013 16:46:18 +0000 (+0000) Subject: Update search X-Git-Tag: v1.0~74 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=036287735b00967ec3f618cb014427c53709aa81;p=web%2FMichiganTrailMaps.git Update search Adding ajax call to update the counties select when using the region select on the search form. Output a text description of what the user is looking for in their search. --- diff --git a/Toolkit/Members/SearchDisplay.php b/Toolkit/Members/SearchDisplay.php new file mode 100644 index 0000000..8b25acd --- /dev/null +++ b/Toolkit/Members/SearchDisplay.php @@ -0,0 +1,183 @@ + + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @version SVN: (0.1) + * @link <> + */ + +/** + * Toolkit_Package_SearchDisplay + * + * Description of SearchDisplay + * + * @category Toolkit + * @package Members + * @author Steve Sutton + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @release Release: (0.1) + * @link <> + */ +class Toolkit_Members_SearchDisplay +{ + private $_searchTerms; + + public function __construct(PDO $dbh) + { + $this->_searchTerms = new ArrayObject(); + $posted = filter_var_array( + $_REQUEST, + array( + 'member_name' => FILTER_SANITIZE_STRING, + 'activityId' => FILTER_VALIDATE_INT, + 'parkId' => FILTER_VALIDATE_INT, + 'regionId' => FILTER_VALIDATE_INT, + 'countyId' => FILTER_VALIDATE_INT, + 'amenity' => array( + 'filter' => FILTER_VALIDATE_INT, + 'flags' => FILTER_FORCE_ARRAY + ) + ) + ); + extract($posted); + if ($member_name) { + $this->_searchTerms->offsetSet('member_name', $member_name); + } + if ($activityId) { + $activityName = $this->getCategoryName($dbh, $activityId); + $this->_searchTerms->offsetSet('activity', $activityName); + } + if ($parkId) { + $parkName = $this->getCategoryName($dbh, $parkId); + $this->_searchTerms->offsetSet('park', $parkName); + } + if ($regionId) { + $regionName = $this->getRegionName($dbh, $regionId); + $this->_searchTerms->offsetSet('region', $regionName); + } + if ($countyId) { + $countyName = $this->getCountyName($dbh, $countyId); + $this->_searchTerms->offsetSet('county', $countyName); + } + if (!empty($amenity) && $amenity[0]) { + $amenities = array(); + foreach ($amenity as $amm) { + $amenities[] = $this->getAmenityName($dbh, $amm); + } + $this->_searchTerms->offsetSet('amenity', $amenities); + } + } + + public function toHtml() + { + $html = ''; + if (count($this->_searchTerms) > 0) { + $html .= 'Searching for '; + if ($this->_searchTerms->offsetExists('activity')) { + $activity = $this->_searchTerms->offsetGet('activity'); + $html .= $activity . ' '; + if (!preg_match('%trails%i', $activity)) { + $html .= 'Trials '; + } + } else { + $html .= 'Trials '; + } + if ($this->_searchTerms->offsetExists('amenity')) { + $amenities = $this->_searchTerms->offsetGet('amenity'); + $html .= 'that are ' . implode(' or ', $amenities) . ' '; + } + if ($this->_searchTerms->offsetExists('park')) { + $park = $this->_searchTerms->offsetGet('park'); + $html .= 'in ' . $park . ' '; + if (!preg_match('%parks%i', $park)) { + $html .= 'Parks '; + } + } + if ($this->_searchTerms->offsetExists('region')) { + $html .= 'in ' . $this->_searchTerms->offsetGet('region') + . ' Region '; + } + if ($this->_searchTerms->offsetExists('county')) { + $html .= 'in ' . $this->_searchTerms->offsetGet('county') + . ' County '; + } + + } + return $html; + } + + protected function getCategoryName(PDO $dbh, $id) + { + try { + $sql = " + SELECT name + FROM members.category + WHERE category_id = :id"; + $stmt = $dbh->prepare($sql); + $stmt->bindParam(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchColumn(); + } catch (PDOException $e) { + Toolkit_Common::handleError($e); + } + + } + + protected function getRegionName(PDO $dbh, $id) + { + try { + $sql = " + SELECT region_name + FROM members.region + WHERE region_id = :id"; + $stmt = $dbh->prepare($sql); + $stmt->bindParam(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchColumn(); + } catch (PDOException $e) { + Toolkit_Common::handleError($e); + } + } + + protected function getCountyName(PDO $dbh, $id) + { + try { + $sql = " + SELECT county_name + FROM members.county + WHERE county_id = :id"; + $stmt = $dbh->prepare($sql); + $stmt->bindParam(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchColumn(); + } catch (PDOException $e) { + Toolkit_Common::handleError($e); + } + } + + protected function getAmenityName(PDO $dbh, $id) + { + try { + $sql = " + SELECT amenity_name + FROM members.amenity + WHERE amenity_id = :id"; + $stmt = $dbh->prepare($sql); + $stmt->bindParam(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchColumn(); + } catch (PDOException $e) { + Toolkit_Common::handleError($e); + } + } +} + diff --git a/Toolkit/Members/SearchQueryGenerator.php b/Toolkit/Members/SearchQueryGenerator.php index 636edf8..30b5cc6 100644 --- a/Toolkit/Members/SearchQueryGenerator.php +++ b/Toolkit/Members/SearchQueryGenerator.php @@ -152,6 +152,13 @@ class Toolkit_Members_SearchQueryGenerator // }}} + public function getSearchTerms(PDO $dbh) + { + $terms = new Toolkit_Members_SearchDisplay($dbh); + + return $terms->toHtml(); + } + // {{{ getQuery() /** @@ -263,13 +270,13 @@ class Toolkit_Members_SearchQueryGenerator WHERE amenity_id IN (" .implode(',', $amenity)."))"; } - if (!empty($activity) && $activity[0]) { - $params[] = "m.member_id IN ( - SELECT member_id - FROM member_category - WHERE category_id IN (" - .implode(',', $activity)."))"; - } +// if (!empty($activity) && $activity[0]) { +// $params[] = "m.member_id IN ( +// SELECT member_id +// FROM member_category +// WHERE category_id IN (" +// .implode(',', $activity)."))"; +// } if ($activityId) { $params[] = "m.member_id IN ( SELECT member_id @@ -282,19 +289,19 @@ class Toolkit_Members_SearchQueryGenerator FROM member_category WHERE category_id = {$parkId})"; } - if ($regionId) { - $params[] = "m.region = {$regionId}"; - } +// if ($regionId) { +// $params[] = "m.region = {$regionId}"; +// } if ($countyId) { $params[] = "m.county = {$countyId}"; } - if (!empty($park) && $park[0]) { - $params[] = "m.member_id IN ( - SELECT member_id - FROM member_category - WHERE category_id IN (" - .implode(',', $park)."))"; - } +// if (!empty($park) && $park[0]) { +// $params[] = "m.member_id IN ( +// SELECT member_id +// FROM member_category +// WHERE category_id IN (" +// .implode(',', $park)."))"; +// } // Limit to members who have the category assigned to them // that a user selected from the member type box. diff --git a/Toolkit/Members/formAjax.php b/Toolkit/Members/formAjax.php index e6d2e79..12babaa 100644 --- a/Toolkit/Members/formAjax.php +++ b/Toolkit/Members/formAjax.php @@ -10,7 +10,24 @@ try { if ($activityId) { $sql = " - SELECT "; + SELECT * + FROM members.county + WHERE region_id = :region + AND region_id IN ( + SELECT region + FROM members.member + WHERE active = true + AND member_id IN ( + SELECT member_id + FROM members.member_category + WHERE + ) + ) + ORDER BY county_name"; + $stmt = $dbh->prepare($sql); + $stmt->bindParam(':region', $regionId, PDO::PARAM_INT); + $stmt->execute(); + echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); } if ($regionId) { $sql = " diff --git a/Toolkit/Template/Page/Member.php b/Toolkit/Template/Page/Member.php index 4918b27..0ae6cd5 100644 --- a/Toolkit/Template/Page/Member.php +++ b/Toolkit/Template/Page/Member.php @@ -130,6 +130,10 @@ class Toolkit_Template_Page_Member extends Toolkit_Template_Page_Toolbox ); $sql = $searchQuery->getQuery($dbh); + $searchTerms = $searchQuery->getSearchTerms($dbh); + if ($searchTerms) { + $html .= $searchTerms; + } $searchList = new Toolkit_Members_SearchList( $dbh, 50,