Add search extension
[civicrm-core.git] / ext / search / ang / search / SaveSmartGroup.ctrl.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('search').controller('SaveSmartGroup', function ($scope, crmApi4, dialogService) {
5 var ts = $scope.ts = CRM.ts(),
6 model = $scope.model;
7 $scope.groupEntityRefParams = {
8 entity: 'Group',
9 api: {
10 params: {is_hidden: 0, is_active: 1, 'saved_search_id.api_entity': model.entity},
11 extra: ['saved_search_id', 'description', 'visibility', 'group_type']
12 },
13 select: {
14 allowClear: true,
15 minimumInputLength: 0,
16 placeholder: ts('Select existing group')
17 }
18 };
19 if (!CRM.checkPerm('administer reserved groups')) {
20 $scope.groupEntityRefParams.api.params.is_reserved = 0;
21 }
22 $scope.perm = {
23 administerReservedGroups: CRM.checkPerm('administer reserved groups')
24 };
25 $scope.groupFields = _.indexBy(_.find(CRM.vars.search.schema, {name: 'Group'}).fields, 'name');
26 $scope.$watch('model.id', function (id) {
27 if (id) {
28 _.assign(model, $('#api-save-search-select-group').select2('data').extra);
29 }
30 });
31 $scope.cancel = function () {
32 dialogService.cancel('saveSearchDialog');
33 };
34 $scope.save = function () {
35 $('.ui-dialog:visible').block();
36 var group = model.id ? {id: model.id} : {title: model.title};
37 group.description = model.description;
38 group.visibility = model.visibility;
39 group.group_type = model.group_type;
40 group.saved_search_id = '$id';
41 var savedSearch = {
42 api_entity: model.entity,
43 api_params: model.params
44 };
45 if (group.id) {
46 savedSearch.id = model.saved_search_id;
47 }
48 crmApi4('SavedSearch', 'save', {records: [savedSearch], chain: {group: ['Group', 'save', {'records': [group]}]}})
49 .then(function (result) {
50 dialogService.close('saveSearchDialog', result[0]);
51 });
52 };
53 });
54 })(angular, CRM.$, CRM._);