1 (function(angular
, $, _
) {
4 angular
.module('searchActions').controller('SaveSmartGroup', function ($scope
, $element
, $timeout
, crmApi4
, dialogService
, searchMeta
) {
5 var ts
= $scope
.ts
= CRM
.ts(),
7 joins
= _
.pluck((model
.api_params
.join
|| []), 0),
9 $scope
.groupEntityRefParams
= {
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']
17 minimumInputLength
: 0,
18 placeholder
: ts('Select existing group')
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') {
30 id
: prefix
+ field
.name
,
31 text
: entity
.titlePlural
+ (entityCount
[entityName
] ? ' ' + entityCount
[entityName
] : '') + ': ' + field
.label
,
36 entityCount
[entityName
] = 1 + (entityCount
[entityName
] || 1);
39 if (!$scope
.columns
.length
) {
40 CRM
.alert(ts('Cannot create smart group; search does not include any contacts.'), ts('Error'));
42 dialogService
.cancel('saveSearchDialog');
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
);
50 if (!CRM
.checkPerm('administer reserved groups')) {
51 $scope
.groupEntityRefParams
.api
.params
.is_reserved
= 0;
54 administerReservedGroups
: CRM
.checkPerm('administer reserved groups')
56 $scope
.groupOptions
= CRM
.searchActions
.groupOptions
;
57 $element
.on('change', '#api-save-search-select-group', function() {
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
;
68 $scope
.cancel = function () {
69 dialogService
.cancel('saveSearchDialog');
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
);
80 api_entity
: model
.api_entity
,
81 api_params
: model
.api_params
84 savedSearch
.id
= model
.saved_search_id
;
86 crmApi4('SavedSearch', 'save', {records
: [savedSearch
], chain
: {group
: ['Group', 'save', {'records': [group
]}]}})
87 .then(function (result
) {
88 dialogService
.close('saveSearchDialog', result
[0]);
92 })(angular
, CRM
.$, CRM
._
);