| 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.recipients.groups.include", refreshRecipients); |
| 50 | $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients); |
| 51 | $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients); |
| 52 | $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients); |
| 53 | |
| 54 | $scope.previewRecipients = function previewRecipients() { |
| 55 | return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) { |
| 56 | var model = { |
| 57 | count: $scope.recipients, |
| 58 | sample: recipients, |
| 59 | sampleLimit: RECIPIENTS_PREVIEW_LIMIT |
| 60 | }; |
| 61 | var options = CRM.utils.adjustDialogDefaults({ |
| 62 | width: '40%', |
| 63 | autoOpen: false, |
| 64 | title: ts('Preview (%1)', { |
| 65 | 1: $scope.getRecipientsEstimate() |
| 66 | }) |
| 67 | }); |
| 68 | dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); |
| 69 | })); |
| 70 | }; |
| 71 | |
| 72 | // Open a dialog for editing the advanced recipient options. |
| 73 | $scope.editOptions = function editOptions(mailing) { |
| 74 | var options = CRM.utils.adjustDialogDefaults({ |
| 75 | autoOpen: false, |
| 76 | width: '40%', |
| 77 | height: 'auto', |
| 78 | title: ts('Edit Options') |
| 79 | }); |
| 80 | $q.when(crmMetadata.getFields('Mailing')).then(function(fields) { |
| 81 | var model = { |
| 82 | fields: fields, |
| 83 | mailing: mailing |
| 84 | }; |
| 85 | dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options); |
| 86 | }); |
| 87 | }; |
| 88 | }); |
| 89 | |
| 90 | })(angular, CRM.$, CRM._); |