+ // Controller for the edit-recipients fields (
+ // WISHLIST: Move most of this to a (cache-enabled) service
+ // Scope members:
+ // - [input] mailing: object
+ // - [output] recipients: array of recipient records
+ crmMailing2.controller('EditRecipCtrl', function ($scope, dialogService, crmApi) {
+ // TODO load & live update real recipients list
+ $scope.recipients = null;
+ $scope.getRecipientsEstimate = function () {
+ var ts = $scope.ts;
+ if ($scope.recipients == null)
+ return ts('(Estimating)');
+ if ($scope.recipients.length == 0)
+ return ts('No recipients');
+ if ($scope.recipients.length == 1)
+ return ts('~1 recipient');
+ if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT)
+ return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT});
+ return ts('~%1 recipients', {1: $scope.recipients.length});
+ };
+ // 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;
+ // To get list of recipients, we tentatively save the mailing and
+ // get the resulting recipients -- then rollback any changes.
+ var params = _.extend({}, $scope.mailing, {
+ options: {force_rollback: 1},
+ 'api.MailingRecipients.get': {
+ mailing_id: '$value.id',
+ options: {limit: RECIPIENTS_PREVIEW_LIMIT},
+ 'api.contact.getvalue': {'return': 'display_name'},
+ 'api.email.getvalue': {'return': 'email'}
+ }
+ });
+
+ crmApi('Mailing', 'create', params)
+ .then(function (recipResult) {
+ $scope.$apply(function () {
+ $scope.recipients = recipResult.values[recipResult.id]['api.MailingRecipients.get'].values;
+ });
+ });
+ });
+ }, RECIPIENTS_DEBOUNCE_MS);
+ $scope.$watchCollection("mailing.groups.include", refreshRecipients);
+ $scope.$watchCollection("mailing.groups.exclude", refreshRecipients);
+ $scope.$watchCollection("mailing.mailings.include", refreshRecipients);
+ $scope.$watchCollection("mailing.mailings.exclude", refreshRecipients);
+
+ $scope.previewRecipients = function () {
+ var model = {
+ recipients: $scope.recipients
+ };
+ var options = {
+ autoOpen: false,
+ modal: true,
+ title: ts('Preview (%1)', {
+ 1: $scope.getRecipientsEstimate()
+ }),
+ };
+ dialogService.open('recipDialog', partialUrl('dialog/recipients.html'), model, options)
+ .then(
+ function (result) {
+ // console.log('Closed!');
+ },
+ function (error) {
+ // console.log('Cancelled!');
+ }
+ );
+ };
+ });
+
+ // Controller for the "Preview Recipients" dialog
+ // Note: Expects $scope.model to be an object with properties:
+ // - recipients: array of contacts
+ crmMailing2.controller('PreviewRecipCtrl', function ($scope) {
+ $scope.ts = CRM.ts('CiviMail');
+ });