Merge pull request #11466 from coolbit/CRM-21602
[civicrm-core.git] / ang / crmMailingAB / EditCtrl.js
CommitLineData
679996bb
TO
1(function(angular, $, _) {
2
3 angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus, $q, $location, crmBlocker, $interval, $timeout, CrmAutosaveCtrl, dialogService) {
4 $scope.abtest = abtest;
5 var ts = $scope.ts = CRM.ts(null);
6 var block = $scope.block = crmBlocker();
7 $scope.crmUrl = CRM.url;
8 var myAutosave = null;
9 $scope.crmMailingABCriteria = crmMailingABCriteria;
10 $scope.crmMailingConst = CRM.crmMailing;
11 $scope.checkPerm = CRM.checkPerm;
12
13 $scope.isSubmitted = function isSubmitted() {
14 return _.size(abtest.mailings.a.jobs) > 0 || _.size(abtest.mailings.b.jobs) > 0;
15 };
16
17 $scope.sync = function sync() {
18 abtest.mailings.a.name = ts('Test A (%1)', {1: abtest.ab.name});
19 abtest.mailings.b.name = ts('Test B (%1)', {1: abtest.ab.name});
20 abtest.mailings.c.name = ts('Final (%1)', {1: abtest.ab.name});
21
22 if (abtest.ab.testing_criteria) {
23 // TODO review fields exposed in UI and make sure the sync rules match
24 switch (abtest.ab.testing_criteria) {
25 case 'subject':
9a60b9d8 26 var exclude_subject = [
679996bb
TO
27 'name',
28 'recipients',
29 'subject'
9a60b9d8 30 ];
31 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, exclude_subject);
32 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings.a, exclude_subject);
679996bb
TO
33 break;
34 case 'from':
9a60b9d8 35 var exclude_from = [
679996bb
TO
36 'name',
37 'recipients',
38 'from_name',
39 'from_email'
9a60b9d8 40 ];
41 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, exclude_from);
42 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings.a, exclude_from);
679996bb
TO
43 break;
44 case 'full_email':
9a60b9d8 45 var exclude_full_email = [
679996bb
TO
46 'name',
47 'recipients',
48 'subject',
49 'from_name',
50 'from_email',
51 'replyto_email',
52 'override_verp', // keep override_verp and replyto_Email linked
53 'body_html',
54 'body_text'
9a60b9d8 55 ];
56 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, exclude_full_email);
57 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings.a, exclude_full_email);
679996bb
TO
58 break;
59 default:
60 throw "Unrecognized testing_criteria";
61 }
62 }
679996bb
TO
63 return true;
64 };
65
66 // @return Promise
67 $scope.save = function save() {
68 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, abtest.save()));
69 };
70
71 // @return Promise
72 $scope.previewMailing = function previewMailing(mailingName, mode) {
73 return crmMailingPreviewMgr.preview(abtest.mailings[mailingName], mode);
74 };
75
76 // @return Promise
77 $scope.sendTest = function sendTest(mailingName, recipient) {
78 return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
79 .then(function() {
80 crmMailingPreviewMgr.sendTest(abtest.mailings[mailingName], recipient);
81 }));
82 };
83
84 // @return Promise
85 $scope.delete = function() {
86 return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')}, abtest.delete().then($scope.leave)));
87 };
88
89 // @return Promise
90 $scope.submit = function submit() {
91 if (block.check() || $scope.crmMailingAB.$invalid) {
92 return;
93 }
94 return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
95 .then(function() {
96 return crmStatus({
97 start: ts('Submitting...'),
98 success: ts('Submitted')
99 }, myAutosave.suspend(abtest.submitTest()));
100 // Note: We're going to leave, so we don't care that submit() modifies several server-side records.
101 // If we stayed on this page, then we'd care about updating and call: abtest.submitTest().then(...abtest.load()...)
102 })
103 );
104 };
105
106 $scope.leave = function leave() {
107 $location.path('abtest');
108 $location.replace();
109 };
110
111 $scope.selectWinner = function selectWinner(mailingName) {
112 var model = {
113 abtest: $scope.abtest,
114 mailingName: mailingName
115 };
116 var options = CRM.utils.adjustDialogDefaults({
117 autoOpen: false,
118 height: 'auto',
119 width: '40%',
120 title: ts('Select Final Mailing (Test %1)', {
121 1: mailingName.toUpperCase()
122 })
123 });
124 return myAutosave.suspend(dialogService.open('selectWinnerDialog', '~/crmMailingAB/WinnerDialogCtrl.html', model, options));
125 };
126
127 // initialize
128 var syncJob = $interval($scope.sync, 333);
129 $scope.$on('$destroy', function() {
130 $interval.cancel(syncJob);
131 });
132
133 myAutosave = new CrmAutosaveCtrl({
134 save: $scope.save,
135 saveIf: function() {
136 return abtest.ab.status == 'Draft' && $scope.sync();
137 },
138 model: function() {
139 return abtest.getAutosaveSignature();
140 },
141 form: function() {
142 return $scope.crmMailingAB;
143 }
144 });
145 $timeout(myAutosave.start);
146 $scope.$on('$destroy', myAutosave.stop);
147 });
148
149})(angular, CRM.$, CRM._);