+++ /dev/null
-<?php
-// vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker syntax=php:
-
-/**
- * Member Search Form Class
- *
- * PHP version 5
- *
- * @category MembersDB
- * @package Toolkit_Members
- * @author Jamie Kahgee <steve@gaslightmedia.com>
- * @copyright 2009 Jamie Kahgee
- * @license http://www.gaslightmedia.com Gaslightmedia
- * @version CVS: $Id: UserSearchForm.php,v 1.27 2010/07/14 23:31:14 jamie Exp $
- * @link http://demo.gaslightmedia.com
- */
-
-/**
- * Member Search Form
- *
- * @category MembersDB
- * @package Toolkit_Members
- * @author Jamie Kahgee <steve@gaslightmedia.com>
- * @copyright 2009 Jamie Kahgee
- * @license http://www.gaslightmedia.com Gaslightmedia
- * @link http://demo.gaslightmedia.com
- */
-class Toolkit_Members_UserSearchForm
- extends Toolkit_FormBuilder
-{
- // {{{ properties
-
-
- /**
- * Description for protected
- * @var array
- * @access protected
- */
- protected $memberCatsAssignedToPage = array();
-
- /**
- * Description for $memberRegiansAssignedToPage
- * @var array
- * @access protected
- */
- protected $memberRegionsAssignedToPage = array();
-
- /**
- * The Table name used to store the data of the member record in the database.
- *
- * @var string
- * @access public
- */
- public $tableName = 'member';
-
- /**
- * Array of data that holds the meta data info on the table
- *
- * Contains information on the type of fields in the database.
- * That way when we run our automated SQL queries with our PDO
- * we can properly bind data to our SQL queries. This will
- * allow for one more layer of protection against any sql
- * injection attempts.
- *
- * @var string
- * @access public
- */
- public $tableMetaData;
-
- /**
- * Flexy options used in the renderer
- *
- * @var array
- * @access protected
- */
- protected $flexyOptions;
-
- /**
- * The name of the template used to render the member info form
- *
- * @var string
- * @access protected
- */
- protected $formTemplate = 'memberSearchForm.tpl';
-
- /**
- * Form access from inside the template
- *
- * @var object
- * @access protected
- */
- protected $view;
-
- /**
- * registered rules for the form
- *
- * @var array
- * @access protected
- */
- protected $registeredRules = array();
-
- /**
- * catid to use in the form
- *
- * @var integer
- * @access private
- */
- private $_catid;
-
- // }}}
-
- // {{{ configureConstants()
-
- /**
- * Sets the constants for the form
- *
- * The member category select list must always default
- * to the -- Choose Category -- option
- *
- * @return void
- * @access public
- */
- public function configureConstants()
- {
- $c = array(
- 'catid' => $this->_catid,
- 'search' => 1,
- );
-
- $this->setupConstants($c);
- }
-
- // }}}
- // {{{ configureDefaults()
-
- /**
- * Sets the defaults for the an existing member
- *
- * Populates data for the main member form. Also grabs
- * data to populate the modules on the form if needed.
- *
- * @return array $defaults Returns the array of defaults
- * so children who call this function
- * can obtain a copy of these values.
- * @access public
- */
- public function configureDefaults()
- {
- $d = array();
-
- $this->setupDefaults($d);
-
- return $d;
- }
-
- // }}}
- // {{{ configureElements()
-
- /**
- * Setup the elements to use on the form.
- *
- * @param PDO $dbh Database handler
- * @param Config_Container $c Configuration object
- *
- * @return void
- * @access public
- */
- public function configureElements(PDO $dbh, Config_Container $c)
- {
- $e = array();
-
- // get reference to [listing type] section of config file
- $singularType = $c->getItem('section', 'listing type')
- ->getItem('directive', 'singular')
- ->getContent();
- $pluralType = $c->getItem('section', 'listing type')
- ->getItem('directive', 'plural')
- ->getContent();
- $hasRegions = $c->getItem('section', 'conf')
- ->getItem('directive', 'regions')
- ->getContent();
-
- $memberCategories = $this->getMemberCats($dbh);
- if (!isset($_GET['category_id'])) {
- $_GET['category_id'] = null;
- }
- $memberSubCategories
- = $this->getMemberSubCats(
- $dbh,
- $memberCategories,
- $_GET['category_id']
- );
- $memberAmenities = $this->getMemberAmenities($dbh);
-
- $regions = ($hasRegions) ? $this->getAvailableRegions($dbh) : array();
- $this->createAvailableMemberTypesFromRegions($dbh, $regions);
-
- // All Grouped Elements are created here.
- foreach ($memberAmenities as $k => $v) {
- $amenities[] = array(
- 'type' => 'checkbox',
- 'req' => false,
- 'name' => $v,
- 'display' => $k
- );
- }
-
- // All Elements are created here. This includes group element definitions.
- $e[] = array(
- 'type' => 'header',
- 'req' => false,
- 'name' => 'SearchForm_hdr'
- );
- $e[] = array(
- 'type' => 'hidden',
- 'req' => false,
- 'name' => 'catid'
- );
- $e[] = array(
- 'type' => 'hidden',
- 'req' => false,
- 'name' => 'search'
- );
- $e[] = array(
- 'type' => 'text',
- 'req' => false,
- 'name' => 'member_name',
- 'display' => "Name",
- 'opts' => array('class' => 'text')
- );
- if (isset($regions) && !empty($regions) && count($regions) > 1) {
- $e[] = array(
- 'type' => 'select',
- 'req' => false,
- 'name' => 'region_id',
- 'display' => 'Region',
- 'opts' => array('' => '-- Select --') + $regions,
- );
- } else if (isset($regions) && count($regions) == 1) {
-// $e[] = array(
-// 'type' => 'hidden',
-// 'req' => false,
-// 'name' => 'region_id',
-// 'display' => implode('', array_keys($regions))
-// );
- }
- if ( isset($memberCategories)
- && is_array($memberCategories)
- && !empty($memberCategories)
- ) {
- $e[] = array(
- 'type' => 'select',
- 'req' => false,
- 'name' => 'category_id',
- 'display' => "Category",
- 'opts' => array('' => '-- Select --') + $memberCategories
- );
- }
- if ( isset($memberSubCategories)
- && is_array($memberSubCategories)
- && !empty($memberSubCategories)
- ) {
- $e[] = array(
- 'type' => 'select',
- 'req' => false,
- 'name' => 'sub_category_id',
- 'display' => "Subcategory",
- 'opts' => array('' => '-- Select --') + $memberSubCategories
- );
- }
- // Only show do if we have amenities to show.
- if (!empty($memberAmenities)) {
- $e[] = array(
- 'type' => 'group',
- 'req' => false,
- 'name' => 'amenities',
- 'group' => $amenities,
- 'seperator' => '',
- 'appendName' => true
- );
- $e[] = array(
- 'type' => 'checkbox',
- 'req' => false,
- 'name' => 'search_all_amenity',
- 'opts' => 'Only show members who offer all selected amenities'
- );
- }
-
- $this->setupElements($e);
- }
-
- // }}}
- // {{{ configureFilters()
-
- /**
- * Setup the filters to apply to the elements before we are
- * handed the values submitted
- *
- * @return void
- * @access public
- */
- public function configureFilters()
- {
- $f = array();
-
- $f[] = array(
- 'element' => '__ALL__',
- 'filter' => 'trim'
- );
-
- $this->setupFilters($f);
- }
-
- // }}}
- // {{{ configureForm()
-
- /**
- * Helper method to configure the entire form
- *
- * @param PDO $dbh Database handler
- * @param Config_Container $c Configuration object
- *
- * @return void
- * @access public
- */
- public function configureForm(PDO $dbh, Config_Container $c)
- {
- $this->configureElements($dbh, $c);
- $this->configureRules();
- $this->configureFilters();
- $this->configureDefaults();
- $this->configureConstants();
- }
-
- // }}}
- // {{{ configureRules()
-
- /**
- * Sets up all the rules to be used when the form is validated.
- *
- * @return void
- * @access public
- */
- public function configureRules()
- {
- // No Rules
- $r = array();
- $this->setupRules($r);
- }
-
- // }}}
- // {{{ createAvailableMemberTypesFromRegions()
-
- /**
- * Get available categories from members assigned to regions
- *
- * @param PDO $dbh Database handler
- * @param array $regions Available regions to get members from
- *
- * @return void
- * @access protected
- */
- protected function createAvailableMemberTypesFromRegions(
- PDO $dbh,
- array $regions
- ) {
- try {
- $regionCats = array();
- $availRegions = array();
- $availCats = array();
- $mainCats = array();
- if (!empty($this->memberCatsAssignedToPage)) {
- foreach ($this->memberCatsAssignedToPage as $i) {
- $cats = Toolkit_Common::getHierarchicalTreeStructure(
- $dbh,
- 'category',
- 'category_id',
- 'parent_id',
- 'pos',
- $i,
- 0,
- false
- );
- if (is_array($cats) && !empty($cats)) {
- $availCats = array_merge($availCats, array_keys($cats));
- }
- }
- }
- if ($availCats && !empty($availCats)) {
- $availCats = implode(', ', $availCats);
- }
-
- if (!empty($this->memberRegionsAssignedToPage)) {
- $availRegions = implode(', ', $availRegions);
- }
- $pNameSql = "
- SELECT name
- FROM category
- WHERE parent_id = :cid";
- $getParentName = $dbh->prepare($pNameSql);
- $sql = "SELECT distinct c.category_id, c.parent_id,
- ( SELECT name FROM category WHERE category_id = c.parent_id) AS parent,
- c.name AS category
- FROM category c
- WHERE c.category_id IN (
- SELECT distinct c.category_id
- FROM category c, member_category mc, member m
- WHERE
- c.category_id = mc.category_id
- AND mc.member_id = m.member_id
- AND m.active";
- if ($regions) {
- $sql .= " AND (region = :rid OR region IS NULL) ";
- }
-
- $sql .= ")
- AND c.parent_id > 0 ";
- if ($availRegions) {
- $sql .= " AND region in ($availRegions) ";
- }
- if ($availCats) {
- $sql .= " AND c.category_id in ($availCats) ";
- }
- $sql .= " ORDER BY c.name";
- $stmt = $dbh->prepare($sql);
- if ($regions) {
- foreach ($regions as $i => $j) {
- if (is_numeric($i)) {
- $stmt->bindParam(':rid', $i, PDO::PARAM_INT);
- $stmt->execute();
- $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($row as $x) {
- $regionCats[$i][$x['parent_id']][$x['category_id']] = $x['category'];
- $mainCats[$x['parent_id']] = $x['parent'];
- }
- }
- }
- } else {
- $stmt->execute();
- $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($row as $x) {
- $regionCats[$i][$x['parent_id']][$x['category_id']] = $x['category'];
- $mainCats[$x['parent_id']] = $x['parent'];
- }
- }
-
- asort($mainCats);
- $this->regionCategories = $regionCats;
- $this->mainCats = $mainCats;
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- }
-
- // }}}
-
- // {{{ getAvailableRegions()
-
- /**
- * Returns all available regions that derive from members assigned to
- * this page
- *
- * @param PDO $dbh Database handler
- *
- * @return array regions available to this page
- * @access protected
- */
- protected function getAvailableRegions(PDO $dbh)
- {
- // Make sure we have all sub-cats nos also
-
-
- try {
- $sql = "SELECT DISTINCT c.category_id
- FROM category c, member_category mc, member m
- WHERE c.category_id = mc.category_id
- AND m.member_id = mc.member_id
- AND m.active ";
- if (is_array($this->memberCatsAssignedToPage)
- && !empty($this->memberCatsAssignedToPage)) {
- $mcats = implode(',', $this->memberCatsAssignedToPage);
- $sql .= " AND c.parent_id IN ($mcats)";
- }
- $mcstmt = $this->dbh->prepare($sql);
- $mcstmt->execute();
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- while ($row = $mcstmt->fetch()) {
- $this->memberCatsAssignedToPage[] = $row['category_id'];
- }
-
- $regions = array();
- $memCats = array();
- try {
-
- $sql = "
- SELECT *
- FROM region
- WHERE region_id in (
- SELECT distinct(region)
- FROM member
- WHERE active = 't'
- AND region is not null";
- if (!empty($this->memberCatsAssignedToPage)) {
- $sql .= " AND member_id in (SELECT distinct(member_id)
- FROM member_category
- WHERE category_id in (".implode(',', $this->memberCatsAssignedToPage).")
- )";
- }
-
- if (!empty($this->memberRegionsAssignedToPage)) {
- $sql .= " AND region_id in (".implode(',', $this->memberRegionsAssignedToPage).") ";
- }
-
- $sql .= " )
- ORDER BY region_name";
-
- $stmt = $dbh->prepare($sql);
- $stmt->execute();
- while ($row = $stmt->fetch()) {
- $regions[$row['region_id']] = $row['region_name'];
- }
-
- asort($regions);
-
- return $regions;
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- }
-
- // }}}
- // {{{ getMemberAmenities()
-
- /**
- * Get all the amenities for the member
- *
- * @param PDO $dbh Database handler
- *
- * @return array member amenities
- * @access protected
- */
- protected function getMemberAmenities(PDO $dbh)
- {
- try {
- $amenities = array();
- $sql = "
- SELECT accommodations
- FROM category
- WHERE category_id = :cid";
- $isAccommodationCategory = $dbh->prepare($sql);
- $sql = "
- SELECT a.*
- FROM amenity a
- NATURAL JOIN member_amenity ma
- NATURAL JOIN member_category mc
- NATURAL JOIN member m
- WHERE m.new_member <> CAST(1 AS BOOLEAN)
- AND m.active = CAST(1 AS BOOLEAN)
- AND a.display_form = CAST(1 AS BOOLEAN)
- AND (mc.category_id = :cid
- OR mc.category_id in (
- SELECT category_id
- FROM category
- WHERE parent_id = :cid))
- ORDER BY amenity_name";
-
- $stmt = $dbh->prepare($sql);
- foreach ($this->memberCatsAssignedToPage as $id) {
- $isAccommodationCategory->bindParam(':cid', $id, PDO::PARAM_INT);
- $isAccommodationCategory->execute();
- if (!$isAccommodationCategory->fetchColumn()) {
- continue;
- }
- $stmt->bindParam(':cid', $id, PDO::PARAM_INT);
- $stmt->execute();
- while ($row = $stmt->fetch()) {
- $amenities[$row['amenity_name']] = $row['amenity_id'];
- }
- }
- return $amenities;
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- }
-
- // }}}
- // {{{ getMemberCats()
-
- /**
- * Get all the main categories available from the categories that
- * are assigned to the page.
- *
- * If a main category is assigned to a page, then add to list.
- *
- * If a sub category is assgined to page, then add parent (main) category
- * to the list.
- *
- * @param PDO $dbh Database handler
- *
- * @return array Main categories available for page
- * @access protected
- */
- protected function getMemberCats(PDO $dbh)
- {
- try {
-
- $sql = "
- SELECT *
- FROM category
- WHERE category_id = :cid";
-
- $stmt = $dbh->prepare($sql);
- $categories = array();
- foreach ($this->memberCatsAssignedToPage as $id) {
- $stmt->bindParam(':cid', $id, PDO::PARAM_INT);
- $stmt->execute();
- if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- if ($row['parent_id'] == '0') {
- $categories[$row['category_id']] = $row['name'];
- } else {
- $stmt->bindParam(':cid', $row['parent_id'], PDO::PARAM_INT);
- $stmt->execute();
- if ($parRow = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $categories[$parRow['category_id']] = $parRow['name'];
- }
- }
- }
- }
- asort($categories);
-
- return $categories;
-
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- }
-
- // }}}
- // {{{ getMemberSubCats()
-
- /**
- * Get all the sub categories available from the main categories that
- * are available to the page.
- *
- * @param PDO $dbh Database handler
- * @param array $categories Main level categories to get sub levels for
- * @param integer $parentIdKey (optional) Main level that was searched on
- *
- * @return array Sub categories available for page
- * @access protected
- */
- protected function getMemberSubCats(
- PDO $dbh,
- array $categories,
- $parentIdKey = null
- ) {
- if (empty($categories)) {
- return array();
- }
-
- try {
- // Get the category information
- $sql = "
- SELECT *
- FROM category
- WHERE category_id = :cid";
-
- $catStmt = $dbh->prepare($sql);
-
- // Get the sub-category information
- $sql = "
- SELECT c.*
- FROM category c, member_category mc, member m
- WHERE c.parent_id = :parent_id
- AND c.category_id = mc.category_id
- AND mc.member_id = m.member_id
- AND m.active
- ";
- $subCatStmt = $dbh->prepare($sql);
- $subCategories = array();
-
- while (list($id,) = each($categories)) {
- $catStmt->bindParam(':cid', $id, PDO::PARAM_INT);
- $catStmt->execute();
- $category = $catStmt->fetch();
- try {
- $subCatStmt->bindParam(
- ':parent_id',
- $category['category_id'],
- PDO::PARAM_INT
- );
- $subCatStmt->execute();
- if ($subCatData = $subCatStmt->fetchAll()) {
- foreach ($subCatData as $key => $val) {
- $catStmt->bindParam(
- ':cid',
- $val['category_id'],
- PDO::PARAM_INT
- );
- $catStmt->execute();
- $subCat = $catStmt->fetch();
- if (in_array($subCat['category_id'], $this->memberCatsAssignedToPage)) {
- // Subcat assgined to page, only assign
- // child of parent cat that is assigned
- // to page.
- $subCategories[$subCat['parent_id']][$subCat['category_id']] = $subCat['name'];
- } elseif (in_array($subCat['parent_id'], $this->memberCatsAssignedToPage)) {
- // Parent category assigned to page, add all
- // children found
- $subCategories[$subCat['parent_id']][$subCat['category_id']] = $subCat['name'];
- }
- }
- }
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- }
-
- if ( is_numeric($parentIdKey)
- && array_key_exists($parentIdKey, $subCategories)
- ) {
- return $subCategories[$parentIdKey];
- } else {
- $subCategories
- = Toolkit_Common::arrayFlatten($subCategories, 2);
- asort($subCategories);
- return $subCategories;
- }
- } catch (PDOException $e) {
- return Toolkit_Common::handleError($e);
- }
- }
-
- // }}}
-
- // {{{ toHtml()
-
- /**
- * Renders the form
- *
- * sets the page the form should be redirected to instead of coming back
- * around to itself.
- *
- * @param array $flexyOpts Flexy template engine options
- * @param Toolkit_Toolbox_PageGatewayAbstract $pageGateway Page gateway
- *
- * @return string The rendered form
- * @access public
- */
- public function toHtml(
- array $flexyOpts,
- Toolkit_Toolbox_PageGatewayAbstract $pageGateway
- ) {
- $GLOBALS['bottomScripts'][]
- = BASE_URL . 'Toolkit/Members/libjs/business-search.js';
-
- $GLOBALS['styleSheets'][] = BASE_URL . 'Toolkit/Members/css/member.css';
-
- // We need to validate (and freeze if needed)
- // before we render the form. That way the
- // template knows about any errors on the form.
- $this->validated = $this->validate();
-
- $page = $pageGateway->find($this->_catid);
-
- // ProcessData handles settingup the lat/lon coordinates if they were not entered
- // into the form. these values ar calculated and then inserted into the forms
- // element values. So we need to process the data first and then render the form.
- $this->setupRenderers($flexyOpts);
-
- return $this->template->bufferedOutputObject($this->view);
- }
-
- // }}}
-
- // {{{ setCatId()
-
- /**
- * set the catid to use in the form
- *
- * @param integer $catid page catid
- *
- * @return PEAR Error return a PEAR error if an invalid catid is passed in
- * @access public
- */
- public function setCatId($catid)
- {
- if (ctype_digit((string)$catid)) {
- $this->_catid = $catid;
- } else {
- return PEAR::raiseError('Invalid catid');
- }
- }
-
- // }}}
- // {{{ setPageMemberCategories()
-
- /**
- * Relate toolbox categories with main member categories
- *
- * @param Toolkit_Toolbox_GatewayAbstract $gateway Page gateway to get categories from
- *
- * @return array toolbox -> category relations
- * @access public
- */
- public function setPageMemberCategories(
- Toolkit_Toolbox_GatewayAbstract $gateway
- ) {
- $page = $gateway->find($this->_catid);
-
- $this->memberCatsAssignedToPage = $page['member_categories'];
-
- // If there's no member category filter selected, use all with members
- if (count($this->memberCatsAssignedToPage) == 0) {
- $sql = "SELECT DISTINCT c.category_id
- FROM category c
- WHERE (
- c.category_id IN (
- SELECT distinct c.category_id
- FROM category c, member_category mc, member m
- WHERE c.parent_id = 0
- AND c.category_id = mc.category_id
- AND mc.member_id = m.member_id
- AND m.active
- )
- )
- OR (
- c.category_id IN (
- SELECT distinct sc.parent_id
- FROM category sc, member_category smc, member sm
- WHERE sc.parent_id > 0
- AND sc.parent_id != sc.category_id
- AND sc.category_id = smc.category_id
- AND smc.member_id = sm.member_id
- AND sm.active
- )
- )
- ;";
- $mcstmt = $this->dbh->prepare($sql);
- $mcstmt->execute();
- while ($row = $mcstmt->fetch()) {
- $this->memberCatsAssignedToPage[] = $row['category_id'];
- }
- $page['member_categories'] = $this->memberCatsAssignedToPage;
-
- // Otherwise add any subgategories for the selected cats with active members
- } else {
-
- $sql = "SELECT DISTINCT c.category_id
- FROM category c, member_category mc, member m
- WHERE c.category_id = mc.category_id
- AND m.member_id = mc.member_id
- AND m.active";
- if (is_array($this->memberCatsAssignedToPage)
- && !empty($this->memberCatsAssignedToPage)) {
- $mcats = implode(',', $this->memberCatsAssignedToPage);
- $sql .= " AND c.parent_id IN ($mcats)";
- }
- $mcstmt = $this->dbh->prepare($sql);
- $mcstmt->execute();
- while ($row = $mcstmt->fetch()) {
- $this->memberCatsAssignedToPage[] = $row['category_id'];
- }
- $page['member_categories'] = $this->memberCatsAssignedToPage;
- }
-
- return $page['member_categories'];
- }
-
- // }}}
- // {{{ setPageMemberRegions()
-
- /**
- * Relate toolbox member cities
- *
- * @param Toolkit_Toolbox_GatewayAbstract $gateway Page gateway to get categories from
- *
- * @return array toolbox -> category relations
- * @access public
- */
- public function setPageMemberRegions(
- Toolkit_Toolbox_GatewayAbstract $gateway
- ) {
- $page = $gateway->find($this->_catid);
- $this->memberRegionsAssignedToPage = $page['member_regions'];
-
- // If no regions specified then use all regions
- if (count($this->memberRegionsAssignedToPage) == 0) {
- $sql .= "
- SELECT DISTINCT region
- FROM member
- WHERE active
- AND region is not null;";
- $stmt = $this->dbh->prepare($sql);
- $stmt->execute();
- while ($row = $stmt->fetch()) {
- $this->memberRegionsAssignedToPage[] = $row['region'];
- }
- $page['member_regions'] = $this->memberRegionsAssignedToPage;
- }
-
- return $page['member_regions'];
- }
-
- // }}}
- // {{{ setupRenderers()
-
- /**
- * Sets up the Flexy template
- *
- * @param array $flexyOpts Flexy template engine options
- *
- * @return void
- * @access protected
- */
- protected function setupRenderers(array $flexyOpts)
- {
- $renderer = new HTML_QuickForm_Renderer_Object(true);
-
- $this->accept($renderer);
-
- $this->template = new HTML_Template_Flexy($flexyOpts);
-
- // Make the view a copy of the $this object
- // That way we have access to call functions in
- // this class from within the template.
- $this->view = $this;
- $this->view->form = $renderer->toObject();
- $this->template->compile($this->formTemplate);
- }
-
- // }}}
-}
-?>