4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2018 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2018
34 class CRM_Utils_GeocodeProvider
{
37 * Caches the provider class name. Disables geocoding when set to FALSE.
41 private static $providerClassName;
44 * Instantiate a geocode object of the system-configured type.
46 * @return CRM_Utils_Geocode
47 * @throws CRM_Core_Exception
49 public static function getConfiguredProvider() {
50 $geoCodeClassName = self
::getUsableClassName();
51 if ($geoCodeClassName === FALSE) {
52 throw new CRM_Core_Exception('No valid geocoding provider enabled');
54 return new $geoCodeClassName();
58 * Get the name of the geocoding class if enabled.
60 * This retrieves the geocoding class, checking it can be accessed.
61 * Checks are done to mitigate the possibility it has been configured
62 * and then the file has been removed.
65 * Class name if usable, else false.
67 public static function getUsableClassName() {
68 if (self
::$providerClassName === NULL) {
69 $provider = Civi
::settings()->get('geoProvider');
70 if (!class_exists($provider)) {
71 if (class_exists('CRM_Utils_Geocode_' . $provider)) {
72 $provider = 'CRM_Utils_Geocode_' . $provider;
75 if (strlen($provider)) {
77 ->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]);
83 // Ideally geocoding providers would be required to implement an interface
84 // or extend a base class. While we identify and implement a geocoding
85 // abstraction library (rather than continue to roll our own), we settle for
87 if (!method_exists($provider, 'format')) {
88 Civi
::log()->error('Configured geocoder is invalid, must provide a format method', ['geocode_class' => $provider]);
92 self
::$providerClassName = $provider;
95 return self
::$providerClassName;
99 * Disable GeoProvider within a session.
101 * This disables geocoding by causing getUsableClassName() to bail out.
103 public static function disableForSession() {
104 self
::$providerClassName = FALSE;
108 * Reset geoprovider (after it has been disabled).
110 public static function reset() {
111 self
::$providerClassName = NULL;
112 self
::getUsableClassName();