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