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
, crmNow
) {
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.
85 crmMailingAB
.mailings
.a
= crmMailingMgr
.create(mailingDefaults
);
86 crmMailingAB
.mailings
.b
= crmMailingMgr
.create(mailingDefaults
);
87 crmMailingAB
.mailings
.c
= crmMailingMgr
.create(mailingDefaults
);
88 crmMailingAB
.attachments
.a
= new CrmAttachments(function () {
89 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_a
};
91 crmMailingAB
.attachments
.b
= new CrmAttachments(function () {
92 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_b
};
94 crmMailingAB
.attachments
.c
= new CrmAttachments(function () {
95 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_c
};
99 dfr
.resolve(crmMailingAB
);
103 return crmApi('MailingAB', 'get', {id
: crmMailingAB
.id
})
104 .then(function (abResult
) {
105 if (abResult
.count
!= 1) {
106 throw "Failed to load AB Test";
108 crmMailingAB
.ab
= abResult
.values
[abResult
.id
];
109 return crmMailingAB
._loadMailings();
113 // @return Promise CrmMailingAB
114 save
: function save() {
115 var crmMailingAB
= this;
116 return crmMailingAB
._saveMailings()
118 return crmApi('MailingAB', 'create', crmMailingAB
.ab
)
119 .then(function (abResult
) {
120 if (!crmMailingAB
.id
) {
121 crmMailingAB
.id
= crmMailingAB
.ab
.id
= abResult
.id
;
130 // @return Promise CrmMailingAB
131 // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
132 submitTest
: function submitTest() {
133 var crmMailingAB
= this;
137 approval_date
: crmNow(),
138 scheduled_date
: this.mailings
.a
.scheduled_date
? this.mailings
.a
.scheduled_date
: crmNow()
140 return crmApi('MailingAB', 'submit', params
)
142 return crmMailingAB
.load();
145 // Schedule the final mailing
146 // @return Promise CrmMailingAB
147 // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
148 submitFinal
: function submitFinal() {
149 var crmMailingAB
= this;
153 approval_date
: crmNow(),
154 scheduled_date
: this.mailings
.c
.scheduled_date
? this.mailings
.c
.scheduled_date
: crmNow()
156 return crmApi('MailingAB', 'submit', params
)
161 // @param mailing Object (per APIv3)
163 'delete': function () {
165 return crmApi('MailingAB', 'delete', {id
: this.id
});
173 // Load mailings A, B, and C (if available)
174 // @return Promise CrmMailingAB
175 _loadMailings
: function _loadMailings() {
176 var crmMailingAB
= this;
178 _
.each(['a', 'b', 'c'], function (mkey
) {
179 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
180 todos
[mkey
] = crmMailingMgr
.get(crmMailingAB
.ab
['mailing_id_' + mkey
])
181 .then(function (mailing
) {
182 crmMailingAB
.mailings
[mkey
] = mailing
;
183 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
184 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
186 return crmMailingAB
.attachments
[mkey
].load();
190 crmMailingAB
.mailings
[mkey
] = crmMailingMgr
.create();
191 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
192 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
196 return $q
.all(todos
).then(function () {
200 // Save mailings A, B, and C (if available)
201 // @return Promise CrmMailingAB
202 _saveMailings
: function _saveMailings() {
203 var crmMailingAB
= this;
205 var p
= $q
.when(true);
206 _
.each(['a', 'b', 'c'], function (mkey
) {
207 if (!crmMailingAB
.mailings
[mkey
]) {
210 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
211 // paranoia: in case caller forgot to manage id on mailing
212 crmMailingAB
.mailings
[mkey
].id
= crmMailingAB
.ab
['mailing_id_' + mkey
];
214 p
= p
.then(function(){
215 return crmMailingMgr
.save(crmMailingAB
.mailings
[mkey
])
217 crmMailingAB
.ab
['mailing_id_' + mkey
] = crmMailingAB
.mailings
[mkey
].id
;
218 return crmMailingAB
.attachments
[mkey
].save();
222 return p
.then(function () {
231 })(angular
, CRM
.$, CRM
._
);