where('`centerType` > 0'); // remove filter "Show on MADA" that added in config unset($this->_filter['madaMember']); // 0 - should match Collision, Service, and Collision & Service Centers if (empty($this->_filter['centerType'])) { unset($this->_filter['centerType']); } parent::_filter($select); $select = $this->_getSelect(); if (isset($this->_filter['centerType'])) { $select->where('centerType & ? = ?', $this->_filter['centerType'], Qs_Db::INT_TYPE); } if (!empty($this->_filter['manufacturerId'])) { $select->where("EXISTS(" . $this->_getSubSelectWhereManufacturer($this->_filter['manufacturerId']) . ")"); } if (!empty($this->_filter['zipCode'])) { if (!empty($this->_filter['radius'])) { $location = self::getLocation($this->_filter['zipCode']); $latitude = $this->_db->quote($location['lat'], 'float'); $longitude = $this->_db->quote($location['lng'], 'float'); $latitudeField = '`m_Dealer`.`latitude`'; $longitudeField = '`m_Dealer`.`longitude`'; // 3959 - radius of the earth in miles $where = " ROUND( 3959 * ACOS( COS(RADIANS({$latitude})) * COS(RADIANS({$latitudeField})) * COS(RADIANS({$longitudeField}) - RADIANS({$longitude})) + SIN(RADIANS({$latitude})) * SIN(RADIANS({$latitudeField})) ), 1 ) < " . $this->_db->quote($this->_filter['radius'], 'float') . " "; $select->where($where); } else { $select->where('`m_Dealer`.`zip` LIKE ?', '%' . $this->_filter['zipCode'] . '%'); } } $this->setSelect($select); return $this; } protected function _getSubSelectWhereManufacturer($manufacturerId) { $select = $this->_db->select(); $select->from($this->_getPair('m_Dealer2Manufacturer'), new Zend_Db_Expr('1')); $select->join( $this->_getPair('m_Manufacturer'), '`m_Manufacturer`.`id` = `m_Dealer2Manufacturer`.`manufacturerId`', array() ); $select->where('`m_Dealer2Manufacturer`.`manufacturerId` = ? ', $manufacturerId, Qs_Db::INT_TYPE); $select->where('`m_Dealer2Manufacturer`.`dealerId` = ' . $this->_quoteField('id')); return $select; } public static function getLocation($zip) { $location = null; $findResult = self::_getGeoCode()->findAddress($zip . ', USA'); if (!empty($findResult) && is_array($findResult)) { $findResult = reset($findResult); $location = Qs_Array::get($findResult, 'geometry[location]'); } return $location; } public function getMapOptions(array $list) { $options = array(); foreach ($list as $dealer) { if (null === $dealer['latitude'] || null === $dealer['longitude']) { continue; } /** @var $doc Qs_Doc */ $doc = Zend_Registry::get('doc'); $doc->assign('item', $dealer); $options['list'][$dealer['id']] = array( 'id' => $dealer['id'], 'title' => $dealer['title'], 'latitude' => $dealer['latitude'], 'longitude' => $dealer['longitude'], 'infoWindowHtml' => $doc->fetch('Dealer/info-window.tpl') ); } $filter = $this->getFilter(); $options['radius'] = (float)Qs_Array::get($filter, 'radius', self::DEFAULT_RADIUS); if (empty($options['list'])) { if (empty($filter['zipCode'])) { //show Minnesota/Minneapolis map $options['center'] = self::_getGeoCode()->findAddressLocation(self::DEFAULT_ADDRESS); } else { $options['center'] = App_Dealer_Obj::getLocation($filter['zipCode']); } $latitudes = array($options['center']['lat'], $options['center']['lat'] ); $longitudes = array($options['center']['lng'], $options['center']['lng']); } else { $latitudes = array_map('floatval', Qs_Array::fetchCol($options['list'], 'latitude')); $longitudes = array_map('floatval', Qs_Array::fetchCol($options['list'], 'longitude')); $maxLatitudes = max($latitudes); $maxLongitudes = max($longitudes); $minLatitudes = min($latitudes); $minLongitudes = min($longitudes); $options['center']['lat'] = ($maxLatitudes + $minLatitudes) / 2; $options['center']['lng'] = ($maxLongitudes + $minLongitudes) / 2; } if (empty($filter['zipCode'])) { $options['radius'] = null; } $options['bounds'] = array( 'southWest' => array('lat' => min($latitudes), 'lng' => min($longitudes)), 'northEast' => array('lat' => max($latitudes), 'lng' => max($longitudes)) ); $minDiff = self::MIN_DIFF; if ($options['bounds']['southWest']['lat'] - $options['bounds']['northEast']['lat'] < $minDiff) { $options['bounds']['southWest']['lat'] -= $minDiff / 2; $options['bounds']['northEast']['lat'] += $minDiff / 2; } if ($options['bounds']['southWest']['lng'] - $options['bounds']['northEast']['lng'] < $minDiff) { $options['bounds']['southWest']['lng'] -= $minDiff / 2; $options['bounds']['northEast']['lng'] += $minDiff / 2; } return $options; } }