1 (function(angular
, $, _
) {
3 angular
.module('crmMailingAB').controller('CrmMailingABEditCtrl', function($scope
, abtest
, crmMailingABCriteria
, crmMailingMgr
, crmMailingPreviewMgr
, crmStatus
, $q
, $location
, crmBlocker
, $interval
, $timeout
, CrmAutosaveCtrl
, dialogService
, mailingFields
) {
4 $scope
.abtest
= abtest
;
5 var ts
= $scope
.ts
= CRM
.ts(null);
6 var block
= $scope
.block
= crmBlocker();
7 $scope
.crmUrl
= CRM
.url
;
9 $scope
.crmMailingABCriteria
= crmMailingABCriteria
;
10 $scope
.crmMailingConst
= CRM
.crmMailing
;
11 $scope
.checkPerm
= CRM
.checkPerm
;
12 $scope
.mailingFields
= mailingFields
;
14 $scope
.isSubmitted
= function isSubmitted() {
15 return _
.size(abtest
.mailings
.a
.jobs
) > 0 || _
.size(abtest
.mailings
.b
.jobs
) > 0;
18 $scope
.sync
= function sync() {
19 abtest
.mailings
.a
.name
= ts('Test A (%1)', {1: abtest
.ab
.name
});
20 abtest
.mailings
.b
.name
= ts('Test B (%1)', {1: abtest
.ab
.name
});
21 abtest
.mailings
.c
.name
= ts('Final (%1)', {1: abtest
.ab
.name
});
23 if (abtest
.ab
.testing_criteria
) {
24 // TODO review fields exposed in UI and make sure the sync rules match
25 switch (abtest
.ab
.testing_criteria
) {
27 var exclude_subject
= [
32 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, exclude_subject
);
33 crmMailingMgr
.mergeInto(abtest
.mailings
.c
, abtest
.mailings
.a
, exclude_subject
);
42 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, exclude_from
);
43 crmMailingMgr
.mergeInto(abtest
.mailings
.c
, abtest
.mailings
.a
, exclude_from
);
46 var exclude_full_email
= [
53 'override_verp', // keep override_verp and replyto_Email linked
57 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, exclude_full_email
);
58 crmMailingMgr
.mergeInto(abtest
.mailings
.c
, abtest
.mailings
.a
, exclude_full_email
);
61 throw "Unrecognized testing_criteria";
68 $scope
.save
= function save() {
69 return block(crmStatus({start
: ts('Saving...'), success
: ts('Saved')}, abtest
.save()));
73 $scope
.previewMailing
= function previewMailing(mailingName
, mode
) {
74 return crmMailingPreviewMgr
.preview(abtest
.mailings
[mailingName
], mode
);
78 $scope
.sendTest
= function sendTest(mailingName
, recipient
) {
79 return block(crmStatus({start
: ts('Saving...'), success
: ''}, abtest
.save())
81 crmMailingPreviewMgr
.sendTest(abtest
.mailings
[mailingName
], recipient
);
86 $scope
.delete = function() {
87 return block(crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')}, abtest
.delete().then($scope
.leave
)));
91 $scope
.submit
= function submit() {
92 if (block
.check() || $scope
.crmMailingAB
.$invalid
) {
95 return block(crmStatus({start
: ts('Saving...'), success
: ''}, abtest
.save())
98 start
: ts('Submitting...'),
99 success
: ts('Submitted')
100 }, myAutosave
.suspend(abtest
.submitTest()));
101 // Note: We're going to leave, so we don't care that submit() modifies several server-side records.
102 // If we stayed on this page, then we'd care about updating and call: abtest.submitTest().then(...abtest.load()...)
107 $scope
.leave
= function leave() {
108 $location
.path('abtest');
112 $scope
.selectWinner
= function selectWinner(mailingName
) {
114 abtest
: $scope
.abtest
,
115 mailingName
: mailingName
117 var options
= CRM
.utils
.adjustDialogDefaults({
121 title
: ts('Select Final Mailing (Test %1)', {
122 1: mailingName
.toUpperCase()
125 return myAutosave
.suspend(dialogService
.open('selectWinnerDialog', '~/crmMailingAB/WinnerDialogCtrl.html', model
, options
));
129 var syncJob
= $interval($scope
.sync
, 333);
130 $scope
.$on('$destroy', function() {
131 $interval
.cancel(syncJob
);
134 myAutosave
= new CrmAutosaveCtrl({
137 return abtest
.ab
.status
== 'Draft' && $scope
.sync();
140 return abtest
.getAutosaveSignature();
143 return $scope
.crmMailingAB
;
146 $timeout(myAutosave
.start
);
147 $scope
.$on('$destroy', myAutosave
.stop
);
150 })(angular
, CRM
.$, CRM
._
);