Avoid CiviCRM running full drupal cache flush, as this results in CiviCRM clobbering...
[civicrm-core.git] / ang / crmMailingAB / EditCtrl.js
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':
26 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
27 'name',
28 'recipients',
29 'subject'
30 ]);
31 break;
32 case 'from':
33 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
34 'name',
35 'recipients',
36 'from_name',
37 'from_email'
38 ]);
39 break;
40 case 'full_email':
41 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
42 'name',
43 'recipients',
44 'subject',
45 'from_name',
46 'from_email',
47 'replyto_email',
48 'override_verp', // keep override_verp and replyto_Email linked
49 'body_html',
50 'body_text'
51 ]);
52 break;
53 default:
54 throw "Unrecognized testing_criteria";
55 }
56 }
57 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings.a, ['name']);
58 return true;
59 };
60
61 // @return Promise
62 $scope.save = function save() {
63 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, abtest.save()));
64 };
65
66 // @return Promise
67 $scope.previewMailing = function previewMailing(mailingName, mode) {
68 return crmMailingPreviewMgr.preview(abtest.mailings[mailingName], mode);
69 };
70
71 // @return Promise
72 $scope.sendTest = function sendTest(mailingName, recipient) {
73 return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
74 .then(function() {
75 crmMailingPreviewMgr.sendTest(abtest.mailings[mailingName], recipient);
76 }));
77 };
78
79 // @return Promise
80 $scope.delete = function() {
81 return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')}, abtest.delete().then($scope.leave)));
82 };
83
84 // @return Promise
85 $scope.submit = function submit() {
86 if (block.check() || $scope.crmMailingAB.$invalid) {
87 return;
88 }
89 return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
90 .then(function() {
91 return crmStatus({
92 start: ts('Submitting...'),
93 success: ts('Submitted')
94 }, myAutosave.suspend(abtest.submitTest()));
95 // Note: We're going to leave, so we don't care that submit() modifies several server-side records.
96 // If we stayed on this page, then we'd care about updating and call: abtest.submitTest().then(...abtest.load()...)
97 })
98 );
99 };
100
101 $scope.leave = function leave() {
102 $location.path('abtest');
103 $location.replace();
104 };
105
106 $scope.selectWinner = function selectWinner(mailingName) {
107 var model = {
108 abtest: $scope.abtest,
109 mailingName: mailingName
110 };
111 var options = CRM.utils.adjustDialogDefaults({
112 autoOpen: false,
113 height: 'auto',
114 width: '40%',
115 title: ts('Select Final Mailing (Test %1)', {
116 1: mailingName.toUpperCase()
117 })
118 });
119 return myAutosave.suspend(dialogService.open('selectWinnerDialog', '~/crmMailingAB/WinnerDialogCtrl.html', model, options));
120 };
121
122 // initialize
123 var syncJob = $interval($scope.sync, 333);
124 $scope.$on('$destroy', function() {
125 $interval.cancel(syncJob);
126 });
127
128 myAutosave = new CrmAutosaveCtrl({
129 save: $scope.save,
130 saveIf: function() {
131 return abtest.ab.status == 'Draft' && $scope.sync();
132 },
133 model: function() {
134 return abtest.getAutosaveSignature();
135 },
136 form: function() {
137 return $scope.crmMailingAB;
138 }
139 });
140 $timeout(myAutosave.start);
141 $scope.$on('$destroy', myAutosave.stop);
142 });
143
144 })(angular, CRM.$, CRM._);