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 $scope
.isSubmitted
= function isSubmitted() {
17 return _
.size($scope
.mailing
.jobs
) > 0;
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
));
27 $scope
.previewMailing
= function previewMailing(mailing
, mode
) {
28 return crmMailingPreviewMgr
.preview(mailing
, mode
);
32 $scope
.sendTest
= function sendTest(mailing
, attachments
, recipient
) {
33 var savePromise
= crmMailingMgr
.save(mailing
)
35 return attachments
.save();
37 return block(crmStatus({start
: ts('Saving...'), success
: ''}, savePromise
)
39 crmMailingPreviewMgr
.sendTest(mailing
, recipient
);
44 $scope
.submit
= function submit(options
) {
45 options
= options
|| {};
46 if (block
.check() || $scope
.crmMailing
.$invalid
) {
50 var promise
= crmMailingMgr
.save($scope
.mailing
)
52 // pre-condition: the mailing exists *before* saving attachments to it
53 return $scope
.attachments
.save();
56 return crmMailingMgr
.submit($scope
.mailing
);
60 $scope
.leave('scheduled');
64 return block(crmStatus({start
: ts('Submitting...'), success
: ts('Submitted')}, promise
));
68 $scope
.save
= function save() {
69 return block(crmStatus(null,
73 // pre-condition: the mailing exists *before* saving attachments to it
74 return $scope
.attachments
.save();
80 $scope
.delete = function cancel() {
81 return block(crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')},
82 crmMailingMgr
.delete($scope
.mailing
)
84 $scope
.leave('unscheduled');
89 // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
90 $scope
.leave
= function leave(listingScreen
) {
91 switch (listingScreen
) {
93 window
.location
= CRM
.url('civicrm/mailing/browse/archived', {
98 window
.location
= CRM
.url('civicrm/mailing/browse/scheduled', {
106 window
.location
= CRM
.url('civicrm/mailing/browse/unscheduled', {
113 myAutosave
= new CrmAutosaveCtrl({
119 return [$scope
.mailing
, $scope
.attachments
.getAutosaveSignature()];
122 return $scope
.crmMailing
;
125 $timeout(myAutosave
.start
);
126 $scope
.$on('$destroy', myAutosave
.stop
);
129 })(angular
, CRM
.$, CRM
._
);