Merge pull request #4745 from totten/master-civimail-previewbug
[civicrm-core.git] / js / angular-crmMailingAB.js
1 (function (angular, $, _) {
2
3 var partialUrl = function (relPath, module) {
4 if (!module) {
5 module = 'crmMailingAB';
6 }
7 return CRM.resourceUrls['civicrm'] + '/partials/' + module + '/' + relPath;
8 };
9
10 angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize', 'crmUi', 'crmAttachment', 'crmMailing']);
11 angular.module('crmMailingAB').config([
12 '$routeProvider',
13 function ($routeProvider) {
14 $routeProvider.when('/abtest', {
15 templateUrl: partialUrl('list.html'),
16 controller: 'CrmMailingABListCtrl',
17 resolve: {
18 mailingABList: function ($route, crmApi) {
19 return crmApi('MailingAB', 'get', {rowCount: 0});
20 }
21 }
22 });
23 $routeProvider.when('/abtest/:id', {
24 templateUrl: partialUrl('edit.html'),
25 controller: 'CrmMailingABEditCtrl',
26 resolve: {
27 abtest: function ($route, CrmMailingAB) {
28 var abtest = new CrmMailingAB($route.current.params.id == 'new' ? null : $route.current.params.id);
29 return abtest.load();
30 }
31 }
32 });
33 }
34 ]);
35
36 angular.module('crmMailingAB').controller('CrmMailingABListCtrl', function ($scope, mailingABList, crmMailingABCriteria) {
37 $scope.mailingABList = mailingABList.values;
38 $scope.testing_criteria = crmMailingABCriteria.getAll();
39 });
40
41 angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus) {
42 window.ab = abtest;
43 $scope.abtest = abtest;
44 var ts = $scope.ts = CRM.ts('CiviMail');
45 $scope.crmMailingABCriteria = crmMailingABCriteria;
46 $scope.crmMailingConst = CRM.crmMailing;
47 $scope.partialUrl = partialUrl;
48
49 $scope.sync = function sync() {
50 abtest.mailings.a.name = ts('Test A (%1)', {1: abtest.ab.name});
51 abtest.mailings.b.name = ts('Test B (%1)', {1: abtest.ab.name});
52 abtest.mailings.c.name = ts('Winner (%1)', {1: abtest.ab.name});
53
54 var criteria = crmMailingABCriteria.get(abtest.ab.testing_criteria_id);
55 if (criteria) {
56 // TODO review fields exposed in UI and make sure the sync rules match
57 switch (criteria.name) {
58 case 'Subject lines':
59 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, ['name', 'subject']);
60 break;
61 case 'From names':
62 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, ['name', 'from_name', 'from_email']);
63 break;
64 case 'Two different emails':
65 crmMailingMgr.mergeInto(abtest.mailings.b, abtest.mailings.a, [
66 'name',
67 'subject',
68 'from_name',
69 'from_email',
70 'body_html',
71 'body_text'
72 ]);
73 break;
74 default:
75 throw "Unrecognized testing_criteria";
76 }
77 }
78 crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings.a, ['name']);
79 };
80 $scope.save = function save() {
81 $scope.sync();
82 return crmStatus({start: ts('Saving...'), success: ts('Saved')}, abtest.save());
83 };
84 // @return Promise
85 $scope.previewMailing = function previewMailing(mailingName, mode) {
86 return crmMailingPreviewMgr.preview(abtest.mailings[mailingName], mode);
87 };
88
89 // @return Promise
90 $scope.sendTest = function sendTest(mailingName, recipient) {
91 return crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
92 .then(function () {
93 crmMailingPreviewMgr.sendTest(abtest.mailings[mailingName], recipient);
94 });
95 };
96 $scope.delete = function () {
97 throw "Not implemented: EditCtrl.delete"
98 };
99 $scope.submit = function () {
100 throw "Not implemented: EditCtrl.submit"
101 };
102
103 function updateCriteriaName() {
104 var criteria = crmMailingABCriteria.get($scope.abtest.ab.testing_criteria_id)
105 $scope.criteriaName = criteria ? criteria.name : null;
106 }
107
108 // initialize
109 updateCriteriaName();
110 $scope.sync();
111 $scope.$watch('abtest.ab.testing_criteria_id', updateCriteriaName);
112 });
113
114 })(angular, CRM.$, CRM._);