Commit | Line | Data |
---|---|---|
6b8bd380 TO |
1 | (function(angular, $, _) { |
2 | ||
3 | // Controller for the edit-recipients fields ( | |
4 | // WISHLIST: Move most of this to a (cache-enabled) service | |
5 | // Scope members: | |
6 | // - [input] mailing: object | |
7 | // - [output] recipients: array of recipient records | |
8 | angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata) { | |
9 | // Time to wait before triggering AJAX update to recipients list | |
10 | var RECIPIENTS_DEBOUNCE_MS = 100; | |
11 | var RECIPIENTS_PREVIEW_LIMIT = 10000; | |
12 | ||
13 | var ts = $scope.ts = CRM.ts(null); | |
14 | ||
15 | $scope.isMailingList = function isMailingList(group) { | |
16 | var GROUP_TYPE_MAILING_LIST = '2'; | |
17 | return _.contains(group.group_type, GROUP_TYPE_MAILING_LIST); | |
18 | }; | |
19 | ||
20 | $scope.recipients = null; | |
21 | $scope.getRecipientsEstimate = function() { | |
22 | var ts = $scope.ts; | |
23 | if ($scope.recipients === null) { | |
24 | return ts('(Estimating)'); | |
25 | } | |
26 | if ($scope.recipients.length === 0) { | |
27 | return ts('No recipients'); | |
28 | } | |
29 | if ($scope.recipients.length === 1) { | |
30 | return ts('~1 recipient'); | |
31 | } | |
32 | if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT) { | |
33 | return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT}); | |
34 | } | |
35 | return ts('~%1 recipients', {1: $scope.recipients.length}); | |
36 | }; | |
37 | ||
38 | // We monitor four fields -- use debounce so that changes across the | |
39 | // four fields can settle-down before AJAX. | |
40 | var refreshRecipients = _.debounce(function() { | |
41 | $scope.$apply(function() { | |
42 | $scope.recipients = null; | |
43 | if (!$scope.mailing) { | |
44 | return; | |
45 | } | |
46 | crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) { | |
47 | $scope.recipients = recipients; | |
48 | }); | |
49 | }); | |
50 | }, RECIPIENTS_DEBOUNCE_MS); | |
51 | $scope.$watchCollection("mailing.recipients.groups.include", refreshRecipients); | |
52 | $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients); | |
53 | $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients); | |
54 | $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients); | |
55 | ||
56 | $scope.previewRecipients = function previewRecipients() { | |
57 | var model = { | |
58 | recipients: $scope.recipients | |
59 | }; | |
60 | var options = CRM.utils.adjustDialogDefaults({ | |
61 | width: '40%', | |
62 | autoOpen: false, | |
63 | title: ts('Preview (%1)', { | |
64 | 1: $scope.getRecipientsEstimate() | |
65 | }) | |
66 | }); | |
fd9c35ce | 67 | dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); |
6b8bd380 TO |
68 | }; |
69 | ||
70 | // Open a dialog for editing the advanced recipient options. | |
71 | $scope.editOptions = function editOptions(mailing) { | |
72 | var options = CRM.utils.adjustDialogDefaults({ | |
73 | autoOpen: false, | |
74 | width: '40%', | |
75 | height: 'auto', | |
76 | title: ts('Edit Options') | |
77 | }); | |
78 | $q.when(crmMetadata.getFields('Mailing')).then(function(fields) { | |
79 | var model = { | |
80 | fields: fields, | |
81 | mailing: mailing | |
82 | }; | |
fd9c35ce | 83 | dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options); |
6b8bd380 TO |
84 | }); |
85 | }; | |
86 | }); | |
87 | ||
88 | })(angular, CRM.$, CRM._); |