From 11f907d483c5dd835555bd163894a2cb37d4f770 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 22 Jan 2019 12:02:25 -0500 Subject: [PATCH] Fix entityRef contact type filter --- js/Common.js | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/js/Common.js b/js/Common.js index 985e7d616c..2eeec554d6 100644 --- a/js/Common.js +++ b/js/Common.js @@ -718,18 +718,16 @@ if (!CRM.vars) CRM.vars = {}; function getEntityRefFilters($el) { var entity = $el.data('api-entity').toLowerCase(), - filters = $.extend([], CRM.config.entityRef.filters[entity] || []), + filters = CRM.config.entityRef.filters[entity] || [], params = $.extend({params: {}}, $el.data('api-params') || {}).params, result = []; $.each(filters, function() { var filter = $.extend({type: 'select', 'attributes': {}, entity: entity}, this); - if (typeof params[filter.key] === 'undefined') { + $.extend(this, filter); + if (!params[filter.key]) { result.push(filter); } else if (filter.key == 'contact_type' && typeof params.contact_sub_type === 'undefined') { - filter.options = _.remove(filter.options, function(option) { - return option.key.indexOf(params.contact_type + '__') === 0; - }); result.push(filter); } }); @@ -772,11 +770,7 @@ if (!CRM.vars) CRM.vars = {}; attrs += ' ' + attr + '="' + val + '"'; }); if (filterSpec.type === 'select') { - markup = ''; - if (filterSpec.options) { - markup += CRM.utils.renderOptions(filterSpec.options, filter.value); - } - markup += ''; + markup = ''; } else { markup = ''; } @@ -789,6 +783,7 @@ if (!CRM.vars) CRM.vars = {}; */ function renderEntityRefFilterValue($el) { var + entity = $el.data('api-entity').toLowerCase(), filter = $el.data('user-filter') || {}, filterSpec = filter.key ? _.find(getEntityRefFilters($el), {key: filter.key}) : null, $keyField = $('.crm-entityref-filter-key', '#select2-drop'), @@ -797,7 +792,7 @@ if (!CRM.vars) CRM.vars = {}; $('.crm-entityref-filter-value', '#select2-drop').remove(); $valField = $(entityRefFilterValueMarkup(filter, filterSpec)); $keyField.after($valField); - if (filterSpec.type === 'select' && !filterSpec.options) { + if (filterSpec.type === 'select') { loadEntityRefFilterOptions(filter, filterSpec, $valField, $el); } } else { @@ -806,24 +801,38 @@ if (!CRM.vars) CRM.vars = {}; } /** - * Fetch options for a filter via ajax api + * Fetch options for a filter from cache or ajax api */ function loadEntityRefFilterOptions(filter, filterSpec, $valField, $el) { - $valField.prop('disabled', true); // Fieldname may be prefixed with joins - strip those out - var fieldName = _.last(filter.key.split('.')); + var fieldName = _.last(filter.key.split('.')), + params = $.extend({params: {}}, $el.data('api-params') || {}).params; + if (filterSpec.options) { + setEntityRefFilterOptions($valField, fieldName, params, filterSpec); + return; + } + $('.crm-entityref-filters select', '#select2-drop').prop('disabled', true); CRM.api3(filterSpec.entity, 'getoptions', {field: fieldName, context: 'search', sequential: 1}) .done(function(result) { - var entity = $el.data('api-entity').toLowerCase(), - globalFilterSpec = _.find(CRM.config.entityRef.filters[entity], {key: filter.key}) || {}; + var entity = $el.data('api-entity').toLowerCase(); // Store options globally so we don't have to look them up again - globalFilterSpec.options = result.values; - $valField.prop('disabled', false); - CRM.utils.setOptions($valField, result.values); + filterSpec.options = result.values; + $('.crm-entityref-filters select', '#select2-drop').prop('disabled', false); + setEntityRefFilterOptions($valField, fieldName, params, filterSpec); $valField.val(filter.value || ''); }); } + function setEntityRefFilterOptions($valField, fieldName, params, filterSpec) { + var values = _.cloneDeep(filterSpec.options); + if (fieldName === 'contact_type' && params.contact_type) { + values = _.remove(values, function(option) { + return option.key.indexOf(params.contact_type + '__') === 0; + }); + } + CRM.utils.setOptions($valField, values); + } + //CRM-15598 - Override url validator method to allow relative url's (e.g. /index.htm) $.validator.addMethod("url", function(value, element) { if (/^\//.test(value)) { -- 2.25.1