Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dev/core/91) Search Builder Improvements #12058

Merged
merged 1 commit into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions CRM/Contact/Form/Search/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place in the code where CRM_Core_SelectValues::getSearchBuilderOperators() is called with $fieldType - I think it makes sense to remove that variable from the function signature

),
));
//get the saved search mapping id
Expand Down
15 changes: 2 additions & 13 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public static function getJobFrequency() {
* @return array
*/
public static function getSearchBuilderOperators($fieldType = NULL) {
$builderOperators = array(
return [
'=' => '=',
'!=' => '≠',
'>' => '>',
Expand All @@ -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;
];
}

/**
Expand Down
29 changes: 17 additions & 12 deletions templates/CRM/Contact/Form/Search/Builder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// http://civicrm.org/licensing
(function($, CRM) {
(function($, CRM, _) {
'use strict';

/* jshint validthis: true */
Expand All @@ -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') {
CRM.searchBuilder.generalOperators = _.omit(CRM.searchBuilder.generalOperators, ['IS NOT EMPTY', 'IS EMPTY']);
}
else if (CRM.searchBuilder.fieldTypes[field] == 'String') {
CRM.searchBuilder.generalOperators = _.omit(CRM.searchBuilder.generalOperators, ['>', '<', '>=', '<=']);
}
}
buildOperator(operator, CRM.searchBuilder.generalOperators);
}

// These Ops don't get any input field.
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -296,4 +301,4 @@
$('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
}
});
})(cj, CRM);
})(cj, CRM, CRM._);