Commit | Line | Data |
---|---|---|
70980d8e TO |
1 | (function (angular, $, _) { |
2 | ||
63430d3c TO |
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; | |
7 | }; | |
8 | this.getByName = function get(name) { | |
9 | var r = _.where(values, {name: '' + name}); | |
10 | return r.length > 0 ? r[0] : null; | |
11 | }; | |
12 | this.getAll = function getAll() { | |
13 | return values; | |
14 | }; | |
15 | } | |
16 | ||
efd95528 | 17 | angular.module('crmMailingAB').factory('crmMailingABCriteria', function () { |
70980d8e TO |
18 | // TODO Get data from server |
19 | var values = { | |
05aaa6ae TO |
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')} | |
70980d8e | 23 | }; |
63430d3c TO |
24 | return new OptionGroup(values); |
25 | }); | |
26 | ||
27 | angular.module('crmMailingAB').factory('crmMailingABStatus', function () { | |
28 | // TODO Get data from server | |
29 | var values = { | |
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')} | |
70980d8e | 33 | }; |
63430d3c | 34 | return new OptionGroup(values); |
70980d8e TO |
35 | }); |
36 | ||
aafc090b TO |
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"). | |
39 | // | |
40 | // example: | |
41 | // var abtest = new CrmMailingAB(123); | |
42 | // abtest.load().then(function(){ | |
43 | // alert("Mailing A is named "+abtest.mailings.a.name); | |
44 | // }); | |
d3312ed5 | 45 | angular.module('crmMailingAB').factory('CrmMailingAB', function (crmApi, crmMailingMgr, $q, CrmAttachments) { |
70980d8e TO |
46 | function CrmMailingAB(id) { |
47 | this.id = id; | |
48 | this.mailings = {}; | |
bcdd7f49 | 49 | this.attachments = {}; |
70980d8e TO |
50 | } |
51 | ||
52 | angular.extend(CrmMailingAB.prototype, { | |
a12bd71d TO |
53 | getAutosaveSignature: function() { |
54 | return [ | |
55 | this.ab, | |
56 | this.mailings, | |
57 | this.attachments.a.getAutosaveSignature(), | |
58 | this.attachments.b.getAutosaveSignature(), | |
59 | this.attachments.c.getAutosaveSignature() | |
60 | ]; | |
61 | }, | |
70980d8e TO |
62 | // @return Promise CrmMailingAB |
63 | load: function load() { | |
64 | var crmMailingAB = this; | |
65 | if (!crmMailingAB.id) { | |
66 | crmMailingAB.ab = { | |
2784283f | 67 | name: '', |
c9e9a71e | 68 | status: 'Draft', |
70980d8e TO |
69 | mailing_id_a: null, |
70 | mailing_id_b: null, | |
71 | mailing_id_c: null, | |
72 | domain_id: null, | |
05aaa6ae TO |
73 | testing_criteria: 'subject', |
74 | winner_criteria: null, | |
70980d8e TO |
75 | specific_url: '', |
76 | declare_winning_time: null, | |
77 | group_percentage: 10 | |
78 | }; | |
657b8692 TO |
79 | var mailingDefaults = { |
80 | // Most defaults provided by Mailing.create API, but we | |
81 | // want to force-enable tracking. | |
82 | open_tracking: "1", | |
e81bac46 | 83 | url_tracking: "1", |
f72b68c0 | 84 | mailing_type:"experiment" |
657b8692 TO |
85 | }; |
86 | crmMailingAB.mailings.a = crmMailingMgr.create(mailingDefaults); | |
87 | crmMailingAB.mailings.b = crmMailingMgr.create(mailingDefaults); | |
e81bac46 | 88 | mailingDefaults.mailing_type = 'winner'; |
657b8692 | 89 | crmMailingAB.mailings.c = crmMailingMgr.create(mailingDefaults); |
bcdd7f49 | 90 | crmMailingAB.attachments.a = new CrmAttachments(function () { |
f2bad133 | 91 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_a}; |
bcdd7f49 TO |
92 | }); |
93 | crmMailingAB.attachments.b = new CrmAttachments(function () { | |
f2bad133 | 94 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_b}; |
bcdd7f49 TO |
95 | }); |
96 | crmMailingAB.attachments.c = new CrmAttachments(function () { | |
f2bad133 | 97 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_c}; |
bcdd7f49 | 98 | }); |
70980d8e TO |
99 | |
100 | var dfr = $q.defer(); | |
101 | dfr.resolve(crmMailingAB); | |
102 | return dfr.promise; | |
103 | } | |
104 | else { | |
105 | return crmApi('MailingAB', 'get', {id: crmMailingAB.id}) | |
106 | .then(function (abResult) { | |
86c3a327 TO |
107 | if (abResult.count != 1) { |
108 | throw "Failed to load AB Test"; | |
109 | } | |
70980d8e TO |
110 | crmMailingAB.ab = abResult.values[abResult.id]; |
111 | return crmMailingAB._loadMailings(); | |
112 | }); | |
113 | } | |
114 | }, | |
115 | // @return Promise CrmMailingAB | |
116 | save: function save() { | |
117 | var crmMailingAB = this; | |
70980d8e TO |
118 | return crmMailingAB._saveMailings() |
119 | .then(function () { | |
120 | return crmApi('MailingAB', 'create', crmMailingAB.ab) | |
121 | .then(function (abResult) { | |
ce245e83 TO |
122 | if (!crmMailingAB.id) { |
123 | crmMailingAB.id = crmMailingAB.ab.id = abResult.id; | |
124 | } | |
70980d8e TO |
125 | }); |
126 | }) | |
127 | .then(function () { | |
128 | return crmMailingAB; | |
129 | }); | |
130 | }, | |
c9e9a71e TO |
131 | // Schedule the test |
132 | // @return Promise CrmMailingAB | |
32b8b0bf TO |
133 | // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...) |
134 | submitTest: function submitTest() { | |
c9e9a71e TO |
135 | var crmMailingAB = this; |
136 | var params = { | |
8717390f | 137 | id: this.ab.id, |
32b8b0bf | 138 | status: 'Testing', |
d3312ed5 | 139 | approval_date: 'now', |
140 | scheduled_date: this.mailings.a.scheduled_date ? this.mailings.a.scheduled_date : 'now' | |
c9e9a71e TO |
141 | }; |
142 | return crmApi('MailingAB', 'submit', params) | |
143 | .then(function () { | |
6877567a | 144 | return crmMailingAB.load(); |
c9e9a71e TO |
145 | }); |
146 | }, | |
32b8b0bf TO |
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()...) | |
88e6ecf0 | 150 | submitFinal: function submitFinal(winner_id) { |
32b8b0bf TO |
151 | var crmMailingAB = this; |
152 | var params = { | |
153 | id: this.ab.id, | |
154 | status: 'Final', | |
88e6ecf0 | 155 | winner_id: winner_id, |
d3312ed5 | 156 | approval_date: 'now', |
157 | scheduled_date: this.mailings.c.scheduled_date ? this.mailings.c.scheduled_date : 'now' | |
32b8b0bf TO |
158 | }; |
159 | return crmApi('MailingAB', 'submit', params) | |
160 | .then(function () { | |
00d99fa7 | 161 | return crmMailingAB.load(); |
32b8b0bf TO |
162 | }); |
163 | }, | |
86c3a327 TO |
164 | // @param mailing Object (per APIv3) |
165 | // @return Promise | |
166 | 'delete': function () { | |
167 | if (this.id) { | |
168 | return crmApi('MailingAB', 'delete', {id: this.id}); | |
169 | } | |
170 | else { | |
171 | var d = $q.defer(); | |
172 | d.resolve(); | |
173 | return d.promise; | |
174 | } | |
175 | }, | |
70980d8e TO |
176 | // Load mailings A, B, and C (if available) |
177 | // @return Promise CrmMailingAB | |
178 | _loadMailings: function _loadMailings() { | |
179 | var crmMailingAB = this; | |
180 | var todos = {}; | |
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; | |
bcdd7f49 TO |
186 | crmMailingAB.attachments[mkey] = new CrmAttachments(function () { |
187 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_' + mkey]}; | |
188 | }); | |
189 | return crmMailingAB.attachments[mkey].load(); | |
70980d8e TO |
190 | }); |
191 | } | |
192 | else { | |
193 | crmMailingAB.mailings[mkey] = crmMailingMgr.create(); | |
bcdd7f49 TO |
194 | crmMailingAB.attachments[mkey] = new CrmAttachments(function () { |
195 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_' + mkey]}; | |
196 | }); | |
70980d8e TO |
197 | } |
198 | }); | |
199 | return $q.all(todos).then(function () { | |
200 | return crmMailingAB; | |
201 | }); | |
202 | }, | |
203 | // Save mailings A, B, and C (if available) | |
204 | // @return Promise CrmMailingAB | |
205 | _saveMailings: function _saveMailings() { | |
206 | var crmMailingAB = this; | |
207 | var todos = {}; | |
c9e9a71e | 208 | var p = $q.when(true); |
70980d8e TO |
209 | _.each(['a', 'b', 'c'], function (mkey) { |
210 | if (!crmMailingAB.mailings[mkey]) { | |
211 | return; | |
212 | } | |
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]; | |
216 | } | |
c9e9a71e TO |
217 | p = p.then(function(){ |
218 | return crmMailingMgr.save(crmMailingAB.mailings[mkey]) | |
219 | .then(function () { | |
220 | crmMailingAB.ab['mailing_id_' + mkey] = crmMailingAB.mailings[mkey].id; | |
221 | return crmMailingAB.attachments[mkey].save(); | |
222 | }); | |
223 | }); | |
70980d8e | 224 | }); |
c9e9a71e | 225 | return p.then(function () { |
70980d8e TO |
226 | return crmMailingAB; |
227 | }); | |
228 | } | |
229 | ||
230 | }); | |
231 | return CrmMailingAB; | |
232 | }); | |
233 | ||
234 | })(angular, CRM.$, CRM._); |