1 (function (angular
, $, _
) {
3 // FIXME: surely there's already some helper which can do this in one line?
4 // @return string "YYYY-MM-DD hh:mm:ss"
5 var createNow = function () {
6 var currentdate
= new Date();
7 var yyyy
= currentdate
.getFullYear();
8 var mm
= currentdate
.getMonth() + 1;
9 mm
= mm
< 10 ? '0' + mm
: mm
;
10 var dd
= currentdate
.getDate();
11 dd
= dd
< 10 ? '0' + dd
: dd
;
12 var hh
= currentdate
.getHours();
13 hh
= hh
< 10 ? '0' + hh
: hh
;
14 var min
= currentdate
.getMinutes();
15 min
= min
< 10 ? '0' + min
: min
;
16 var sec
= currentdate
.getSeconds();
17 sec
= sec
< 10 ? '0' + sec
: sec
;
18 return yyyy
+ "-" + mm
+ "-" + dd
+ " " + hh
+ ":" + min
+ ":" + sec
;
21 angular
.module('crmMailingAB').factory('crmMailingABCriteria', function () {
22 // TODO Get data from server
24 '1': {value
: '1', name
: 'Subject lines', label
: ts('Test different "Subject" lines')},
25 '2': {value
: '2', name
: 'From names', label
: ts('Test different "From" lines')},
26 '3': {value
: '3', name
: 'Two different emails', label
: ts('Test entirely different emails')}
29 get: function get(value
) {
30 var r
= _
.where(values
, {value
: '' + value
});
31 return r
.length
> 0 ? r
[0] : null;
33 getAll
: function getAll() {
39 // CrmMailingAB is a data-model which combines an AB test (APIv3 "MailingAB"), three mailings (APIv3 "Mailing"),
40 // and three sets of attachments (APIv3 "Attachment").
43 // var abtest = new CrmMailingAB(123);
44 // abtest.load().then(function(){
45 // alert("Mailing A is named "+abtest.mailings.a.name);
47 angular
.module('crmMailingAB').factory('CrmMailingAB', function (crmApi
, crmMailingMgr
, $q
, CrmAttachments
) {
48 function CrmMailingAB(id
) {
51 this.attachments
= {};
54 angular
.extend(CrmMailingAB
.prototype, {
55 // @return Promise CrmMailingAB
56 load
: function load() {
57 var crmMailingAB
= this;
58 if (!crmMailingAB
.id
) {
60 name
: 'Example', // FIXME
66 testing_criteria_id
: 1, // FIXME
67 winner_criteria_id
: null,
69 declare_winning_time
: null,
72 crmMailingAB
.mailings
.a
= crmMailingMgr
.create();
73 crmMailingAB
.mailings
.b
= crmMailingMgr
.create();
74 crmMailingAB
.mailings
.c
= crmMailingMgr
.create();
75 crmMailingAB
.attachments
.a
= new CrmAttachments(function () {
76 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_a']};
78 crmMailingAB
.attachments
.b
= new CrmAttachments(function () {
79 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_b']};
81 crmMailingAB
.attachments
.c
= new CrmAttachments(function () {
82 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_c']};
86 dfr
.resolve(crmMailingAB
);
90 return crmApi('MailingAB', 'get', {id
: crmMailingAB
.id
})
91 .then(function (abResult
) {
92 if (abResult
.count
!= 1) {
93 throw "Failed to load AB Test";
95 crmMailingAB
.ab
= abResult
.values
[abResult
.id
];
96 return crmMailingAB
._loadMailings();
100 // @return Promise CrmMailingAB
101 save
: function save() {
102 var crmMailingAB
= this;
103 return crmMailingAB
._saveMailings()
105 return crmApi('MailingAB', 'create', crmMailingAB
.ab
)
106 .then(function (abResult
) {
107 crmMailingAB
.ab
.id
= abResult
.id
;
115 // @return Promise CrmMailingAB
116 // Note: Submission may cause the server state to change. Consider abtest.submit().then(abtest.load)
117 submit
: function submit(newStatus
) {
118 var crmMailingAB
= this;
122 approval_date
: createNow(),
123 scheduled_date
: this.mailings
.a
.scheduled_date
? this.mailings
.a
.scheduled_date
: createNow()
125 return crmApi('MailingAB', 'submit', params
)
130 // @param mailing Object (per APIv3)
132 'delete': function () {
134 return crmApi('MailingAB', 'delete', {id
: this.id
});
142 // Load mailings A, B, and C (if available)
143 // @return Promise CrmMailingAB
144 _loadMailings
: function _loadMailings() {
145 var crmMailingAB
= this;
147 _
.each(['a', 'b', 'c'], function (mkey
) {
148 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
149 todos
[mkey
] = crmMailingMgr
.get(crmMailingAB
.ab
['mailing_id_' + mkey
])
150 .then(function (mailing
) {
151 crmMailingAB
.mailings
[mkey
] = mailing
;
152 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
153 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
155 return crmMailingAB
.attachments
[mkey
].load();
159 crmMailingAB
.mailings
[mkey
] = crmMailingMgr
.create();
160 crmMailingAB
.attachments
[mkey
] = new CrmAttachments(function () {
161 return {entity_table
: 'civicrm_mailing', entity_id
: crmMailingAB
.ab
['mailing_id_' + mkey
]};
165 return $q
.all(todos
).then(function () {
169 // Save mailings A, B, and C (if available)
170 // @return Promise CrmMailingAB
171 _saveMailings
: function _saveMailings() {
172 var crmMailingAB
= this;
174 var p
= $q
.when(true);
175 _
.each(['a', 'b', 'c'], function (mkey
) {
176 if (!crmMailingAB
.mailings
[mkey
]) {
179 if (crmMailingAB
.ab
['mailing_id_' + mkey
]) {
180 // paranoia: in case caller forgot to manage id on mailing
181 crmMailingAB
.mailings
[mkey
].id
= crmMailingAB
.ab
['mailing_id_' + mkey
];
183 p
= p
.then(function(){
184 return crmMailingMgr
.save(crmMailingAB
.mailings
[mkey
])
186 crmMailingAB
.ab
['mailing_id_' + mkey
] = crmMailingAB
.mailings
[mkey
].id
;
187 return crmMailingAB
.attachments
[mkey
].save();
191 return p
.then(function () {
200 })(angular
, CRM
.$, CRM
._
);