From: Uelen Paulo Date: Tue, 8 Mar 2016 01:42:20 +0000 (-0300) Subject: CRM-17571 - changing search builder to consider operators by field types X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=759094bdfa40531e4ec0cf0a644d9edd6b9247cf;p=civicrm-core.git CRM-17571 - changing search builder to consider operators by field types --- diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 85077a6d74..29af74fd15 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -94,11 +94,16 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { // Get fields of type date // FIXME: This is a hack until our fields contain this meta-data $dateFields = array(); + $stringFields = array(); $searchByLabelFields = array(); 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; + } // it's necessary to know which of the fields are searchable by label if (isset($field['searchByLabel']) && $field['searchByLabel']) { $searchByLabelFields[] = $name; @@ -113,7 +118,10 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { 'newBlock' => $this->get('newBlock'), 'dateFields' => $dateFields, 'fieldOptions' => self::fieldOptions(), + 'stringFields' => $stringFields, 'searchByLabelFields' => $searchByLabelFields, + '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 c54b58ea88..aa4f9ab56d 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -910,8 +910,8 @@ class CRM_Core_SelectValues { * * @return array */ - public static function getSearchBuilderOperators() { - return array( + public static function getSearchBuilderOperators($fieldType = null) { + $builderOperators = array( '=' => '=', '!=' => '≠', '>' => '>', @@ -928,6 +928,17 @@ class CRM_Core_SelectValues { '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 5ac08815bd..4301e3d796 100644 --- a/templates/CRM/Contact/Form/Search/Builder.js +++ b/templates/CRM/Contact/Form/Search/Builder.js @@ -12,7 +12,19 @@ function handleUserInputField() { var row = $(this).closest('tr'); var field = $('select[id^=mapper][id$="_1"]', row).val(); - var op = $('select[id^=operator]', 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); + } + } // These Ops don't get any input field. var noFieldOps = ['', 'IS EMPTY', 'IS NOT EMPTY', 'IS NULL', 'IS NOT NULL']; @@ -39,6 +51,20 @@ } } + /** + * Add appropriate operator to selected field + * @param operator: jQuery object + * @param options: array + */ + function buildOperator(operator, options) { + var selected = operator.val(); + operator.html(''); + $.each(options, function(value, label) { + operator.append(''); + }); + operator.val(selected); + } + /** * Add select list if appropriate for this operation * @param row: jQuery object