From 6d04b72725991096a67ae64b4a11b36192a941e3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Frank=20J=2E=20G=C3=B3mez?= Date: Fri, 2 Feb 2018 08:53:10 -0500 Subject: [PATCH] CRM-19784: Encapsulated interactions with geocoding providers. Adopts an approach like the factory pattern PHP developers will find familiar, with plenty of deprecation notices re the changing approach. A specific goal was to avoid fatal errors in which a geocoder that no longer exists is called. This applies specifically to the Yahoo class but also generally, e.g., if an extension that provides a geocoder goes missing due to deletion, changed paths, etc. --- CRM/Core/BAO/Address.php | 20 +++++++++ CRM/Utils/GeocodeProvider.php | 81 +++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 CRM/Utils/GeocodeProvider.php diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index a3ca4343e1..801d8a041e 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -1340,4 +1340,24 @@ SELECT is_primary, return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); } + /** + * Add data from the configured geocoding provider. + * + * Generally this means latitude & longitude data. + * + * @param array $params + * @return bool + * TRUE if params could be passed to a provider, else FALSE. + */ + public static function addGeocoderData(&$params) { + try { + $provider = CRM_Utils_GeocodeProvider::getConfiguredProvider(); + } + catch (CRM_Core_Exception $e) { + return FALSE; + } + $provider::format($params); + return TRUE; + } + } diff --git a/CRM/Utils/GeocodeProvider.php b/CRM/Utils/GeocodeProvider.php new file mode 100644 index 0000000000..172814aceb --- /dev/null +++ b/CRM/Utils/GeocodeProvider.php @@ -0,0 +1,81 @@ +get('geoProvider'); + if (!class_exists($provider)) { + if (strlen($provider)) { + Civi::log()->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]); + } + return FALSE; + } + + // Ideally geocoding providers would be required to implement an interface + // or extend a base class. While we identify and implement a geocoding + // abstraction library (rather than continue to roll our own), we settle for + // this check. + if (!method_exists($provider, 'format')) { + Civi::log()->error('Configured geocoder is invalid, must provide a format method', ['geocode_class' => $provider]); + return FALSE; + } + + return $provider; + } + +} -- 2.25.1