Skip to content

Commit

Permalink
(dev/core/91) Search Builder Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Apr 30, 2018
1 parent daa46b9 commit 80beace
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
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),
),
));
//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._);

0 comments on commit 80beace

Please sign in to comment.