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

SearchKit - Fix display of links in aggregated columns #21420

Merged
merged 1 commit into from
Sep 15, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,36 @@ protected function augmentSelectClause(&$apiParams): void {
preg_match_all('/\\[([^]]+)\\]/', $possibleTokens, $tokens);
// Only add fields not already in SELECT clause
$additions = array_diff(array_merge($additions, $tokens[1]), $existing);
// Tokens for aggregated columns start with 'GROUP_CONCAT_'
foreach ($additions as $index => $alias) {
if (strpos($alias, 'GROUP_CONCAT_') === 0) {
$additions[$index] = 'GROUP_CONCAT(' . $this->getJoinFromAlias(explode('_', $alias, 3)[2]) . ') AS ' . $alias;
}
}
$apiParams['select'] = array_unique(array_merge($apiParams['select'], $additions));
}

/**
* Given an alias like Contact_Email_01_location_type_id
* this will return Contact_Email_01.location_type_id
* @param string $alias
* @return string
*/
protected function getJoinFromAlias(string $alias) {
$result = '';
foreach ($this->savedSearch['api_params']['join'] ?? [] as $join) {
$joinName = explode(' AS ', $join[0])[1];
if (strpos($alias, $joinName) === 0) {
$parsed = $joinName . '.' . substr($alias, strlen($joinName) + 1);
// Ensure we are using the longest match
if (strlen($parsed) > strlen($result)) {
$result = $parsed;
}
}
}
return $result;
}

/**
* Checks if a filter contains a non-empty value
*
Expand Down
5 changes: 3 additions & 2 deletions ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
if (ctrl.canAggregate(col)) {
// Ensure all non-grouped columns are aggregated if using GROUP BY
if (!info.fn || info.fn.category !== 'aggregate') {
ctrl.savedSearch.api_params.select[pos] = ctrl.DEFAULT_AGGREGATE_FN + '(DISTINCT ' + fieldExpr + ') AS ' + ctrl.DEFAULT_AGGREGATE_FN + '_DISTINCT_' + fieldExpr.replace(/[.:]/g, '_');
ctrl.savedSearch.api_params.select[pos] = ctrl.DEFAULT_AGGREGATE_FN + '(DISTINCT ' + fieldExpr + ') AS ' + ctrl.DEFAULT_AGGREGATE_FN + '_' + fieldExpr.replace(/[.:]/g, '_');
}
} else {
// Remove aggregate functions when no grouping
Expand Down Expand Up @@ -620,11 +620,12 @@
joinEntity = searchMeta.getEntity(join.entity),
primaryKey = joinEntity.primary_key[0],
isAggregate = ctrl.canAggregate(join.alias + '.' + primaryKey),
joinPrefix = (isAggregate ? 'GROUP_CONCAT_' : '') + join.alias + '.',
bridgeEntity = _.isString(joinClause[2]) ? searchMeta.getEntity(joinClause[2]) : null;
_.each(joinEntity.paths, function(path) {
var link = _.cloneDeep(path);
link.isAggregate = isAggregate;
link.path = link.path.replace(/\[/g, '[' + join.alias + '.');
link.path = link.path.replace(/\[/g, '[' + joinPrefix).replace(/[.:]/g, '_');
link.join = join.alias;
addTitle(link, join.label);
links.push(link);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@
return !col.image && !col.rewrite && !col.link && !info.fn && info.field && !info.field.readonly;
};

// Aggregate functions (COUNT, AVG, MAX) cannot display as links, except for GROUP_CONCAT
// which gets special treatment in APIv4 to convert it to an array.
this.canBeLink = function(col) {
var expr = ctrl.getExprFromSelect(col.key),
info = searchMeta.parseExpr(expr);
return !info.fn || info.fn.category !== 'aggregate' || info.fn.name === 'GROUP_CONCAT';
};

this.toggleLink = function(column) {
if (column.link) {
ctrl.onChangeLink(column, column.link.path, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

// Make a sql-friendly alias for this expression
function makeAlias() {
return (ctrl.fn + '_' + (ctrl.modifier ? ctrl.modifier + '_' : '') + ctrl.path).replace(/[.:]/g, '_');
return (ctrl.fn + '_' + ctrl.path).replace(/[.:]/g, '_');
}

this.writeExpr = function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-inline crm-search-admin-flex-row">
<div class="form-inline crm-search-admin-flex-row" ng-if=":: $ctrl.parent.canBeLink(col)">
<label title="{{:: ts('Display as clickable link') }}" >
<input type="checkbox" ng-checked="col.link" ng-click="$ctrl.parent.toggleLink(col)" >
{{:: ts('Link') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
function getViewLink(fieldExpr, links) {
var info = searchMeta.parseExpr(fieldExpr),
entity = searchMeta.getEntity(info.field.entity);
if (!info.fn && entity && info.field.fieldName === entity.label_field) {
if (entity && info.field.fieldName === entity.label_field) {
var joinEntity = searchMeta.getJoinEntity(info);
return _.find(links, {join: joinEntity, action: 'view'});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testRunWithAfform() {
'select' => [
'id',
'display_name',
'GROUP_CONCAT(DISTINCT Contact_Email_contact_id_01.email) AS GROUP_CONCAT_DISTINCT_Contact_Email_contact_id_01_email',
'GROUP_CONCAT(DISTINCT Contact_Email_contact_id_01.email) AS GROUP_CONCAT_Contact_Email_contact_id_01_email',
],
'orderBy' => [],
'where' => [
Expand Down Expand Up @@ -77,7 +77,7 @@ public function testRunWithAfform() {
'type' => 'field',
],
[
'key' => 'GROUP_CONCAT_DISTINCT_Contact_Email_contact_id_01_email',
'key' => 'GROUP_CONCAT_Contact_Email_contact_id_01_email',
'label' => 'Emails',
'dataType' => 'String',
'type' => 'field',
Expand Down