1 (function(angular
, $, _
) {
4 angular
.module('crmSearchActions').controller('SaveSmartGroup', function ($scope
, $element
, $timeout
, crmApi4
, dialogService
, searchMeta
) {
5 var ts
= $scope
.ts
= CRM
.ts(),
7 $scope
.groupEntityRefParams
= {
10 params
: {is_hidden
: 0, is_active
: 1, 'saved_search_id.api_entity': model
.api_entity
},
11 extra
: ['saved_search_id', 'description', 'visibility', 'group_type']
15 minimumInputLength
: 0,
16 placeholder
: ts('Select existing group')
19 $scope
.columns
= searchMeta
.getSmartGroupColumns(model
.api_entity
, model
.api_params
);
21 if (!$scope
.columns
.length
) {
22 CRM
.alert(ts('Cannot create smart group; search does not include any contacts.'), ts('Error'));
24 dialogService
.cancel('saveSearchDialog');
29 // Pick the first applicable column for contact id
30 model
.api_params
.select
.unshift(_
.intersection(model
.api_params
.select
, _
.pluck($scope
.columns
, 'id'))[0] || $scope
.columns
[0].id
);
32 if (!CRM
.checkPerm('administer reserved groups')) {
33 $scope
.groupEntityRefParams
.api
.params
.is_reserved
= 0;
36 administerReservedGroups
: CRM
.checkPerm('administer reserved groups')
38 $scope
.groupOptions
= CRM
.crmSearchActions
.groupOptions
;
39 $element
.on('change', '#api-save-search-select-group', function() {
41 $scope
.$apply(function() {
42 var group
= $('#api-save-search-select-group').select2('data').extra
;
43 model
.saved_search_id
= group
.saved_search_id
;
44 model
.description
= group
.description
|| '';
45 model
.group_type
= group
.group_type
|| [];
46 model
.visibility
= group
.visibility
;
50 $scope
.cancel = function () {
51 dialogService
.cancel('saveSearchDialog');
53 $scope
.save = function () {
54 $('.ui-dialog:visible').block();
55 var group
= model
.id
? {id
: model
.id
} : {title
: model
.title
};
56 group
.description
= model
.description
;
57 group
.visibility
= model
.visibility
;
58 group
.group_type
= model
.group_type
;
59 group
.saved_search_id
= '$id';
60 model
.api_params
.select
= _
.unique(model
.api_params
.select
);
62 api_entity
: model
.api_entity
,
63 api_params
: model
.api_params
66 savedSearch
.id
= model
.saved_search_id
;
68 crmApi4('SavedSearch', 'save', {records
: [savedSearch
], chain
: {group
: ['Group', 'save', {'records': [group
]}]}})
69 .then(function (result
) {
70 dialogService
.close('saveSearchDialog', result
[0]);
74 })(angular
, CRM
.$, CRM
._
);