Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Class that uses Yahoo! PlaceFinder API to retrieve the lat/long of an address | |
38 | * Documentation is at http://developer.yahoo.com/geo/placefinder/ | |
39 | */ | |
40 | class CRM_Utils_Geocode_Yahoo { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Server to retrieve the lat/long |
6a488035 TO |
44 | * |
45 | * @var string | |
46 | * @static | |
47 | */ | |
77b99945 | 48 | static protected $_server = 'query.yahooapis.com'; |
6a488035 TO |
49 | |
50 | /** | |
100fef9d | 51 | * Uri of service |
6a488035 TO |
52 | * |
53 | * @var string | |
54 | * @static | |
55 | */ | |
77b99945 | 56 | static protected $_uri = '/v1/public/yql'; |
6a488035 TO |
57 | |
58 | /** | |
100fef9d | 59 | * Function that takes an address array and gets the latitude / longitude |
6a488035 TO |
60 | * and postal code for this address. Note that at a later stage, we could |
61 | * make this function also clean up the address into a more valid format | |
62 | * | |
63 | * @param array $values associative array of address data: country, street_address, city, state_province, postal code | |
64 | * @param boolean $stateName this parameter currently has no function | |
65 | * | |
66 | * @return boolean true if we modified the address, false otherwise | |
67 | * @static | |
68 | */ | |
00be9182 | 69 | public static function format(&$values, $stateName = FALSE) { |
6a488035 TO |
70 | CRM_Utils_System::checkPHPVersion(5, TRUE); |
71 | ||
72 | $config = CRM_Core_Config::singleton(); | |
73 | ||
77b99945 | 74 | $whereComponents = array(); |
6a488035 | 75 | |
a7488080 | 76 | if (!empty($values['street_address'])) { |
77b99945 | 77 | $whereComponents['street'] = $values['street_address']; |
6a488035 TO |
78 | } |
79 | ||
80 | if ($city = CRM_Utils_Array::value('city', $values)) { | |
77b99945 | 81 | $whereComponents['city'] = $city; |
6a488035 TO |
82 | } |
83 | ||
a7488080 CW |
84 | if (!empty($values['state_province'])) { |
85 | if (!empty($values['state_province_id'])) { | |
6a488035 TO |
86 | $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']); |
87 | } | |
88 | else { | |
89 | if (!$stateName) { | |
90 | $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', | |
91 | $values['state_province'], | |
92 | 'name', | |
93 | 'abbreviation' | |
94 | ); | |
95 | } | |
96 | else { | |
97 | $stateProvince = $values['state_province']; | |
98 | } | |
99 | } | |
100 | ||
101 | // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632) | |
102 | if ($stateProvince != $city) { | |
77b99945 | 103 | $whereComponents['state'] = $stateProvince; |
6a488035 TO |
104 | } |
105 | } | |
106 | ||
a7488080 | 107 | if (!empty($values['postal_code'])) { |
77b99945 | 108 | $whereComponents['postal'] = $values['postal_code']; |
6a488035 TO |
109 | } |
110 | ||
a7488080 | 111 | if (!empty($values['country'])) { |
77b99945 | 112 | $whereComponents['country'] = $values['country']; |
113 | } | |
114 | ||
115 | foreach ($whereComponents as $componentName => $componentValue) { | |
116 | $whereComponents[$componentName] = urlencode("$componentName=\"$componentValue\""); | |
6a488035 TO |
117 | } |
118 | ||
77b99945 | 119 | $add = 'q=' . urlencode('select * from geo.placefinder where '); |
120 | ||
121 | $add .= join(urlencode(' and '), $whereComponents); | |
122 | ||
123 | $add .= "&appid=" . urlencode($config->mapAPIKey); | |
124 | ||
6a488035 TO |
125 | $query = 'http://' . self::$_server . self::$_uri . '?' . $add; |
126 | ||
127 | require_once 'HTTP/Request.php'; | |
128 | $request = new HTTP_Request($query); | |
129 | $request->sendRequest(); | |
130 | $string = $request->getResponseBody(); | |
131 | // see CRM-11359 for why we suppress errors with @ | |
132 | $xml = @simplexml_load_string($string); | |
133 | ||
134 | if ($xml === FALSE) { | |
135 | // account blocked maybe? | |
136 | CRM_Core_Error::debug_var('Geocoding failed. Message from Yahoo:', $string); | |
137 | return FALSE; | |
138 | } | |
139 | ||
77b99945 | 140 | if ($xml->getName() == 'error') { |
141 | CRM_Core_Error::debug_var('query', $query); | |
142 | CRM_Core_Error::debug_log_message('Geocoding failed. Message from Yahoo: ' . (string) $xml->description); | |
6a488035 TO |
143 | return FALSE; |
144 | } | |
145 | ||
77b99945 | 146 | if (is_a($xml->results->Result, 'SimpleXMLElement')) { |
6a488035 | 147 | $result = array(); |
77b99945 | 148 | $result = get_object_vars($xml->results->Result); |
6a488035 TO |
149 | foreach ($result as $key => $val) { |
150 | if (is_scalar($val) && | |
151 | strlen($val) | |
152 | ) { | |
77b99945 | 153 | $ret[(string) $key] = (string) $val; |
6a488035 TO |
154 | } |
155 | } | |
156 | ||
157 | $values['geo_code_1'] = $ret['latitude']; | |
158 | $values['geo_code_2'] = $ret['longitude']; | |
159 | ||
4f67502c | 160 | if (!empty($ret['postal'])) { |
6a488035 TO |
161 | $current_pc = CRM_Utils_Array::value('postal_code', $values); |
162 | $skip_postal = FALSE; | |
163 | ||
164 | if ($current_pc) { | |
77b99945 | 165 | $current_pc_suffix = CRM_Utils_Array::value('postal_code_suffix', $values); |
6a488035 | 166 | $current_pc_complete = $current_pc . $current_pc_suffix; |
77b99945 | 167 | $new_pc_complete = preg_replace("/[+-]/", '', $ret['postal']); |
6a488035 TO |
168 | |
169 | // if a postal code was already entered, don't change it, except to make it more precise | |
170 | if (strpos($new_pc_complete, $current_pc_complete) !== 0) { | |
171 | // Don't bother anonymous users with the message - they can't change a form they just submitted anyway | |
77b99945 | 172 | if (CRM_Utils_System::isUserLoggedIn()) { |
10a5be27 | 173 | $msg = ts('The Yahoo Geocoding system returned a different postal code (%1) than the one you entered (%2). If you want the Yahoo value, please delete the current postal code and save again.', array( |
6a488035 TO |
174 | 1 => $ret['postal'], |
175 | 2 => $current_pc_suffix ? "$current_pc-$current_pc_suffix" : $current_pc | |
10a5be27 | 176 | )); |
450f494d | 177 | |
6a488035 TO |
178 | CRM_Core_Session::setStatus($msg, ts('Postal Code Mismatch'), 'error'); |
179 | } | |
180 | $skip_postal = TRUE; | |
181 | } | |
182 | } | |
183 | ||
184 | if (!$skip_postal) { | |
185 | $values['postal_code'] = $ret['postal']; | |
186 | ||
187 | /* the following logic to split the string was borrowed from | |
188 | CRM/Core/BAO/Address.php -- CRM_Core_BAO_Address::fixAddress. | |
189 | This is actually the function that calls the geocoding | |
190 | script to begin with, but the postal code business takes | |
191 | place before geocoding gets called. | |
192 | */ | |
193 | ||
194 | if (preg_match('/^(\d{4,5})[+-](\d{4})$/', | |
77b99945 | 195 | $ret['postal'], |
196 | $match | |
197 | ) | |
198 | ) { | |
6a488035 TO |
199 | $values['postal_code'] = $match[1]; |
200 | $values['postal_code_suffix'] = $match[2]; | |
201 | } | |
202 | } | |
203 | } | |
204 | return TRUE; | |
205 | } | |
206 | ||
207 | // reset the geo code values if we did not get any good values | |
208 | $values['geo_code_1'] = $values['geo_code_2'] = 'null'; | |
209 | return FALSE; | |
210 | } | |
232624b1 | 211 | } |