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