| 1 | (function(angular, $, _) { |
| 2 | |
| 3 | angular.module('crmMailing').controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmStatus, attachments, crmMailingPreviewMgr, crmBlocker, CrmAutosaveCtrl, $timeout, crmUiHelp) { |
| 4 | var APPROVAL_STATUSES = {'Approved': 1, 'Rejected': 2, 'None': 3}; |
| 5 | |
| 6 | $scope.mailing = selectedMail; |
| 7 | $scope.attachments = attachments; |
| 8 | $scope.crmMailingConst = CRM.crmMailing; |
| 9 | $scope.checkPerm = CRM.checkPerm; |
| 10 | |
| 11 | var ts = $scope.ts = CRM.ts(null); |
| 12 | $scope.hs = crmUiHelp({file: 'CRM/Mailing/MailingUI'}); |
| 13 | var block = $scope.block = crmBlocker(); |
| 14 | var myAutosave = null; |
| 15 | |
| 16 | $scope.isSubmitted = function isSubmitted() { |
| 17 | return _.size($scope.mailing.jobs) > 0; |
| 18 | }; |
| 19 | |
| 20 | // usage: approve('Approved') |
| 21 | $scope.approve = function approve(status, options) { |
| 22 | $scope.mailing.approval_status_id = APPROVAL_STATUSES[status]; |
| 23 | return myAutosave.suspend($scope.submit(options)); |
| 24 | }; |
| 25 | |
| 26 | // @return Promise |
| 27 | $scope.previewMailing = function previewMailing(mailing, mode) { |
| 28 | return crmMailingPreviewMgr.preview(mailing, mode); |
| 29 | }; |
| 30 | |
| 31 | // @return Promise |
| 32 | $scope.sendTest = function sendTest(mailing, attachments, recipient) { |
| 33 | var savePromise = crmMailingMgr.save(mailing) |
| 34 | .then(function() { |
| 35 | return attachments.save(); |
| 36 | }); |
| 37 | return block(crmStatus({start: ts('Saving...'), success: ''}, savePromise) |
| 38 | .then(function() { |
| 39 | crmMailingPreviewMgr.sendTest(mailing, recipient); |
| 40 | })); |
| 41 | }; |
| 42 | |
| 43 | // @return Promise |
| 44 | $scope.submit = function submit(options) { |
| 45 | options = options || {}; |
| 46 | if (block.check() || $scope.crmMailing.$invalid) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | var promise = crmMailingMgr.save($scope.mailing) |
| 51 | .then(function() { |
| 52 | // pre-condition: the mailing exists *before* saving attachments to it |
| 53 | return $scope.attachments.save(); |
| 54 | }) |
| 55 | .then(function() { |
| 56 | return crmMailingMgr.submit($scope.mailing); |
| 57 | }) |
| 58 | .then(function() { |
| 59 | if (!options.stay) { |
| 60 | $scope.leave('scheduled'); |
| 61 | } |
| 62 | }) |
| 63 | ; |
| 64 | return block(crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, promise)); |
| 65 | }; |
| 66 | |
| 67 | // @return Promise |
| 68 | $scope.save = function save() { |
| 69 | return block(crmStatus(null, |
| 70 | crmMailingMgr |
| 71 | .save($scope.mailing) |
| 72 | .then(function() { |
| 73 | // pre-condition: the mailing exists *before* saving attachments to it |
| 74 | return $scope.attachments.save(); |
| 75 | }) |
| 76 | )); |
| 77 | }; |
| 78 | |
| 79 | // @return Promise |
| 80 | $scope.delete = function cancel() { |
| 81 | return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')}, |
| 82 | crmMailingMgr.delete($scope.mailing) |
| 83 | .then(function() { |
| 84 | $scope.leave('unscheduled'); |
| 85 | }) |
| 86 | )); |
| 87 | }; |
| 88 | |
| 89 | // @param string listingScreen 'archive', 'scheduled', 'unscheduled' |
| 90 | $scope.leave = function leave(listingScreen) { |
| 91 | switch (listingScreen) { |
| 92 | case 'archive': |
| 93 | window.location = CRM.url('civicrm/mailing/browse/archived', { |
| 94 | reset: 1 |
| 95 | }); |
| 96 | break; |
| 97 | case 'scheduled': |
| 98 | window.location = CRM.url('civicrm/mailing/browse/scheduled', { |
| 99 | reset: 1, |
| 100 | scheduled: 'true' |
| 101 | }); |
| 102 | break; |
| 103 | case 'unscheduled': |
| 104 | /* falls through */ |
| 105 | default: |
| 106 | window.location = CRM.url('civicrm/mailing/browse/unscheduled', { |
| 107 | reset: 1, |
| 108 | scheduled: 'false' |
| 109 | }); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | myAutosave = new CrmAutosaveCtrl({ |
| 114 | save: $scope.save, |
| 115 | saveIf: function() { |
| 116 | return true; |
| 117 | }, |
| 118 | model: function() { |
| 119 | return [$scope.mailing, $scope.attachments.getAutosaveSignature()]; |
| 120 | }, |
| 121 | form: function() { |
| 122 | return $scope.crmMailing; |
| 123 | } |
| 124 | }); |
| 125 | $timeout(myAutosave.start); |
| 126 | $scope.$on('$destroy', myAutosave.stop); |
| 127 | }); |
| 128 | |
| 129 | })(angular, CRM.$, CRM._); |