From bef4d7eef5e2900af600798263014a80014626bc Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 7 Feb 2018 13:45:16 +1300 Subject: [PATCH] CRM-21742 use validation rather than error when geocoding not possible --- CRM/Contact/Form/Search/Custom.php | 9 +++ CRM/Contact/Form/Search/Custom/Base.php | 14 +++++ CRM/Contact/Form/Search/Custom/Proximity.php | 58 ++++++++++++++------ CRM/Report/Form/Contribute/Repeat.php | 6 +- CRM/Report/Form/Contribute/SoftCredit.php | 6 +- 5 files changed, 69 insertions(+), 24 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom.php b/CRM/Contact/Form/Search/Custom.php index e82d7ad71f..4af599800f 100644 --- a/CRM/Contact/Form/Search/Custom.php +++ b/CRM/Contact/Form/Search/Custom.php @@ -83,6 +83,8 @@ class CRM_Contact_Form_Search_Custom extends CRM_Contact_Form_Search { // instantiate the new class $this->_customClass = new $this->_customSearchClass($this->_formValues); + $this->addFormRule(array($this->_customClass, 'formRule'), $this); + // CRM-12747 if (isset($this->_customClass->_permissionedComponent) && !self::isPermissioned($this->_customClass->_permissionedComponent) @@ -91,6 +93,13 @@ class CRM_Contact_Form_Search_Custom extends CRM_Contact_Form_Search { } } + /** + * Add local and global form rules. + */ + public function addRules() { + $this->addFormRule(array($this->_customClass, 'formRule')); + } + /** * Set the default values of various form elements. * diff --git a/CRM/Contact/Form/Search/Custom/Base.php b/CRM/Contact/Form/Search/Custom/Base.php index 19c1a91a4d..74ed765057 100644 --- a/CRM/Contact/Form/Search/Custom/Base.php +++ b/CRM/Contact/Form/Search/Custom/Base.php @@ -257,4 +257,18 @@ class CRM_Contact_Form_Search_Custom_Base { } } + /** + * Validate form input. + * + * @param array $fields + * @param array $files + * @param CRM_Core_Form $self + * + * @return array + * Input errors from the form. + */ + public function formRule($fields, $files, $self) { + return []; + } + } diff --git a/CRM/Contact/Form/Search/Custom/Proximity.php b/CRM/Contact/Form/Search/Custom/Proximity.php index 5a86cebdd9..9d90c55a8a 100644 --- a/CRM/Contact/Form/Search/Custom/Proximity.php +++ b/CRM/Contact/Form/Search/Custom/Proximity.php @@ -55,24 +55,7 @@ class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_C if (!empty($this->_formValues)) { // add the country and state - if (!empty($this->_formValues['country_id'])) { - $this->_formValues['country'] = CRM_Core_PseudoConstant::country($this->_formValues['country_id']); - } - - if (!empty($this->_formValues['state_province_id'])) { - $this->_formValues['state_province'] = CRM_Core_PseudoConstant::stateProvince($this->_formValues['state_province_id']); - } - - // use the address to get the latitude and longitude - 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']) - ) { - throw new CRM_Core_Exception(ts('Could not geocode input')); - } - + self::addGeocodingData($this->_formValues); $this->_latitude = $this->_formValues['geo_code_1']; $this->_longitude = $this->_formValues['geo_code_2']; @@ -320,4 +303,43 @@ AND cgc.group_id = {$this->_group} list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias); } + /** + * Validate form input. + * + * @param array $fields + * @param array $files + * @param CRM_Core_Form $self + * + * @return array + * Input errors from the form. + */ + public function formRule($fields, $files, $self) { + $this->addGeocodingData($fields); + + if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $fields)) || + !is_numeric(CRM_Utils_Array::value('geo_code_2', $fields)) || + !isset($fields['distance']) + ) { + $errorMessage = ts('Could not determine co-ordinates for provided data'); + return array_fill_keys(['street_address', 'city', 'postal_code', 'country_id', 'state_province_id'], $errorMessage); + } + return []; + } + + /** + * Add the geocoding data to the fields supplied. + * + * @param array $fields + */ + protected function addGeocodingData(&$fields) { + if (!empty($fields['country_id'])) { + $fields['country'] = CRM_Core_PseudoConstant::country($fields['country_id']); + } + + if (!empty($fields['state_province_id'])) { + $fields['state_province'] = CRM_Core_PseudoConstant::stateProvince($fields['state_province_id']); + } + CRM_Core_BAO_Address::addGeocoderData($fields); + } + } diff --git a/CRM/Report/Form/Contribute/Repeat.php b/CRM/Report/Form/Contribute/Repeat.php index 1d84e5bdba..900825bdf0 100644 --- a/CRM/Report/Form/Contribute/Repeat.php +++ b/CRM/Report/Form/Contribute/Repeat.php @@ -513,9 +513,9 @@ LEFT JOIN $this->tempTableRepeat2 {$this->_aliases['civicrm_contribution']}2 } /** - * @param $fields - * @param $files - * @param $self + * @param array $fields + * @param array $files + * @param CRM_Core_Form $self * * @return array */ diff --git a/CRM/Report/Form/Contribute/SoftCredit.php b/CRM/Report/Form/Contribute/SoftCredit.php index 0ad8445917..e9ef1772b7 100644 --- a/CRM/Report/Form/Contribute/SoftCredit.php +++ b/CRM/Report/Form/Contribute/SoftCredit.php @@ -417,9 +417,9 @@ class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form { } /** - * @param $fields - * @param $files - * @param $self + * @param array $fields + * @param array $files + * @param CRM_Core_Form $self * * @return array */ -- 2.25.1