Merge pull request #11542 from eileenmcnaughton/prox
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Proximity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33 class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
34
35 protected $_latitude = NULL;
36 protected $_longitude = NULL;
37 protected $_distance = NULL;
38 protected $_aclFrom = NULL;
39 protected $_aclWhere = NULL;
40
41 /**
42 * Class constructor.
43 *
44 * @param array $formValues
45 *
46 * @throws Exception
47 */
48 public function __construct(&$formValues) {
49 parent::__construct($formValues);
50
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']);
55
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']);
60 }
61
62 if (!empty($this->_formValues['state_province_id'])) {
63 $this->_formValues['state_province'] = CRM_Core_PseudoConstant::stateProvince($this->_formValues['state_province_id']);
64 }
65
66 // use the address to get the latitude and longitude
67 CRM_Core_BAO_Address::addGeocoderData($this->_formValues);
68
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'])
72 ) {
73 throw new CRM_Core_Exception(ts('Could not geocode input'));
74 }
75
76 $this->_latitude = $this->_formValues['geo_code_1'];
77 $this->_longitude = $this->_formValues['geo_code_2'];
78
79 if ($this->_formValues['prox_distance_unit'] == "miles") {
80 $conversionFactor = 1609.344;
81 }
82 else {
83 $conversionFactor = 1000;
84 }
85 $this->_distance = $this->_formValues['distance'] * $conversionFactor;
86 }
87 $this->_group = CRM_Utils_Array::value('group', $this->_formValues);
88
89 $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues);
90
91 $this->_columns = array(
92 ts('Name') => 'sort_name',
93 ts('Street Address') => 'street_address',
94 ts('City') => 'city',
95 ts('Postal Code') => 'postal_code',
96 ts('State') => 'state_province',
97 ts('Country') => 'country',
98 );
99 }
100
101 /**
102 * @param CRM_Core_Form $form
103 */
104 public function buildForm(&$form) {
105
106 $config = CRM_Core_Config::singleton();
107 $countryDefault = $config->defaultContactCountry;
108
109 $form->add('text', 'distance', ts('Distance'), NULL, TRUE);
110
111 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
112 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, TRUE);
113
114 $form->add('text',
115 'street_address',
116 ts('Street Address')
117 );
118
119 $form->add('text',
120 'city',
121 ts('City')
122 );
123
124 $form->add('text',
125 'postal_code',
126 ts('Postal Code')
127 );
128
129 $defaults = array();
130 if ($countryDefault) {
131 $defaults['country_id'] = $countryDefault;
132 }
133 $form->addChainSelect('state_province_id');
134
135 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country();
136 $form->add('select', 'country_id', ts('Country'), $country, TRUE, array('class' => 'crm-select2'));
137
138 $form->add('text', 'geo_code_1', ts('Latitude'));
139 $form->add('text', 'geo_code_2', ts('Longitude'));
140
141 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup();
142 $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge'));
143
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'));
146
147 /**
148 * You can define a custom title for the search form
149 */
150 $this->setTitle('Proximity Search');
151
152 /**
153 * if you are using the standard template, this array tells the template what elements
154 * are part of the search criteria
155 */
156 $form->assign('elements', array(
157 'distance',
158 'prox_distance_unit',
159 'street_address',
160 'city',
161 'postal_code',
162 'country_id',
163 'state_province_id',
164 'group',
165 'tag',
166 ));
167 }
168
169 /**
170 * @param int $offset
171 * @param int $rowcount
172 * @param null $sort
173 * @param bool $includeContactIDs
174 * @param bool $justIDs
175 *
176 * @return string
177 */
178 public function all(
179 $offset = 0, $rowcount = 0, $sort = NULL,
180 $includeContactIDs = FALSE, $justIDs = FALSE
181 ) {
182 if ($justIDs) {
183 $selectClause = "contact_a.id as contact_id";
184 }
185 else {
186 $selectClause = "
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
194 ";
195 }
196
197 return $this->sql($selectClause,
198 $offset, $rowcount, $sort,
199 $includeContactIDs, NULL
200 );
201 }
202
203 /**
204 * @return string
205 */
206 public function from() {
207 $this->buildACLClause('contact_a');
208 $f = "
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}
214 ";
215
216 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
217 if ($this->_tag) {
218 $f .= "
219 LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact' AND contact_a.id = t.entity_id)
220 ";
221 }
222 if ($this->_group) {
223 $f .= "
224 LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id AND cgc.status = 'Added')
225 ";
226 }
227
228 return $f;
229 }
230
231 /**
232 * @param bool $includeContactIDs
233 *
234 * @return string
235 */
236 public function where($includeContactIDs = FALSE) {
237 $params = array();
238 $clause = array();
239
240 $where = CRM_Contact_BAO_ProximityQuery::where($this->_latitude,
241 $this->_longitude,
242 $this->_distance,
243 'address'
244 );
245
246 if ($this->_tag) {
247 $where .= "
248 AND t.tag_id = {$this->_tag}
249 ";
250 }
251 if ($this->_group) {
252 $where .= "
253 AND cgc.group_id = {$this->_group}
254 ";
255 }
256
257 $where .= " AND contact_a.is_deleted != 1 ";
258
259 if ($this->_aclWhere) {
260 $where .= " AND {$this->_aclWhere} ";
261 }
262
263 return $this->whereClause($where, $params);
264 }
265
266 /**
267 * @return string
268 */
269 public function templateFile() {
270 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
271 }
272
273 /**
274 * @return array|null
275 */
276 public function setDefaultValues() {
277 if (!empty($this->_formValues)) {
278 return $this->_formValues;
279 }
280 $config = CRM_Core_Config::singleton();
281 $countryDefault = $config->defaultContactCountry;
282 $stateprovinceDefault = $config->defaultContactStateProvince;
283 $defaults = array();
284
285 if ($countryDefault) {
286 if ($countryDefault == '1228' || $countryDefault == '1226') {
287 $defaults['prox_distance_unit'] = 'miles';
288 }
289 else {
290 $defaults['prox_distance_unit'] = 'km';
291 }
292 $defaults['country_id'] = $countryDefault;
293 if ($stateprovinceDefault) {
294 $defaults['state_province_id'] = $stateprovinceDefault;
295 }
296 return $defaults;
297 }
298 return NULL;
299 }
300
301 /**
302 * @param $row
303 */
304 public function alterRow(&$row) {
305 }
306
307 /**
308 * @param $title
309 */
310 public function setTitle($title) {
311 if ($title) {
312 CRM_Utils_System::setTitle($title);
313 }
314 else {
315 CRM_Utils_System::setTitle(ts('Search'));
316 }
317 }
318
319 /**
320 * @param string $tableAlias
321 */
322 public function buildACLClause($tableAlias = 'contact') {
323 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
324 }
325
326 }