From b05e28de884792a3916a883842c2b7a7d4b85049 Mon Sep 17 00:00:00 2001 From: "Donald A. Lobo" Date: Thu, 18 Apr 2013 16:23:22 -0700 Subject: [PATCH] CRM-12368 ---------------------------------------- * CRM-12368: proximity search via advanced search broken http://issues.civicrm.org/jira/browse/CRM-12368 --- CRM/Admin/Form/Setting/Localization.php | 7 ++-- CRM/Contact/BAO/ProximityQuery.php | 50 +++++++++++++++++-------- CRM/Contact/Form/Edit/Address.php | 2 +- CRM/Core/PseudoConstant.php | 25 +++++++++++++ 4 files changed, 65 insertions(+), 19 deletions(-) diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index a3ec2fc794..11975ee901 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -133,7 +133,7 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { $includeState->setButtonAttributes('remove', array('value' => ts('<< Remove'))); $this->addElement('select', 'defaultContactCountry', ts('Default Country'), array('' => ts('- select -')) + $country); - + /***Default State/Province***/ $stateCountryMap = array(); $stateCountryMap[] = array( @@ -157,8 +157,9 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { // state country js CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap); + + $defaults = array(); CRM_Core_BAO_Address::fixAllStateSelects($form, $defaults); - /***Default State/Province***/ // we do this only to initialize currencySymbols, kinda hackish but works! $config->defaultCurrencySymbol(); @@ -226,7 +227,7 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { ) ) { $errors['defaultContactCountry'] = ts('Please select a default country that is in the list of available countries.'); - } + } return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Contact/BAO/ProximityQuery.php b/CRM/Contact/BAO/ProximityQuery.php index 02a57f999d..1cc25cd1b7 100644 --- a/CRM/Contact/BAO/ProximityQuery.php +++ b/CRM/Contact/BAO/ProximityQuery.php @@ -290,25 +290,25 @@ $earthDistanceSQL <= $distance $qill[] = $proximityAddress['state_province']; } + $config = CRM_Core_Config::singleton(); + if (!isset($proximityAddress['country_id'])) { + // get it from state if state is present + if (isset($proximityAddress['state_province_id'])) { + $proximityAddress['country_id'] = CRM_Core_PseudoConstant::countryForState($proximityAddress['state_province_id']); + } + else { + $proximityAddress['country_id'] = $config->defaultContactCountry; + } + } + if (isset($proximityAddress['country_id'])) { $proximityAddress['country'] = CRM_Core_PseudoConstant::country($proximityAddress['country_id']); $qill[] = $proximityAddress['country']; } - $config = CRM_Core_Config::singleton(); - if (empty($config->geocodeMethod)) { - CRM_Core_Error::fatal(ts('Proximity searching requires you to set a valid geocoding provider')); - } - - require_once (str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php'); - eval($config->geocodeMethod . '::format( $proximityAddress );'); - if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $proximityAddress)) || - !is_numeric(CRM_Utils_Array::value('geo_code_2', $proximityAddress)) - ) { - return; - } - if (isset($proximityAddress['distance_unit']) && + if ( + isset($proximityAddress['distance_unit']) && $proximityAddress['distance_unit'] == 'miles' ) { $qillUnits = " {$distance} " . ts('miles'); @@ -326,12 +326,32 @@ $earthDistanceSQL <= $distance ) ); + $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')); + } + + require_once (str_replace('_', DIRECTORY_SEPARATOR, $fnName) . '.php'); + $fnName::format($proximityAddress); + if ( + !is_numeric(CRM_Utils_Array::value('geo_code_1', $proximityAddress)) || + !is_numeric(CRM_Utils_Array::value('geo_code_2', $proximityAddress)) + ) { + // we are setting the where clause to 0 here, so we wont return anything + $qill .= ': ' . ts('We could not geocode the destination address.'); + $query->_qill[$grouping][] = $qill; + $query->_where[$grouping][] = ' (0) '; + return; + } + + $query->_qill[$grouping][] = $qill; $query->_tables['civicrm_address'] = $query->_whereTables['civicrm_address'] = 1; - $query->_where[$grouping][] = self::where($proximityAddress['geo_code_1'], + $query->_where[$grouping][] = self::where( + $proximityAddress['geo_code_1'], $proximityAddress['geo_code_2'], $distance ); - $query->_qill[$grouping][] = $qill; + return; } diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index 2fe49ac9f2..ec1a78ae24 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -436,7 +436,7 @@ class CRM_Contact_Form_Edit_Address { } // CRM-7296 freeze the select for state if address is shared with household - // CRM-9070 freeze the select for state if it is view only + // CRM-9070 freeze the select for state if it is view only if (isset($form->_fields) && CRM_Utils_Array::value($stateElementName, $form->_fields) && (CRM_Utils_Array::value('is_shared', $form->_fields[$stateElementName]) || diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index f149f5949e..a0eff17b67 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -1932,6 +1932,31 @@ ORDER BY name"; return $result; } + /** + * Given a state ID return the country ID, this allows + * us to populate forms and values for downstream code + * + * @param $stateID int + * + * @return int the country id that the state belongs to + * @static + * @public + */ + static function countryIDForStateID($stateID) { + if (empty($stateID)) { + return NULL; + } + + $query = " +SELECT country_id +FROM civicrm_state_province +WHERE id = %1 +"; + $params = array(1 => array($stateID, 'Integer')); + + return CRM_Core_DAO::singleValueQuery($query, $params); + } + /** * Get all types of Greetings. * -- 2.25.1