From d23bc8464e196a5278e90cab423be8a315be2f2b Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Mon, 30 Apr 2018 13:15:19 +0530 Subject: [PATCH] (dev/core/91) Search Builder Improvements --- CRM/Contact/Form/Search/Builder.php | 28 +++++++++++--------- CRM/Core/SelectValues.php | 15 ++--------- templates/CRM/Contact/Form/Search/Builder.js | 25 ++++++++++------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 6da450868009..2a5dd79351b4 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -91,18 +91,22 @@ public function preProcess() { */ public function buildQuickForm() { $fields = self::fields(); - // Get fields of type date - // FIXME: This is a hack until our fields contain this meta-data - $dateFields = array(); - $stringFields = array(); $searchByLabelFields = array(); + // This array contain list of available fields and their corresponding data type, + // later assigned as json string, to be used to filter list of mysql operators + $fieldNameTypes = []; + $dataType = [ + CRM_Utils_Type::T_STRING => 'String', + CRM_Utils_Type::T_TEXT => 'String', + CRM_Utils_Type::T_LONGTEXT => 'String', + CRM_Utils_Type::T_BOOLEAN => 'Boolean', + CRM_Utils_Type::T_DATE => 'Date', + CRM_Utils_Type::T_TIMESTAMP => 'Date', + ]; foreach ($fields as $name => $field) { - if (strpos($name, '_date') || CRM_Utils_Array::value('data_type', $field) == 'Date') { - $dateFields[] = $name; - } - // it's necessary to know which of the fields are from string data type - if (isset($field['type']) && $field['type'] === CRM_Utils_Type::T_STRING) { - $stringFields[] = $name; + // Assign date type to respective field name, which will be later used to modify operator list + if (isset($field['type']) && array_key_exists($field['type'], $dataType)) { + $fieldNameTypes[$name] = $dataType[$field['type']]; } // it's necessary to know which of the fields are searchable by label if (isset($field['searchByLabel']) && $field['searchByLabel']) { @@ -116,12 +120,10 @@ public function buildQuickForm() { 'searchBuilder' => array( // Index of newly added/expanded block (1-based index) 'newBlock' => $this->get('newBlock'), - 'dateFields' => $dateFields, 'fieldOptions' => self::fieldOptions(), - 'stringFields' => $stringFields, 'searchByLabelFields' => $searchByLabelFields, + 'fieldTypes' => $fieldNameTypes, 'generalOperators' => array('' => ts('-operator-')) + CRM_Core_SelectValues::getSearchBuilderOperators(), - 'stringOperators' => array('' => ts('-operator-')) + CRM_Core_SelectValues::getSearchBuilderOperators(CRM_Utils_Type::T_STRING), ), )); //get the saved search mapping id diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 07c11e022c40..73d92562d0f6 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -896,7 +896,7 @@ public static function getJobFrequency() { * @return array */ public static function getSearchBuilderOperators($fieldType = NULL) { - $builderOperators = array( + return [ '=' => '=', '!=' => '≠', '>' => '>', @@ -912,18 +912,7 @@ public static function getSearchBuilderOperators($fieldType = NULL) { 'IS NOT EMPTY' => ts('Not Empty'), 'IS NULL' => ts('Is Null'), 'IS NOT NULL' => ts('Not Null'), - ); - if ($fieldType) { - switch ($fieldType) { - case CRM_Utils_Type::T_STRING: - unset($builderOperators['>']); - unset($builderOperators['<']); - unset($builderOperators['>=']); - unset($builderOperators['<=']); - break; - } - } - return $builderOperators; + ]; } /** diff --git a/templates/CRM/Contact/Form/Search/Builder.js b/templates/CRM/Contact/Form/Search/Builder.js index 171ccf396682..3818fec1f318 100644 --- a/templates/CRM/Contact/Form/Search/Builder.js +++ b/templates/CRM/Contact/Form/Search/Builder.js @@ -14,16 +14,19 @@ var field = $('select[id^=mapper][id$="_1"]', row).val(); var operator = $('select[id^=operator]', row); var op = operator.val(); - + var patt = /_1$/; // pattern to check if the change event came from field name if (field !== null && patt.test(this.id)) { - if ($.inArray(field, CRM.searchBuilder.stringFields) >= 0) { - // string operators - buildOperator(operator, CRM.searchBuilder.stringOperators); - } else { - // general operators - buildOperator(operator, CRM.searchBuilder.generalOperators); + // based on data type remove invalid operators e.g. IS EMPTY doesn't work with Boolean type column + if ((field in CRM.searchBuilder.fieldTypes) === true) { + if (CRM.searchBuilder.fieldTypes[field] == 'Boolean') { + ['IS NOT EMPTY', 'IS EMPTY'].forEach(e => delete CRM.searchBuilder.generalOperators[e]); + } + else if (CRM.searchBuilder.fieldTypes[field] == 'String') { + ['>', '<', '>=', '<='].forEach(e => delete CRM.searchBuilder.generalOperators[e]); + } } + buildOperator(operator, CRM.searchBuilder.generalOperators); } // These Ops don't get any input field. @@ -43,11 +46,13 @@ buildSelect(row, field, op); } - if ($.inArray(field, CRM.searchBuilder.dateFields) < 0) { - removeDate(row); + if ((field in CRM.searchBuilder.fieldTypes) === true && + CRM.searchBuilder.fieldTypes[field] == 'Date' + ) { + buildDate(row, op); } else { - buildDate(row, op); + removeDate(row); } }