X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=ang%2FcrmMailing%2FEditRecipCtrl.js;h=9fca637a73d8be3edf87b04e009f1487de97bffb;hb=3bbe587eca52e262562cd1b06484eefa30480a08;hp=16c125f8cfcad2fd5f50663615392202f6137b4d;hpb=c32ee3b843155f673248969ec562bda753611ff1;p=civicrm-core.git diff --git a/ang/crmMailing/EditRecipCtrl.js b/ang/crmMailing/EditRecipCtrl.js index 16c125f8cf..9fca637a73 100644 --- a/ang/crmMailing/EditRecipCtrl.js +++ b/ang/crmMailing/EditRecipCtrl.js @@ -5,9 +5,10 @@ // Scope members: // - [input] mailing: object // - [output] recipients: array of recipient records - angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata, crmStatus) { + angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata, crmStatus, crmMailingCache) { // Time to wait before triggering AJAX update to recipients list var RECIPIENTS_DEBOUNCE_MS = 100; + var SETTING_DEBOUNCE_MS = 5000; var RECIPIENTS_PREVIEW_LIMIT = 50; var ts = $scope.ts = CRM.ts(null); @@ -18,29 +19,45 @@ }; $scope.recipients = null; + $scope.outdated = null; + $scope.permitRecipientRebuild = null; + $scope.getRecipientsEstimate = function() { var ts = $scope.ts; if ($scope.recipients === null) { - return ts('(Estimating)'); + return ts('Estimating...'); } if ($scope.recipients === 0) { - return ts('No recipients'); + return ts('Estimate recipient count'); + } + return ts('Refresh recipient count'); + }; + + $scope.getRecipientCount = function() { + var ts = $scope.ts; + if ($scope.recipients === 0) { + return ts('No Recipients'); + } + else if ($scope.recipients > 0) { + return ts('~%1 recipients', {1 : $scope.recipients}); } - if ($scope.recipients === 1) { - return ts('~1 recipient'); + else if ($scope.outdated) { + return ts('(unknown)'); + } + else { + return $scope.permitRecipientRebuild ? ts('(unknown)') : ts('Estimating...'); } - return ts('~%1 recipients', {1: $scope.recipients}); }; // We monitor four fields -- use debounce so that changes across the // four fields can settle-down before AJAX. var refreshRecipients = _.debounce(function() { $scope.$apply(function() { - $scope.recipients = null; if (!$scope.mailing) { return; } - crmMailingMgr.previewRecipientCount($scope.mailing).then(function(recipients) { + crmMailingMgr.previewRecipientCount($scope.mailing, crmMailingCache, !$scope.permitRecipientRebuild).then(function(recipients) { + $scope.outdated = ($scope.permitRecipientRebuild && _.difference($scope.mailing.recipients, crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-params')) !== 0); $scope.recipients = recipients; }); }); @@ -53,22 +70,49 @@ $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients); $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients); - $scope.previewRecipients = function previewRecipients() { - return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) { - var model = { - count: $scope.recipients, - sample: recipients, - sampleLimit: RECIPIENTS_PREVIEW_LIMIT - }; - var options = CRM.utils.adjustDialogDefaults({ - width: '40%', - autoOpen: false, - title: ts('Preview (%1)', { - 1: $scope.getRecipientsEstimate() - }) + // refresh setting at a duration on 5sec + var refreshSetting = _.debounce(function() { + $scope.$apply(function() { + crmApi('Setting', 'getvalue', {"name": 'auto_recipient_rebuild', "return": "value"}).then(function(response) { + $scope.permitRecipientRebuild = (response.result === 0); }); - dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); - })); + }); + }, SETTING_DEBOUNCE_MS); + $scope.$watchCollection("permitRecipientRebuild", refreshSetting); + + $scope.previewRecipients = function previewRecipients() { + var model = { + count: $scope.recipients, + sample: crmMailingCache.get('mailing-' + $scope.mailing.id + '-recipient-list'), + sampleLimit: RECIPIENTS_PREVIEW_LIMIT + }; + var options = CRM.utils.adjustDialogDefaults({ + width: '40%', + autoOpen: false, + title: ts('Preview (%1)', {1: $scope.getRecipientCount()}) + }); + + // don't open preview dialog if there is no recipient to show. + if ($scope.recipients !== 0 && !$scope.outdated) { + if (!_.isEmpty(model.sample)) { + dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); + } + else { + return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) { + model.sample = recipients; + dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options); + })); + } + } + }; + + $scope.rebuildRecipients = function rebuildRecipients() { + // setting null will put 'Estimating..' text on refresh button + $scope.recipients = null; + return crmMailingMgr.previewRecipientCount($scope.mailing, crmMailingCache, true).then(function(recipients) { + $scope.outdated = (recipients === 0) ? true : false; + $scope.recipients = recipients; + }); }; // Open a dialog for editing the advanced recipient options.