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