*/
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.
*
* 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();
}
}
'geoProvider' => "Google",
));
- // Set geocodeMethod to empty.
- $config = CRM_Core_Config::singleton();
- $config->geocodeMethod = '';
+ CRM_Utils_GeocodeProvider::disableForSession();
// Save a contact with geo coding disabled.
$params = array(
$this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty');
// Run the geocode job on that specific contact
- $config->geocodeMethod = 'CRM_Utils_Geocode_Google';
-
+ CRM_Utils_GeocodeProvider::reset();
try {
$params_geocode = array(
'start' => $contact_values['id'],