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