Merge pull request #18770 from civicrm/5.31
[civicrm-core.git] / ext / search / ang / searchActions / SaveSmartGroup.ctrl.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('searchActions').controller('SaveSmartGroup', function ($scope, $element, $timeout, crmApi4, dialogService, searchMeta) {
5 var ts = $scope.ts = CRM.ts(),
6 model = $scope.model,
7 joins = _.pluck((model.api_params.join || []), 0),
8 entityCount = {};
9 $scope.groupEntityRefParams = {
10 entity: 'Group',
11 api: {
12 params: {is_hidden: 0, is_active: 1, 'saved_search_id.api_entity': model.api_entity},
13 extra: ['saved_search_id', 'description', 'visibility', 'group_type']
14 },
15 select: {
16 allowClear: true,
17 minimumInputLength: 0,
18 placeholder: ts('Select existing group')
19 }
20 };
21 // Find all possible search columns that could serve as contact_id for the smart group
22 $scope.columns = _.transform([model.api_entity].concat(joins), function(columns, joinExpr) {
23 var joinName = joinExpr.split(' AS '),
24 entityName = joinName[0],
25 entity = searchMeta.getEntity(entityName),
26 prefix = joinName[1] ? joinName[1] + '.' : '';
27 _.each(entity.fields, function(field) {
28 if ((entityName === 'Contact' && field.name === 'id') || field.fk_entity === 'Contact') {
29 columns.push({
30 id: prefix + field.name,
31 text: entity.titlePlural + (entityCount[entityName] ? ' ' + entityCount[entityName] : '') + ': ' + field.label,
32 icon: entity.icon
33 });
34 }
35 });
36 entityCount[entityName] = 1 + (entityCount[entityName] || 1);
37 });
38
39 if (!$scope.columns.length) {
40 CRM.alert(ts('Cannot create smart group; search does not include any contacts.'), ts('Error'));
41 $timeout(function() {
42 dialogService.cancel('saveSearchDialog');
43 });
44 return;
45 }
46
47 // Pick the first applicable column for contact id
48 model.api_params.select.unshift(_.intersection(model.api_params.select, _.pluck($scope.columns, 'id'))[0] || $scope.columns[0].id);
49
50 if (!CRM.checkPerm('administer reserved groups')) {
51 $scope.groupEntityRefParams.api.params.is_reserved = 0;
52 }
53 $scope.perm = {
54 administerReservedGroups: CRM.checkPerm('administer reserved groups')
55 };
56 $scope.groupOptions = CRM.searchActions.groupOptions;
57 $element.on('change', '#api-save-search-select-group', function() {
58 if ($(this).val()) {
59 $scope.$apply(function() {
60 var group = $('#api-save-search-select-group').select2('data').extra;
61 model.saved_search_id = group.saved_search_id;
62 model.description = group.description || '';
63 model.group_type = group.group_type || [];
64 model.visibility = group.visibility;
65 });
66 }
67 });
68 $scope.cancel = function () {
69 dialogService.cancel('saveSearchDialog');
70 };
71 $scope.save = function () {
72 $('.ui-dialog:visible').block();
73 var group = model.id ? {id: model.id} : {title: model.title};
74 group.description = model.description;
75 group.visibility = model.visibility;
76 group.group_type = model.group_type;
77 group.saved_search_id = '$id';
78 model.api_params.select = _.unique(model.api_params.select);
79 var savedSearch = {
80 api_entity: model.api_entity,
81 api_params: model.api_params
82 };
83 if (group.id) {
84 savedSearch.id = model.saved_search_id;
85 }
86 crmApi4('SavedSearch', 'save', {records: [savedSearch], chain: {group: ['Group', 'save', {'records': [group]}]}})
87 .then(function (result) {
88 dialogService.close('saveSearchDialog', result[0]);
89 });
90 };
91 });
92 })(angular, CRM.$, CRM._);