Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search ext: Aggregate field fixes #18520

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 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 All @@ -87,6 +101,9 @@
ctrl.params.orderBy = {};
}
ctrl.params.orderBy[col] = dir;
if (ctrl.results) {
ctrl.refreshPage();
}
};

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

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

Expand Down Expand Up @@ -233,6 +262,10 @@
};

function onChangeSelect(newSelect, oldSelect) {
// When removing a column from SELECT, also remove from ORDER BY
_.each(_.difference(_.keys(ctrl.params.orderBy), newSelect), function(col) {
delete ctrl.params.orderBy[col];
});
// Re-arranging or removing columns doesn't merit a refresh, only adding columns does
if (!oldSelect || _.difference(newSelect, oldSelect).length) {
if (ctrl.autoSearch) {
Expand All @@ -243,12 +276,6 @@
}
}

function onChangeOrderBy() {
if (ctrl.results) {
ctrl.refreshPage();
}
}

function onChangeFilters() {
ctrl.stale = true;
ctrl.selectedRows.length = 0;
Expand Down Expand Up @@ -480,7 +507,6 @@
format: 'json',
default: {}
});
$scope.$watchCollection('$ctrl.params.orderBy', onChangeOrderBy);

$scope.$bindToRoute({
expr: '$ctrl.params.where',
Expand Down
2 changes: 1 addition & 1 deletion ext/search/ang/search/crmSearchFunction.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-inline">
<label>{{ $ctrl.field.title }}:</label>
<label>{{ $ctrl.field.label }}:</label>
<input class="form-control" style="width: 15em;" ng-model="$ctrl.fn" crm-ui-select="{data: $ctrl.functions, placeholder: ts('Select')}" ng-change="$ctrl.selectFunction()">
</div>