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

Fix inconsistent handling when searching contribution text fields #14354

Merged
merged 1 commit into from
May 28, 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
30 changes: 24 additions & 6 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public static function where(&$query) {
*
* @param array $values
* @param CRM_Contact_BAO_Query $query
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
*/
public static function whereClauseSingle(&$values, &$query) {
list($name, $op, $value, $grouping, $wildcard) = $values;
Expand Down Expand Up @@ -914,16 +917,33 @@ public static function defaultReturnProperties($mode, $includeCustomFields = TRU
return $properties;
}

/**
* Get the metadata for fields to be included on the search form.
*
* @throws \CiviCRM_API3_Exception
*/
public static function getSearchFieldMetadata() {
$fields = [
'contribution_source',
'cancel_reason',
'invoice_number',
];
$metadata = civicrm_api3('Contribution', 'getfields', [])['values'];
return array_intersect_key($metadata, array_flip($fields));
}

/**
* Add all the elements shared between contribute search and advnaced search.
*
* @param CRM_Core_Form $form
* @param \CRM_Contribute_Form_Search $form
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function buildSearchForm(&$form) {

// Added contribution source
$form->addElement('text', 'contribution_source', ts('Contribution Source'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'source'));

$form->addSearchFieldMetadata(['Contribution' => self::getSearchFieldMetadata()]);
$form->addFormFieldsFromMetadata();
CRM_Core_Form_Date::buildDateRange($form, 'contribution_date', 1, '_low', '_high', ts('From:'), FALSE);
// CRM-17602
// This hidden element added for displaying Date Range error correctly. Definitely a dirty hack, but... it works.
Expand All @@ -936,7 +956,6 @@ public static function buildSearchForm(&$form) {
$form->add('text', 'contribution_amount_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
$form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('99.99', ' ')]), 'money');

$form->addField('cancel_reason', ['entity' => 'Contribution']);
CRM_Core_Form_Date::buildDateRange($form, 'contribution_cancel_date', 1, '_low', '_high', ts('From:'), FALSE);
$form->addElement('hidden', 'contribution_cancel_date_range_error');

Expand Down Expand Up @@ -991,7 +1010,6 @@ public static function buildSearchForm(&$form) {

// Add field for transaction ID search
$form->addElement('text', 'contribution_trxn_id', ts("Transaction ID"));
$form->addElement('text', 'invoice_number', ts("Invoice Number"));
$form->addElement('text', 'contribution_check_number', ts('Check Number'));

// Add field for pcp display in roll search
Expand Down
6 changes: 4 additions & 2 deletions CRM/Contribute/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public function setDefaultValues() {

/**
* Build the form object.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function buildQuickForm() {
if ($this->isFormInViewOrEditMode()) {
Expand Down Expand Up @@ -261,7 +264,7 @@ public function postProcess() {
if (!empty($_POST) && !$this->_force) {
$this->_formValues = $this->controller->exportValues($this->_name);
}

$this->convertTextStringsToUseLikeOperator();
$this->fixFormValues();

// We don't show test records in summaries or dashboards
Expand All @@ -284,7 +287,6 @@ public function postProcess() {
'financial_type_id',
'contribution_soft_credit_type_id',
'contribution_status_id',
'contribution_source',
'contribution_trxn_id',
'contribution_page_id',
'contribution_product_id',
Expand Down
17 changes: 17 additions & 0 deletions CRM/Core/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ protected function getEntityDefaults($entity) {
return $defaults;
}

/**
* Convert any submitted text fields to use 'like' rather than '=' as the operator.
*
* This excludes any with options.
*
* Note this will only pick up fields declared via metadata.
*/
protected function convertTextStringsToUseLikeOperator() {
foreach ($this->getSearchFieldMetadata()[$this->getDefaultEntity()] as $fieldName => $field) {
if (!empty($this->_formValues[$fieldName]) && empty($field['options']) && empty($field['pseudoconstant'])) {
if (in_array($field['type'], [CRM_Utils_Type::T_STRING, CRM_Utils_Type::T_TEXT])) {
$this->_formValues[$fieldName] = ['LIKE' => CRM_Contact_BAO_Query::getWildCardedValue(TRUE, 'LIKE', $this->_formValues[$fieldName])];
}
}
}
}

/**
* Add checkboxes for each row plus a master checkbox.
*
Expand Down