Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#1059 Fix contribution search to work with url parameters in force mode #14624

Merged
merged 2 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CRM/Contribute/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function getDefaultEntity() {

/**
* Processing needed for buildForm and later.
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
*/
public function preProcess() {
$this->set('searchFormName', 'Search');
Expand Down Expand Up @@ -103,6 +106,9 @@ public function preProcess() {
}

if ($this->_force) {
// Search field metadata is normally added in buildForm but we are bypassing that in this flow
// (I've always found the flow kinda confusing & perhaps that is the problem but this mitigates)
$this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query::getSearchFieldMetadata()]);
$this->postProcess();
$this->set('force', 0);
}
Expand Down Expand Up @@ -163,7 +169,8 @@ public function setDefaultValues() {
CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_high not end');
}
$this->_defaults = parent::setDefaultValues();
if (empty($this->_defaults['contribution_status_id'])) {
if (empty($this->_defaults['contribution_status_id']) && !$this->_force) {
// In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
$this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey(
'CRM_Contribute_BAO_Contribution',
'contribution_status_id',
Expand Down Expand Up @@ -406,6 +413,8 @@ public function fixFormValues() {
if (!$this->_force) {
return;
}
// Start by loading url defaults.
$this->_formValues = $this->setDefaultValues();

$status = CRM_Utils_Request::retrieve('status', 'String');
if ($status) {
Expand Down
22 changes: 14 additions & 8 deletions CRM/Core/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public function addSearchFieldMetadata($searchFieldMetadata) {
* @throws \Exception
*/
public function setDefaultValues() {
return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues);
$defaults = (array) $this->_formValues;
foreach (['Contact', $this->getDefaultEntity()] as $entity) {
$defaults = array_merge($this->getEntityDefaults($entity), $defaults);
}
return $defaults;
}

/**
Expand Down Expand Up @@ -251,16 +255,16 @@ protected function getValidationTypeForField($entity, $fieldName) {
*/
protected function getEntityDefaults($entity) {
$defaults = [];
foreach ($this->getSearchFieldMetadata()[$entity] as $fieldName => $fieldSpec) {
if (empty($_POST[$fieldSpec['name']])) {
$value = CRM_Utils_Request::retrieveValue($fieldName, $this->getValidationTypeForField($entity, $fieldName), FALSE, NULL, 'GET');
foreach (CRM_Utils_Array::value($entity, $this->getSearchFieldMetadata(), []) as $fieldName => $fieldSpec) {
if (empty($_POST[$fieldName])) {
$value = CRM_Utils_Request::retrieveValue($fieldName, $this->getValidationTypeForField($entity, $fieldName), NULL, NULL, 'GET');
if ($value !== NULL) {
$defaults[$fieldName] = $value;
}
if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) {
$low = CRM_Utils_Request::retrieveValue($fieldName . '_low', 'Timestamp', FALSE, NULL, 'GET');
$high = CRM_Utils_Request::retrieveValue($fieldName . '_high', 'Timestamp', FALSE, NULL, 'GET');
if ($low !== FALSE || $high !== FALSE) {
$low = CRM_Utils_Request::retrieveValue($fieldName . '_low', 'Timestamp', NULL, NULL, 'GET');
$high = CRM_Utils_Request::retrieveValue($fieldName . '_high', 'Timestamp', NULL, NULL, 'GET');
if ($low !== NULL || $high !== NULL) {
$defaults[$fieldName . '_relative'] = 0;
$defaults[$fieldName . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL;
$defaults[$fieldName . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL;
Expand Down Expand Up @@ -327,12 +331,14 @@ public function addTaskMenu($tasks) {
* to define the string.
*/
protected function addSortNameField() {
$title = civicrm_api3('setting', 'getvalue', ['name' => 'includeEmailInName', 'group' => 'Search Preferences']) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail();
$this->addElement(
'text',
'sort_name',
civicrm_api3('setting', 'getvalue', ['name' => 'includeEmailInName', 'group' => 'Search Preferences']) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(),
$title,
CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
);
$this->searchFieldMetadata['Contact']['sort_name'] = ['name' => 'sort_name', 'title' => $title, 'type' => CRM_Utils_Type::T_STRING];
}

/**
Expand Down
13 changes: 0 additions & 13 deletions CRM/Event/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,6 @@ public function postProcess() {
public function addRules() {
}

/**
* Set the default form values.
*
*
* @return array
* the default array reference
*/
public function setDefaultValues() {
$defaults = [];
$defaults = $this->_formValues;
return $defaults;
}

public function fixFormValues() {
// if this search has been forced
// then see if there are any get values, and if so over-ride the post values
Expand Down