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, | |
05aaa6ae TO |
72 | testing_criteria: 'subject', |
73 | winner_criteria: null, | |
70980d8e TO |
74 | specific_url: '', |
75 | declare_winning_time: null, | |
76 | group_percentage: 10 | |
77 | }; | |
657b8692 TO |
78 | var mailingDefaults = { |
79 | // Most defaults provided by Mailing.create API, but we | |
80 | // want to force-enable tracking. | |
81 | open_tracking: "1", | |
e81bac46 | 82 | url_tracking: "1", |
f72b68c0 | 83 | mailing_type:"experiment" |
657b8692 TO |
84 | }; |
85 | crmMailingAB.mailings.a = crmMailingMgr.create(mailingDefaults); | |
86 | crmMailingAB.mailings.b = crmMailingMgr.create(mailingDefaults); | |
e81bac46 | 87 | mailingDefaults.mailing_type = 'winner'; |
657b8692 | 88 | crmMailingAB.mailings.c = crmMailingMgr.create(mailingDefaults); |
bcdd7f49 | 89 | crmMailingAB.attachments.a = new CrmAttachments(function () { |
f2bad133 | 90 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_a}; |
bcdd7f49 TO |
91 | }); |
92 | crmMailingAB.attachments.b = new CrmAttachments(function () { | |
f2bad133 | 93 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_b}; |
bcdd7f49 TO |
94 | }); |
95 | crmMailingAB.attachments.c = new CrmAttachments(function () { | |
f2bad133 | 96 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_c}; |
bcdd7f49 | 97 | }); |
70980d8e TO |
98 | |
99 | var dfr = $q.defer(); | |
100 | dfr.resolve(crmMailingAB); | |
101 | return dfr.promise; | |
102 | } | |
103 | else { | |
104 | return crmApi('MailingAB', 'get', {id: crmMailingAB.id}) | |
105 | .then(function (abResult) { | |
86c3a327 TO |
106 | if (abResult.count != 1) { |
107 | throw "Failed to load AB Test"; | |
108 | } | |
70980d8e TO |
109 | crmMailingAB.ab = abResult.values[abResult.id]; |
110 | return crmMailingAB._loadMailings(); | |
111 | }); | |
112 | } | |
113 | }, | |
114 | // @return Promise CrmMailingAB | |
115 | save: function save() { | |
116 | var crmMailingAB = this; | |
70980d8e TO |
117 | return crmMailingAB._saveMailings() |
118 | .then(function () { | |
119 | return crmApi('MailingAB', 'create', crmMailingAB.ab) | |
120 | .then(function (abResult) { | |
ce245e83 TO |
121 | if (!crmMailingAB.id) { |
122 | crmMailingAB.id = crmMailingAB.ab.id = abResult.id; | |
123 | } | |
70980d8e TO |
124 | }); |
125 | }) | |
126 | .then(function () { | |
127 | return crmMailingAB; | |
128 | }); | |
129 | }, | |
c9e9a71e TO |
130 | // Schedule the test |
131 | // @return Promise CrmMailingAB | |
32b8b0bf TO |
132 | // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...) |
133 | submitTest: function submitTest() { | |
c9e9a71e TO |
134 | var crmMailingAB = this; |
135 | var params = { | |
8717390f | 136 | id: this.ab.id, |
32b8b0bf | 137 | status: 'Testing', |
d3312ed5 | 138 | approval_date: 'now', |
139 | scheduled_date: this.mailings.a.scheduled_date ? this.mailings.a.scheduled_date : 'now' | |
c9e9a71e TO |
140 | }; |
141 | return crmApi('MailingAB', 'submit', params) | |
142 | .then(function () { | |
6877567a | 143 | return crmMailingAB.load(); |
c9e9a71e TO |
144 | }); |
145 | }, | |
32b8b0bf TO |
146 | // Schedule the final mailing |
147 | // @return Promise CrmMailingAB | |
148 | // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...) | |
88e6ecf0 | 149 | submitFinal: function submitFinal(winner_id) { |
32b8b0bf TO |
150 | var crmMailingAB = this; |
151 | var params = { | |
152 | id: this.ab.id, | |
153 | status: 'Final', | |
88e6ecf0 | 154 | winner_id: winner_id, |
d3312ed5 | 155 | approval_date: 'now', |
156 | scheduled_date: this.mailings.c.scheduled_date ? this.mailings.c.scheduled_date : 'now' | |
32b8b0bf TO |
157 | }; |
158 | return crmApi('MailingAB', 'submit', params) | |
159 | .then(function () { | |
00d99fa7 | 160 | return crmMailingAB.load(); |
32b8b0bf TO |
161 | }); |
162 | }, | |
86c3a327 TO |
163 | // @param mailing Object (per APIv3) |
164 | // @return Promise | |
165 | 'delete': function () { | |
166 | if (this.id) { | |
167 | return crmApi('MailingAB', 'delete', {id: this.id}); | |
168 | } | |
169 | else { | |
170 | var d = $q.defer(); | |
171 | d.resolve(); | |
172 | return d.promise; | |
173 | } | |
174 | }, | |
70980d8e TO |
175 | // Load mailings A, B, and C (if available) |
176 | // @return Promise CrmMailingAB | |
177 | _loadMailings: function _loadMailings() { | |
178 | var crmMailingAB = this; | |
179 | var todos = {}; | |
180 | _.each(['a', 'b', 'c'], function (mkey) { | |
181 | if (crmMailingAB.ab['mailing_id_' + mkey]) { | |
182 | todos[mkey] = crmMailingMgr.get(crmMailingAB.ab['mailing_id_' + mkey]) | |
183 | .then(function (mailing) { | |
184 | crmMailingAB.mailings[mkey] = mailing; | |
bcdd7f49 TO |
185 | crmMailingAB.attachments[mkey] = new CrmAttachments(function () { |
186 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_' + mkey]}; | |
187 | }); | |
188 | return crmMailingAB.attachments[mkey].load(); | |
70980d8e TO |
189 | }); |
190 | } | |
191 | else { | |
192 | crmMailingAB.mailings[mkey] = crmMailingMgr.create(); | |
bcdd7f49 TO |
193 | crmMailingAB.attachments[mkey] = new CrmAttachments(function () { |
194 | return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_' + mkey]}; | |
195 | }); | |
70980d8e TO |
196 | } |
197 | }); | |
198 | return $q.all(todos).then(function () { | |
199 | return crmMailingAB; | |
200 | }); | |
201 | }, | |
202 | // Save mailings A, B, and C (if available) | |
203 | // @return Promise CrmMailingAB | |
204 | _saveMailings: function _saveMailings() { | |
205 | var crmMailingAB = this; | |
206 | var todos = {}; | |
c9e9a71e | 207 | var p = $q.when(true); |
70980d8e TO |
208 | _.each(['a', 'b', 'c'], function (mkey) { |
209 | if (!crmMailingAB.mailings[mkey]) { | |
210 | return; | |
211 | } | |
212 | if (crmMailingAB.ab['mailing_id_' + mkey]) { | |
213 | // paranoia: in case caller forgot to manage id on mailing | |
214 | crmMailingAB.mailings[mkey].id = crmMailingAB.ab['mailing_id_' + mkey]; | |
215 | } | |
c9e9a71e TO |
216 | p = p.then(function(){ |
217 | return crmMailingMgr.save(crmMailingAB.mailings[mkey]) | |
218 | .then(function () { | |
219 | crmMailingAB.ab['mailing_id_' + mkey] = crmMailingAB.mailings[mkey].id; | |
220 | return crmMailingAB.attachments[mkey].save(); | |
221 | }); | |
222 | }); | |
70980d8e | 223 | }); |
c9e9a71e | 224 | return p.then(function () { |
70980d8e TO |
225 | return crmMailingAB; |
226 | }); | |
227 | } | |
228 | ||
229 | }); | |
230 | return CrmMailingAB; | |
231 | }); | |
232 | ||
233 | })(angular, CRM.$, CRM._); |