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 | |
a670e04e | 8 | angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata, crmStatus) { |
6b8bd380 TO |
9 | // Time to wait before triggering AJAX update to recipients list |
10 | var RECIPIENTS_DEBOUNCE_MS = 100; | |
a670e04e | 11 | var RECIPIENTS_PREVIEW_LIMIT = 50; |
6b8bd380 TO |
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 | } | |
a670e04e | 26 | if ($scope.recipients === 0) { |
6b8bd380 TO |
27 | return ts('No recipients'); |
28 | } | |
a670e04e | 29 | if ($scope.recipients === 1) { |
6b8bd380 TO |
30 | return ts('~1 recipient'); |
31 | } | |
0d77c9a7 | 32 | return ts('~%1 recipients', {1: $scope.recipients}); |
6b8bd380 TO |
33 | }; |
34 | ||
35 | // We monitor four fields -- use debounce so that changes across the | |
36 | // four fields can settle-down before AJAX. | |
37 | var refreshRecipients = _.debounce(function() { | |
38 | $scope.$apply(function() { | |
39 | $scope.recipients = null; | |
40 | if (!$scope.mailing) { | |
41 | return; | |
42 | } | |
a670e04e | 43 | crmMailingMgr.previewRecipientCount($scope.mailing).then(function(recipients) { |
6b8bd380 TO |
44 | $scope.recipients = recipients; |
45 | }); | |
46 | }); | |
47 | }, RECIPIENTS_DEBOUNCE_MS); | |
18a48f55 | 48 | $scope.$watchCollection("mailing.dedupe_email", refreshRecipients); |
3d7f740a TO |
49 | $scope.$watchCollection("mailing.location_type_id", refreshRecipients); |
50 | $scope.$watchCollection("mailing.email_selection_method", refreshRecipients); | |
6b8bd380 TO |
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() { | |
a670e04e TO |
57 | return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) { |
58 | var model = { | |
59 | count: $scope.recipients, | |
60 | sample: recipients, | |
61 | sampleLimit: RECIPIENTS_PREVIEW_LIMIT | |
62 | }; | |
63 | var options = CRM.utils.adjustDialogDefaults({ | |
64 | width: '40%', | |
65 | autoOpen: false, | |
66 | title: ts('Preview (%1)', { | |
67 | 1: $scope.getRecipientsEstimate() | |
68 | }) | |
69 | }); | |
70 | dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); | |
71 | })); | |
6b8bd380 TO |
72 | }; |
73 | ||
74 | // Open a dialog for editing the advanced recipient options. | |
75 | $scope.editOptions = function editOptions(mailing) { | |
76 | var options = CRM.utils.adjustDialogDefaults({ | |
77 | autoOpen: false, | |
78 | width: '40%', | |
79 | height: 'auto', | |
80 | title: ts('Edit Options') | |
81 | }); | |
82 | $q.when(crmMetadata.getFields('Mailing')).then(function(fields) { | |
83 | var model = { | |
84 | fields: fields, | |
85 | mailing: mailing | |
86 | }; | |
fd9c35ce | 87 | dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options); |
6b8bd380 TO |
88 | }); |
89 | }; | |
90 | }); | |
91 | ||
92 | })(angular, CRM.$, CRM._); |