CRM-15854 - crmMailing, crmMailingAB - Respect 'delete in CiviMail' permission
[civicrm-core.git] / js / angular-crmMailingAB.js
1 (function (angular, $, _) {
2
3 angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'crmUi', 'crmAttachment', 'crmMailing', 'crmD3']);
4 angular.module('crmMailingAB').config([
5 '$routeProvider',
6 function ($routeProvider) {
7 $routeProvider.when('/abtest', {
8 templateUrl: '~/crmMailingAB/list.html',
9 controller: 'CrmMailingABListCtrl',
10 resolve: {
11 mailingABList: function ($route, crmApi) {
12 return crmApi('MailingAB', 'get', {rowCount: 0});
13 },
14 fields: function(crmMetadata){
15 return crmMetadata.getFields('MailingAB');
16 }
17 }
18 });
19 $routeProvider.when('/abtest/new', {
20 template: '<p>' + ts('Initializing...') + '</p>',
21 controller: 'CrmMailingABNewCtrl',
22 resolve: {
23 abtest: function ($route, CrmMailingAB) {
24 var abtest = new CrmMailingAB(null);
25 return abtest.load().then(function(){
26 return abtest.save();
27 });
28 }
29 }
30 });
31 $routeProvider.when('/abtest/:id', {
32 templateUrl: '~/crmMailingAB/main.html',
33 controller: 'CrmMailingABEditCtrl',
34 resolve: {
35 abtest: function ($route, CrmMailingAB) {
36 var abtest = new CrmMailingAB($route.current.params.id == 'new' ? null : $route.current.params.id);
37 return abtest.load();
38 }
39 }
40 });
41 }
42 ]);
43
44 angular.module('crmMailingAB').controller('CrmMailingABListCtrl', function($scope, mailingABList, crmMailingABCriteria, crmMailingABStatus, fields) {
45 var ts = $scope.ts = CRM.ts(null);
46 $scope.mailingABList = _.values(mailingABList.values);
47 $scope.crmMailingABCriteria = crmMailingABCriteria;
48 $scope.crmMailingABStatus = crmMailingABStatus;
49 $scope.fields = fields;
50 $scope.filter = {};
51 });
52
53 angular.module('crmMailingAB').controller('CrmMailingABNewCtrl', function ($scope, abtest, $location) {
54 // Transition URL "/abtest/new/foo" => "/abtest/123/foo"
55 var parts = $location.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard"
56 parts[2] = abtest.id;
57 $location.path(parts.join('/'));
58 $location.replace();
59 });
60
61 angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus, $q, $location, crmBlocker, $interval, $timeout, CrmAutosaveCtrl, dialogService) {
62 $scope.abtest = abtest;
63 var ts = $scope.ts = CRM.ts(null);
64 var block = $scope.block = crmBlocker();
65 $scope.crmUrl = CRM.url;
66 var myAutosave = null;
67 $scope.crmMailingABCriteria = crmMailingABCriteria;
68 $scope.crmMailingConst = CRM.crmMailing;
69 $scope.checkPerm = CRM.checkPerm;
70
71 $scope.isSubmitted = function isSubmitted() {
72 return _.size(abtest.mailings.a.jobs) > 0 || _.size(abtest.mailings.b.jobs) > 0;
73 };
74
75 $scope.sync = function sync() {
76 abtest.mailings.a.name = ts('Test A (%1)', {1: abtest.ab.name});
77 abtest.mailings.b.name = ts('Test B (%1)', {1: abtest.ab.name});
78 abtest.mailings.c.name = ts('Final (%1)', {1: abtest.ab.name});
79
80 if (abtest.ab.testing_criteria) {
81 // TODO review fields exposed in UI and make sure the sync rules match
82 switch (abtest.ab.testing_criteria) {
83 case 'subject':
84 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
85 'name',
86 'recipients',
87 'subject'
88 ]);
89 break;
90 case 'from':
91 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
92 'name',
93 'recipients',
94 'from_name',
95 'from_email'
96 ]);
97 break;
98 case 'full_email':
99 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
100 'name',
101 'recipients',
102 'subject',
103 'from_name',
104 'from_email',
105 'replyto_email',
106 'override_verp', // keep override_verp and replyto_Email linked
107 'body_html',
108 'body_text'
109 ]);
110 break;
111 default:
112 throw "Unrecognized testing_criteria";
113 }
114 }
115 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings.a, ['name']);
116 return true;
117 };
118
119 // @return Promise
120 $scope.save = function save() {
121 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, abtest.save()));
122 };
123
124 // @return Promise
125 $scope.previewMailing = function previewMailing(mailingName, mode) {
126 return crmMailingPreviewMgr.preview(abtest.mailings[mailingName], mode);
127 };
128
129 // @return Promise
130 $scope.sendTest = function sendTest(mailingName, recipient) {
131 return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
132 .then(function () {
133 crmMailingPreviewMgr.sendTest(abtest.mailings[mailingName], recipient);
134 }));
135 };
136
137 // @return Promise
138 $scope.delete = function () {
139 return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')}, abtest.delete().then($scope.leave)));
140 };
141
142 // @return Promise
143 $scope.submit = function submit() {
144 if (block.check() || $scope.crmMailingAB.$invalid) {
145 return;
146 }
147 return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
148 .then(function() {
149 return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, myAutosave.suspend(abtest.submitTest()));
150 // Note: We're going to leave, so we don't care that submit() modifies several server-side records.
151 // If we stayed on this page, then we'd care about updating and call: abtest.submitTest().then(...abtest.load()...)
152 })
153 );
154 };
155
156 $scope.leave = function leave() {
157 $location.path('abtest');
158 $location.replace();
159 };
160
161 $scope.selectWinner = function selectWinner(mailingName) {
162 var model = {
163 abtest: $scope.abtest,
164 mailingName: mailingName
165 };
166 var options = CRM.utils.adjustDialogDefaults({
167 autoOpen: false,
168 title: ts('Select Final Mailing (Test %1)', {
169 1: mailingName.toUpperCase()
170 })
171 });
172 return myAutosave.suspend(dialogService.open('selectWinnerDialog', '~/crmMailingAB/selectWinner.html', model, options));
173 };
174
175 // initialize
176 var syncJob = $interval($scope.sync, 333);
177 $scope.$on('$destroy', function(){
178 $interval.cancel(syncJob);
179 });
180
181 myAutosave = new CrmAutosaveCtrl({
182 save: $scope.save,
183 saveIf: function(){
184 return abtest.ab.status == 'Draft' && $scope.sync();
185 },
186 model: function(){
187 return abtest.getAutosaveSignature();
188 },
189 form: function() {
190 return $scope.crmMailingAB;
191 }
192 });
193 $timeout(myAutosave.start);
194 $scope.$on('$destroy', myAutosave.stop);
195 });
196
197 angular.module('crmMailingAB').controller('CrmMailingABReportCtrl', function ($scope, crmApi, crmMailingStats) {
198 var ts = $scope.ts = CRM.ts(null);
199
200 var CrmMailingABReportCnt = 1, activeMailings = null;
201 $scope.getActiveMailings = function() {
202 if ($scope.abtest.$CrmMailingABReportCnt != CrmMailingABReportCnt) {
203 $scope.abtest.$CrmMailingABReportCnt = ++CrmMailingABReportCnt;
204 activeMailings = [
205 {name: 'a', title: ts('Mailing A'), mailing: $scope.abtest.mailings.a, attachments: $scope.abtest.attachments.a},
206 {name: 'b', title: ts('Mailing B'), mailing: $scope.abtest.mailings.b, attachments: $scope.abtest.attachments.b}
207 ];
208 if ($scope.abtest.ab.status == 'Final') {
209 activeMailings.push({name: 'c', title: ts('Final'), mailing: $scope.abtest.mailings.c, attachments: $scope.abtest.attachments.c});
210 }
211 }
212 return activeMailings;
213 };
214
215 crmMailingStats.getStats({
216 a: $scope.abtest.ab.mailing_id_a,
217 b: $scope.abtest.ab.mailing_id_b,
218 c: $scope.abtest.ab.mailing_id_c
219 }).then(function(stats) {
220 $scope.stats = stats;
221 });
222
223 $scope.statTypes = crmMailingStats.getStatTypes();
224 $scope.statUrl = function statUrl(mailing, statType, view) {
225 return crmMailingStats.getUrl(mailing, statType, view, 'abtest/' + $scope.abtest.ab.id);
226 };
227
228 $scope.checkPerm = CRM.checkPerm;
229 });
230
231 angular.module('crmMailingAB').controller('CrmMailingABWinnerDialogCtrl', function ($scope, $timeout, dialogService, crmMailingMgr, crmStatus) {
232 var ts = $scope.ts = CRM.ts(null);
233 var abtest = $scope.abtest = $scope.model.abtest;
234 var mailingName = $scope.model.mailingName;
235
236 var titles = {a: ts('Mailing A'), b: ts('Mailing B')};
237 $scope.mailingTitle = titles[mailingName];
238
239 function init() {
240 // When using dialogService with a button bar, the major button actions
241 // need to be registered with the dialog widget (and not embedded in
242 // the body of the dialog).
243 var buttons = {};
244 buttons[ts('Submit final mailing')] = function () {
245 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings[mailingName], [
246 'name',
247 'recipients',
248 'scheduled_date'
249 ]);
250 crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
251 .then(function () {
252 return crmStatus({start: ts('Submitting...'), success: ts('Submitted')},
253 abtest.submitFinal().then(function(r){
254 delete abtest.$CrmMailingABReportCnt;
255 return r;
256 }));
257 })
258 .then(function(){
259 dialogService.close('selectWinnerDialog', abtest);
260 });
261 };
262 buttons[ts('Cancel')] = function () {
263 dialogService.cancel('selectWinnerDialog');
264 };
265 dialogService.setButtons('selectWinnerDialog', buttons);
266 }
267
268 $timeout(init);
269 });
270
271 })(angular, CRM.$, CRM._);