Merge pull request #9678 from GinkgoFJG/CRM-19877-joomla-setting-create
[civicrm-core.git] / CRM / Utils / Geocode / Yahoo.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
35 * Class that uses Yahoo! PlaceFinder API to retrieve the lat/long of an address
36 * Documentation is at http://developer.yahoo.com/geo/placefinder/
37 */
38class CRM_Utils_Geocode_Yahoo {
39
40 /**
100fef9d 41 * Server to retrieve the lat/long
6a488035
TO
42 *
43 * @var string
6a488035 44 */
77b99945 45 static protected $_server = 'query.yahooapis.com';
6a488035
TO
46
47 /**
fe482240 48 * Uri of service.
6a488035
TO
49 *
50 * @var string
6a488035 51 */
77b99945 52 static protected $_uri = '/v1/public/yql';
6a488035
TO
53
54 /**
100fef9d 55 * Function that takes an address array and gets the latitude / longitude
6a488035
TO
56 * and postal code for this address. Note that at a later stage, we could
57 * make this function also clean up the address into a more valid format
58 *
77855840
TO
59 * @param array $values
60 * Associative array of address data: country, street_address, city, state_province, postal code.
61 * @param bool $stateName
62 * This parameter currently has no function.
6a488035 63 *
608e6658 64 * @return bool
a6c01b45 65 * true if we modified the address, false otherwise
6a488035 66 */
00be9182 67 public static function format(&$values, $stateName = FALSE) {
6a488035
TO
68 CRM_Utils_System::checkPHPVersion(5, TRUE);
69
70 $config = CRM_Core_Config::singleton();
71
77b99945 72 $whereComponents = array();
6a488035 73
a7488080 74 if (!empty($values['street_address'])) {
77b99945 75 $whereComponents['street'] = $values['street_address'];
6a488035
TO
76 }
77
78 if ($city = CRM_Utils_Array::value('city', $values)) {
77b99945 79 $whereComponents['city'] = $city;
6a488035
TO
80 }
81
a7488080
CW
82 if (!empty($values['state_province'])) {
83 if (!empty($values['state_province_id'])) {
6a488035
TO
84 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']);
85 }
86 else {
87 if (!$stateName) {
88 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince',
89 $values['state_province'],
90 'name',
91 'abbreviation'
92 );
93 }
94 else {
95 $stateProvince = $values['state_province'];
96 }
97 }
98
99 // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
100 if ($stateProvince != $city) {
77b99945 101 $whereComponents['state'] = $stateProvince;
6a488035
TO
102 }
103 }
104
a7488080 105 if (!empty($values['postal_code'])) {
77b99945 106 $whereComponents['postal'] = $values['postal_code'];
6a488035
TO
107 }
108
a7488080 109 if (!empty($values['country'])) {
77b99945 110 $whereComponents['country'] = $values['country'];
111 }
112
113 foreach ($whereComponents as $componentName => $componentValue) {
114 $whereComponents[$componentName] = urlencode("$componentName=\"$componentValue\"");
6a488035
TO
115 }
116
77b99945 117 $add = 'q=' . urlencode('select * from geo.placefinder where ');
118
608e6658 119 $add .= implode(urlencode(' and '), $whereComponents);
77b99945 120
121 $add .= "&appid=" . urlencode($config->mapAPIKey);
122
6a488035
TO
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 // see CRM-11359 for why we suppress errors with @
130 $xml = @simplexml_load_string($string);
9862d91a 131 CRM_Utils_Hook::geocoderFormat('Yahoo', $values, $xml);
6a488035
TO
132
133 if ($xml === FALSE) {
134 // account blocked maybe?
135 CRM_Core_Error::debug_var('Geocoding failed. Message from Yahoo:', $string);
136 return FALSE;
137 }
138
77b99945 139 if ($xml->getName() == 'error') {
140 CRM_Core_Error::debug_var('query', $query);
141 CRM_Core_Error::debug_log_message('Geocoding failed. Message from Yahoo: ' . (string) $xml->description);
6a488035
TO
142 return FALSE;
143 }
144
77b99945 145 if (is_a($xml->results->Result, 'SimpleXMLElement')) {
6a488035 146 $result = array();
77b99945 147 $result = get_object_vars($xml->results->Result);
6a488035
TO
148 foreach ($result as $key => $val) {
149 if (is_scalar($val) &&
150 strlen($val)
151 ) {
77b99945 152 $ret[(string) $key] = (string) $val;
6a488035
TO
153 }
154 }
155
156 $values['geo_code_1'] = $ret['latitude'];
157 $values['geo_code_2'] = $ret['longitude'];
158
4f67502c 159 if (!empty($ret['postal'])) {
6a488035
TO
160 $current_pc = CRM_Utils_Array::value('postal_code', $values);
161 $skip_postal = FALSE;
162
163 if ($current_pc) {
77b99945 164 $current_pc_suffix = CRM_Utils_Array::value('postal_code_suffix', $values);
6a488035 165 $current_pc_complete = $current_pc . $current_pc_suffix;
77b99945 166 $new_pc_complete = preg_replace("/[+-]/", '', $ret['postal']);
6a488035
TO
167
168 // if a postal code was already entered, don't change it, except to make it more precise
169 if (strpos($new_pc_complete, $current_pc_complete) !== 0) {
170 // Don't bother anonymous users with the message - they can't change a form they just submitted anyway
77b99945 171 if (CRM_Utils_System::isUserLoggedIn()) {
10a5be27 172 $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 173 1 => $ret['postal'],
21dfd5f5 174 2 => $current_pc_suffix ? "$current_pc-$current_pc_suffix" : $current_pc,
10a5be27 175 ));
450f494d 176
6a488035
TO
177 CRM_Core_Session::setStatus($msg, ts('Postal Code Mismatch'), 'error');
178 }
179 $skip_postal = TRUE;
180 }
181 }
182
183 if (!$skip_postal) {
184 $values['postal_code'] = $ret['postal'];
185
186 /* the following logic to split the string was borrowed from
e70a7fc0
TO
187 CRM/Core/BAO/Address.php -- CRM_Core_BAO_Address::fixAddress.
188 This is actually the function that calls the geocoding
189 script to begin with, but the postal code business takes
190 place before geocoding gets called.
191 */
6a488035
TO
192
193 if (preg_match('/^(\d{4,5})[+-](\d{4})$/',
77b99945 194 $ret['postal'],
195 $match
196 )
197 ) {
6a488035
TO
198 $values['postal_code'] = $match[1];
199 $values['postal_code_suffix'] = $match[2];
200 }
201 }
202 }
203 return TRUE;
204 }
205
206 // reset the geo code values if we did not get any good values
207 $values['geo_code_1'] = $values['geo_code_2'] = 'null';
208 return FALSE;
209 }
96025800 210
232624b1 211}