Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | class CRM_Contact_BAO_ProximityQuery { | |
34 | ||
35 | /** | |
36 | * Trigonometry for calculating geographical distances. | |
6c57bee6 DL |
37 | * |
38 | * Modification made in: CRM-13904 | |
39 | * http://en.wikipedia.org/wiki/Great-circle_distance | |
40 | * http://www.movable-type.co.uk/scripts/latlong.html | |
41 | * | |
6a488035 TO |
42 | * All function arguments and return values measure distances in metres |
43 | * and angles in degrees. The ellipsoid model is from the WGS-84 datum. | |
44 | * Ka-Ping Yee, 2003-08-11 | |
6a488035 TO |
45 | * earth_radius_semimajor = 6378137.0; |
46 | * earth_flattening = 1/298.257223563; | |
47 | * earth_radius_semiminor = $earth_radius_semimajor * (1 - $earth_flattening); | |
48 | * earth_eccentricity_sq = 2*$earth_flattening - pow($earth_flattening, 2); | |
6a488035 TO |
49 | * This library is an implementation of UCB CS graduate student, Ka-Ping Yee (http://www.zesty.ca). |
50 | * This version has been taken from Drupal's location module: http://drupal.org/project/location | |
bed98343 | 51 | */ |
6a488035 TO |
52 | |
53 | static protected $_earthFlattening; | |
54 | static protected $_earthRadiusSemiMinor; | |
55 | static protected $_earthRadiusSemiMajor; | |
56 | static protected $_earthEccentricitySQ; | |
57 | ||
00be9182 | 58 | public static function initialize() { |
6a488035 TO |
59 | static $_initialized = FALSE; |
60 | ||
61 | if (!$_initialized) { | |
62 | $_initialized = TRUE; | |
63 | ||
64 | self::$_earthFlattening = 1.0 / 298.257223563; | |
65 | self::$_earthRadiusSemiMajor = 6378137.0; | |
66 | self::$_earthRadiusSemiMinor = self::$_earthRadiusSemiMajor * (1.0 - self::$_earthFlattening); | |
67 | self::$_earthEccentricitySQ = 2 * self::$_earthFlattening - pow(self::$_earthFlattening, 2); | |
68 | } | |
69 | } | |
70 | ||
1054415f | 71 | /* |
6a488035 TO |
72 | * Latitudes in all of U. S.: from -7.2 (American Samoa) to 70.5 (Alaska). |
73 | * Latitudes in continental U. S.: from 24.6 (Florida) to 49.0 (Washington). | |
74 | * Average latitude of all U. S. zipcodes: 37.9. | |
75 | */ | |
76 | ||
1054415f CW |
77 | /** |
78 | * Estimate the Earth's radius at a given latitude. | |
79 | * Default to an approximate average radius for the United States. | |
80 | * | |
81 | * @param float $latitude | |
82 | * @return float | |
83 | */ | |
00be9182 | 84 | public static function earthRadius($latitude) { |
6a488035 TO |
85 | $lat = deg2rad($latitude); |
86 | ||
87 | $x = cos($lat) / self::$_earthRadiusSemiMajor; | |
88 | $y = sin($lat) / self::$_earthRadiusSemiMinor; | |
89 | return 1.0 / sqrt($x * $x + $y * $y); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Convert longitude and latitude to earth-centered earth-fixed coordinates. | |
94 | * X axis is 0 long, 0 lat; Y axis is 90 deg E; Z axis is north pole. | |
1054415f CW |
95 | * |
96 | * @param float $longitude | |
97 | * @param float $latitude | |
ad37ac8e | 98 | * @param float|int $height |
607fa308 | 99 | * |
1054415f | 100 | * @return array |
6a488035 | 101 | */ |
00be9182 | 102 | public static function earthXYZ($longitude, $latitude, $height = 0) { |
6a488035 TO |
103 | $long = deg2rad($longitude); |
104 | $lat = deg2rad($latitude); | |
105 | ||
106 | $cosLong = cos($long); | |
353ffa53 | 107 | $cosLat = cos($lat); |
6a488035 | 108 | $sinLong = sin($long); |
353ffa53 | 109 | $sinLat = sin($lat); |
6a488035 TO |
110 | |
111 | $radius = self::$_earthRadiusSemiMajor / sqrt(1 - self::$_earthEccentricitySQ * $sinLat * $sinLat); | |
112 | ||
113 | $x = ($radius + $height) * $cosLat * $cosLong; | |
114 | $y = ($radius + $height) * $cosLat * $sinLong; | |
115 | $z = ($radius * (1 - self::$_earthEccentricitySQ) + $height) * $sinLat; | |
116 | ||
117 | return array($x, $y, $z); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Convert a given angle to earth-surface distance. | |
1054415f CW |
122 | * |
123 | * @param float $angle | |
124 | * @param float $latitude | |
125 | * @return float | |
6a488035 | 126 | */ |
00be9182 | 127 | public static function earthArcLength($angle, $latitude) { |
6a488035 TO |
128 | return deg2rad($angle) * self::earthRadius($latitude); |
129 | } | |
130 | ||
6a488035 TO |
131 | /** |
132 | * Estimate the min and max longitudes within $distance of a given location. | |
1054415f CW |
133 | * |
134 | * @param float $longitude | |
135 | * @param float $latitude | |
136 | * @param float $distance | |
137 | * @return array | |
6a488035 | 138 | */ |
00be9182 | 139 | public static function earthLongitudeRange($longitude, $latitude, $distance) { |
353ffa53 TO |
140 | $long = deg2rad($longitude); |
141 | $lat = deg2rad($latitude); | |
6a488035 TO |
142 | $radius = self::earthRadius($latitude); |
143 | ||
353ffa53 TO |
144 | $angle = $distance / $radius; |
145 | $diff = asin(sin($angle) / cos($lat)); | |
6a488035 TO |
146 | $minLong = $long - $diff; |
147 | $maxLong = $long + $diff; | |
148 | ||
353ffa53 | 149 | if ($minLong < -pi()) { |
6a488035 TO |
150 | $minLong = $minLong + pi() * 2; |
151 | } | |
152 | ||
153 | if ($maxLong > pi()) { | |
154 | $maxLong = $maxLong - pi() * 2; | |
155 | } | |
156 | ||
353ffa53 TO |
157 | return array( |
158 | rad2deg($minLong), | |
6a488035 TO |
159 | rad2deg($maxLong), |
160 | ); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Estimate the min and max latitudes within $distance of a given location. | |
1054415f CW |
165 | * |
166 | * @param float $longitude | |
167 | * @param float $latitude | |
168 | * @param float $distance | |
169 | * @return array | |
6a488035 | 170 | */ |
00be9182 | 171 | public static function earthLatitudeRange($longitude, $latitude, $distance) { |
353ffa53 TO |
172 | $long = deg2rad($longitude); |
173 | $lat = deg2rad($latitude); | |
6a488035 TO |
174 | $radius = self::earthRadius($latitude); |
175 | ||
353ffa53 TO |
176 | $angle = $distance / $radius; |
177 | $minLat = $lat - $angle; | |
178 | $maxLat = $lat + $angle; | |
6a488035 TO |
179 | $rightangle = pi() / 2.0; |
180 | ||
181 | // wrapped around the south pole | |
353ffa53 | 182 | if ($minLat < -$rightangle) { |
6a488035 TO |
183 | $overshoot = -$minLat - $rightangle; |
184 | $minLat = -$rightangle + $overshoot; | |
185 | if ($minLat > $maxLat) { | |
186 | $maxLat = $minLat; | |
187 | } | |
188 | $minLat = -$rightangle; | |
189 | } | |
190 | ||
191 | // wrapped around the north pole | |
192 | if ($maxLat > $rightangle) { | |
193 | $overshoot = $maxLat - $rightangle; | |
194 | $maxLat = $rightangle - $overshoot; | |
195 | if ($maxLat < $minLat) { | |
196 | $minLat = $maxLat; | |
197 | } | |
198 | $maxLat = $rightangle; | |
199 | } | |
200 | ||
353ffa53 TO |
201 | return array( |
202 | rad2deg($minLat), | |
6a488035 TO |
203 | rad2deg($maxLat), |
204 | ); | |
205 | } | |
206 | ||
86538308 | 207 | /** |
1054415f | 208 | * @param float $latitude |
bed98343 | 209 | * @param float $longitude |
1054415f | 210 | * @param float $distance |
86538308 EM |
211 | * @param string $tablePrefix |
212 | * | |
213 | * @return string | |
214 | */ | |
00be9182 | 215 | public static function where($latitude, $longitude, $distance, $tablePrefix = 'civicrm_address') { |
6a488035 TO |
216 | self::initialize(); |
217 | ||
218 | $params = array(); | |
219 | $clause = array(); | |
220 | ||
bed98343 | 221 | list($minLongitude, $maxLongitude) = self::earthLongitudeRange($longitude, $latitude, $distance); |
222 | list($minLatitude, $maxLatitude) = self::earthLatitudeRange($longitude, $latitude, $distance); | |
6a488035 | 223 | |
06415a75 PJ |
224 | // DONT consider NAN values (which is returned by rad2deg php function) |
225 | // for checking BETWEEN geo_code's criteria as it throws obvious 'NAN' field not found DB: Error | |
226 | $geoCodeWhere = array(); | |
227 | if (!is_nan($minLatitude)) { | |
228 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_1 >= $minLatitude "; | |
229 | } | |
230 | if (!is_nan($maxLatitude)) { | |
231 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_1 <= $maxLatitude "; | |
232 | } | |
233 | if (!is_nan($minLongitude)) { | |
234 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_2 >= $minLongitude "; | |
235 | } | |
236 | if (!is_nan($maxLongitude)) { | |
237 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_2 <= $maxLongitude "; | |
238 | } | |
239 | $geoCodeWhereClause = implode(' AND ', $geoCodeWhere); | |
240 | ||
6a488035 | 241 | $where = " |
06415a75 | 242 | {$geoCodeWhereClause} AND |
84f81538 DL |
243 | ACOS( |
244 | COS(RADIANS({$tablePrefix}.geo_code_1)) * | |
245 | COS(RADIANS($latitude)) * | |
246 | COS(RADIANS({$tablePrefix}.geo_code_2) - RADIANS($longitude)) + | |
247 | SIN(RADIANS({$tablePrefix}.geo_code_1)) * | |
248 | SIN(RADIANS($latitude)) | |
249 | ) * 6378137 <= $distance | |
6a488035 | 250 | "; |
6a488035 TO |
251 | return $where; |
252 | } | |
253 | ||
86538308 | 254 | /** |
67d19299 | 255 | * Process form. |
256 | * | |
257 | * @param CRM_Contact_BAO_Query $query | |
1054415f | 258 | * @param array $values |
86538308 | 259 | * |
67d19299 | 260 | * @return null |
86538308 EM |
261 | * @throws Exception |
262 | */ | |
00be9182 | 263 | public static function process(&$query, &$values) { |
6a488035 TO |
264 | list($name, $op, $distance, $grouping, $wildcard) = $values; |
265 | ||
266 | // also get values array for all address related info | |
267 | $proximityVars = array( | |
268 | 'street_address' => 1, | |
269 | 'city' => 1, | |
270 | 'postal_code' => 1, | |
271 | 'state_province_id' => 0, | |
272 | 'country_id' => 0, | |
273 | 'state_province' => 0, | |
274 | 'country' => 0, | |
275 | 'distance_unit' => 0, | |
276 | ); | |
277 | ||
278 | $proximityAddress = array(); | |
279 | $qill = array(); | |
280 | foreach ($proximityVars as $var => $recordQill) { | |
281 | $proximityValues = $query->getWhereValues("prox_{$var}", $grouping); | |
282 | if (!empty($proximityValues) && | |
283 | !empty($proximityValues[2]) | |
284 | ) { | |
285 | $proximityAddress[$var] = $proximityValues[2]; | |
286 | if ($recordQill) { | |
287 | $qill[] = $proximityValues[2]; | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | if (empty($proximityAddress)) { | |
bed98343 | 293 | return NULL; |
6a488035 TO |
294 | } |
295 | ||
296 | if (isset($proximityAddress['state_province_id'])) { | |
297 | $proximityAddress['state_province'] = CRM_Core_PseudoConstant::stateProvince($proximityAddress['state_province_id']); | |
298 | $qill[] = $proximityAddress['state_province']; | |
299 | } | |
300 | ||
b05e28de DL |
301 | $config = CRM_Core_Config::singleton(); |
302 | if (!isset($proximityAddress['country_id'])) { | |
303 | // get it from state if state is present | |
304 | if (isset($proximityAddress['state_province_id'])) { | |
af7c77f2 | 305 | $proximityAddress['country_id'] = CRM_Core_PseudoConstant::countryIDForStateID($proximityAddress['state_province_id']); |
b05e28de | 306 | } |
a5175a12 | 307 | elseif (isset($config->defaultContactCountry)) { |
b05e28de DL |
308 | $proximityAddress['country_id'] = $config->defaultContactCountry; |
309 | } | |
310 | } | |
311 | ||
836f1f3f | 312 | if (!empty($proximityAddress['country_id'])) { |
6a488035 TO |
313 | $proximityAddress['country'] = CRM_Core_PseudoConstant::country($proximityAddress['country_id']); |
314 | $qill[] = $proximityAddress['country']; | |
315 | } | |
316 | ||
b05e28de DL |
317 | if ( |
318 | isset($proximityAddress['distance_unit']) && | |
6a488035 TO |
319 | $proximityAddress['distance_unit'] == 'miles' |
320 | ) { | |
321 | $qillUnits = " {$distance} " . ts('miles'); | |
322 | $distance = $distance * 1609.344; | |
323 | } | |
324 | else { | |
325 | $qillUnits = " {$distance} " . ts('km'); | |
326 | $distance = $distance * 1000.00; | |
327 | } | |
328 | ||
329 | $qill = ts('Proximity search to a distance of %1 from %2', | |
330 | array( | |
331 | 1 => $qillUnits, | |
21dfd5f5 | 332 | 2 => implode(', ', $qill), |
6a488035 TO |
333 | ) |
334 | ); | |
335 | ||
b05e28de DL |
336 | $fnName = isset($config->geocodeMethod) ? $config->geocodeMethod : NULL; |
337 | if (empty($fnName)) { | |
338 | CRM_Core_Error::fatal(ts('Proximity searching requires you to set a valid geocoding provider')); | |
339 | } | |
340 | ||
6d6c8253 DL |
341 | $query->_tables['civicrm_address'] = $query->_whereTables['civicrm_address'] = 1; |
342 | ||
d3e86119 | 343 | require_once str_replace('_', DIRECTORY_SEPARATOR, $fnName) . '.php'; |
b05e28de DL |
344 | $fnName::format($proximityAddress); |
345 | if ( | |
346 | !is_numeric(CRM_Utils_Array::value('geo_code_1', $proximityAddress)) || | |
347 | !is_numeric(CRM_Utils_Array::value('geo_code_2', $proximityAddress)) | |
348 | ) { | |
349 | // we are setting the where clause to 0 here, so we wont return anything | |
350 | $qill .= ': ' . ts('We could not geocode the destination address.'); | |
351 | $query->_qill[$grouping][] = $qill; | |
352 | $query->_where[$grouping][] = ' (0) '; | |
bed98343 | 353 | return NULL; |
b05e28de DL |
354 | } |
355 | ||
356 | $query->_qill[$grouping][] = $qill; | |
b05e28de DL |
357 | $query->_where[$grouping][] = self::where( |
358 | $proximityAddress['geo_code_1'], | |
6a488035 TO |
359 | $proximityAddress['geo_code_2'], |
360 | $distance | |
361 | ); | |
b05e28de | 362 | |
bed98343 | 363 | return NULL; |
6a488035 TO |
364 | } |
365 | ||
86538308 | 366 | /** |
1054415f | 367 | * @param array $input |
bed98343 | 368 | * retun void |
cde2037d EM |
369 | * |
370 | * @return null | |
86538308 | 371 | */ |
00be9182 | 372 | public static function fixInputParams(&$input) { |
6a488035 TO |
373 | foreach ($input as $param) { |
374 | if (CRM_Utils_Array::value('0', $param) == 'prox_distance') { | |
375 | // add prox_ prefix to these | |
376 | $param_alter = array('street_address', 'city', 'postal_code', 'state_province', 'country'); | |
377 | ||
378 | foreach ($input as $key => $_param) { | |
379 | if (in_array($_param[0], $param_alter)) { | |
380 | $input[$key][0] = 'prox_' . $_param[0]; | |
381 | ||
382 | // _id suffix where needed | |
383 | if ($_param[0] == 'country' || $_param[0] == 'state_province') { | |
384 | $input[$key][0] .= '_id'; | |
385 | ||
386 | // flatten state_province array | |
387 | if (is_array($input[$key][2])) { | |
388 | $input[$key][2] = $input[$key][2][0]; | |
389 | } | |
390 | } | |
391 | } | |
392 | } | |
bed98343 | 393 | return NULL; |
6a488035 TO |
394 | } |
395 | } | |
396 | } | |
96025800 | 397 | |
6a488035 | 398 | } |