1 (function(angular
, $, _
) {
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};
6 $scope
.mailing
= selectedMail
;
7 $scope
.attachments
= attachments
;
8 $scope
.crmMailingConst
= CRM
.crmMailing
;
9 $scope
.checkPerm
= CRM
.checkPerm
;
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;
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
;
20 $scope
.isSubmitted
= function isSubmitted() {
21 return _
.size($scope
.mailing
.jobs
) > 0;
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
));
31 $scope
.previewMailing
= function previewMailing(mailing
, mode
) {
32 return crmMailingPreviewMgr
.preview(mailing
, mode
);
36 $scope
.sendTest
= function sendTest(mailing
, attachments
, recipient
) {
37 var savePromise
= crmMailingMgr
.save(mailing
)
39 return attachments
.save();
41 return block(crmStatus({start
: ts('Saving...'), success
: ''}, savePromise
)
43 crmMailingPreviewMgr
.sendTest(mailing
, recipient
);
48 $scope
.submit
= function submit(options
) {
49 options
= options
|| {};
54 var promise
= crmMailingMgr
.save($scope
.mailing
)
56 // pre-condition: the mailing exists *before* saving attachments to it
57 return $scope
.attachments
.save();
60 return crmMailingMgr
.submit($scope
.mailing
);
64 $scope
.leave('scheduled');
68 return block(crmStatus({start
: ts('Submitting...'), success
: ts('Submitted')}, promise
));
72 $scope
.save
= function save() {
73 return block(crmStatus(null,
77 // pre-condition: the mailing exists *before* saving attachments to it
78 return $scope
.attachments
.save();
84 $scope
.delete = function cancel() {
85 return block(crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')},
86 crmMailingMgr
.delete($scope
.mailing
)
88 $scope
.leave('unscheduled');
93 // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
94 $scope
.leave
= function leave(listingScreen
) {
95 switch (listingScreen
) {
97 window
.location
= CRM
.url('civicrm/mailing/browse/archived', {
102 window
.location
= CRM
.url('civicrm/mailing/browse/scheduled', {
110 window
.location
= CRM
.url('civicrm/mailing/browse/unscheduled', {
117 myAutosave
= new CrmAutosaveCtrl({
123 return [$scope
.mailing
, $scope
.attachments
.getAutosaveSignature()];
126 return $scope
.crmMailing
;
129 $timeout(myAutosave
.start
);
130 $scope
.$on('$destroy', myAutosave
.stop
);
133 })(angular
, CRM
.$, CRM
._
);