Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | /** | |
43 | * server to retrieve the lat/long | |
44 | * | |
45 | * @var string | |
46 | * @static | |
47 | */ | |
48 | static protected $_server = 'where.yahooapis.com'; | |
49 | ||
50 | /** | |
51 | * uri of service | |
52 | * | |
53 | * @var string | |
54 | * @static | |
55 | */ | |
56 | static protected $_uri = '/geocode'; | |
57 | ||
58 | /** | |
59 | * function that takes an address array and gets the latitude / longitude | |
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 | */ | |
69 | static function format(&$values, $stateName = FALSE) { | |
70 | CRM_Utils_System::checkPHPVersion(5, TRUE); | |
71 | ||
72 | $config = CRM_Core_Config::singleton(); | |
73 | ||
74 | $add = "appid=" . urlencode($config->mapAPIKey); | |
75 | ||
76 | $add .= '&location='; | |
77 | ||
78 | if (CRM_Utils_Array::value('street_address', $values)) { | |
79 | $add .= urlencode($values['street_address']); | |
80 | $add .= ',+'; | |
81 | } | |
82 | ||
83 | if ($city = CRM_Utils_Array::value('city', $values)) { | |
84 | $add .= urlencode($city); | |
85 | $add .= ',+'; | |
86 | } | |
87 | ||
88 | if (CRM_Utils_Array::value('state_province', $values)) { | |
89 | if (CRM_Utils_Array::value('state_province_id', $values)) { | |
90 | $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']); | |
91 | } | |
92 | else { | |
93 | if (!$stateName) { | |
94 | $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', | |
95 | $values['state_province'], | |
96 | 'name', | |
97 | 'abbreviation' | |
98 | ); | |
99 | } | |
100 | else { | |
101 | $stateProvince = $values['state_province']; | |
102 | } | |
103 | } | |
104 | ||
105 | // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632) | |
106 | if ($stateProvince != $city) { | |
107 | $add .= urlencode($stateProvince); | |
108 | $add .= ',+'; | |
109 | } | |
110 | } | |
111 | ||
112 | if (CRM_Utils_Array::value('postal_code', $values)) { | |
113 | $add .= urlencode($values['postal_code']); | |
114 | $add .= ',+'; | |
115 | } | |
116 | ||
117 | if (CRM_Utils_Array::value('country', $values)) { | |
118 | $add .= urlencode($values['country']); | |
119 | } | |
120 | ||
121 | $query = 'http://' . self::$_server . self::$_uri . '?' . $add; | |
122 | ||
123 | require_once 'HTTP/Request.php'; | |
124 | $request = new HTTP_Request($query); | |
125 | $request->sendRequest(); | |
126 | $string = $request->getResponseBody(); | |
127 | // see CRM-11359 for why we suppress errors with @ | |
128 | $xml = @simplexml_load_string($string); | |
129 | ||
130 | if ($xml === FALSE) { | |
131 | // account blocked maybe? | |
132 | CRM_Core_Error::debug_var('Geocoding failed. Message from Yahoo:', $string); | |
133 | return FALSE; | |
134 | } | |
135 | ||
136 | if ((integer)$xml->Error != 0) { | |
137 | CRM_Core_Error::debug_var('Geocoding failed. Message from Yahoo:', (string)$xml->ErrorMessage); | |
138 | return FALSE; | |
139 | } | |
140 | ||
141 | if (is_a($xml->Result[0], 'SimpleXMLElement')) { | |
142 | $result = array(); | |
143 | $result = get_object_vars($xml->Result[0]); | |
144 | ||
145 | foreach ($result as $key => $val) { | |
146 | if (is_scalar($val) && | |
147 | strlen($val) | |
148 | ) { | |
149 | $ret[(string)$key] = (string)$val; | |
150 | } | |
151 | } | |
152 | ||
153 | $values['geo_code_1'] = $ret['latitude']; | |
154 | $values['geo_code_2'] = $ret['longitude']; | |
155 | ||
156 | if ($ret['postal']) { | |
157 | $current_pc = CRM_Utils_Array::value('postal_code', $values); | |
158 | $skip_postal = FALSE; | |
159 | ||
160 | if ($current_pc) { | |
161 | $current_pc_suffix = CRM_Utils_Array::value('postal_code_suffix', $values); | |
162 | $current_pc_complete = $current_pc . $current_pc_suffix; | |
163 | $new_pc_complete = preg_replace("/[+-]/", '', $ret['postal']); | |
164 | ||
165 | // if a postal code was already entered, don't change it, except to make it more precise | |
166 | if (strpos($new_pc_complete, $current_pc_complete) !== 0) { | |
167 | // Don't bother anonymous users with the message - they can't change a form they just submitted anyway | |
168 | if(CRM_Utils_System::isUserLoggedIn()) { | |
10a5be27 | 169 | $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 |
170 | 1 => $ret['postal'], |
171 | 2 => $current_pc_suffix ? "$current_pc-$current_pc_suffix" : $current_pc | |
10a5be27 | 172 | )); |
450f494d | 173 | |
6a488035 TO |
174 | CRM_Core_Session::setStatus($msg, ts('Postal Code Mismatch'), 'error'); |
175 | } | |
176 | $skip_postal = TRUE; | |
177 | } | |
178 | } | |
179 | ||
180 | if (!$skip_postal) { | |
181 | $values['postal_code'] = $ret['postal']; | |
182 | ||
183 | /* the following logic to split the string was borrowed from | |
184 | CRM/Core/BAO/Address.php -- CRM_Core_BAO_Address::fixAddress. | |
185 | This is actually the function that calls the geocoding | |
186 | script to begin with, but the postal code business takes | |
187 | place before geocoding gets called. | |
188 | */ | |
189 | ||
190 | if (preg_match('/^(\d{4,5})[+-](\d{4})$/', | |
191 | $ret['postal'], | |
192 | $match | |
193 | )) { | |
194 | $values['postal_code'] = $match[1]; | |
195 | $values['postal_code_suffix'] = $match[2]; | |
196 | } | |
197 | } | |
198 | } | |
199 | return TRUE; | |
200 | } | |
201 | ||
202 | // reset the geo code values if we did not get any good values | |
203 | $values['geo_code_1'] = $values['geo_code_2'] = 'null'; | |
204 | return FALSE; | |
205 | } | |
206 | } | |
207 |