From 291db41c7324f31fc6e3fb4e95ad2a15abcd39d5 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 10 Dec 2018 16:13:45 -0500 Subject: [PATCH] Better support for search builder date fields --- CRM/Contact/Form/Search/Builder.php | 10 +++++++--- templates/CRM/Contact/Form/Search/Builder.js | 18 +++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 4b5f0ddc29..8623f529c4 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -97,9 +97,7 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { $fieldNameTypes = []; foreach ($fields as $name => $field) { // Assign date type to respective field name, which will be later used to modify operator list - if ($type = CRM_Utils_Array::key(CRM_Utils_Array::value('type', $field), CRM_Utils_Type::getValidTypes())) { - $fieldNameTypes[$name] = $type; - } + $fieldNameTypes[$name] = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $field)); // it's necessary to know which of the fields are searchable by label if (isset($field['searchByLabel']) && $field['searchByLabel']) { $searchByLabelFields[] = $name; @@ -278,6 +276,12 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { } elseif (trim($v[2])) { //else check value for rest of the Operators + if ($type == 'Date' || $type == 'Timestamp') { + $v[2] = CRM_Utils_Date::processDate($v[2]); + if ($type == 'Date') { + $v[2] = substr($v[2], 0, 8); + } + } $error = CRM_Utils_Type::validate($v[2], $type, FALSE); if ($error != $v[2]) { $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); diff --git a/templates/CRM/Contact/Form/Search/Builder.js b/templates/CRM/Contact/Form/Search/Builder.js index 227395a0c2..fd6a984c13 100644 --- a/templates/CRM/Contact/Form/Search/Builder.js +++ b/templates/CRM/Contact/Form/Search/Builder.js @@ -48,10 +48,8 @@ buildSelect(row, field, op, false); } - if ((field in CRM.searchBuilder.fieldTypes) === true && - CRM.searchBuilder.fieldTypes[field] == 'Date' - ) { - buildDate(row, op); + if (CRM.searchBuilder.fieldTypes[field] === 'Date' || CRM.searchBuilder.fieldTypes[field] === 'Timestamp') { + buildDate(row, op, CRM.searchBuilder.fieldTypes[field] === 'Timestamp'); } else { removeDate(row); @@ -178,7 +176,7 @@ * @param row: jQuery object */ function removeSelect(row) { - $('.crm-search-value input', row).show(); + $('.crm-search-value input', row).not('.crm-hidden-date').show(); $('.crm-search-value select', row).remove(); } @@ -186,7 +184,7 @@ * Add a datepicker if appropriate for this operation * @param row: jQuery object */ - function buildDate(row, op) { + function buildDate(row, op, time) { var input = $('.crm-search-value input', row); // These are operations that should not get a datepicker var datePickerOp = ($.inArray(op, ['IN', 'NOT IN', 'LIKE', 'RLIKE']) < 0); @@ -198,18 +196,20 @@ var val = input.val(); if (val && val.length === 8) { input.val(val.substr(0, 4) + '-' + val.substr(4, 2) + '-' + val.substr(6, 2)); + } else if (val && val.length === 14) { + input.val(val.substr(0, 4) + '-' + val.substr(4, 2) + '-' + val.substr(6, 2) + ' ' + val.substr(8, 2) + ':' + val.substr(10, 2) + ':' + val.substr(12, 2)); } input .on('change.searchBuilder', function() { if ($(this).val()) { - $(this).val($(this).val().replace(/-/g, '')); + $(this).val($(this).val().replace(/[: -]/g, '')); } }) .crmDatepicker({ - time: false, + time: time, yearRange: '-100:+20' }) - .trigger('change', ['userInput']); + .triggerHandler('change', ['userInput']); } } -- 2.25.1