Skip to content

Commit

Permalink
Search ext: Ensure all non-grouped columns are aggregated if using GR…
Browse files Browse the repository at this point in the history
…OUP BY
  • Loading branch information
colemanw committed Sep 18, 2020
1 parent 7da9d07 commit 5fcd63f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ext/search/ang/search/crmSearch.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
});
}
};

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -181,6 +206,7 @@

function loadResults() {
$scope.loading = true;
aggregateGroupByColumns();
_loadResults();
}

Expand Down

0 comments on commit 5fcd63f

Please sign in to comment.