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 | |
7a646a01 | 8 | angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata, crmStatus, crmMailingCache) { |
6b8bd380 TO |
9 | // Time to wait before triggering AJAX update to recipients list |
10 | var RECIPIENTS_DEBOUNCE_MS = 100; | |
05752ee5 | 11 | var SETTING_DEBOUNCE_MS = 5000; |
a670e04e | 12 | var RECIPIENTS_PREVIEW_LIMIT = 50; |
6b8bd380 TO |
13 | |
14 | var ts = $scope.ts = CRM.ts(null); | |
15 | ||
16 | $scope.isMailingList = function isMailingList(group) { | |
17 | var GROUP_TYPE_MAILING_LIST = '2'; | |
18 | return _.contains(group.group_type, GROUP_TYPE_MAILING_LIST); | |
19 | }; | |
20 | ||
21 | $scope.recipients = null; | |
7a646a01 | 22 | $scope.outdated = null; |
05752ee5 | 23 | $scope.permitRecipientRebuild = null; |
7a646a01 | 24 | |
6b8bd380 TO |
25 | $scope.getRecipientsEstimate = function() { |
26 | var ts = $scope.ts; | |
27 | if ($scope.recipients === null) { | |
05752ee5 | 28 | return ts('Estimating...'); |
6b8bd380 | 29 | } |
a670e04e | 30 | if ($scope.recipients === 0) { |
7a646a01 | 31 | return ts('Estimate recipient count'); |
6b8bd380 | 32 | } |
7a646a01 | 33 | return ts('Refresh recipient count'); |
34 | }; | |
35 | ||
36 | $scope.getRecipientCount = function() { | |
37 | var ts = $scope.ts; | |
44f6941d | 38 | if ($scope.recipients === 0) { |
39 | return ts('No Recipients'); | |
40 | } | |
41 | else if ($scope.recipients > 0) { | |
42 | return ts('~%1 recipients', {1 : $scope.recipients}); | |
43 | } | |
44 | else if ($scope.outdated) { | |
45 | return ts('(unknown)'); | |
46 | } | |
47 | else { | |
48 | return $scope.permitRecipientRebuild ? ts('(unknown)') : ts('Estimating...'); | |
6b8bd380 | 49 | } |
6b8bd380 TO |
50 | }; |
51 | ||
52 | // We monitor four fields -- use debounce so that changes across the | |
53 | // four fields can settle-down before AJAX. | |
54 | var refreshRecipients = _.debounce(function() { | |
55 | $scope.$apply(function() { | |
6b8bd380 TO |
56 | if (!$scope.mailing) { |
57 | return; | |
58 | } | |
05752ee5 | 59 | crmMailingMgr.previewRecipientCount($scope.mailing, crmMailingCache, !$scope.permitRecipientRebuild).then(function(recipients) { |
60 | $scope.outdated = ($scope.permitRecipientRebuild && _.difference($scope.mailing.recipients, crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-params')) !== 0); | |
6b8bd380 TO |
61 | $scope.recipients = recipients; |
62 | }); | |
63 | }); | |
64 | }, RECIPIENTS_DEBOUNCE_MS); | |
18a48f55 | 65 | $scope.$watchCollection("mailing.dedupe_email", refreshRecipients); |
3d7f740a TO |
66 | $scope.$watchCollection("mailing.location_type_id", refreshRecipients); |
67 | $scope.$watchCollection("mailing.email_selection_method", refreshRecipients); | |
6b8bd380 TO |
68 | $scope.$watchCollection("mailing.recipients.groups.include", refreshRecipients); |
69 | $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients); | |
70 | $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients); | |
71 | $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients); | |
72 | ||
05752ee5 | 73 | // refresh setting at a duration on 5sec |
74 | var refreshSetting = _.debounce(function() { | |
75 | $scope.$apply(function() { | |
76 | crmApi('Setting', 'getvalue', {"name": 'auto_recipient_rebuild', "return": "value"}).then(function(response) { | |
77 | $scope.permitRecipientRebuild = (response.result === 0); | |
78 | }); | |
79 | }); | |
80 | }, SETTING_DEBOUNCE_MS); | |
81 | $scope.$watchCollection("permitRecipientRebuild", refreshSetting); | |
82 | ||
6b8bd380 | 83 | $scope.previewRecipients = function previewRecipients() { |
7a646a01 | 84 | var model = { |
85 | count: $scope.recipients, | |
86 | sample: crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-list'), | |
87 | sampleLimit: RECIPIENTS_PREVIEW_LIMIT | |
88 | }; | |
89 | var options = CRM.utils.adjustDialogDefaults({ | |
90 | width: '40%', | |
91 | autoOpen: false, | |
92 | title: ts('Preview (%1)', {1: $scope.getRecipientCount()}) | |
93 | }); | |
94 | ||
95 | // don't open preview dialog if there is no recipient to show. | |
05752ee5 | 96 | if ($scope.recipients !== 0 && !$scope.outdated) { |
7a646a01 | 97 | if (!_.isEmpty(model.sample)) { |
98 | dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); | |
99 | } | |
100 | else { | |
101 | return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) { | |
102 | model.sample = recipients; | |
103 | dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); | |
104 | })); | |
105 | } | |
106 | } | |
107 | }; | |
108 | ||
109 | $scope.rebuildRecipients = function rebuildRecipients() { | |
05752ee5 | 110 | // setting null will put 'Estimating..' text on refresh button |
111 | $scope.recipients = null; | |
7a646a01 | 112 | return crmMailingMgr.previewRecipientCount($scope.mailing, crmMailingCache, true).then(function(recipients) { |
113 | $scope.outdated = (recipients === 0) ? true : false; | |
114 | $scope.recipients = recipients; | |
115 | }); | |
6b8bd380 TO |
116 | }; |
117 | ||
118 | // Open a dialog for editing the advanced recipient options. | |
119 | $scope.editOptions = function editOptions(mailing) { | |
120 | var options = CRM.utils.adjustDialogDefaults({ | |
121 | autoOpen: false, | |
122 | width: '40%', | |
123 | height: 'auto', | |
124 | title: ts('Edit Options') | |
125 | }); | |
126 | $q.when(crmMetadata.getFields('Mailing')).then(function(fields) { | |
127 | var model = { | |
128 | fields: fields, | |
129 | mailing: mailing | |
130 | }; | |
fd9c35ce | 131 | dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options); |
6b8bd380 TO |
132 | }); |
133 | }; | |
134 | }); | |
135 | ||
136 | })(angular, CRM.$, CRM._); |