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 google geocoder | |
38 | */ | |
39 | class CRM_Utils_Geocode_Google { | |
40 | ||
41 | /** | |
100fef9d | 42 | * Server to retrieve the lat/long |
6a488035 TO |
43 | * |
44 | * @var string | |
6a488035 TO |
45 | */ |
46 | static protected $_server = 'maps.googleapis.com'; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Uri of service |
6a488035 TO |
50 | * |
51 | * @var string | |
6a488035 TO |
52 | */ |
53 | static protected $_uri = '/maps/api/geocode/xml?sensor=false&address='; | |
54 | ||
55 | /** | |
100fef9d | 56 | * Function that takes an address object and gets the latitude / longitude for this |
6a488035 TO |
57 | * address. Note that at a later stage, we could make this function also clean up |
58 | * the address into a more valid format | |
59 | * | |
c490a46a | 60 | * @param array $values |
77b97be7 EM |
61 | * @param bool $stateName |
62 | * | |
bed98343 | 63 | * @return bool |
a6c01b45 | 64 | * true if we modified the address, false otherwise |
6a488035 | 65 | */ |
00be9182 | 66 | public static function format(&$values, $stateName = FALSE) { |
6a488035 | 67 | // we need a valid country, else we ignore |
a7488080 | 68 | if (empty($values['country'])) { |
6a488035 TO |
69 | return FALSE; |
70 | } | |
71 | ||
72 | $config = CRM_Core_Config::singleton(); | |
73 | ||
74 | $add = ''; | |
75 | ||
a7488080 | 76 | if (!empty($values['street_address'])) { |
6a488035 TO |
77 | $add = urlencode(str_replace('', '+', $values['street_address'])); |
78 | $add .= ',+'; | |
79 | } | |
80 | ||
81 | $city = CRM_Utils_Array::value('city', $values); | |
82 | if ($city) { | |
83 | $add .= '+' . urlencode(str_replace('', '+', $city)); | |
84 | $add .= ',+'; | |
85 | } | |
86 | ||
a7488080 CW |
87 | if (!empty($values['state_province'])) { |
88 | if (!empty($values['state_province_id'])) { | |
6a488035 TO |
89 | $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']); |
90 | } | |
91 | else { | |
92 | if (!$stateName) { | |
93 | $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', | |
94 | $values['state_province'], | |
95 | 'name', | |
96 | 'abbreviation' | |
97 | ); | |
98 | } | |
99 | else { | |
100 | $stateProvince = $values['state_province']; | |
101 | } | |
102 | } | |
103 | ||
104 | // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632) | |
105 | if ($stateProvince != $city) { | |
106 | $add .= '+' . urlencode(str_replace('', '+', $stateProvince)); | |
107 | $add .= ',+'; | |
108 | } | |
109 | } | |
110 | ||
a7488080 | 111 | if (!empty($values['postal_code'])) { |
6a488035 TO |
112 | $add .= '+' . urlencode(str_replace('', '+', $values['postal_code'])); |
113 | $add .= ',+'; | |
114 | } | |
115 | ||
a7488080 | 116 | if (!empty($values['country'])) { |
6a488035 TO |
117 | $add .= '+' . urlencode(str_replace('', '+', $values['country'])); |
118 | } | |
119 | ||
120 | $query = 'http://' . self::$_server . self::$_uri . $add; | |
121 | ||
122 | require_once 'HTTP/Request.php'; | |
123 | $request = new HTTP_Request($query); | |
124 | $request->sendRequest(); | |
125 | $string = $request->getResponseBody(); | |
126 | ||
127 | libxml_use_internal_errors(TRUE); | |
128 | $xml = @simplexml_load_string($string); | |
129 | if ($xml === FALSE) { | |
130 | // account blocked maybe? | |
131 | CRM_Core_Error::debug_var('Geocoding failed. Message from Google:', $string); | |
132 | return FALSE; | |
133 | } | |
134 | ||
135 | if (isset($xml->status)) { | |
136 | if ($xml->status == 'OK' && | |
137 | is_a($xml->result->geometry->location, | |
138 | 'SimpleXMLElement' | |
139 | ) | |
140 | ) { | |
141 | $ret = $xml->result->geometry->location->children(); | |
142 | if ($ret->lat && $ret->lng) { | |
e7292422 TO |
143 | $values['geo_code_1'] = (float) $ret->lat; |
144 | $values['geo_code_2'] = (float) $ret->lng; | |
6a488035 TO |
145 | return TRUE; |
146 | } | |
147 | } | |
148 | elseif ($xml->status == 'OVER_QUERY_LIMIT') { | |
79f1148d DL |
149 | CRM_Core_Error::debug_var('Geocoding failed. Message from Google: ', (string ) $xml->status); |
150 | $values['geo_code_1'] = $values['geo_code_2'] = 'null'; | |
151 | $values['geo_code_error'] = $xml->status; | |
152 | return FALSE; | |
6a488035 TO |
153 | } |
154 | } | |
155 | ||
156 | // reset the geo code values if we did not get any good values | |
157 | $values['geo_code_1'] = $values['geo_code_2'] = 'null'; | |
158 | return FALSE; | |
159 | } | |
96025800 | 160 | |
6a488035 | 161 | } |