Rebuild recipient list and calculate count on demand, store result in
[civicrm-core.git] / ang / crmMailing / EditRecipCtrl.js
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, crmStatus, crmMailingCache) {
9 // Time to wait before triggering AJAX update to recipients list
10 var RECIPIENTS_DEBOUNCE_MS = 100;
11 var RECIPIENTS_PREVIEW_LIMIT = 50;
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.outdated = null;
22
23 $scope.getRecipientsEstimate = function() {
24 var ts = $scope.ts;
25 if ($scope.recipients === null) {
26 return ts('(Estimating)');
27 }
28 if ($scope.recipients === 0) {
29 return ts('Estimate recipient count');
30 }
31 return ts('Refresh recipient count');
32 };
33
34 $scope.getRecipientCount = function() {
35 var ts = $scope.ts;
36 if ($scope.recipients === 0) {
37 return ts('(unknown)');
38 }
39 return ($scope.recipients === 1) ? ts('~1 recipient') : ts('~%1 recipients', {1 : $scope.recipients});
40 };
41
42 // We monitor four fields -- use debounce so that changes across the
43 // four fields can settle-down before AJAX.
44 var refreshRecipients = _.debounce(function() {
45 $scope.$apply(function() {
46 if (!$scope.mailing) {
47 return;
48 }
49 crmMailingMgr.previewRecipientCount($scope.mailing, crmMailingCache, false).then(function(recipients) {
50 $scope.outdated = (_.difference($scope.mailing.recipients, crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-params')) !== 0);
51 $scope.recipients = recipients;
52 });
53 });
54 }, RECIPIENTS_DEBOUNCE_MS);
55 $scope.$watchCollection("mailing.dedupe_email", refreshRecipients);
56 $scope.$watchCollection("mailing.location_type_id", refreshRecipients);
57 $scope.$watchCollection("mailing.email_selection_method", refreshRecipients);
58 $scope.$watchCollection("mailing.recipients.groups.include", refreshRecipients);
59 $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients);
60 $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients);
61 $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients);
62
63 $scope.previewRecipients = function previewRecipients() {
64 var model = {
65 count: $scope.recipients,
66 sample: crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-list'),
67 sampleLimit: RECIPIENTS_PREVIEW_LIMIT
68 };
69 var options = CRM.utils.adjustDialogDefaults({
70 width: '40%',
71 autoOpen: false,
72 title: ts('Preview (%1)', {1: $scope.getRecipientCount()})
73 });
74
75 // don't open preview dialog if there is no recipient to show.
76 if ($scope.recipients !== 0) {
77 if (!_.isEmpty(model.sample)) {
78 dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options);
79 }
80 else {
81 return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) {
82 model.sample = recipients;
83 dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options);
84 }));
85 }
86 }
87 };
88
89 $scope.rebuildRecipients = function rebuildRecipients() {
90 return crmMailingMgr.previewRecipientCount($scope.mailing, crmMailingCache, true).then(function(recipients) {
91 $scope.outdated = (recipients === 0) ? true : false;
92 $scope.recipients = recipients;
93 });
94 };
95
96 // Open a dialog for editing the advanced recipient options.
97 $scope.editOptions = function editOptions(mailing) {
98 var options = CRM.utils.adjustDialogDefaults({
99 autoOpen: false,
100 width: '40%',
101 height: 'auto',
102 title: ts('Edit Options')
103 });
104 $q.when(crmMetadata.getFields('Mailing')).then(function(fields) {
105 var model = {
106 fields: fields,
107 mailing: mailing
108 };
109 dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options);
110 });
111 };
112 });
113
114 })(angular, CRM.$, CRM._);