Skip to content

Commit

Permalink
Merge pull request #14354 from eileenmcnaughton/cont_search
Browse files Browse the repository at this point in the history
Fix inconsistent handling when searching contribution text fields
  • Loading branch information
eileenmcnaughton authored May 28, 2019
2 parents e345d7f + 7123f88 commit abf04d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
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

0 comments on commit abf04d2

Please sign in to comment.