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