From 6b77aac89c68d477cee3eea98e95f214ed9dfb49 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 11 May 2022 10:30:36 -0400 Subject: [PATCH] SearchKit - Allow functions in the GROUP BY clause Fixes dev/core#2485 --- .../ang/crmSearchAdmin/compose.html | 5 ++- .../crmSearchAdmin.component.js | 4 +++ .../crmSearchFunction.component.js | 2 +- .../api/v4/SearchDisplay/SearchRunTest.php | 35 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ext/search_kit/ang/crmSearchAdmin/compose.html b/ext/search_kit/ang/crmSearchAdmin/compose.html index 173da60ff2ab..3d773cfcd25c 100644 --- a/ext/search_kit/ang/crmSearchAdmin/compose.html +++ b/ext/search_kit/ang/crmSearchAdmin/compose.html @@ -26,7 +26,10 @@
- + + + +
diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js index 93d7180e5f0f..6b1a1c1a93a4 100644 --- a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js @@ -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; diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchFunction.component.js b/ext/search_kit/ang/crmSearchAdmin/crmSearchFunction.component.js index 4de24f01223a..251afd3d5074 100644 --- a/ext/search_kit/ang/crmSearchAdmin/crmSearchFunction.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchFunction.component.js @@ -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'); diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index 2b8a2c02c67e..7a2b6e0386aa 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -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); + } + }