| 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 SETTING_DEBOUNCE_MS = 5000; |
| 12 | var RECIPIENTS_PREVIEW_LIMIT = 50; |
| 13 | |
| 14 | var ts = $scope.ts = CRM.ts(null); |
| 15 | |
| 16 | $scope.isMailingList = function isMailingList(group) { |
| 17 | var GROUP_TYPE_MAILING_LIST = '2'; |
| 18 | return _.contains(group.group_type, GROUP_TYPE_MAILING_LIST); |
| 19 | }; |
| 20 | |
| 21 | $scope.recipients = null; |
| 22 | $scope.outdated = null; |
| 23 | $scope.permitRecipientRebuild = null; |
| 24 | |
| 25 | $scope.getRecipientsEstimate = function() { |
| 26 | var ts = $scope.ts; |
| 27 | if ($scope.recipients === null) { |
| 28 | return ts('Estimating...'); |
| 29 | } |
| 30 | if ($scope.recipients === 0) { |
| 31 | return ts('Estimate recipient count'); |
| 32 | } |
| 33 | return ts('Refresh recipient count'); |
| 34 | }; |
| 35 | |
| 36 | $scope.getRecipientCount = function() { |
| 37 | var ts = $scope.ts; |
| 38 | if ($scope.recipients === 0) { |
| 39 | return ts('No Recipients'); |
| 40 | } |
| 41 | else if ($scope.recipients > 0) { |
| 42 | return ts('~%1 recipients', {1 : $scope.recipients}); |
| 43 | } |
| 44 | else if ($scope.outdated) { |
| 45 | return ts('(unknown)'); |
| 46 | } |
| 47 | else { |
| 48 | return $scope.permitRecipientRebuild ? ts('(unknown)') : ts('Estimating...'); |
| 49 | } |
| 50 | }; |
| 51 | |
| 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) { |
| 57 | return; |
| 58 | } |
| 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; |
| 62 | }); |
| 63 | }); |
| 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); |
| 72 | |
| 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); |
| 78 | }); |
| 79 | }); |
| 80 | }, SETTING_DEBOUNCE_MS); |
| 81 | $scope.$watchCollection("permitRecipientRebuild", refreshSetting); |
| 82 | |
| 83 | $scope.previewRecipients = function previewRecipients() { |
| 84 | var model = { |
| 85 | count: $scope.recipients, |
| 86 | sample: crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-list'), |
| 87 | sampleLimit: RECIPIENTS_PREVIEW_LIMIT |
| 88 | }; |
| 89 | var options = CRM.utils.adjustDialogDefaults({ |
| 90 | width: '40%', |
| 91 | autoOpen: false, |
| 92 | title: ts('Preview (%1)', {1: $scope.getRecipientCount()}) |
| 93 | }); |
| 94 | |
| 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); |
| 99 | } |
| 100 | else { |
| 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); |
| 104 | })); |
| 105 | } |
| 106 | } |
| 107 | }; |
| 108 | |
| 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; |
| 115 | }); |
| 116 | }; |
| 117 | |
| 118 | // Open a dialog for editing the advanced recipient options. |
| 119 | $scope.editOptions = function editOptions(mailing) { |
| 120 | var options = CRM.utils.adjustDialogDefaults({ |
| 121 | autoOpen: false, |
| 122 | width: '40%', |
| 123 | height: 'auto', |
| 124 | title: ts('Edit Options') |
| 125 | }); |
| 126 | $q.when(crmMetadata.getFields('Mailing')).then(function(fields) { |
| 127 | var model = { |
| 128 | fields: fields, |
| 129 | mailing: mailing |
| 130 | }; |
| 131 | dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options); |
| 132 | }); |
| 133 | }; |
| 134 | }); |
| 135 | |
| 136 | })(angular, CRM.$, CRM._); |