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