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