Skip to content

Commit

Permalink
Merge pull request #23434 from colemanw/groupByFn
Browse files Browse the repository at this point in the history
SearchKit - Allow functions in the GROUP BY clause
  • Loading branch information
eileenmcnaughton authored May 12, 2022
2 parents fec18a5 + 6b77aac commit 696bbcf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion ext/search_kit/ang/crmSearchAdmin/compose.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
<fieldset ng-if=":: $ctrl.paramExists('groupBy')">
<div class="form-inline" ng-repeat="groupBy in $ctrl.savedSearch.api_params.groupBy">
<label for="crm-search-groupBy-{{ $index }}">{{:: ts('Group By') }}</label>
<input id="crm-search-groupBy-{{ $index }}" class="form-control huge" ng-model="$ctrl.savedSearch.api_params.groupBy[$index]" crm-ui-select="{placeholder: ' ', data: fieldsForGroupBy}" ng-change="changeGroupBy($index)" />
<crm-search-function class="form-group" expr="$ctrl.savedSearch.api_params.groupBy[$index]" mode="groupBy"></crm-search-function>
<span ng-if="!$ctrl.hasFunction($ctrl.savedSearch.api_params.groupBy[$index])">
<input id="crm-search-groupBy-{{ $index }}" class="form-control huge" ng-model="$ctrl.savedSearch.api_params.groupBy[$index]" crm-ui-select="{placeholder: ' ', data: fieldsForGroupBy}" ng-change="changeGroupBy($index)" />
</span>
<hr>
</div>
<div class="form-inline">
Expand Down
4 changes: 4 additions & 0 deletions ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@
return _.includes(searchMeta.getEntity(ctrl.savedSearch.api_entity).params, param);
};

this.hasFunction = function(expr) {
return expr.indexOf('(') > -1;
};

this.addDisplay = function(type) {
var count = _.filter(ctrl.savedSearch.displays, {type: type}).length,
searchLabel = ctrl.savedSearch.label || searchMeta.getEntity(ctrl.savedSearch.api_entity).title_plural;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
this.getFunctions = function() {
var allowedTypes = [], functions = [];
if (ctrl.expr && ctrl.fieldArg) {
if (ctrl.crmSearchAdmin.canAggregate(ctrl.expr)) {
if (ctrl.mode !== 'groupBy' && ctrl.crmSearchAdmin.canAggregate(ctrl.expr)) {
allowedTypes.push('aggregate');
} else {
allowedTypes.push('comparison', 'string');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1079,4 +1079,39 @@ public function testGroupByContactType(): void {
$this->assertEquals(1, $data['Household']);
}

public function testGroupByFunction(): void {
$source = uniqid(__FUNCTION__);
$sampleData = [
['birth_date' => '2009-02-05'],
['birth_date' => '1999-02-22'],
['birth_date' => '2012-05-06'],
];
Contact::save(FALSE)
->addDefault('source', $source)
->setRecords($sampleData)
->execute();

$params = [
'checkPermissions' => FALSE,
'return' => 'page:1',
'savedSearch' => [
'api_entity' => 'Contact',
'api_params' => [
'version' => 4,
'select' => ['COUNT(id) AS COUNT_id'],
'where' => [['source', '=', $source]],
'groupBy' => ['MONTH(birth_date)'],
],
],
'display' => NULL,
'afform' => NULL,
];

$result = civicrm_api4('SearchDisplay', 'run', $params);
$this->assertCount(2, $result);
$data = array_column(array_column((array) $result, 'data'), 'COUNT_id');
sort($data);
$this->assertEquals([1, 2], $data);
}

}

0 comments on commit 696bbcf

Please sign in to comment.