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