Skip to content

Commit

Permalink
Merge pull request #12351 from JMAConsulting/dev-core-41
Browse files Browse the repository at this point in the history
dev/core#41: Search Builder: Not empty with date or integer custom fields gives a sql warning
  • Loading branch information
eileenmcnaughton authored Jun 21, 2018
2 parents 0234f5b + 89d4a22 commit a76517f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
6 changes: 3 additions & 3 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5662,11 +5662,11 @@ public static function buildClause($field, $op, $value = NULL, $dataType = NULL)
return $clause;

case 'IS EMPTY':
$clause = " (NULLIF($field, '') IS NULL) ";
$clause = ($dataType == 'Date') ? " $field IS NULL " : " (NULLIF($field, '') IS NULL) ";
return $clause;

case 'IS NOT EMPTY':
$clause = " (NULLIF($field, '') IS NOT NULL) ";
$clause = ($dataType == 'Date') ? " $field IS NOT NULL " : " (NULLIF($field, '') IS NOT NULL) ";
return $clause;

case 'IN':
Expand All @@ -5677,7 +5677,7 @@ public static function buildClause($field, $op, $value = NULL, $dataType = NULL)
}

default:
if (empty($dataType)) {
if (empty($dataType) || $dataType == 'Date') {
$dataType = 'String';
}
if (is_array($value)) {
Expand Down
18 changes: 6 additions & 12 deletions CRM/Contact/Form/Search/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,10 @@ public function buildQuickForm() {
// 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) {
// 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']];
if ($type = CRM_Utils_Array::key(CRM_Utils_Array::value('type', $field), CRM_Utils_Type::getValidTypes())) {
$fieldNameTypes[$name] = $type;
}
// it's necessary to know which of the fields are searchable by label
if (isset($field['searchByLabel']) && $field['searchByLabel']) {
Expand Down Expand Up @@ -477,8 +469,10 @@ public static function fieldOptions() {
$options[substr($field, 0, -3)] = $entity;
}
}
elseif (!empty($info['data_type']) && in_array($info['data_type'], array('StateProvince', 'Country'))) {
$options[$field] = $entity;
elseif (!empty($info['data_type'])) {
if (in_array($info['data_type'], array('StateProvince', 'Country'))) {
$options[$field] = $entity;
}
}
elseif (in_array(substr($field, 0, 3), array(
'is_',
Expand Down
23 changes: 23 additions & 0 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ public static function &dataType() {
return self::$_dataType;
}

/**
* Build the map of custom field's data types and there respective Util type
*
* @return array
* Data data-type => CRM_Utils_Type
*/
public static function dataToType() {
return [
'String' => CRM_Utils_Type::T_STRING,
'Int' => CRM_Utils_Type::T_INT,
'Float' => CRM_Utils_Type::T_FLOAT,
'Money' => CRM_Utils_Type::T_FLOAT,
'Memo' => CRM_Utils_Type::T_TEXT,
'Date' => CRM_Utils_Type::T_DATE,
'Boolean' => CRM_Utils_Type::T_BOOLEAN,
'StateProvince' => CRM_Utils_Type::T_INT,
'Country' => CRM_Utils_Type::T_INT,
'Link' => CRM_Utils_Type::T_STRING,
'ContactReference' => CRM_Utils_Type::T_INT,
];
}

/**
* Get data to html array.
*
Expand Down Expand Up @@ -691,6 +713,7 @@ public static function getFieldsForImport(
$regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values));
$importableFields[$key] = array(
'name' => $key,
'type' => CRM_Utils_Array::value(CRM_Utils_Array::value('data_type', $values), self::dataToType()),
'title' => CRM_Utils_Array::value('label', $values),
'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
'import' => 1,
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public function where() {
break;

case 'Date':
$this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
$this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Date');
list($qillOp, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $field['label'], $value, $op, array(), CRM_Utils_Type::T_DATE);
$this->_qill[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
break;
Expand Down
9 changes: 5 additions & 4 deletions templates/CRM/Contact/Form/Search/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
var patt = /_1$/; // pattern to check if the change event came from field name
if (field !== null && patt.test(this.id)) {
// based on data type remove invalid operators e.g. IS EMPTY doesn't work with Boolean type column
var operators = CRM.searchBuilder.generalOperators;
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']);
if ($.inArray(CRM.searchBuilder.fieldTypes[field], ['Boolean', 'Int']) > -1) {
operators = _.omit(operators, ['IS NOT EMPTY', 'IS EMPTY']);
}
else if (CRM.searchBuilder.fieldTypes[field] == 'String') {
CRM.searchBuilder.generalOperators = _.omit(CRM.searchBuilder.generalOperators, ['>', '<', '>=', '<=']);
operators = _.omit(operators, ['>', '<', '>=', '<=']);
}
}
buildOperator(operator, CRM.searchBuilder.generalOperators);
buildOperator(operator, operators);
}

// These Ops don't get any input field.
Expand Down

0 comments on commit a76517f

Please sign in to comment.