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