1 (function(angular
, $, _
) {
3 // Controller for the edit-recipients fields (
4 // WISHLIST: Move most of this to a (cache-enabled) service
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 SETTING_DEBOUNCE_MS
= 5000;
12 var RECIPIENTS_PREVIEW_LIMIT
= 50;
14 var ts
= $scope
.ts
= CRM
.ts(null);
16 $scope
.isMailingList
= function isMailingList(group
) {
17 var GROUP_TYPE_MAILING_LIST
= '2';
18 return _
.contains(group
.group_type
, GROUP_TYPE_MAILING_LIST
);
21 $scope
.recipients
= null;
22 $scope
.outdated
= null;
23 $scope
.permitRecipientRebuild
= null;
25 $scope
.getRecipientsEstimate = function() {
27 if ($scope
.recipients
=== null) {
28 return ts('Estimating...');
30 if ($scope
.recipients
=== 0) {
31 return ts('Estimate recipient count');
33 return ts('Refresh recipient count');
36 $scope
.getRecipientCount = function() {
38 if ($scope
.recipients
=== 0) {
39 return ts('No Recipients');
41 else if ($scope
.recipients
> 0) {
42 return ts('~%1 recipients', {1 : $scope
.recipients
});
44 else if ($scope
.outdated
) {
45 return ts('(unknown)');
48 return $scope
.permitRecipientRebuild
? ts('(unknown)') : ts('Estimating...');
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() {
56 if (!$scope
.mailing
) {
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);
61 $scope
.recipients
= recipients
;
64 }, RECIPIENTS_DEBOUNCE_MS
);
65 $scope
.$watchCollection("mailing.dedupe_email", refreshRecipients
);
66 $scope
.$watchCollection("mailing.location_type_id", refreshRecipients
);
67 $scope
.$watchCollection("mailing.email_selection_method", refreshRecipients
);
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
);
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);
80 }, SETTING_DEBOUNCE_MS
);
81 $scope
.$watchCollection("permitRecipientRebuild", refreshSetting
);
83 $scope
.previewRecipients
= function previewRecipients() {
85 count
: $scope
.recipients
,
86 sample
: crmMailingCache
.get('mailing-' + $scope
.mailing
.id
+ '-recipient-list'),
87 sampleLimit
: RECIPIENTS_PREVIEW_LIMIT
89 var options
= CRM
.utils
.adjustDialogDefaults({
92 title
: ts('Preview (%1)', {1: $scope
.getRecipientCount()})
95 // don't open preview dialog if there is no recipient to show.
96 if ($scope
.recipients
!== 0 && !$scope
.outdated
) {
97 if (!_
.isEmpty(model
.sample
)) {
98 dialogService
.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model
, options
);
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
);
109 $scope
.rebuildRecipients
= function rebuildRecipients() {
110 // setting null will put 'Estimating..' text on refresh button
111 $scope
.recipients
= null;
112 return crmMailingMgr
.previewRecipientCount($scope
.mailing
, crmMailingCache
, true).then(function(recipients
) {
113 $scope
.outdated
= (recipients
=== 0) ? true : false;
114 $scope
.recipients
= recipients
;
118 // Open a dialog for editing the advanced recipient options.
119 $scope
.editOptions
= function editOptions(mailing
) {
120 var options
= CRM
.utils
.adjustDialogDefaults({
124 title
: ts('Edit Options')
126 $q
.when(crmMetadata
.getFields('Mailing')).then(function(fields
) {
131 dialogService
.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model
, options
);
136 })(angular
, CRM
.$, CRM
._
);