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
) {
9 // Time to wait before triggering AJAX update to recipients list
10 var RECIPIENTS_DEBOUNCE_MS
= 100;
11 var RECIPIENTS_PREVIEW_LIMIT
= 50;
13 var ts
= $scope
.ts
= CRM
.ts(null);
15 $scope
.isMailingList
= function isMailingList(group
) {
16 var GROUP_TYPE_MAILING_LIST
= '2';
17 return _
.contains(group
.group_type
, GROUP_TYPE_MAILING_LIST
);
20 $scope
.recipients
= null;
21 $scope
.getRecipientsEstimate = function() {
23 if ($scope
.recipients
=== null) {
24 return ts('(Estimating)');
26 if ($scope
.recipients
=== 0) {
27 return ts('No recipients');
29 if ($scope
.recipients
=== 1) {
30 return ts('~1 recipient');
32 return ts('~%1 recipients', {1: $scope
.recipients
});
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
) {
43 crmMailingMgr
.previewRecipientCount($scope
.mailing
).then(function(recipients
) {
44 $scope
.recipients
= recipients
;
47 }, RECIPIENTS_DEBOUNCE_MS
);
48 $scope
.$watchCollection("mailing.dedupe_email", refreshRecipients
);
49 $scope
.$watchCollection("mailing.location_type_id", refreshRecipients
);
50 $scope
.$watchCollection("mailing.email_selection_method", refreshRecipients
);
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
);
56 $scope
.previewRecipients
= function previewRecipients() {
57 return crmStatus({start
: ts('Previewing...'), success
: ''}, crmMailingMgr
.previewRecipients($scope
.mailing
, RECIPIENTS_PREVIEW_LIMIT
).then(function(recipients
) {
59 count
: $scope
.recipients
,
61 sampleLimit
: RECIPIENTS_PREVIEW_LIMIT
63 var options
= CRM
.utils
.adjustDialogDefaults({
66 title
: ts('Preview (%1)', {
67 1: $scope
.getRecipientsEstimate()
70 dialogService
.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model
, options
);
74 // Open a dialog for editing the advanced recipient options.
75 $scope
.editOptions
= function editOptions(mailing
) {
76 var options
= CRM
.utils
.adjustDialogDefaults({
80 title
: ts('Edit Options')
82 $q
.when(crmMetadata
.getFields('Mailing')).then(function(fields
) {
87 dialogService
.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model
, options
);
92 })(angular
, CRM
.$, CRM
._
);