1 (function(angular
, $, _
) {
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};
6 $scope
.mailing
= selectedMail
;
7 $scope
.attachments
= attachments
;
8 $scope
.crmMailingConst
= CRM
.crmMailing
;
9 $scope
.checkPerm
= CRM
.checkPerm
;
10 $scope
.mailingFields
= mailingFields
;
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;
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
;
21 $scope
.isSubmitted
= function isSubmitted() {
22 return _
.size($scope
.mailing
.jobs
) > 0;
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
));
32 $scope
.previewMailing
= function previewMailing(mailing
, mode
) {
33 return crmMailingPreviewMgr
.preview(mailing
, mode
);
37 $scope
.sendTest
= function sendTest(mailing
, attachments
, recipient
) {
38 var savePromise
= crmMailingMgr
.save(mailing
)
40 return attachments
.save();
42 return block(crmStatus({start
: ts('Saving...'), success
: ''}, savePromise
)
44 crmMailingPreviewMgr
.sendTest(mailing
, recipient
);
49 $scope
.submit
= function submit(options
) {
50 options
= options
|| {};
55 var promise
= crmMailingMgr
.save($scope
.mailing
)
57 // pre-condition: the mailing exists *before* saving attachments to it
58 return $scope
.attachments
.save();
61 return crmMailingMgr
.submit($scope
.mailing
);
65 $scope
.leave('scheduled');
69 return block(crmStatus({start
: ts('Submitting...'), success
: ts('Submitted')}, promise
));
73 $scope
.save
= function save() {
74 return block(crmStatus(null,
78 // pre-condition: the mailing exists *before* saving attachments to it
79 return $scope
.attachments
.save();
85 $scope
.delete = function cancel() {
86 return block(crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')},
87 crmMailingMgr
.delete($scope
.mailing
)
89 $scope
.leave('unscheduled');
94 // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
95 $scope
.leave
= function leave(listingScreen
) {
96 switch (listingScreen
) {
98 window
.location
= CRM
.url('civicrm/mailing/browse/archived', {
103 window
.location
= CRM
.url('civicrm/mailing/browse/scheduled', {
111 window
.location
= CRM
.url('civicrm/mailing/browse/unscheduled', {
118 myAutosave
= new CrmAutosaveCtrl({
124 return [$scope
.mailing
, $scope
.attachments
.getAutosaveSignature()];
127 return $scope
.crmMailing
;
130 $timeout(myAutosave
.start
);
131 $scope
.$on('$destroy', myAutosave
.stop
);
134 })(angular
, CRM
.$, CRM
._
);