1 (function (angular
, $, _
) {
3 function OptionGroup(values
) {
4 this.get = function get(value
) {
5 var r
= _
.where(values
, {value
: '' + value
});
6 return r
.length
> 0 ? r
[0] : null;
8 this.getByName
= function get(name
) {
9 var r
= _
.where(values
, {name
: '' + name
});
10 return r
.length
> 0 ? r
[0] : null;
12 this.getAll
= function getAll() {
17 angular
.module('crmMailingAB').factory('crmMailingABCriteria', function () {
18 // TODO Get data from server
20 '1': {value
: 'subject', name
: 'subject', label
: ts('Test different "Subject" lines')},
21 '2': {value
: 'from', name
: 'from', label
: ts('Test different "From" lines')},
22 '3': {value
: 'full_email', name
: 'full_email', label
: ts('Test entirely different emails')}
24 return new OptionGroup(values
);
27 angular
.module('crmMailingAB').factory('crmMailingABStatus', function () {
28 // TODO Get data from server
30 '1': {value
: '1', name
: 'Draft', label
: ts('Draft')},
31 '2': {value
: '2', name
: 'Testing', label
: ts('Testing')},
32 '3': {value
: '3', name
: 'Final', label
: ts('Final')}
34 return new OptionGroup(values
);
37 // CrmMailingAB is a data-model which combines an AB test (APIv3 "MailingAB"), three mailings (APIv3 "Mailing"),
38 // and three sets of attachments (APIv3 "Attachment").
41 // var abtest = new CrmMailingAB(123);
42 // abtest.load().then(function(){
43 // alert("Mailing A is named "+abtest.mailings.a.name);
45 angular
.module('crmMailingAB').factory('CrmMailingAB', function (crmApi
, crmMailingMgr
, $q
, CrmAttachments
) {
46 function CrmMailingAB(id
) {
49 this.attachments
= {};
52 angular
.extend(CrmMailingAB
.prototype, {
53 getAutosaveSignature: function() {
57 this.attachments
.a
.getAutosaveSignature(),
58 this.attachments
.b
.getAutosaveSignature(),
59 this.attachments
.c
.getAutosaveSignature()
62 // @return Promise CrmMailingAB
63 load
: function load() {
64 var crmMailingAB
= this;
65 if (!crmMailingAB
.id
) {
73 testing_criteria
: 'subject',
74 winner_criteria
: null,
76 declare_winning_time
: null,
79 var mailingDefaults
= {
80 // Most defaults provided by Mailing.create API, but we
81 // want to force-enable tracking.
84 mailing_type
:"experiment"
86 crmMailingAB
.mailings
.a
= crmMailingMgr
.create(mailingDefaults
);
87 crmMailingAB
.mailings
.b
= crmMailingMgr
.create(mailingDefaults
);
88 mailingDefaults
.mailing_type
= 'winner';
89 crmMailingAB
.mailings
.c
= crmMailingMgr
.create(mailingDefaults
);
90 crmMailingAB
.attachments
.a
= new CrmAttachments(function () {
91 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_a
};
93 crmMailingAB
.attachments
.b
= new CrmAttachments(function () {
94 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_b
};
96 crmMailingAB
.attachments
.c
= new CrmAttachments(function () {
97 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_c
};
100 var dfr
= $q
.defer();
101 dfr
.resolve(crmMailingAB
);
105 return crmApi('MailingAB', 'get', {id
: crmMailingAB
.id
})
106 .then(function (abResult
) {
107 if (abResult
.count
!= 1) {
108 throw "Failed to load AB Test";
110 crmMailingAB
.ab
= abResult
.values
[abResult
.id
];
111 return crmMailingAB
._loadMailings();
115 // @return Promise CrmMailingAB
116 save
: function save() {
117 var crmMailingAB
= this;
118 return crmMailingAB
._saveMailings()
120 return crmApi('MailingAB', 'create', crmMailingAB
.ab
)
121 .then(function (abResult
) {
122 if (!crmMailingAB
.id
) {
123 crmMailingAB
.id
= crmMailingAB
.ab
.id
= abResult
.id
;
132 // @return Promise CrmMailingAB
133 // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
134 submitTest
: function submitTest() {
135 var crmMailingAB
= this;
139 approval_date
: 'now',
140 scheduled_date
: this.mailings
.a
.scheduled_date
? this.mailings
.a
.scheduled_date
: 'now'
142 return crmApi('MailingAB', 'submit', params
)
144 return crmMailingAB
.load();
147 // Schedule the final mailing
148 // @return Promise CrmMailingAB
149 // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
150 submitFinal
: function submitFinal(winner_id
) {
151 var crmMailingAB
= this;
155 winner_id
: winner_id
,
156 approval_date
: 'now',
157 scheduled_date
: this.mailings
.c
.scheduled_date
? this.mailings
.c
.scheduled_date
: 'now'
159 return crmApi('MailingAB', 'submit', params
)
161 return crmMailingAB
.load();
164 // @param mailing Object (per APIv3)
166 'delete': function () {
168 return crmApi('MailingAB', 'delete', {id
: this.id
});
176 // Load mailings A, B, and C (if available)
177 // @return Promise CrmMailingAB
178 _loadMailings
: function _loadMailings() {
179 var crmMailingAB
= this;
181 _
.each(['a', 'b', 'c'], function (mkey
) {
182 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
183 todos
[mkey
] = crmMailingMgr
.get(crmMailingAB
.ab
['mailing_id_' + mkey
])
184 .then(function (mailing
) {
185 crmMailingAB
.mailings
[mkey
] = mailing
;
186 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
187 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
189 return crmMailingAB
.attachments
[mkey
].load();
193 crmMailingAB
.mailings
[mkey
] = crmMailingMgr
.create();
194 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
195 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
199 return $q
.all(todos
).then(function () {
203 // Save mailings A, B, and C (if available)
204 // @return Promise CrmMailingAB
205 _saveMailings
: function _saveMailings() {
206 var crmMailingAB
= this;
208 var p
= $q
.when(true);
209 _
.each(['a', 'b', 'c'], function (mkey
) {
210 if (!crmMailingAB
.mailings
[mkey
]) {
213 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
214 // paranoia: in case caller forgot to manage id on mailing
215 crmMailingAB
.mailings
[mkey
].id
= crmMailingAB
.ab
['mailing_id_' + mkey
];
217 p
= p
.then(function(){
218 return crmMailingMgr
.save(crmMailingAB
.mailings
[mkey
])
220 crmMailingAB
.ab
['mailing_id_' + mkey
] = crmMailingAB
.mailings
[mkey
].id
;
221 return crmMailingAB
.attachments
[mkey
].save();
225 return p
.then(function () {
234 })(angular
, CRM
.$, CRM
._
);