Skip to content

Commit

Permalink
Merge pull request #11643 from eileenmcnaughton/prox_search
Browse files Browse the repository at this point in the history
CRM-21742 Proximity search validation improvement
  • Loading branch information
colemanw authored Feb 7, 2018
2 parents 361d008 + bef4d7e commit c10a4b1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
9 changes: 9 additions & 0 deletions CRM/Contact/Form/Search/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function preProcess() {
// 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)
Expand All @@ -91,6 +93,13 @@ public function preProcess() {
}
}

/**
* Add local and global form rules.
*/
public function addRules() {
$this->addFormRule(array($this->_customClass, 'formRule'));
}

/**
* Set the default values of various form elements.
*
Expand Down
14 changes: 14 additions & 0 deletions CRM/Contact/Form/Search/Custom/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,18 @@ public function setTitle($title) {
}
}

/**
* 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 [];
}

}
58 changes: 40 additions & 18 deletions CRM/Contact/Form/Search/Custom/Proximity.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,7 @@ public function __construct(&$formValues) {

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'];

Expand Down Expand Up @@ -323,4 +306,43 @@ public function buildACLClause($tableAlias = 'contact') {
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);
}

}
6 changes: 3 additions & 3 deletions CRM/Report/Form/Contribute/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ public function where() {
}

/**
* @param $fields
* @param $files
* @param $self
* @param array $fields
* @param array $files
* @param CRM_Core_Form $self
*
* @return array
*/
Expand Down
6 changes: 3 additions & 3 deletions CRM/Report/Form/Contribute/SoftCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,9 @@ public function select() {
}

/**
* @param $fields
* @param $files
* @param $self
* @param array $fields
* @param array $files
* @param CRM_Core_Form $self
*
* @return array
*/
Expand Down

0 comments on commit c10a4b1

Please sign in to comment.