Merge pull request #12308 from eileenmcnaughton/getty
[civicrm-core.git] / CRM / Utils / GeocodeProvider.php
index 172814aceb5bd7b88d8380b585ac889332083754..f87c8eac1150f8102a1136e4be7bbba8af49888d 100644 (file)
@@ -2,9 +2,9 @@
 
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.7                                                |
+  | CiviCRM version 5                                                  |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2017                                |
+  | Copyright CiviCRM LLC (c) 2004-2018                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2017
+ * @copyright CiviCRM LLC (c) 2004-2018
  */
 class CRM_Utils_GeocodeProvider {
 
+  /**
+   * Caches the provider class name. Disables geocoding when set to FALSE.
+   *
+   * @var string|bool
+   */
+  private static $providerClassName;
+
   /**
    * Instantiate a geocode object of the system-configured type.
    *
@@ -58,24 +65,51 @@ class CRM_Utils_GeocodeProvider {
    *   Class name if usable, else false.
    */
   public static function getUsableClassName() {
-    $provider = Civi::settings()->get('geoProvider');
-    if (!class_exists($provider)) {
-      if (strlen($provider)) {
-        Civi::log()->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]);
+    if (self::$providerClassName === NULL) {
+      $provider = Civi::settings()->get('geoProvider');
+      if (!class_exists($provider)) {
+        if (class_exists('CRM_Utils_Geocode_' . $provider)) {
+          $provider = 'CRM_Utils_Geocode_' . $provider;
+        }
+        else {
+          if (strlen($provider)) {
+            Civi::log()
+              ->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]);
+          }
+          $provider = 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]);
+        $provider = FALSE;
       }
-      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;
+      self::$providerClassName = $provider;
     }
 
-    return $provider;
+    return self::$providerClassName;
+  }
+
+  /**
+   * Disable GeoProvider within a session.
+   *
+   * This disables geocoding by causing getUsableClassName() to bail out.
+   */
+  public static function disableForSession() {
+    self::$providerClassName = FALSE;
+  }
+
+  /**
+   * Reset geoprovider (after it has been disabled).
+   */
+  public static function reset() {
+    self::$providerClassName = NULL;
+    self::getUsableClassName();
   }
 
 }