Commit | Line | Data |
---|---|---|
8dfd5110 TO |
1 | (function (angular, $, _) { |
2 | var partialUrl = function (relPath) { | |
3 | return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath; | |
4 | }; | |
5 | ||
6 | var crmMailing2 = angular.module('crmMailing2'); | |
7 | ||
a0214785 TO |
8 | // The representation of from/reply-to addresses is inconsistent in the mailing data-model, |
9 | // so the UI must do some adaptation. The crmFromAddresses provides a richer way to slice/dice | |
10 | // the available "From:" addrs. Records are like the underlying OptionValues -- but add "email" | |
11 | // and "author". | |
12 | crmMailing2.factory('crmFromAddresses', function($q, crmApi) { | |
13 | var emailRegex = /^"(.*)" \<([^@\>]*@[^@\>]*)\>$/; | |
14 | var addrs = _.map(CRM.crmMailing.fromAddress, function(addr){ | |
15 | var match = emailRegex.exec(addr.label); | |
16 | return _.extend({}, addr, { | |
17 | email: match ? match[2] : '(INVALID)', | |
18 | author: match ? match[1] : '(INVALID)' | |
19 | }); | |
20 | }); | |
21 | function first(array) { | |
22 | return (array.length == 0) ? null : array[0]; | |
52f515c6 | 23 | } |
a0214785 TO |
24 | |
25 | return { | |
26 | getAll: function getAll() { | |
27 | return addrs; | |
28 | }, | |
29 | getByAuthorEmail: function getByAuthorEmail(author, email, autocreate) { | |
30 | var result = null; | |
31 | _.each(addrs, function(addr){ | |
32 | if (addr.author == author && addr.email == email) { | |
33 | result = addr; | |
34 | } | |
35 | }); | |
36 | if (!result && autocreate) { | |
37 | result = { | |
38 | label: '(INVALID) "' + author + '" <' + email + '>', | |
39 | author: author, | |
40 | email: email | |
41 | }; | |
42 | addrs.push(result); | |
43 | } | |
44 | return result; | |
45 | }, | |
46 | getByEmail: function getByEmail(email) { | |
47 | return first(_.where(addrs, {email: email})); | |
48 | }, | |
49 | getByLabel: function(label) { | |
50 | return first(_.where(addrs, {label: label})); | |
51 | }, | |
52 | getDefault: function getDefault() { | |
53 | return first(_.where(addrs, {is_default: "1"})); | |
54 | } | |
55 | }; | |
56 | }); | |
57 | ||
744bebee TO |
58 | crmMailing2.factory('crmMsgTemplates', function($q, crmApi) { |
59 | var tpls = _.map(CRM.crmMailing.mesTemplate, function(tpl){ | |
60 | return _.extend({}, tpl, { | |
61 | //id: tpl parseInt(tpl.id) | |
62 | }); | |
63 | }); | |
64 | window.tpls = tpls; | |
65 | var lastModifiedTpl = null; | |
66 | return { | |
67 | // @return Promise MessageTemplate (per APIv3) | |
68 | get: function get(id) { | |
69 | id = ''+id; // parseInt(id); | |
70 | var dfr = $q.defer(); | |
71 | var tpl = _.where(tpls, {id: id}); | |
72 | if (id && tpl && tpl[0]) { | |
73 | dfr.resolve(tpl[0]); | |
74 | } else { | |
75 | dfr.reject(id); | |
76 | } | |
77 | return dfr.promise; | |
78 | }, | |
79 | // Save a template | |
80 | // @param tpl MessageTemplate (per APIv3) For new templates, omit "id" | |
81 | // @return Promise MessageTemplate (per APIv3) | |
82 | save: function(tpl) { | |
83 | return crmApi('MessageTemplate', 'create', tpl).then(function(response){ | |
84 | if (!tpl.id) { | |
85 | tpl.id = ''+response.id; //parseInt(response.id); | |
86 | tpls.push(tpl); | |
87 | } | |
88 | lastModifiedTpl = tpl | |
89 | return tpl; | |
90 | }); | |
91 | }, | |
92 | // @return Object MessageTemplate (per APIv3) | |
93 | getLastModifiedTpl: function() { | |
94 | return lastModifiedTpl; | |
95 | }, | |
96 | getAll: function getAll() { | |
97 | return tpls; | |
98 | } | |
99 | }; | |
100 | }); | |
101 | ||
a0214785 TO |
102 | // The crmMailingMgr service provides business logic for loading, saving, previewing, etc |
103 | crmMailing2.factory('crmMailingMgr', function($q, crmApi, crmFromAddresses) { | |
4dd19229 | 104 | var pickDefaultMailComponent = function pickDefaultMailComponent(type) { |
8dfd5110 TO |
105 | var mcs = _.where(CRM.crmMailing.headerfooterList, { |
106 | component_type:type, | |
107 | is_default: "1" | |
108 | }); | |
109 | return (mcs.length >= 1) ? mcs[0].id : null; | |
110 | }; | |
111 | ||
112 | return { | |
113 | // @param scalar idExpr a number or the literal string 'new' | |
114 | // @return Promise|Object Mailing (per APIv3) | |
4dd19229 | 115 | getOrCreate: function getOrCreate(idExpr) { |
8dfd5110 TO |
116 | return (idExpr == 'new') ? this.create() : this.get(idExpr); |
117 | }, | |
118 | // @return Promise Mailing (per APIv3) | |
4dd19229 | 119 | get: function get(id) { |
8dfd5110 TO |
120 | return crmApi('Mailing', 'getsingle', {id: id}).then(function(mailing){ |
121 | return crmApi('MailingGroup', 'get', {mailing_id: id}).then(function(groupResult){ | |
122 | mailing.groups = {include: [], exclude: []}; | |
123 | mailing.mailings = {include: [], exclude: []}; | |
124 | _.each(groupResult.values, function(mailingGroup) { | |
125 | var bucket = (mailingGroup.entity_table == 'civicrm_group') ? 'groups' : 'mailings'; | |
89a50c67 TO |
126 | var entityId = parseInt(mailingGroup.entity_id); |
127 | mailing[bucket][mailingGroup.group_type].push(entityId); | |
8dfd5110 TO |
128 | }); |
129 | return mailing; | |
130 | }); | |
131 | }); | |
132 | }, | |
133 | // @return Object Mailing (per APIv3) | |
4dd19229 | 134 | create: function create() { |
8dfd5110 TO |
135 | return { |
136 | name: "revert this", // fixme | |
137 | campaign_id: null, | |
a0214785 TO |
138 | from_name: crmFromAddresses.getDefault().author, |
139 | from_email: crmFromAddresses.getDefault().email, | |
8dfd5110 TO |
140 | replyto_email: "", |
141 | subject: "For {contact.display_name}", // fixme | |
142 | dedupe_email: "1", | |
143 | groups: {include: [2], exclude: [4]}, // fixme | |
144 | mailings: {include: [], exclude: []}, | |
145 | body_html: "<b>Hello</b> {contact.display_name}", // fixme | |
146 | body_text: "Hello {contact.display_name}", // fixme | |
147 | footer_id: null, // pickDefaultMailComponent('Footer'), | |
148 | header_id: null, // pickDefaultMailComponent('Header'), | |
149 | visibility: "Public Pages", | |
150 | url_tracking: "1", | |
151 | dedupe_email: "1", | |
152 | forward_replies: "0", | |
153 | auto_responder: "0", | |
154 | open_tracking: "1", | |
155 | override_verp: "1", | |
156 | optout_id: pickDefaultMailComponent('OptOut'), | |
157 | reply_id: pickDefaultMailComponent('Reply'), | |
158 | resubscribe_id: pickDefaultMailComponent('Resubscribe'), | |
159 | unsubscribe_id: pickDefaultMailComponent('Unsubscribe') | |
160 | }; | |
161 | }, | |
162 | ||
705c61e9 TO |
163 | // @param mailing Object (per APIv3) |
164 | // @return Promise | |
165 | 'delete': function(mailing) { | |
166 | if (mailing.id) { | |
167 | return crmApi('Mailing', 'delete', {id: mailing.id}); | |
168 | } else { | |
169 | var d = $q.defer(); | |
170 | d.resolve(); | |
171 | return d.promise; | |
172 | } | |
173 | }, | |
174 | ||
493eb47a TO |
175 | // @param mailing Object (per APIv3) |
176 | // @return Promise an object with "subject", "body_text", "body_html" | |
177 | preview: function preview(mailing) { | |
178 | var params = _.extend({}, mailing, { | |
179 | options: {force_rollback: 1}, | |
180 | 'api.Mailing.preview': { | |
52f515c6 | 181 | id: '$value.id' |
493eb47a TO |
182 | } |
183 | }); | |
184 | return crmApi('Mailing', 'create', params).then(function(result){ | |
beab9d1b | 185 | // changes rolled back, so we don't care about updating mailing |
493eb47a TO |
186 | return result.values[result.id]['api.Mailing.preview'].values; |
187 | }); | |
188 | }, | |
189 | ||
8dfd5110 TO |
190 | // @param mailing Object (per APIv3) |
191 | // @param int previewLimit | |
192 | // @return Promise for a list of recipients (mailing_id, contact_id, api.contact.getvalue, api.email.getvalue) | |
4dd19229 | 193 | previewRecipients: function previewRecipients(mailing, previewLimit) { |
8dfd5110 TO |
194 | // To get list of recipients, we tentatively save the mailing and |
195 | // get the resulting recipients -- then rollback any changes. | |
196 | var params = _.extend({}, mailing, { | |
197 | options: {force_rollback: 1}, | |
7575b840 | 198 | 'api.mailing_job.create': 1, // note: exact match to API default |
8dfd5110 TO |
199 | 'api.MailingRecipients.get': { |
200 | mailing_id: '$value.id', | |
201 | options: { limit: previewLimit }, | |
202 | 'api.contact.getvalue': {'return': 'display_name'}, | |
203 | 'api.email.getvalue': {'return': 'email'} | |
204 | } | |
205 | }); | |
206 | return crmApi('Mailing', 'create', params).then(function(recipResult){ | |
beab9d1b | 207 | // changes rolled back, so we don't care about updating mailing |
8dfd5110 TO |
208 | return recipResult.values[recipResult.id]['api.MailingRecipients.get'].values; |
209 | }); | |
beab9d1b TO |
210 | }, |
211 | ||
705c61e9 TO |
212 | // @param mailing Object (per APIv3) |
213 | // @return Promise | |
214 | save: function(mailing) { | |
7575b840 TO |
215 | var params = _.extend({}, mailing, { |
216 | 'api.mailing_job.create': 0 // note: exact match to API default | |
217 | }); | |
705c61e9 TO |
218 | return crmApi('Mailing', 'create', params).then(function(result){ |
219 | if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id | |
220 | return result.values[result.id]; | |
221 | }); | |
222 | }, | |
223 | ||
224 | // Schedule/send the mailing | |
225 | // @param mailing Object (per APIv3) | |
226 | // @return Promise | |
227 | submit: function(mailing) { | |
228 | throw 'Not implemented: crmMailingMgr.submit'; | |
7575b840 TO |
229 | // var params = _.extend({}, mailing, { |
230 | // 'api.mailing_job.create': 1 // note: exact match to API default | |
231 | // }); | |
705c61e9 TO |
232 | // return crmApi('Mailing', 'create', params).then(function(result){ |
233 | // if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id | |
234 | // return result.values[result.id]; | |
235 | // }); | |
236 | }, | |
237 | ||
238 | // Immediately send a test message | |
beab9d1b TO |
239 | // @param mailing Object (per APIv3) |
240 | // @param testEmail string | |
241 | // @param testGroup int (id#) | |
242 | // @return Promise for a list of delivery reports | |
243 | sendTest: function(mailing, testEmail, testGroup) { | |
244 | var params = _.extend({}, mailing, { | |
245 | // options: {force_rollback: 1}, | |
246 | 'api.Mailing.send_test': { | |
247 | mailing_id: '$value.id', | |
248 | test_email: testEmail, | |
249 | test_group: testGroup | |
250 | } | |
251 | }); | |
252 | return crmApi('Mailing', 'create', params).then(function(result){ | |
253 | if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id | |
254 | return result.values[result.id]['api.Mailing.send_test'].values; | |
255 | }); | |
8dfd5110 TO |
256 | } |
257 | }; | |
258 | }); | |
8dfd5110 | 259 | })(angular, CRM.$, CRM._); |