Commit | Line | Data |
---|---|---|
70980d8e TO |
1 | (function (angular, $, _) { |
2 | ||
3 | var partialUrl = function (relPath, module) { | |
4 | if (!module) { | |
efd95528 | 5 | module = 'crmMailingAB'; |
70980d8e TO |
6 | } |
7 | return CRM.resourceUrls['civicrm'] + '/partials/' + module + '/' + relPath; | |
8 | }; | |
9 | ||
efd95528 TO |
10 | angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize', 'crmUi', 'crmAttachment', 'crmMailing']); |
11 | angular.module('crmMailingAB').config([ | |
70980d8e TO |
12 | '$routeProvider', |
13 | function ($routeProvider) { | |
efd95528 | 14 | $routeProvider.when('/abtest', { |
70980d8e | 15 | templateUrl: partialUrl('list.html'), |
efd95528 | 16 | controller: 'CrmMailingABListCtrl', |
70980d8e TO |
17 | resolve: { |
18 | mailingABList: function ($route, crmApi) { | |
19 | return crmApi('MailingAB', 'get', {rowCount: 0}); | |
20 | } | |
21 | } | |
22 | }); | |
efd95528 | 23 | $routeProvider.when('/abtest/:id', { |
70980d8e | 24 | templateUrl: partialUrl('edit.html'), |
efd95528 | 25 | controller: 'CrmMailingABEditCtrl', |
70980d8e TO |
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 | ||
efd95528 | 36 | angular.module('crmMailingAB').controller('CrmMailingABListCtrl', function ($scope, mailingABList, crmMailingABCriteria) { |
70980d8e TO |
37 | $scope.mailingABList = mailingABList.values; |
38 | $scope.testing_criteria = crmMailingABCriteria.getAll(); | |
a7931384 | 39 | }); |
70980d8e | 40 | |
58dfba8d | 41 | angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus) { |
bcdd7f49 | 42 | window.ab = abtest; |
70980d8e | 43 | $scope.abtest = abtest; |
58dfba8d | 44 | var ts = $scope.ts = CRM.ts('CiviMail'); |
af6962d8 | 45 | $scope.crmMailingABCriteria = crmMailingABCriteria; |
bcdd7f49 | 46 | $scope.crmMailingConst = CRM.crmMailing; |
22bc3e48 | 47 | $scope.partialUrl = partialUrl; |
af6962d8 | 48 | |
70980d8e TO |
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) { | |
d3b6424f | 56 | // TODO review fields exposed in UI and make sure the sync rules match |
70980d8e TO |
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(); | |
58dfba8d TO |
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 | }); | |
70980d8e TO |
95 | }; |
96 | $scope.delete = function () { | |
97 | throw "Not implemented: EditCtrl.delete" | |
98 | }; | |
22bc3e48 TO |
99 | $scope.submit = function () { |
100 | throw "Not implemented: EditCtrl.submit" | |
101 | }; | |
102 | ||
103 | function updateCriteriaName() { | |
bcdd7f49 TO |
104 | var criteria = crmMailingABCriteria.get($scope.abtest.ab.testing_criteria_id) |
105 | $scope.criteriaName = criteria ? criteria.name : null; | |
22bc3e48 | 106 | } |
70980d8e | 107 | |
22bc3e48 TO |
108 | // initialize |
109 | updateCriteriaName(); | |
70980d8e | 110 | $scope.sync(); |
22bc3e48 | 111 | $scope.$watch('abtest.ab.testing_criteria_id', updateCriteriaName); |
70980d8e TO |
112 | }); |
113 | ||
114 | })(angular, CRM.$, CRM._); |