3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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_Core_BAO_Address
::addGeocoderData($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 throw new CRM_Core_Exception(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 $form->add('text', 'geo_code_1', ts('Latitude'));
139 $form->add('text', 'geo_code_2', ts('Longitude'));
141 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant
::nestedGroup();
142 $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge'));
144 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
145 $form->addElement('select', 'tag', ts('Tag'), $tag, array('class' => 'crm-select2 huge'));
148 * You can define a custom title for the search form
150 $this->setTitle('Proximity Search');
153 * if you are using the standard template, this array tells the template what elements
154 * are part of the search criteria
156 $form->assign('elements', array(
158 'prox_distance_unit',
171 * @param int $rowcount
173 * @param bool $includeContactIDs
174 * @param bool $justIDs
179 $offset = 0, $rowcount = 0, $sort = NULL,
180 $includeContactIDs = FALSE, $justIDs = FALSE
183 $selectClause = "contact_a.id as contact_id";
187 contact_a.id as contact_id ,
188 contact_a.sort_name as sort_name ,
189 address.street_address as street_address,
190 address.city as city ,
191 address.postal_code as postal_code ,
192 state_province.name as state_province,
193 country.name as country
197 return $this->sql($selectClause,
198 $offset, $rowcount, $sort,
199 $includeContactIDs, NULL
206 public function from() {
207 $this->buildACLClause('contact_a');
209 FROM civicrm_contact contact_a
210 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
211 address.is_primary = 1 )
212 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
213 LEFT JOIN civicrm_country country ON country.id = address.country_id {$this->_aclFrom}
216 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
219 LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact' AND contact_a.id = t.entity_id)
224 LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id AND cgc.status = 'Added')
232 * @param bool $includeContactIDs
236 public function where($includeContactIDs = FALSE) {
240 $where = CRM_Contact_BAO_ProximityQuery
::where($this->_latitude
,
248 AND t.tag_id = {$this->_tag}
253 AND cgc.group_id = {$this->_group}
257 $where .= " AND contact_a.is_deleted != 1 ";
259 if ($this->_aclWhere
) {
260 $where .= " AND {$this->_aclWhere} ";
263 return $this->whereClause($where, $params);
269 public function templateFile() {
270 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
276 public function setDefaultValues() {
277 if (!empty($this->_formValues
)) {
278 return $this->_formValues
;
280 $config = CRM_Core_Config
::singleton();
281 $countryDefault = $config->defaultContactCountry
;
282 $stateprovinceDefault = $config->defaultContactStateProvince
;
285 if ($countryDefault) {
286 if ($countryDefault == '1228' ||
$countryDefault == '1226') {
287 $defaults['prox_distance_unit'] = 'miles';
290 $defaults['prox_distance_unit'] = 'km';
292 $defaults['country_id'] = $countryDefault;
293 if ($stateprovinceDefault) {
294 $defaults['state_province_id'] = $stateprovinceDefault;
304 public function alterRow(&$row) {
310 public function setTitle($title) {
312 CRM_Utils_System
::setTitle($title);
315 CRM_Utils_System
::setTitle(ts('Search'));
320 * @param string $tableAlias
322 public function buildACLClause($tableAlias = 'contact') {
323 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);