Ian province abbreviation patch - issue 724
[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-2015 |
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-2015
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_Utils_Geocode_Google::format($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 CRM_Core_Error::fatal(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 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup();
139 $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge'));
140
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'));
143
144 /**
145 * You can define a custom title for the search form
146 */
147 $this->setTitle('Proximity Search');
148
149 /**
150 * if you are using the standard template, this array tells the template what elements
151 * are part of the search criteria
152 */
153 $form->assign('elements', array(
154 'distance',
155 'prox_distance_unit',
156 'street_address',
157 'city',
158 'postal_code',
159 'country_id',
160 'state_province_id',
161 'group',
162 'tag',
163 ));
164 }
165
166 /**
167 * @param int $offset
168 * @param int $rowcount
169 * @param null $sort
170 * @param bool $includeContactIDs
171 * @param bool $justIDs
172 *
173 * @return string
174 */
175 public function all(
176 $offset = 0, $rowcount = 0, $sort = NULL,
177 $includeContactIDs = FALSE, $justIDs = FALSE
178 ) {
179 if ($justIDs) {
180 $selectClause = "contact_a.id as contact_id";
181 }
182 else {
183 $selectClause = "
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
191 ";
192 }
193
194 return $this->sql($selectClause,
195 $offset, $rowcount, $sort,
196 $includeContactIDs, NULL
197 );
198 }
199
200 /**
201 * @return string
202 */
203 public function from() {
204 $this->buildACLClause('contact_a');
205 $f = "
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}
211 ";
212
213 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
214 if ($this->_tag) {
215 $f .= "
216 LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact' AND contact_a.id = t.entity_id)
217 ";
218 }
219 if ($this->_group) {
220 $f .= "
221 LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id AND cgc.status = 'Added')
222 ";
223 }
224
225 return $f;
226 }
227
228 /**
229 * @param bool $includeContactIDs
230 *
231 * @return string
232 */
233 public function where($includeContactIDs = FALSE) {
234 $params = array();
235 $clause = array();
236
237 $where = CRM_Contact_BAO_ProximityQuery::where($this->_latitude,
238 $this->_longitude,
239 $this->_distance,
240 'address'
241 );
242
243 if ($this->_tag) {
244 $where .= "
245 AND t.tag_id = {$this->_tag}
246 ";
247 }
248 if ($this->_group) {
249 $where .= "
250 AND cgc.group_id = {$this->_group}
251 ";
252 }
253
254 $where .= " AND contact_a.is_deleted != 1 ";
255
256 if ($this->_aclWhere) {
257 $where .= " AND {$this->_aclWhere} ";
258 }
259
260 return $this->whereClause($where, $params);
261 }
262
263 /**
264 * @return string
265 */
266 public function templateFile() {
267 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
268 }
269
270 /**
271 * @return array|null
272 */
273 public function setDefaultValues() {
274 $config = CRM_Core_Config::singleton();
275 $countryDefault = $config->defaultContactCountry;
276 $stateprovinceDefault = $config->defaultContactStateProvince;
277 $defaults = array();
278
279 if ($countryDefault) {
280 if ($countryDefault == '1228' || $countryDefault == '1226') {
281 $defaults['prox_distance_unit'] = 'miles';
282 }
283 else {
284 $defaults['prox_distance_unit'] = 'km';
285 }
286 $defaults['country_id'] = $countryDefault;
287 if ($stateprovinceDefault) {
288 $defaults['state_province_id'] = $stateprovinceDefault;
289 }
290 return $defaults;
291 }
292 return NULL;
293 }
294
295 /**
296 * @param $row
297 */
298 public function alterRow(&$row) {
299 }
300
301 /**
302 * @param $title
303 */
304 public function setTitle($title) {
305 if ($title) {
306 CRM_Utils_System::setTitle($title);
307 }
308 else {
309 CRM_Utils_System::setTitle(ts('Search'));
310 }
311 }
312
313 /**
314 * @param string $tableAlias
315 */
316 public function buildACLClause($tableAlias = 'contact') {
317 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
318 }
319
320 }