becabe19fae5adccdf92f04fb052e3340897dfdb
1 (function (angular
, $, _
) {
3 angular
.module('crmMailingAB').factory('crmMailingABCriteria', function () {
4 // TODO Get data from server
6 '1': {value
: '1', name
: 'Subject lines', label
: ts('Test different "Subject" lines')},
7 '2': {value
: '2', name
: 'From names', label
: ts('Test different "From" lines')},
8 '3': {value
: '3', name
: 'Two different emails', label
: ts('Test entirely different emails')}
11 get: function get(value
) {
12 var r
= _
.where(values
, {value
: '' + value
});
13 return r
.length
> 0 ? r
[0] : null;
15 getAll
: function getAll() {
21 // CrmMailingAB is a data-model which combines an AB test (APIv3 "MailingAB"), three mailings (APIv3 "Mailing"),
22 // and three sets of attachments (APIv3 "Attachment").
25 // var abtest = new CrmMailingAB(123);
26 // abtest.load().then(function(){
27 // alert("Mailing A is named "+abtest.mailings.a.name);
29 angular
.module('crmMailingAB').factory('CrmMailingAB', function (crmApi
, crmMailingMgr
, $q
, CrmAttachments
) {
30 function CrmMailingAB(id
) {
33 this.attachments
= {};
36 angular
.extend(CrmMailingAB
.prototype, {
37 // @return Promise CrmMailingAB
38 load
: function load() {
39 var crmMailingAB
= this;
40 if (!crmMailingAB
.id
) {
42 name
: 'Example', // FIXME
47 testing_criteria_id
: 1, // FIXME
48 winner_criteria_id
: null,
50 declare_winning_time
: null,
53 crmMailingAB
.mailings
.a
= crmMailingMgr
.create();
54 crmMailingAB
.mailings
.b
= crmMailingMgr
.create();
55 crmMailingAB
.mailings
.c
= crmMailingMgr
.create();
56 crmMailingAB
.attachments
.a
= new CrmAttachments(function () {
57 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_a']};
59 crmMailingAB
.attachments
.b
= new CrmAttachments(function () {
60 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_b']};
62 crmMailingAB
.attachments
.c
= new CrmAttachments(function () {
63 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_c']};
67 dfr
.resolve(crmMailingAB
);
71 return crmApi('MailingAB', 'get', {id
: crmMailingAB
.id
})
72 .then(function (abResult
) {
73 if (abResult
.count
!= 1) {
74 throw "Failed to load AB Test";
76 crmMailingAB
.ab
= abResult
.values
[abResult
.id
];
77 return crmMailingAB
._loadMailings();
81 // @return Promise CrmMailingAB
82 save
: function save() {
83 var crmMailingAB
= this;
85 return crmMailingAB
._saveMailings()
87 return crmApi('MailingAB', 'create', crmMailingAB
.ab
)
88 .then(function (abResult
) {
89 crmMailingAB
.ab
.id
= abResult
.id
;
96 // @param mailing Object (per APIv3)
98 'delete': function () {
100 return crmApi('MailingAB', 'delete', {id
: this.id
});
108 // Load mailings A, B, and C (if available)
109 // @return Promise CrmMailingAB
110 _loadMailings
: function _loadMailings() {
111 var crmMailingAB
= this;
113 _
.each(['a', 'b', 'c'], function (mkey
) {
114 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
115 todos
[mkey
] = crmMailingMgr
.get(crmMailingAB
.ab
['mailing_id_' + mkey
])
116 .then(function (mailing
) {
117 crmMailingAB
.mailings
[mkey
] = mailing
;
118 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
119 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
121 return crmMailingAB
.attachments
[mkey
].load();
125 crmMailingAB
.mailings
[mkey
] = crmMailingMgr
.create();
126 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
127 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
131 return $q
.all(todos
).then(function () {
135 // Save mailings A, B, and C (if available)
136 // @return Promise CrmMailingAB
137 _saveMailings
: function _saveMailings() {
138 var crmMailingAB
= this;
140 _
.each(['a', 'b', 'c'], function (mkey
) {
141 if (!crmMailingAB
.mailings
[mkey
]) {
144 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
145 // paranoia: in case caller forgot to manage id on mailing
146 crmMailingAB
.mailings
[mkey
].id
= crmMailingAB
.ab
['mailing_id_' + mkey
];
148 todos
[mkey
] = crmMailingMgr
.save(crmMailingAB
.mailings
[mkey
])
150 crmMailingAB
.ab
['mailing_id_' + mkey
] = crmMailingAB
.mailings
[mkey
].id
;
151 return crmMailingAB
.attachments
[mkey
].save();
154 return $q
.all(todos
).then(function () {
163 })(angular
, CRM
.$, CRM
._
);