Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 google geocoder | |
38 | */ | |
39 | class CRM_Utils_Geocode_Google { | |
40 | ||
41 | /** | |
42 | * server to retrieve the lat/long | |
43 | * | |
44 | * @var string | |
45 | * @static | |
46 | */ | |
47 | static protected $_server = 'maps.googleapis.com'; | |
48 | ||
49 | /** | |
50 | * uri of service | |
51 | * | |
52 | * @var string | |
53 | * @static | |
54 | */ | |
55 | static protected $_uri = '/maps/api/geocode/xml?sensor=false&address='; | |
56 | ||
57 | /** | |
58 | * function that takes an address object and gets the latitude / longitude for this | |
59 | * address. Note that at a later stage, we could make this function also clean up | |
60 | * the address into a more valid format | |
61 | * | |
62 | * @param object $address | |
63 | * | |
64 | * @return boolean true if we modified the address, false otherwise | |
65 | * @static | |
66 | */ | |
67 | static function format(&$values, $stateName = FALSE) { | |
68 | // we need a valid country, else we ignore | |
69 | if (!CRM_Utils_Array::value('country', $values)) { | |
70 | return FALSE; | |
71 | } | |
72 | ||
73 | $config = CRM_Core_Config::singleton(); | |
74 | ||
75 | $add = ''; | |
76 | ||
77 | if (CRM_Utils_Array::value('street_address', $values)) { | |
78 | $add = urlencode(str_replace('', '+', $values['street_address'])); | |
79 | $add .= ',+'; | |
80 | } | |
81 | ||
82 | $city = CRM_Utils_Array::value('city', $values); | |
83 | if ($city) { | |
84 | $add .= '+' . urlencode(str_replace('', '+', $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(str_replace('', '+', $stateProvince)); | |
108 | $add .= ',+'; | |
109 | } | |
110 | } | |
111 | ||
112 | if (CRM_Utils_Array::value('postal_code', $values)) { | |
113 | $add .= '+' . urlencode(str_replace('', '+', $values['postal_code'])); | |
114 | $add .= ',+'; | |
115 | } | |
116 | ||
117 | if (CRM_Utils_Array::value('country', $values)) { | |
118 | $add .= '+' . urlencode(str_replace('', '+', $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 | ||
128 | libxml_use_internal_errors(TRUE); | |
129 | $xml = @simplexml_load_string($string); | |
130 | if ($xml === FALSE) { | |
131 | // account blocked maybe? | |
132 | CRM_Core_Error::debug_var('Geocoding failed. Message from Google:', $string); | |
133 | return FALSE; | |
134 | } | |
135 | ||
136 | if (isset($xml->status)) { | |
137 | if ($xml->status == 'OK' && | |
138 | is_a($xml->result->geometry->location, | |
139 | 'SimpleXMLElement' | |
140 | ) | |
141 | ) { | |
142 | $ret = $xml->result->geometry->location->children(); | |
143 | if ($ret->lat && $ret->lng) { | |
144 | $values['geo_code_1'] = (float)$ret->lat; | |
145 | $values['geo_code_2'] = (float)$ret->lng; | |
146 | return TRUE; | |
147 | } | |
148 | } | |
149 | elseif ($xml->status == 'OVER_QUERY_LIMIT') { | |
79f1148d DL |
150 | CRM_Core_Error::debug_var('Geocoding failed. Message from Google: ', (string ) $xml->status); |
151 | $values['geo_code_1'] = $values['geo_code_2'] = 'null'; | |
152 | $values['geo_code_error'] = $xml->status; | |
153 | return FALSE; | |
6a488035 TO |
154 | } |
155 | } | |
156 | ||
157 | // reset the geo code values if we did not get any good values | |
158 | $values['geo_code_1'] = $values['geo_code_2'] = 'null'; | |
159 | return FALSE; | |
160 | } | |
161 | } | |
162 |