3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
30 * This search now functions as a subset of advanced search since at some point it
31 * was added to advanced search.
34 * @copyright CiviCRM LLC (c) 2004-2018
36 class CRM_Contact_Form_Search_Custom_Proximity
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
38 protected $_latitude = NULL;
39 protected $_longitude = NULL;
40 protected $_distance = NULL;
41 protected $_aclFrom = NULL;
42 protected $_aclWhere = NULL;
47 * @param array $formValues
51 public function __construct(&$formValues) {
52 parent
::__construct($formValues);
54 // unset search profile and other search params if set
55 unset($this->_formValues
['uf_group_id']);
56 unset($this->_formValues
['component_mode']);
57 unset($this->_formValues
['operator']);
59 if (!empty($this->_formValues
)) {
60 // add the country and state
61 self
::addGeocodingData($this->_formValues
);
62 $this->_latitude
= $this->_formValues
['geo_code_1'];
63 $this->_longitude
= $this->_formValues
['geo_code_2'];
65 if ($this->_formValues
['prox_distance_unit'] == "miles") {
66 $conversionFactor = 1609.344;
69 $conversionFactor = 1000;
71 $this->_distance
= $this->_formValues
['distance'] * $conversionFactor;
73 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
75 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
77 $this->_columns
= array(
78 ts('Name') => 'sort_name',
79 ts('Street Address') => 'street_address',
81 ts('Postal Code') => 'postal_code',
82 ts('State') => 'state_province',
83 ts('Country') => 'country',
88 * Get the query object for this selector.
90 * @return CRM_Contact_BAO_Query
92 public function getQueryObj() {
97 * @param CRM_Core_Form $form
99 public function buildForm(&$form) {
101 $config = CRM_Core_Config
::singleton();
102 $countryDefault = $config->defaultContactCountry
;
104 $form->add('text', 'distance', ts('Distance'), NULL, TRUE);
106 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
107 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, TRUE);
125 if ($countryDefault) {
126 $defaults['country_id'] = $countryDefault;
128 $form->addChainSelect('state_province_id');
130 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::country();
131 $form->add('select', 'country_id', ts('Country'), $country, TRUE, array('class' => 'crm-select2'));
133 $form->add('text', 'geo_code_1', ts('Latitude'));
134 $form->add('text', 'geo_code_2', ts('Longitude'));
136 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant
::nestedGroup();
137 $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge'));
139 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
140 $form->addElement('select', 'tag', ts('Tag'), $tag, array('class' => 'crm-select2 huge'));
143 * You can define a custom title for the search form
145 $this->setTitle('Proximity Search');
148 * if you are using the standard template, this array tells the template what elements
149 * are part of the search criteria
151 $form->assign('elements', array(
153 'prox_distance_unit',
166 * @param int $rowcount
168 * @param bool $includeContactIDs
169 * @param bool $justIDs
174 $offset = 0, $rowcount = 0, $sort = NULL,
175 $includeContactIDs = FALSE, $justIDs = FALSE
177 $selectClause = $justIDs ?
"contact_a.id as contact_id" : NULL;
179 return $this->sql($selectClause,
180 $offset, $rowcount, $sort,
181 $includeContactIDs, NULL
186 * Override sql() function to use the Query object rather than generating on the form.
188 * @param string $selectClause
190 * @param int $rowcount
192 * @param bool $includeContactIDs
193 * @param null $groupBy
202 $includeContactIDs = FALSE,
206 $isCountOnly = FALSE;
207 if ($selectClause === 'count(distinct contact_a.id) as total') {
212 ['prox_distance_unit', '=', $this->_formValues
['prox_distance_unit'], 0, 0],
213 ['prox_distance', '=', $this->_formValues
['distance'], 0, 0],
214 ['prox_geo_code_1', '=', $this->_formValues
['geo_code_1'], 0, 0],
215 ['prox_geo_code_2', '=', $this->_formValues
['geo_code_2'], 0, 0],
217 if (!empty($this->_formValues
['group'])) {
218 $searchParams[] = ['group', '=', ['IN', (array) $this->_formValues
['group']][1], 0, 0];
220 if (!empty($this->_formValues
['tag'])) {
221 $searchParams[] = ['contact_tags', '=', ['IN', (array) $this->_formValues
['tag']][1], 0, 0];
224 $display = array_fill_keys(['city', 'state_province', 'country', 'postal_code', 'street_address', 'display_name', 'sort_name'], 1);
225 if ($selectClause === 'contact_a.id as contact_id') {
226 // Not sure when this would happen but calling all with 'justIDs' gets us here.
227 $display = ['contact_id' => 1];
230 $this->_query
= new CRM_Contact_BAO_Query($searchParams, $display);
231 return $this->_query
->searchQuery(
247 public function from() {
252 * @param bool $includeContactIDs
256 public function where($includeContactIDs = FALSE) {
264 public function templateFile() {
265 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
271 public function setDefaultValues() {
272 if (!empty($this->_formValues
)) {
273 return $this->_formValues
;
275 $config = CRM_Core_Config
::singleton();
276 $countryDefault = $config->defaultContactCountry
;
277 $stateprovinceDefault = $config->defaultContactStateProvince
;
280 if ($countryDefault) {
281 if ($countryDefault == '1228' ||
$countryDefault == '1226') {
282 $defaults['prox_distance_unit'] = 'miles';
285 $defaults['prox_distance_unit'] = 'km';
287 $defaults['country_id'] = $countryDefault;
288 if ($stateprovinceDefault) {
289 $defaults['state_province_id'] = $stateprovinceDefault;
299 public function alterRow(&$row) {
305 public function setTitle($title) {
307 CRM_Utils_System
::setTitle($title);
310 CRM_Utils_System
::setTitle(ts('Search'));
315 * Validate form input.
317 * @param array $fields
318 * @param array $files
319 * @param CRM_Core_Form $self
322 * Input errors from the form.
324 public function formRule($fields, $files, $self) {
325 $this->addGeocodingData($fields);
327 if (!is_numeric(CRM_Utils_Array
::value('geo_code_1', $fields)) ||
328 !is_numeric(CRM_Utils_Array
::value('geo_code_2', $fields)) ||
329 !isset($fields['distance'])
331 $errorMessage = ts('Could not determine co-ordinates for provided data');
332 return array_fill_keys(['street_address', 'city', 'postal_code', 'country_id', 'state_province_id'], $errorMessage);
338 * Add the geocoding data to the fields supplied.
340 * @param array $fields
342 protected function addGeocodingData(&$fields) {
343 if (!empty($fields['country_id'])) {
344 $fields['country'] = CRM_Core_PseudoConstant
::country($fields['country_id']);
347 if (!empty($fields['state_province_id'])) {
348 $fields['state_province'] = CRM_Core_PseudoConstant
::stateProvince($fields['state_province_id']);
350 CRM_Core_BAO_Address
::addGeocoderData($fields);