3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Contact_Form_Search_Custom_Proximity
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_latitude = NULL;
36 protected $_longitude = NULL;
37 protected $_distance = NULL;
38 protected $_aclFrom = NULL;
39 protected $_aclWhere = NULL;
44 * @param array $formValues
48 public function __construct(&$formValues) {
49 parent
::__construct($formValues);
51 // unset search profile and other search params if set
52 unset($this->_formValues
['uf_group_id']);
53 unset($this->_formValues
['component_mode']);
54 unset($this->_formValues
['operator']);
56 if (!empty($this->_formValues
)) {
57 // add the country and state
58 if (!empty($this->_formValues
['country_id'])) {
59 $this->_formValues
['country'] = CRM_Core_PseudoConstant
::country($this->_formValues
['country_id']);
62 if (!empty($this->_formValues
['state_province_id'])) {
63 $this->_formValues
['state_province'] = CRM_Core_PseudoConstant
::stateProvince($this->_formValues
['state_province_id']);
66 // use the address to get the latitude and longitude
67 CRM_Utils_Geocode_Google
::format($this->_formValues
);
69 if (!is_numeric(CRM_Utils_Array
::value('geo_code_1', $this->_formValues
)) ||
70 !is_numeric(CRM_Utils_Array
::value('geo_code_2', $this->_formValues
)) ||
71 !isset($this->_formValues
['distance'])
73 CRM_Core_Error
::fatal(ts('Could not geocode input'));
76 $this->_latitude
= $this->_formValues
['geo_code_1'];
77 $this->_longitude
= $this->_formValues
['geo_code_2'];
79 if ($this->_formValues
['prox_distance_unit'] == "miles") {
80 $conversionFactor = 1609.344;
83 $conversionFactor = 1000;
85 $this->_distance
= $this->_formValues
['distance'] * $conversionFactor;
87 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
89 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
91 $this->_columns
= array(
92 ts('Name') => 'sort_name',
93 ts('Street Address') => 'street_address',
95 ts('Postal Code') => 'postal_code',
96 ts('State') => 'state_province',
97 ts('Country') => 'country',
102 * @param CRM_Core_Form $form
104 public function buildForm(&$form) {
106 $config = CRM_Core_Config
::singleton();
107 $countryDefault = $config->defaultContactCountry
;
109 $form->add('text', 'distance', ts('Distance'), NULL, TRUE);
111 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
112 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, TRUE);
130 if ($countryDefault) {
131 $defaults['country_id'] = $countryDefault;
133 $form->addChainSelect('state_province_id');
135 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::country();
136 $form->add('select', 'country_id', ts('Country'), $country, TRUE, array('class' => 'crm-select2'));
138 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant
::nestedGroup();
139 $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge'));
141 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
142 $form->addElement('select', 'tag', ts('Tag'), $tag, array('class' => 'crm-select2 huge'));
145 * You can define a custom title for the search form
147 $this->setTitle('Proximity Search');
150 * if you are using the standard template, this array tells the template what elements
151 * are part of the search criteria
153 $form->assign('elements', array(
155 'prox_distance_unit',
168 * @param int $rowcount
170 * @param bool $includeContactIDs
171 * @param bool $justIDs
176 $offset = 0, $rowcount = 0, $sort = NULL,
177 $includeContactIDs = FALSE, $justIDs = FALSE
180 $selectClause = "contact_a.id as contact_id";
184 contact_a.id as contact_id ,
185 contact_a.sort_name as sort_name ,
186 address.street_address as street_address,
187 address.city as city ,
188 address.postal_code as postal_code ,
189 state_province.name as state_province,
190 country.name as country
194 return $this->sql($selectClause,
195 $offset, $rowcount, $sort,
196 $includeContactIDs, NULL
203 public function from() {
204 $this->buildACLClause('contact_a');
206 FROM civicrm_contact contact_a
207 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
208 address.is_primary = 1 )
209 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
210 LEFT JOIN civicrm_country country ON country.id = address.country_id {$this->_aclFrom}
213 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
216 LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact' AND contact_a.id = t.entity_id)
221 LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id AND cgc.status = 'Added')
229 * @param bool $includeContactIDs
233 public function where($includeContactIDs = FALSE) {
237 $where = CRM_Contact_BAO_ProximityQuery
::where($this->_latitude
,
245 AND t.tag_id = {$this->_tag}
250 AND cgc.group_id = {$this->_group}
254 $where .= " AND contact_a.is_deleted != 1 ";
256 if ($this->_aclWhere
) {
257 $where .= " AND {$this->_aclWhere} ";
260 return $this->whereClause($where, $params);
266 public function templateFile() {
267 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
273 public function setDefaultValues() {
274 $config = CRM_Core_Config
::singleton();
275 $countryDefault = $config->defaultContactCountry
;
276 $stateprovinceDefault = $config->defaultContactStateProvince
;
279 if ($countryDefault) {
280 if ($countryDefault == '1228' ||
$countryDefault == '1226') {
281 $defaults['prox_distance_unit'] = 'miles';
284 $defaults['prox_distance_unit'] = 'km';
286 $defaults['country_id'] = $countryDefault;
287 if ($stateprovinceDefault) {
288 $defaults['state_province_id'] = $stateprovinceDefault;
298 public function alterRow(&$row) {
304 public function setTitle($title) {
306 CRM_Utils_System
::setTitle($title);
309 CRM_Utils_System
::setTitle(ts('Search'));
314 * @param string $tableAlias
316 public function buildACLClause($tableAlias = 'contact') {
317 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);