From 7123f88c5383a5d6c4fcae98e0587ccf562a2db3 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 27 May 2019 11:57:09 +1200 Subject: [PATCH] Fix inconsistent handling when searching contribution text fields I found that if you search on contribution source it adds wildcards & uses 'like' but with cancel_reason not so much. I found that for contribution_source there was field-specific hacky handling, so I converted both fields to be metadata based and added handling for string fields on the form layer --- CRM/Contribute/BAO/Query.php | 30 ++++++++++++++++++++++++------ CRM/Contribute/Form/Search.php | 6 ++++-- CRM/Core/Form/Search.php | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 56e688cf06..e2ad53b260 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -141,6 +141,9 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_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; @@ -914,16 +917,33 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { 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. @@ -936,7 +956,6 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { $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'); @@ -991,7 +1010,6 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { // 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 diff --git a/CRM/Contribute/Form/Search.php b/CRM/Contribute/Form/Search.php index c4281faffe..847f64688b 100644 --- a/CRM/Contribute/Form/Search.php +++ b/CRM/Contribute/Form/Search.php @@ -159,6 +159,9 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { /** * Build the form object. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public function buildQuickForm() { if ($this->isFormInViewOrEditMode()) { @@ -261,7 +264,7 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { 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 @@ -284,7 +287,6 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { 'financial_type_id', 'contribution_soft_credit_type_id', 'contribution_status_id', - 'contribution_source', 'contribution_trxn_id', 'contribution_page_id', 'contribution_product_id', diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index ede46d09d6..b6fb1cb4d4 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -254,6 +254,23 @@ class CRM_Core_Form_Search extends CRM_Core_Form { 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. * -- 2.25.1