From 73d7dbcd37687a6e5a971f48e443161dac02a84b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 29 Sep 2020 11:30:19 -0400 Subject: [PATCH] Search ext: Choose smart group contact column --- ext/search/CRM/Search/Page/Ang.php | 2 +- ext/search/ang/search/SaveSmartGroup.ctrl.js | 42 +++++++++++++++++--- ext/search/ang/search/crmSearch.component.js | 14 +++---- ext/search/ang/search/saveSmartGroup.html | 20 +++++++--- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/ext/search/CRM/Search/Page/Ang.php b/ext/search/CRM/Search/Page/Ang.php index c2619910c7..0c9c228f21 100644 --- a/ext/search/CRM/Search/Page/Ang.php +++ b/ext/search/CRM/Search/Page/Ang.php @@ -90,7 +90,7 @@ class CRM_Search_Page_Ang extends CRM_Core_Page { ->setChain([ 'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']], ])->execute(); - $getFields = ['name', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize']; + $getFields = ['name', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize', 'fk_entity']; foreach ($schema as $entity) { // Skip if entity doesn't have a 'get' action or the user doesn't have permission to use get if ($entity['get']) { diff --git a/ext/search/ang/search/SaveSmartGroup.ctrl.js b/ext/search/ang/search/SaveSmartGroup.ctrl.js index 18b0298e96..ece503cf28 100644 --- a/ext/search/ang/search/SaveSmartGroup.ctrl.js +++ b/ext/search/ang/search/SaveSmartGroup.ctrl.js @@ -1,13 +1,15 @@ (function(angular, $, _) { "use strict"; - angular.module('search').controller('SaveSmartGroup', function ($scope, $element, crmApi4, dialogService) { + angular.module('search').controller('SaveSmartGroup', function ($scope, $element, $timeout, crmApi4, dialogService, searchMeta) { var ts = $scope.ts = CRM.ts(), - model = $scope.model; + model = $scope.model, + joins = _.pluck((model.api_params.join || []), 0), + entityCount = {}; $scope.groupEntityRefParams = { entity: 'Group', api: { - params: {is_hidden: 0, is_active: 1, 'saved_search_id.api_entity': model.entity}, + params: {is_hidden: 0, is_active: 1, 'saved_search_id.api_entity': model.api_entity}, extra: ['saved_search_id', 'description', 'visibility', 'group_type'] }, select: { @@ -16,6 +18,36 @@ placeholder: ts('Select existing group') } }; + // Find all possible search columns that could serve as contact_id for the smart group + $scope.columns = _.transform([model.api_entity].concat(joins), function(columns, joinExpr) { + var joinName = joinExpr.split(' AS '), + entityName = joinName[0], + entity = searchMeta.getEntity(entityName), + prefix = joinName[1] ? joinName[1] + '.' : ''; + _.each(entity.fields, function(field) { + if ((entityName === 'Contact' && field.name === 'id') || field.fk_entity === 'Contact') { + columns.push({ + id: prefix + field.name, + text: entity.titlePlural + (entityCount[entityName] ? ' ' + entityCount[entityName] : '') + ': ' + field.label, + icon: entity.icon + }); + } + }); + entityCount[entityName] = 1 + (entityCount[entityName] || 1); + }); + + if (!$scope.columns.length) { + CRM.alert(ts('Cannot create smart group; search does not include any contacts.'), ts('Error')); + $timeout(function() { + dialogService.cancel('saveSearchDialog'); + }); + return; + } + + // Pick the first applicable column for contact id + model.api_params.select[0] = _.intersection(model.api_params.select, _.pluck($scope.columns, 'id'))[0] || $scope.columns[0].name; + model.api_params.select.length = 1; + if (!CRM.checkPerm('administer reserved groups')) { $scope.groupEntityRefParams.api.params.is_reserved = 0; } @@ -45,8 +77,8 @@ group.group_type = model.group_type; group.saved_search_id = '$id'; var savedSearch = { - api_entity: model.entity, - api_params: model.params + api_entity: model.api_entity, + api_params: model.api_params }; if (group.id) { savedSearch.id = model.saved_search_id; diff --git a/ext/search/ang/search/crmSearch.component.js b/ext/search/ang/search/crmSearch.component.js index 88ba4c7b11..33ed7f3ca9 100644 --- a/ext/search/ang/search/crmSearch.component.js +++ b/ext/search/ang/search/crmSearch.component.js @@ -558,21 +558,19 @@ }; $scope.saveGroup = function() { - var selectField = ctrl.entity === 'Contact' ? 'id' : 'contact_id'; - if (ctrl.entity !== 'Contact' && !searchMeta.getField('contact_id')) { - CRM.alert(ts('Cannot create smart group from %1.', {1: searchMeta.getEntity(true).titlePlural}), ts('Missing contact_id'), 'error', {expires: 5000}); - return; - } var model = { title: '', description: '', visibility: 'User and User Admin Only', group_type: [], id: ctrl.load ? ctrl.load.id : null, - entity: ctrl.entity, - params: angular.extend({}, ctrl.params, {version: 4}) + api_entity: ctrl.entity, + api_params: _.cloneDeep(angular.extend({}, ctrl.params, {version: 4})) }; - delete model.params.orderBy; + delete model.api_params.orderBy; + if (ctrl.load && ctrl.load.api_params) { + model.api_params.select = ctrl.load.api_params.select; + } var options = CRM.utils.adjustDialogDefaults({ autoOpen: false, title: ts('Save smart group') diff --git a/ext/search/ang/search/saveSmartGroup.html b/ext/search/ang/search/saveSmartGroup.html index a0caf4af2c..a2ef056047 100644 --- a/ext/search/ang/search/saveSmartGroup.html +++ b/ext/search/ang/search/saveSmartGroup.html @@ -1,22 +1,30 @@
+
- + +
+
+ + +

-
-
+ +
  +  
- - +
+ + +

-- 2.25.1