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
: '1', name
: 'Subject lines', label
: ts('Test different "Subject" lines')},
21 '2': {value
: '2', name
: 'From names', label
: ts('Test different "From" lines')},
22 '3': {value
: '3', name
: 'Two different emails', 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 // @return Promise CrmMailingAB
54 load
: function load() {
55 var crmMailingAB
= this;
56 if (!crmMailingAB
.id
) {
58 name
: 'Example', // FIXME
64 testing_criteria_id
: 1, // FIXME
65 winner_criteria_id
: null,
67 declare_winning_time
: null,
70 crmMailingAB
.mailings
.a
= crmMailingMgr
.create();
71 crmMailingAB
.mailings
.b
= crmMailingMgr
.create();
72 crmMailingAB
.mailings
.c
= crmMailingMgr
.create();
73 crmMailingAB
.attachments
.a
= new CrmAttachments(function () {
74 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_a
};
76 crmMailingAB
.attachments
.b
= new CrmAttachments(function () {
77 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_b
};
79 crmMailingAB
.attachments
.c
= new CrmAttachments(function () {
80 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
.mailing_id_c
};
84 dfr
.resolve(crmMailingAB
);
88 return crmApi('MailingAB', 'get', {id
: crmMailingAB
.id
})
89 .then(function (abResult
) {
90 if (abResult
.count
!= 1) {
91 throw "Failed to load AB Test";
93 crmMailingAB
.ab
= abResult
.values
[abResult
.id
];
94 return crmMailingAB
._loadMailings();
98 // @return Promise CrmMailingAB
99 save
: function save() {
100 var crmMailingAB
= this;
101 return crmMailingAB
._saveMailings()
103 return crmApi('MailingAB', 'create', crmMailingAB
.ab
)
104 .then(function (abResult
) {
105 crmMailingAB
.id
= crmMailingAB
.ab
.id
= abResult
.id
;
113 // @return Promise CrmMailingAB
114 // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
115 submitTest
: function submitTest() {
116 var crmMailingAB
= this;
120 approval_date
: crmNow(),
121 scheduled_date
: this.mailings
.a
.scheduled_date
? this.mailings
.a
.scheduled_date
: crmNow()
123 return crmApi('MailingAB', 'submit', params
)
128 // Schedule the final mailing
129 // @return Promise CrmMailingAB
130 // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
131 submitFinal
: function submitFinal() {
132 var crmMailingAB
= this;
136 approval_date
: crmNow(),
137 scheduled_date
: this.mailings
.c
.scheduled_date
? this.mailings
.c
.scheduled_date
: crmNow()
139 return crmApi('MailingAB', 'submit', params
)
144 // @param mailing Object (per APIv3)
146 'delete': function () {
148 return crmApi('MailingAB', 'delete', {id
: this.id
});
156 // Load mailings A, B, and C (if available)
157 // @return Promise CrmMailingAB
158 _loadMailings
: function _loadMailings() {
159 var crmMailingAB
= this;
161 _
.each(['a', 'b', 'c'], function (mkey
) {
162 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
163 todos
[mkey
] = crmMailingMgr
.get(crmMailingAB
.ab
['mailing_id_' + mkey
])
164 .then(function (mailing
) {
165 crmMailingAB
.mailings
[mkey
] = mailing
;
166 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
167 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
169 return crmMailingAB
.attachments
[mkey
].load();
173 crmMailingAB
.mailings
[mkey
] = crmMailingMgr
.create();
174 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
175 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
179 return $q
.all(todos
).then(function () {
183 // Save mailings A, B, and C (if available)
184 // @return Promise CrmMailingAB
185 _saveMailings
: function _saveMailings() {
186 var crmMailingAB
= this;
188 var p
= $q
.when(true);
189 _
.each(['a', 'b', 'c'], function (mkey
) {
190 if (!crmMailingAB
.mailings
[mkey
]) {
193 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
194 // paranoia: in case caller forgot to manage id on mailing
195 crmMailingAB
.mailings
[mkey
].id
= crmMailingAB
.ab
['mailing_id_' + mkey
];
197 p
= p
.then(function(){
198 return crmMailingMgr
.save(crmMailingAB
.mailings
[mkey
])
200 crmMailingAB
.ab
['mailing_id_' + mkey
] = crmMailingAB
.mailings
[mkey
].id
;
201 return crmMailingAB
.attachments
[mkey
].save();
205 return p
.then(function () {
214 })(angular
, CRM
.$, CRM
._
);