From e3ea3f68d2327faa3bd64b3d869734df9ee45b2d Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Fri, 29 Jul 2022 15:44:19 -0500 Subject: [PATCH] Fix #3756 hook_civicrm_geocoderFormat does not alter address components --- CRM/Utils/Geocode/Google.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/CRM/Utils/Geocode/Google.php b/CRM/Utils/Geocode/Google.php index 0885a3d3ae..247a541096 100644 --- a/CRM/Utils/Geocode/Google.php +++ b/CRM/Utils/Geocode/Google.php @@ -106,6 +106,8 @@ class CRM_Utils_Geocode_Google { $values['geo_code_error'] = $coord['geo_code_error']; } + CRM_Utils_Hook::geocoderFormat('Google', $values, $coord['request_xml']); + return isset($coord['geo_code_1'], $coord['geo_code_2']); } @@ -122,11 +124,18 @@ class CRM_Utils_Geocode_Google { /** * @param string $add * Url-encoded address + * * @return array + * An array of values with the following possible keys: + * geo_code_error: String error message + * geo_code_1: Float latitude + * geo_code_2: Float longitude + * request_xml: SimpleXMLElement parsed xml from geocoding API + * * @throws \GuzzleHttp\Exception\GuzzleException */ private static function makeRequest($add) { - + $coords = []; $config = CRM_Core_Config::singleton(); if (!empty($config->geoAPIKey)) { $add .= '&key=' . urlencode($config->geoAPIKey); @@ -140,11 +149,11 @@ class CRM_Utils_Geocode_Google { libxml_use_internal_errors(TRUE); $xml = @simplexml_load_string($string); - CRM_Utils_Hook::geocoderFormat('Google', $values, $xml); + $coords['request_xml'] = $xml; if ($xml === FALSE) { // account blocked maybe? CRM_Core_Error::debug_var('Geocoding failed. Message from Google:', $string); - return ['geo_code_error' => $string]; + $coords['geo_code_error'] = $string; } if (isset($xml->status)) { @@ -155,22 +164,18 @@ class CRM_Utils_Geocode_Google { ) { $ret = $xml->result->geometry->location->children(); if ($ret->lat && $ret->lng) { - return [ - 'geo_code_1' => (float) $ret->lat, - 'geo_code_2' => (float) $ret->lng, - ]; + $coords['geo_code_1'] = (float) $ret->lat; + $coords['geo_code_2'] = (float) $ret->lng; } } - elseif ($xml->status == 'ZERO_RESULTS') { - // reset the geo code values if we did not get any good values - return []; - } - else { + elseif ($xml->status != 'ZERO_RESULTS') { + // 'ZERO_RESULTS' is a valid status, in which case we'll change nothing in $ret; + // but if the status is anything else, we need to note the error. CRM_Core_Error::debug_var("Geocoding failed. Message from Google: ({$xml->status})", (string ) $xml->error_message); - return ['geo_code_error' => $xml->status]; + $coords['geo_code_error'] = $xml->status; } } - return []; + return $coords; } } -- 2.25.1