From 4882d275e96d58b14e4a48fc4a9a729ac0d0bf92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Frank=20J=2E=20G=C3=B3mez?= Date: Fri, 2 Feb 2018 09:02:21 -0500 Subject: [PATCH] CRM-19784: Updated direct usages of the geocoding provider to use the new methods instead. --- CRM/Contact/BAO/ProximityQuery.php | 11 ++++------- CRM/Contact/Form/Edit/Address.php | 2 +- CRM/Contact/Form/Search/Criteria.php | 2 +- CRM/Contact/Form/Search/Custom/Proximity.php | 4 ++-- CRM/Contact/Import/Form/DataSource.php | 2 +- CRM/Core/BAO/Address.php | 14 +++++--------- CRM/Utils/Address/BatchUpdate.php | 17 +++++------------ 7 files changed, 19 insertions(+), 33 deletions(-) diff --git a/CRM/Contact/BAO/ProximityQuery.php b/CRM/Contact/BAO/ProximityQuery.php index 0418f5ab2e..9e971404b6 100644 --- a/CRM/Contact/BAO/ProximityQuery.php +++ b/CRM/Contact/BAO/ProximityQuery.php @@ -333,15 +333,12 @@ ACOS( ) ); - $fnName = isset($config->geocodeMethod) ? $config->geocodeMethod : NULL; - if (empty($fnName)) { - CRM_Core_Error::fatal(ts('Proximity searching requires you to set a valid geocoding provider')); - } - $query->_tables['civicrm_address'] = $query->_whereTables['civicrm_address'] = 1; - require_once str_replace('_', DIRECTORY_SEPARATOR, $fnName) . '.php'; - $fnName::format($proximityAddress); + if (!CRM_Core_BAO_Address::addGeocoderData($proximityAddress)) { + throw new CRM_Core_Exception(ts('Proximity searching requires you to set a valid geocoding provider')); + } + if ( !is_numeric(CRM_Utils_Array::value('geo_code_1', $proximityAddress)) || !is_numeric(CRM_Utils_Array::value('geo_code_2', $proximityAddress)) diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index d7b0275ad9..c3da42fa62 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -153,7 +153,7 @@ class CRM_Contact_Form_Edit_Address { // CRM-11665 geocode override option $geoCode = FALSE; - if (!empty($config->geocodeMethod)) { + if (CRM_Utils_GeocodeProvider::getUsableClassName()) { $geoCode = TRUE; $form->addElement('checkbox', "address[$blockId][manual_geo_code]", diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index f876e40e81..8626b77483 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -355,7 +355,7 @@ class CRM_Contact_Form_Search_Criteria { } // extend addresses with proximity search - if (!empty($config->geocodeMethod)) { + if (CRM_Utils_GeocodeProvider::getUsableClassName()) { $form->addElement('text', 'prox_distance', ts('Find contacts within'), array('class' => 'six')); $form->addElement('select', 'prox_distance_unit', NULL, array( 'miles' => ts('Miles'), diff --git a/CRM/Contact/Form/Search/Custom/Proximity.php b/CRM/Contact/Form/Search/Custom/Proximity.php index 8375adee01..5a86cebdd9 100644 --- a/CRM/Contact/Form/Search/Custom/Proximity.php +++ b/CRM/Contact/Form/Search/Custom/Proximity.php @@ -64,13 +64,13 @@ class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_C } // use the address to get the latitude and longitude - CRM_Utils_Geocode_Google::format($this->_formValues); + CRM_Core_BAO_Address::addGeocoderData($this->_formValues); if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $this->_formValues)) || !is_numeric(CRM_Utils_Array::value('geo_code_2', $this->_formValues)) || !isset($this->_formValues['distance']) ) { - CRM_Core_Error::fatal(ts('Could not geocode input')); + throw new CRM_Core_Exception(ts('Could not geocode input')); } $this->_latitude = $this->_formValues['geo_code_1']; diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index dbbf7d6abc..131c00fa73 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -203,7 +203,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form { $config = CRM_Core_Config::singleton(); $geoCode = FALSE; - if (!empty($config->geocodeMethod)) { + if (CRM_Utils_GeocodeProvider::getUsableClassName()) { $geoCode = TRUE; $this->addElement('checkbox', 'doGeocodeAddress', ts('Geocode addresses during import?')); } diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 801d8a041e..b66f01a11b 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -342,8 +342,6 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { $params['country'] = CRM_Core_PseudoConstant::country($params['country_id']); } - $config = CRM_Core_Config::singleton(); - $asp = Civi::settings()->get('address_standardization_provider'); // clean up the address via USPS web services if enabled if ($asp === 'USPS' && @@ -378,14 +376,12 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { $params = array_merge($params, $parsedFields); } - // check if geocode should be skipped (can be forced with an optional parameter through the api) - $skip_geocode = (isset($params['skip_geocode']) && $params['skip_geocode']) ? TRUE : FALSE; - - // add latitude and longitude and format address if needed - if (!$skip_geocode && !empty($config->geocodeMethod) && ($config->geocodeMethod != 'CRM_Utils_Geocode_OpenStreetMaps') && empty($params['manual_geo_code'])) { - $class = $config->geocodeMethod; - $class::format($params); + // skip_geocode is an optional parameter through the api. + // manual_geo_code is on the contact edit form. They do the same thing.... + if (empty($params['skip_geocode']) && empty($params['manual_geo_code'])) { + self::addGeocoderData($params); } + } /** diff --git a/CRM/Utils/Address/BatchUpdate.php b/CRM/Utils/Address/BatchUpdate.php index 06b3839c37..0433067848 100644 --- a/CRM/Utils/Address/BatchUpdate.php +++ b/CRM/Utils/Address/BatchUpdate.php @@ -69,11 +69,9 @@ class CRM_Utils_Address_BatchUpdate { */ public function run() { - $config = &CRM_Core_Config::singleton(); - // do check for geocoding. $processGeocode = FALSE; - if (empty($config->geocodeMethod)) { + if (!CRM_Utils_GeocodeProvider::getUsableClassName()) { if (CRM_Utils_String::strtobool($this->geocoding) === TRUE) { $this->returnMessages[] = ts('Error: You need to set a mapping provider under Administer > System Settings > Mapping and Geocoding'); $this->returnError = 1; @@ -120,20 +118,19 @@ class CRM_Utils_Address_BatchUpdate { } // do check for parse street address. - return $this->processContacts($config, $processGeocode, $parseStreetAddress); + return $this->processContacts($processGeocode, $parseStreetAddress); } /** * Process contacts. * - * @param CRM_Core_Config $config * @param bool $processGeocode * @param bool $parseStreetAddress * * @return array * @throws Exception */ - public function processContacts(&$config, $processGeocode, $parseStreetAddress) { + public function processContacts($processGeocode, $parseStreetAddress) { // build where clause. $clause = array('( c.id = a.contact_id )'); $params = array(); @@ -174,9 +171,6 @@ class CRM_Utils_Address_BatchUpdate { $totalGeocoded = $totalAddresses = $totalAddressParsed = 0; $dao = CRM_Core_DAO::executeQuery($query, $params); - if ($processGeocode) { - require_once str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php'; - } $unparseableContactAddress = array(); while ($dao->fetch()) { @@ -203,8 +197,7 @@ class CRM_Utils_Address_BatchUpdate { usleep(5000000); } - $className = $config->geocodeMethod; - $className::format($params, TRUE); + CRM_Core_BAO_Address::addGeocoderData($params); // see if we got a geocode error, in this case we'll trigger a fatal // CRM-13760 @@ -212,7 +205,7 @@ class CRM_Utils_Address_BatchUpdate { isset($params['geo_code_error']) && $params['geo_code_error'] == 'OVER_QUERY_LIMIT' ) { - CRM_Core_Error::fatal('Aborting batch geocoding. Hit the over query limit on geocoder.'); + throw new CRM_Core_Exception('Aborting batch geocoding. Hit the over query limit on geocoder.'); } array_shift($params); -- 2.25.1