From 71dfa06c158bdeaaf29c122d959d74c304c7975e Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Wed, 10 Jun 2020 11:45:29 +0530 Subject: [PATCH] core#1805: Autocomplete-select custom field is not searchable --- CRM/Activity/Form/Search.php | 2 - CRM/Case/Form/Search.php | 2 - CRM/Contact/BAO/Query.php | 1 + CRM/Contact/Form/Search/Advanced.php | 2 - CRM/Contribute/Form/Search.php | 3 - CRM/Core/BAO/CustomValue.php | 3 + CRM/Custom/Form/Field.php | 4 ++ CRM/Event/Form/Search.php | 2 - CRM/Grant/Form/Search.php | 2 - CRM/Member/Form/Search.php | 2 - .../phpunit/CRM/Core/BAO/CustomValueTest.php | 64 ++++++++++++++----- 11 files changed, 56 insertions(+), 31 deletions(-) diff --git a/CRM/Activity/Form/Search.php b/CRM/Activity/Form/Search.php index 56035b42a1..bbda19840d 100644 --- a/CRM/Activity/Form/Search.php +++ b/CRM/Activity/Form/Search.php @@ -182,8 +182,6 @@ class CRM_Activity_Form_Search extends CRM_Core_Form_Search { $this->_formValues["activity_test"] = 0; } - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); - $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->set('queryParams', $this->_queryParams); diff --git a/CRM/Case/Form/Search.php b/CRM/Case/Form/Search.php index 8d971a851f..b22ab8c08e 100644 --- a/CRM/Case/Form/Search.php +++ b/CRM/Case/Form/Search.php @@ -202,8 +202,6 @@ class CRM_Case_Form_Search extends CRM_Core_Form_Search { if (empty($this->_formValues['case_deleted'])) { $this->_formValues['case_deleted'] = 0; } - // @todo - stop changing formValues - respect submitted form values, change a working array. - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); // @todo - stop changing formValues - respect submitted form values, change a working array. $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 790dc07167..517588b6d5 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1587,6 +1587,7 @@ class CRM_Contact_BAO_Query { } self::filterCountryFromValuesIfStateExists($formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($formValues); foreach ($formValues as $id => $values) { if (self::isAlreadyProcessedForQueryFormat($values)) { diff --git a/CRM/Contact/Form/Search/Advanced.php b/CRM/Contact/Form/Search/Advanced.php index 9336eb604f..f517389c4d 100644 --- a/CRM/Contact/Form/Search/Advanced.php +++ b/CRM/Contact/Form/Search/Advanced.php @@ -297,8 +297,6 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { $this->_sortByCharacter = NULL; } - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); - $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields); $this->_returnProperties = &$this->returnProperties(); parent::postProcess(); diff --git a/CRM/Contribute/Form/Search.php b/CRM/Contribute/Form/Search.php index 3eef9ebb6e..4f1009b41a 100644 --- a/CRM/Contribute/Form/Search.php +++ b/CRM/Contribute/Form/Search.php @@ -307,9 +307,6 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { } } - // @todo - stop changing formValues - respect submitted form values, change a working array. - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); - // @todo - stop changing formValues - respect submitted form values, change a working array. $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Core/BAO/CustomValue.php b/CRM/Core/BAO/CustomValue.php index 3a64cdb29d..555077b042 100644 --- a/CRM/Core/BAO/CustomValue.php +++ b/CRM/Core/BAO/CustomValue.php @@ -175,6 +175,9 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO { ) { $formValues[$key] = ['LIKE' => $formValues[$key]]; } + elseif ($htmlType == 'Autocomplete-Select' && !empty($formValues[$key]) && is_string($formValues[$key]) && (strpos($formValues[$key], ',') != FALSE)) { + $formValues[$key] = ['IN' => explode(',', $formValues[$key])]; + } } } diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index b7597bfe5f..042b711713 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -748,6 +748,10 @@ SELECT count(*) $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values'); $_flagOption = 1; } + if (strpos($fields['option_value'][$start], ',') != FALSE) { + $errors['option_value[' . $start . ']'] = ts('You cannot use comma in Option value'); + $_flagOption = 1; + } $nextIndex++; } $start++; diff --git a/CRM/Event/Form/Search.php b/CRM/Event/Form/Search.php index 8cb0c34121..3000110687 100644 --- a/CRM/Event/Form/Search.php +++ b/CRM/Event/Form/Search.php @@ -285,8 +285,6 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search { $this->_formValues["participant_test"] = 0; } - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); - $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']); $this->set('queryParams', $this->_queryParams); diff --git a/CRM/Grant/Form/Search.php b/CRM/Grant/Form/Search.php index 1ea774db22..b997f2cab7 100644 --- a/CRM/Grant/Form/Search.php +++ b/CRM/Grant/Form/Search.php @@ -162,8 +162,6 @@ class CRM_Grant_Form_Search extends CRM_Core_Form_Search { $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); - $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->set('queryParams', $this->_queryParams); diff --git a/CRM/Member/Form/Search.php b/CRM/Member/Form/Search.php index b8bb22b8be..fc0ba94de0 100644 --- a/CRM/Member/Form/Search.php +++ b/CRM/Member/Form/Search.php @@ -222,8 +222,6 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { $this->_formValues["member_test"] = 0; } - CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); - $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields); $this->set('queryParams', $this->_queryParams); diff --git a/tests/phpunit/CRM/Core/BAO/CustomValueTest.php b/tests/phpunit/CRM/Core/BAO/CustomValueTest.php index 265b513c78..fb88b59588 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomValueTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomValueTest.php @@ -97,28 +97,60 @@ class CRM_Core_BAO_CustomValueTest extends CiviUnitTestCase { } } - public function fixCustomFieldValue() { + public function testFixCustomFieldValue() { $customGroup = $this->customGroupCreate(['extends' => 'Individual']); - - $fields = [ - 'custom_group_id' => $customGroup['id'], - 'data_type' => 'Memo', - 'html_type' => 'TextArea', - 'default_value' => '', - ]; - - $customField = $this->customFieldCreate($fields); - - $custom = 'custom_' . $customField['id']; $params = [ 'email' => 'abc@webaccess.co.in', - $custom => 'note', ]; - CRM_Core_BAO_CustomValue::fixCustomFieldValue($params); - $this->assertEquals($params[$custom], '%note%', 'Checking the returned value of type Memo.'); + foreach ([ + [ + 'custom_group_id' => $customGroup['id'], + 'data_type' => 'Memo', + 'html_type' => 'TextArea', + 'default_value' => '', + 'search_value' => '%note%', + 'expected_value' => ['LIKE' => '%note%'], + ], + [ + 'custom_group_id' => $customGroup['id'], + 'data_type' => 'String', + 'html_type' => 'Autocomplete-Select', + 'default_value' => '', + 'search_value' => 'R,Y', + 'expected_value' => ['IN' => ['R', 'Y']], + 'option_values' => [ + [ + 'label' => 'Red', + 'value' => 'R', + 'weight' => 1, + 'is_active' => 1, + ], + [ + 'label' => 'Yellow', + 'value' => 'Y', + 'weight' => 2, + 'is_active' => 1, + ], + [ + 'label' => 'Green', + 'value' => 'G', + 'weight' => 3, + 'is_active' => 1, + ], + ], + ], + ] as $field) { + $id = $this->customFieldCreate($field)['id']; + $customKey = 'custom_' . $id; + $params[$customKey] = $field['search_value']; + CRM_Core_BAO_CustomValue::fixCustomFieldValue($params); + $this->assertEquals($params[$customKey], $field['expected_value'], 'Checking the returned value of type ' . $field['data_type']); + + // delete created custom field + $this->customFieldDelete($id); + } - $this->customFieldDelete($customField['id']); $this->customGroupDelete($customGroup['id']); } -- 2.25.1