more cleanup batch 16
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Proximity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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 public 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 public function all(
174 $offset = 0, $rowcount = 0, $sort = NULL,
175 $includeContactIDs = FALSE, $justIDs = FALSE
176 ) {
177 if ($justIDs) {
178 $selectClause = "contact_a.id as contact_id";
179 }
180 else {
181 $selectClause = "
182 contact_a.id as contact_id ,
183 contact_a.sort_name as sort_name ,
184 address.street_address as street_address,
185 address.city as city ,
186 address.postal_code as postal_code ,
187 state_province.name as state_province,
188 country.name as country
189 ";
190 }
191
192 return $this->sql($selectClause,
193 $offset, $rowcount, $sort,
194 $includeContactIDs, NULL
195 );
196 }
197
198 /**
199 * @return string
200 */
201 public function from() {
202 $f = "
203 FROM civicrm_contact contact_a
204 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
205 address.is_primary = 1 )
206 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
207 LEFT JOIN civicrm_country country ON country.id = address.country_id
208 ";
209
210 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
211 if ($this->_tag) {
212 $f .= "
213 LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact' AND contact_a.id = t.entity_id)
214 ";
215 }
216 if ($this->_group) {
217 $f .= "
218 LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id AND cgc.status = 'Added')
219 ";
220 }
221
222 return $f;
223 }
224
225 /**
226 * @param bool $includeContactIDs
227 *
228 * @return string
229 */
230 public function where($includeContactIDs = FALSE) {
231 $params = array();
232 $clause = array();
233
234 $where = CRM_Contact_BAO_ProximityQuery::where($this->_latitude,
235 $this->_longitude,
236 $this->_distance,
237 'address'
238 );
239
240 if ($this->_tag) {
241 $where .= "
242 AND t.tag_id = {$this->_tag}
243 ";
244 }
245 if ($this->_group) {
246 $where .= "
247 AND cgc.group_id = {$this->_group}
248 ";
249 }
250
251 $where .= " AND contact_a.is_deleted != 1 ";
252
253 return $this->whereClause($where, $params);
254 }
255
256 /**
257 * @return string
258 */
259 public function templateFile() {
260 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
261 }
262
263 /**
264 * @return array|null
265 */
266 public function setDefaultValues() {
267 $config = CRM_Core_Config::singleton();
268 $countryDefault = $config->defaultContactCountry;
269 $stateprovinceDefault = $config->defaultContactStateProvince;
270 $defaults = array();
271
272 if ($countryDefault) {
273 if ($countryDefault == '1228' || $countryDefault == '1226') {
274 $defaults['prox_distance_unit'] = 'miles';
275 }
276 else {
277 $defaults['prox_distance_unit'] = 'km';
278 }
279 $defaults['country_id'] = $countryDefault;
280 if ($stateprovinceDefault) {
281 $defaults['state_province_id'] = $stateprovinceDefault;
282 }
283 return $defaults;
284 }
285 return NULL;
286 }
287
288 /**
289 * @param $row
290 */
291 public function alterRow(&$row) {
292 }
293
294 /**
295 * @param $title
296 */
297 public function setTitle($title) {
298 if ($title) {
299 CRM_Utils_System::setTitle($title);
300 }
301 else {
302 CRM_Utils_System::setTitle(ts('Search'));
303 }
304 }
305 }