Merge pull request #9648 from MegaphoneJon/CRM-19608
[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) {
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.getRecipientsEstimate = function() {
22 var ts = $scope.ts;
23 if ($scope.recipients === null) {
24 return ts('(Estimating)');
25 }
26 if ($scope.recipients === 0) {
27 return ts('No recipients');
28 }
29 if ($scope.recipients === 1) {
30 return ts('~1 recipient');
31 }
32 return ts('~%1 recipients', {1: $scope.recipients});
33 };
34
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) {
41 return;
42 }
43 crmMailingMgr.previewRecipientCount($scope.mailing).then(function(recipients) {
44 $scope.recipients = recipients;
45 });
46 });
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);
55
56 $scope.previewRecipients = function previewRecipients() {
57 return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) {
58 var model = {
59 count: $scope.recipients,
60 sample: recipients,
61 sampleLimit: RECIPIENTS_PREVIEW_LIMIT
62 };
63 var options = CRM.utils.adjustDialogDefaults({
64 width: '40%',
65 autoOpen: false,
66 title: ts('Preview (%1)', {
67 1: $scope.getRecipientsEstimate()
68 })
69 });
70 dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options);
71 }));
72 };
73
74 // Open a dialog for editing the advanced recipient options.
75 $scope.editOptions = function editOptions(mailing) {
76 var options = CRM.utils.adjustDialogDefaults({
77 autoOpen: false,
78 width: '40%',
79 height: 'auto',
80 title: ts('Edit Options')
81 });
82 $q.when(crmMetadata.getFields('Mailing')).then(function(fields) {
83 var model = {
84 fields: fields,
85 mailing: mailing
86 };
87 dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options);
88 });
89 };
90 });
91
92 })(angular, CRM.$, CRM._);