1 (function (angular
, $, _
) {
3 var partialUrl = function (relPath
, module
) {
5 module
= 'crmMailingAB';
7 return CRM
.resourceUrls
['civicrm'] + '/partials/' + module
+ '/' + relPath
;
10 angular
.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize', 'crmUi', 'crmAttachment', 'crmMailing']);
11 angular
.module('crmMailingAB').config([
13 function ($routeProvider
) {
14 $routeProvider
.when('/abtest', {
15 templateUrl
: partialUrl('list.html'),
16 controller
: 'CrmMailingABListCtrl',
18 mailingABList: function ($route
, crmApi
) {
19 return crmApi('MailingAB', 'get', {rowCount
: 0});
23 $routeProvider
.when('/abtest/:id', {
24 templateUrl
: partialUrl('edit.html'),
25 controller
: 'CrmMailingABEditCtrl',
27 abtest: function ($route
, CrmMailingAB
) {
28 var abtest
= new CrmMailingAB($route
.current
.params
.id
== 'new' ? null : $route
.current
.params
.id
);
36 angular
.module('crmMailingAB').controller('CrmMailingABListCtrl', function ($scope
, mailingABList
, crmMailingABCriteria
) {
37 $scope
.mailingABList
= mailingABList
.values
;
38 $scope
.testing_criteria
= crmMailingABCriteria
.getAll();
41 angular
.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope
, abtest
, crmMailingABCriteria
, crmMailingMgr
, crmMailingPreviewMgr
, crmStatus
) {
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
;
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
});
54 var criteria
= crmMailingABCriteria
.get(abtest
.ab
.testing_criteria_id
);
56 // TODO review fields exposed in UI and make sure the sync rules match
57 switch (criteria
.name
) {
59 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, ['name', 'subject']);
62 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, ['name', 'from_name', 'from_email']);
64 case 'Two different emails':
65 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, [
75 throw "Unrecognized testing_criteria";
78 crmMailingMgr
.mergeInto(abtest
.mailings
.c
, abtest
.mailings
.a
, ['name']);
80 $scope
.save
= function save() {
82 return crmStatus({start
: ts('Saving...'), success
: ts('Saved')}, abtest
.save());
85 $scope
.previewMailing
= function previewMailing(mailingName
, mode
) {
86 return crmMailingPreviewMgr
.preview(abtest
.mailings
[mailingName
], mode
);
90 $scope
.sendTest
= function sendTest(mailingName
, recipient
) {
91 return crmStatus({start
: ts('Saving...'), success
: ''}, abtest
.save())
93 crmMailingPreviewMgr
.sendTest(abtest
.mailings
[mailingName
], recipient
);
96 $scope
.delete = function () {
97 throw "Not implemented: EditCtrl.delete"
99 $scope
.submit = function () {
100 throw "Not implemented: EditCtrl.submit"
103 function updateCriteriaName() {
104 var criteria
= crmMailingABCriteria
.get($scope
.abtest
.ab
.testing_criteria_id
)
105 $scope
.criteriaName
= criteria
? criteria
.name
: null;
109 updateCriteriaName();
111 $scope
.$watch('abtest.ab.testing_criteria_id', updateCriteriaName
);
114 })(angular
, CRM
.$, CRM
._
);