5034d9b96c83b85eb01917c6e95b0337ccf31ee9
[civicrm-core.git] / CRM / Utils / Geocode / Google.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 array $values
63 * @param bool $stateName
64 *
65 * @return boolean true if we modified the address, false otherwise
66 * @static
67 */
68 static function format(&$values, $stateName = FALSE) {
69 // we need a valid country, else we ignore
70 if (empty($values['country'])) {
71 return FALSE;
72 }
73
74 $config = CRM_Core_Config::singleton();
75
76 $add = '';
77
78 if (!empty($values['street_address'])) {
79 $add = urlencode(str_replace('', '+', $values['street_address']));
80 $add .= ',+';
81 }
82
83 $city = CRM_Utils_Array::value('city', $values);
84 if ($city) {
85 $add .= '+' . urlencode(str_replace('', '+', $city));
86 $add .= ',+';
87 }
88
89 if (!empty($values['state_province'])) {
90 if (!empty($values['state_province_id'])) {
91 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']);
92 }
93 else {
94 if (!$stateName) {
95 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince',
96 $values['state_province'],
97 'name',
98 'abbreviation'
99 );
100 }
101 else {
102 $stateProvince = $values['state_province'];
103 }
104 }
105
106 // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
107 if ($stateProvince != $city) {
108 $add .= '+' . urlencode(str_replace('', '+', $stateProvince));
109 $add .= ',+';
110 }
111 }
112
113 if (!empty($values['postal_code'])) {
114 $add .= '+' . urlencode(str_replace('', '+', $values['postal_code']));
115 $add .= ',+';
116 }
117
118 if (!empty($values['country'])) {
119 $add .= '+' . urlencode(str_replace('', '+', $values['country']));
120 }
121
122 $query = 'http://' . self::$_server . self::$_uri . $add;
123
124 require_once 'HTTP/Request.php';
125 $request = new HTTP_Request($query);
126 $request->sendRequest();
127 $string = $request->getResponseBody();
128
129 libxml_use_internal_errors(TRUE);
130 $xml = @simplexml_load_string($string);
131 if ($xml === FALSE) {
132 // account blocked maybe?
133 CRM_Core_Error::debug_var('Geocoding failed. Message from Google:', $string);
134 return FALSE;
135 }
136
137 if (isset($xml->status)) {
138 if ($xml->status == 'OK' &&
139 is_a($xml->result->geometry->location,
140 'SimpleXMLElement'
141 )
142 ) {
143 $ret = $xml->result->geometry->location->children();
144 if ($ret->lat && $ret->lng) {
145 $values['geo_code_1'] = (float)$ret->lat;
146 $values['geo_code_2'] = (float)$ret->lng;
147 return TRUE;
148 }
149 }
150 elseif ($xml->status == 'OVER_QUERY_LIMIT') {
151 CRM_Core_Error::debug_var('Geocoding failed. Message from Google: ', (string ) $xml->status);
152 $values['geo_code_1'] = $values['geo_code_2'] = 'null';
153 $values['geo_code_error'] = $xml->status;
154 return FALSE;
155 }
156 }
157
158 // reset the geo code values if we did not get any good values
159 $values['geo_code_1'] = $values['geo_code_2'] = 'null';
160 return FALSE;
161 }
162 }
163