*/
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']) {
'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
* @return array
*/
public static function getSearchBuilderOperators($fieldType = NULL) {
- $builderOperators = array(
+ return [
'=' => '=',
'!=' => '≠',
'>' => '>',
'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;
+ ];
}
/**
// http://civicrm.org/licensing
-(function($, CRM) {
+(function($, CRM, _) {
'use strict';
/* jshint validthis: true */
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.
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);
}
}
$('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
}
});
-})(cj, CRM);
+})(cj, CRM, CRM._);