From 5fcd63f47dc8fd43621ecbef82da7b6cb9c36ee1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 18 Sep 2020 11:27:20 -0400 Subject: [PATCH] Search ext: Ensure all non-grouped columns are aggregated if using GROUP BY --- ext/search/ang/search/crmSearch.component.js | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ext/search/ang/search/crmSearch.component.js b/ext/search/ang/search/crmSearch.component.js index 703cd364d7..5eaaee5aba 100644 --- a/ext/search/ang/search/crmSearch.component.js +++ b/ext/search/ang/search/crmSearch.component.js @@ -9,6 +9,9 @@ controller: function($scope, $element, $timeout, crmApi4, dialogService, searchMeta, formatForSelect2) { var ts = $scope.ts = CRM.ts(), ctrl = this; + + this.DEFAULT_AGGREGATE_FN = 'GROUP_CONCAT'; + this.selectedRows = []; this.limit = CRM.cache.get('searchPageSize', 30); this.page = 1; @@ -74,6 +77,17 @@ if (!ctrl.params.groupBy[idx]) { ctrl.clearParam('groupBy', idx); } + // Remove aggregate functions when no grouping + if (!ctrl.params.groupBy.length) { + _.each(ctrl.params.select, function(col, pos) { + if (_.contains(col, '(')) { + var info = searchMeta.parseExpr(col); + if (info.fn.category === 'aggregate') { + ctrl.params.select[pos] = info.path + info.suffix; + } + } + }); + } }; /** @@ -133,6 +147,17 @@ $('.crm-search-results', $element).css('height', ''); } + // Ensure all non-grouped columns are aggregated if using GROUP BY + function aggregateGroupByColumns() { + if (ctrl.params.groupBy.length) { + _.each(ctrl.params.select, function(col, pos) { + if (!_.contains(col, '(') && ctrl.canAggregate(col)) { + ctrl.params.select[pos] = ctrl.DEFAULT_AGGREGATE_FN + '(' + col + ')'; + } + }); + } + } + // Debounced callback for loadResults function _loadResultsCallback() { // Multiply limit to read 2 pages at once & save ajax requests @@ -181,6 +206,7 @@ function loadResults() { $scope.loading = true; + aggregateGroupByColumns(); _loadResults(); } -- 2.25.1