Commit | Line | Data |
---|---|---|
8dfd5110 | 1 | (function (angular, $, _) { |
8dfd5110 | 2 | |
a0214785 TO |
3 | // The representation of from/reply-to addresses is inconsistent in the mailing data-model, |
4 | // so the UI must do some adaptation. The crmFromAddresses provides a richer way to slice/dice | |
5 | // the available "From:" addrs. Records are like the underlying OptionValues -- but add "email" | |
6 | // and "author". | |
88e9e883 | 7 | angular.module('crmMailing').factory('crmFromAddresses', function ($q, crmApi) { |
c65ce897 | 8 | var emailRegex = /^"(.*)" *<([^@>]*@[^@>]*)>$/; |
f4f103fa | 9 | var addrs = _.map(CRM.crmMailing.fromAddress, function (addr) { |
a0214785 | 10 | var match = emailRegex.exec(addr.label); |
86c3a327 | 11 | return angular.extend({}, addr, { |
a0214785 TO |
12 | email: match ? match[2] : '(INVALID)', |
13 | author: match ? match[1] : '(INVALID)' | |
14 | }); | |
15 | }); | |
f4f103fa | 16 | |
a0214785 | 17 | function first(array) { |
6bcb856f | 18 | return (array.length === 0) ? null : array[0]; |
52f515c6 | 19 | } |
a0214785 TO |
20 | |
21 | return { | |
22 | getAll: function getAll() { | |
23 | return addrs; | |
24 | }, | |
25 | getByAuthorEmail: function getByAuthorEmail(author, email, autocreate) { | |
26 | var result = null; | |
f4f103fa | 27 | _.each(addrs, function (addr) { |
a0214785 TO |
28 | if (addr.author == author && addr.email == email) { |
29 | result = addr; | |
30 | } | |
31 | }); | |
32 | if (!result && autocreate) { | |
33 | result = { | |
34 | label: '(INVALID) "' + author + '" <' + email + '>', | |
35 | author: author, | |
36 | email: email | |
37 | }; | |
38 | addrs.push(result); | |
39 | } | |
40 | return result; | |
41 | }, | |
42 | getByEmail: function getByEmail(email) { | |
43 | return first(_.where(addrs, {email: email})); | |
44 | }, | |
f4f103fa | 45 | getByLabel: function (label) { |
a0214785 TO |
46 | return first(_.where(addrs, {label: label})); |
47 | }, | |
48 | getDefault: function getDefault() { | |
49 | return first(_.where(addrs, {is_default: "1"})); | |
50 | } | |
51 | }; | |
52 | }); | |
53 | ||
88e9e883 | 54 | angular.module('crmMailing').factory('crmMsgTemplates', function ($q, crmApi) { |
f4f103fa | 55 | var tpls = _.map(CRM.crmMailing.mesTemplate, function (tpl) { |
86c3a327 | 56 | return angular.extend({}, tpl, { |
744bebee TO |
57 | //id: tpl parseInt(tpl.id) |
58 | }); | |
59 | }); | |
60 | window.tpls = tpls; | |
61 | var lastModifiedTpl = null; | |
62 | return { | |
3637550f SL |
63 | // Get a template |
64 | // @param id MessageTemplate id (per APIv3) | |
744bebee TO |
65 | // @return Promise MessageTemplate (per APIv3) |
66 | get: function get(id) { | |
ad51ce06 | 67 | return crmApi('MessageTemplate', 'getsingle', { |
3637550f SL |
68 | "return": "id,msg_subject,msg_html,msg_title,msg_text", |
69 | "id": id | |
3637550f | 70 | }); |
744bebee TO |
71 | }, |
72 | // Save a template | |
73 | // @param tpl MessageTemplate (per APIv3) For new templates, omit "id" | |
74 | // @return Promise MessageTemplate (per APIv3) | |
f4f103fa TO |
75 | save: function (tpl) { |
76 | return crmApi('MessageTemplate', 'create', tpl).then(function (response) { | |
744bebee | 77 | if (!tpl.id) { |
f4f103fa | 78 | tpl.id = '' + response.id; //parseInt(response.id); |
744bebee TO |
79 | tpls.push(tpl); |
80 | } | |
f2bad133 | 81 | lastModifiedTpl = tpl; |
744bebee TO |
82 | return tpl; |
83 | }); | |
84 | }, | |
85 | // @return Object MessageTemplate (per APIv3) | |
f4f103fa | 86 | getLastModifiedTpl: function () { |
744bebee TO |
87 | return lastModifiedTpl; |
88 | }, | |
89 | getAll: function getAll() { | |
90 | return tpls; | |
91 | } | |
92 | }; | |
93 | }); | |
94 | ||
a0214785 | 95 | // The crmMailingMgr service provides business logic for loading, saving, previewing, etc |
36768f7e TO |
96 | angular.module('crmMailing').factory('crmMailingMgr', function ($q, crmApi, crmFromAddresses, crmQueue) { |
97 | var qApi = crmQueue(crmApi); | |
4dd19229 | 98 | var pickDefaultMailComponent = function pickDefaultMailComponent(type) { |
8dfd5110 | 99 | var mcs = _.where(CRM.crmMailing.headerfooterList, { |
f4f103fa | 100 | component_type: type, |
8dfd5110 TO |
101 | is_default: "1" |
102 | }); | |
103 | return (mcs.length >= 1) ? mcs[0].id : null; | |
104 | }; | |
105 | ||
106 | return { | |
107 | // @param scalar idExpr a number or the literal string 'new' | |
108 | // @return Promise|Object Mailing (per APIv3) | |
4dd19229 | 109 | getOrCreate: function getOrCreate(idExpr) { |
8dfd5110 TO |
110 | return (idExpr == 'new') ? this.create() : this.get(idExpr); |
111 | }, | |
112 | // @return Promise Mailing (per APIv3) | |
4dd19229 | 113 | get: function get(id) { |
86c3a327 TO |
114 | var crmMailingMgr = this; |
115 | var mailing; | |
36768f7e | 116 | return qApi('Mailing', 'getsingle', {id: id}) |
86c3a327 TO |
117 | .then(function (getResult) { |
118 | mailing = getResult; | |
119 | return $q.all([ | |
120 | crmMailingMgr._loadGroups(mailing), | |
121 | crmMailingMgr._loadJobs(mailing) | |
122 | ]); | |
123 | }) | |
124 | .then(function () { | |
125 | return mailing; | |
126 | }); | |
127 | }, | |
128 | // Call MailingGroup.get and merge results into "mailing" | |
129 | _loadGroups: function (mailing) { | |
130 | return crmApi('MailingGroup', 'get', {mailing_id: mailing.id}) | |
131 | .then(function (groupResult) { | |
9cd4f489 | 132 | mailing.recipients = {}; |
720c658b | 133 | mailing.recipients.groups = {include: [], exclude: [], base: []}; |
9cd4f489 | 134 | mailing.recipients.mailings = {include: [], exclude: []}; |
f4f103fa | 135 | _.each(groupResult.values, function (mailingGroup) { |
f78abdbe | 136 | var bucket = (/^civicrm_group/.test(mailingGroup.entity_table)) ? 'groups' : 'mailings'; |
89a50c67 | 137 | var entityId = parseInt(mailingGroup.entity_id); |
9cd4f489 | 138 | mailing.recipients[bucket][mailingGroup.group_type.toLowerCase()].push(entityId); |
8dfd5110 | 139 | }); |
8dfd5110 | 140 | }); |
86c3a327 TO |
141 | }, |
142 | // Call MailingJob.get and merge results into "mailing" | |
143 | _loadJobs: function (mailing) { | |
144 | return crmApi('MailingJob', 'get', {mailing_id: mailing.id, is_test: 0}) | |
145 | .then(function (jobResult) { | |
146 | mailing.jobs = mailing.jobs || {}; | |
147 | angular.extend(mailing.jobs, jobResult.values); | |
148 | }); | |
8dfd5110 TO |
149 | }, |
150 | // @return Object Mailing (per APIv3) | |
657b8692 TO |
151 | create: function create(params) { |
152 | var defaults = { | |
86c3a327 | 153 | jobs: {}, // {jobId: JobRecord} |
9cd4f489 | 154 | recipients: { |
720c658b TO |
155 | groups: {include: [], exclude: [], base: []}, |
156 | mailings: {include: [], exclude: []} | |
9cd4f489 | 157 | }, |
3ac6e107 TO |
158 | template_type: "traditional", |
159 | // Workaround CRM-19756 w/template_options.nonce | |
160 | template_options: {nonce: 1}, | |
d78cc635 | 161 | name: "", |
8dfd5110 | 162 | campaign_id: null, |
8dfd5110 | 163 | replyto_email: "", |
d78cc635 | 164 | subject: "", |
13782061 | 165 | body_html: "", |
657b8692 | 166 | body_text: "" |
8dfd5110 | 167 | }; |
657b8692 | 168 | return angular.extend({}, defaults, params); |
8dfd5110 TO |
169 | }, |
170 | ||
705c61e9 TO |
171 | // @param mailing Object (per APIv3) |
172 | // @return Promise | |
f4f103fa | 173 | 'delete': function (mailing) { |
705c61e9 | 174 | if (mailing.id) { |
36768f7e | 175 | return qApi('Mailing', 'delete', {id: mailing.id}); |
f4f103fa TO |
176 | } |
177 | else { | |
705c61e9 TO |
178 | var d = $q.defer(); |
179 | d.resolve(); | |
180 | return d.promise; | |
181 | } | |
182 | }, | |
183 | ||
21fb26b8 TO |
184 | // Search the body, header, and footer for required tokens. |
185 | // ex: var msgs = findMissingTokens(mailing, 'body_html'); | |
186 | findMissingTokens: function(mailing, field) { | |
187 | var missing = {}; | |
d124a91f | 188 | if (!_.isEmpty(mailing[field]) && !CRM.crmMailing.disableMandatoryTokensCheck) { |
21fb26b8 TO |
189 | var body = ''; |
190 | if (mailing.footer_id) { | |
191 | var footer = _.where(CRM.crmMailing.headerfooterList, {id: mailing.footer_id}); | |
192 | body = body + footer[0][field]; | |
193 | ||
194 | } | |
195 | body = body + mailing[field]; | |
196 | if (mailing.header_id) { | |
197 | var header = _.where(CRM.crmMailing.headerfooterList, {id: mailing.header_id}); | |
198 | body = body + header[0][field]; | |
199 | } | |
200 | ||
201 | angular.forEach(CRM.crmMailing.requiredTokens, function(value, token) { | |
202 | if (!_.isObject(value)) { | |
203 | if (body.indexOf('{' + token + '}') < 0) { | |
204 | missing[token] = value; | |
205 | } | |
206 | } | |
207 | else { | |
208 | var count = 0; | |
209 | angular.forEach(value, function(nestedValue, nestedToken) { | |
210 | if (body.indexOf('{' + nestedToken + '}') >= 0) { | |
211 | count++; | |
212 | } | |
213 | }); | |
d03e0d13 | 214 | if (count === 0) { |
21fb26b8 TO |
215 | angular.extend(missing, value); |
216 | } | |
217 | } | |
218 | }); | |
219 | } | |
220 | return missing; | |
221 | }, | |
222 | ||
07fa6426 TO |
223 | // Copy all data fields in (mailingFrom) to (mailingTgt) -- except for (excludes) |
224 | // ex: crmMailingMgr.mergeInto(newMailing, mailingTemplate, ['subject']); | |
225 | mergeInto: function mergeInto(mailingTgt, mailingFrom, excludes) { | |
226 | var MAILING_FIELDS = [ | |
70980d8e | 227 | // always exclude: 'id' |
07fa6426 TO |
228 | 'name', |
229 | 'campaign_id', | |
230 | 'from_name', | |
231 | 'from_email', | |
232 | 'replyto_email', | |
233 | 'subject', | |
234 | 'dedupe_email', | |
9cd4f489 | 235 | 'recipients', |
07fa6426 TO |
236 | 'body_html', |
237 | 'body_text', | |
238 | 'footer_id', | |
239 | 'header_id', | |
240 | 'visibility', | |
241 | 'url_tracking', | |
242 | 'dedupe_email', | |
243 | 'forward_replies', | |
244 | 'auto_responder', | |
245 | 'open_tracking', | |
246 | 'override_verp', | |
247 | 'optout_id', | |
248 | 'reply_id', | |
249 | 'resubscribe_id', | |
250 | 'unsubscribe_id' | |
251 | ]; | |
252 | if (!excludes) { | |
253 | excludes = []; | |
254 | } | |
255 | _.each(MAILING_FIELDS, function (field) { | |
256 | if (!_.contains(excludes, field)) { | |
257 | mailingTgt[field] = mailingFrom[field]; | |
258 | } | |
f2bad133 | 259 | }); |
07fa6426 TO |
260 | }, |
261 | ||
493eb47a TO |
262 | // @param mailing Object (per APIv3) |
263 | // @return Promise an object with "subject", "body_text", "body_html" | |
264 | preview: function preview(mailing) { | |
360d6097 | 265 | if (CRM.crmMailing.workflowEnabled && !CRM.checkPerm('create mailings') && !CRM.checkPerm('access CiviMail')) { |
36768f7e | 266 | return qApi('Mailing', 'preview', {id: mailing.id}).then(function(result) { |
3efe22a0 TO |
267 | return result.values; |
268 | }); | |
269 | } | |
270 | else { | |
271 | // Protect against races in saving and previewing by chaining create+preview. | |
272 | var params = angular.extend({}, mailing, mailing.recipients, { | |
6dd717a6 | 273 | id: mailing.id, |
3efe22a0 TO |
274 | 'api.Mailing.preview': { |
275 | id: '$value.id' | |
276 | } | |
277 | }); | |
80955669 | 278 | delete params.scheduled_date; |
3efe22a0 | 279 | delete params.recipients; // the content was merged in |
d0d7fea5 | 280 | params._skip_evil_bao_auto_recipients_ = 1; // skip recipient rebuild on mail preview |
36768f7e | 281 | return qApi('Mailing', 'create', params).then(function(result) { |
2fc6d711 | 282 | mailing.modified_date = result.values[result.id].modified_date; |
3efe22a0 TO |
283 | // changes rolled back, so we don't care about updating mailing |
284 | return result.values[result.id]['api.Mailing.preview'].values; | |
285 | }); | |
286 | } | |
493eb47a TO |
287 | }, |
288 | ||
8dfd5110 TO |
289 | // @param mailing Object (per APIv3) |
290 | // @param int previewLimit | |
291 | // @return Promise for a list of recipients (mailing_id, contact_id, api.contact.getvalue, api.email.getvalue) | |
4dd19229 | 292 | previewRecipients: function previewRecipients(mailing, previewLimit) { |
8dfd5110 TO |
293 | // To get list of recipients, we tentatively save the mailing and |
294 | // get the resulting recipients -- then rollback any changes. | |
6dd717a6 | 295 | var params = angular.extend({}, mailing.recipients, { |
296 | id: mailing.id, | |
8dfd5110 TO |
297 | 'api.MailingRecipients.get': { |
298 | mailing_id: '$value.id', | |
58dfba8d | 299 | options: {limit: previewLimit}, |
8dfd5110 TO |
300 | 'api.contact.getvalue': {'return': 'display_name'}, |
301 | 'api.email.getvalue': {'return': 'email'} | |
302 | } | |
303 | }); | |
80955669 | 304 | delete params.scheduled_date; |
9cd4f489 | 305 | delete params.recipients; // the content was merged in |
36768f7e | 306 | return qApi('Mailing', 'create', params).then(function (recipResult) { |
beab9d1b | 307 | // changes rolled back, so we don't care about updating mailing |
2fc6d711 | 308 | mailing.modified_date = recipResult.values[recipResult.id].modified_date; |
8dfd5110 TO |
309 | return recipResult.values[recipResult.id]['api.MailingRecipients.get'].values; |
310 | }); | |
beab9d1b TO |
311 | }, |
312 | ||
7a646a01 | 313 | previewRecipientCount: function previewRecipientCount(mailing, crmMailingCache, rebuild) { |
314 | var cachekey = 'mailing-' + mailing.id + '-recipient-count'; | |
315 | var recipientCount = crmMailingCache.get(cachekey); | |
316 | if (rebuild || _.isEmpty(recipientCount)) { | |
317 | // To get list of recipients, we tentatively save the mailing and | |
318 | // get the resulting recipients -- then rollback any changes. | |
319 | var params = angular.extend({}, mailing, mailing.recipients, { | |
6dd717a6 | 320 | id: mailing.id, |
7a646a01 | 321 | 'api.MailingRecipients.getcount': { |
322 | mailing_id: '$value.id' | |
323 | } | |
324 | }); | |
325 | // if this service is executed on rebuild then also fetch the recipients list | |
326 | if (rebuild) { | |
327 | params = angular.extend(params, { | |
328 | 'api.MailingRecipients.get': { | |
329 | mailing_id: '$value.id', | |
330 | options: {limit: 50}, | |
331 | 'api.contact.getvalue': {'return': 'display_name'}, | |
332 | 'api.email.getvalue': {'return': 'email'} | |
333 | } | |
334 | }); | |
335 | crmMailingCache.put('mailing-' + mailing.id + '-recipient-params', params.recipients); | |
79d21cce | 336 | } |
80955669 | 337 | delete params.scheduled_date; |
7a646a01 | 338 | delete params.recipients; // the content was merged in |
339 | recipientCount = qApi('Mailing', 'create', params).then(function (recipResult) { | |
340 | // changes rolled back, so we don't care about updating mailing | |
341 | mailing.modified_date = recipResult.values[recipResult.id].modified_date; | |
342 | if (rebuild) { | |
343 | crmMailingCache.put('mailing-' + mailing.id + '-recipient-list', recipResult.values[recipResult.id]['api.MailingRecipients.get'].values); | |
344 | } | |
345 | return recipResult.values[recipResult.id]['api.MailingRecipients.getcount']; | |
346 | }); | |
347 | crmMailingCache.put(cachekey, recipientCount); | |
348 | } | |
349 | ||
350 | return recipientCount; | |
79d21cce TO |
351 | }, |
352 | ||
43102e47 | 353 | // Save a (draft) mailing |
705c61e9 TO |
354 | // @param mailing Object (per APIv3) |
355 | // @return Promise | |
d78cc635 | 356 | save: function(mailing) { |
9cd4f489 | 357 | var params = angular.extend({}, mailing, mailing.recipients); |
43102e47 | 358 | |
d78cc635 TO |
359 | // Angular ngModel sometimes treats blank fields as undefined. |
360 | angular.forEach(mailing, function(value, key) { | |
e72949ef | 361 | if (value === undefined || value === null) { |
d78cc635 TO |
362 | mailing[key] = ''; |
363 | } | |
364 | }); | |
365 | ||
43102e47 TO |
366 | // WORKAROUND: Mailing.create (aka CRM_Mailing_BAO_Mailing::create()) interprets scheduled_date |
367 | // as an *intent* to schedule and creates tertiary records. Saving a draft with a scheduled_date | |
368 | // is therefore not allowed. Remove this after fixing Mailing.create's contract. | |
369 | delete params.scheduled_date; | |
370 | ||
86c3a327 TO |
371 | delete params.jobs; |
372 | ||
9cd4f489 | 373 | delete params.recipients; // the content was merged in |
d0d7fea5 | 374 | params._skip_evil_bao_auto_recipients_ = 1; // skip recipient rebuild on simple save |
36768f7e | 375 | return qApi('Mailing', 'create', params).then(function(result) { |
f4f103fa TO |
376 | if (result.id && !mailing.id) { |
377 | mailing.id = result.id; | |
378 | } // no rollback, so update mailing.id | |
43102e47 | 379 | // Perhaps we should reload mailing based on result? |
2fc6d711 | 380 | mailing.modified_date = result.values[result.id].modified_date; |
86c3a327 | 381 | return mailing; |
705c61e9 TO |
382 | }); |
383 | }, | |
384 | ||
385 | // Schedule/send the mailing | |
386 | // @param mailing Object (per APIv3) | |
387 | // @return Promise | |
43102e47 | 388 | submit: function (mailing) { |
86c3a327 | 389 | var crmMailingMgr = this; |
6818346a TO |
390 | var params = { |
391 | id: mailing.id, | |
d3312ed5 | 392 | approval_date: 'now', |
393 | scheduled_date: mailing.scheduled_date ? mailing.scheduled_date : 'now' | |
43102e47 | 394 | }; |
36768f7e | 395 | return qApi('Mailing', 'submit', params) |
86c3a327 TO |
396 | .then(function (result) { |
397 | angular.extend(mailing, result.values[result.id]); // Perhaps we should reload mailing based on result? | |
398 | return crmMailingMgr._loadJobs(mailing); | |
399 | }) | |
400 | .then(function () { | |
401 | return mailing; | |
402 | }); | |
705c61e9 TO |
403 | }, |
404 | ||
405 | // Immediately send a test message | |
beab9d1b | 406 | // @param mailing Object (per APIv3) |
58dfba8d | 407 | // @param to Object with either key "email" (string) or "gid" (int) |
beab9d1b | 408 | // @return Promise for a list of delivery reports |
58dfba8d | 409 | sendTest: function (mailing, recipient) { |
9cd4f489 | 410 | var params = angular.extend({}, mailing, mailing.recipients, { |
43102e47 | 411 | // options: {force_rollback: 1}, // Test mailings include tracking features, so the mailing must be persistent |
beab9d1b TO |
412 | 'api.Mailing.send_test': { |
413 | mailing_id: '$value.id', | |
58dfba8d TO |
414 | test_email: recipient.email, |
415 | test_group: recipient.gid | |
beab9d1b TO |
416 | } |
417 | }); | |
43102e47 TO |
418 | |
419 | // WORKAROUND: Mailing.create (aka CRM_Mailing_BAO_Mailing::create()) interprets scheduled_date | |
420 | // as an *intent* to schedule and creates tertiary records. Saving a draft with a scheduled_date | |
421 | // is therefore not allowed. Remove this after fixing Mailing.create's contract. | |
422 | delete params.scheduled_date; | |
423 | ||
86c3a327 TO |
424 | delete params.jobs; |
425 | ||
9cd4f489 TO |
426 | delete params.recipients; // the content was merged in |
427 | ||
d0d7fea5 | 428 | params._skip_evil_bao_auto_recipients_ = 1; // skip recipient rebuild while sending test mail |
429 | ||
36768f7e | 430 | return qApi('Mailing', 'create', params).then(function (result) { |
f4f103fa TO |
431 | if (result.id && !mailing.id) { |
432 | mailing.id = result.id; | |
433 | } // no rollback, so update mailing.id | |
2fc6d711 | 434 | mailing.modified_date = result.values[result.id].modified_date; |
beab9d1b TO |
435 | return result.values[result.id]['api.Mailing.send_test'].values; |
436 | }); | |
8dfd5110 TO |
437 | } |
438 | }; | |
439 | }); | |
58dfba8d TO |
440 | |
441 | // The preview manager performs preview actions while putting up a visible UI (e.g. dialogs & status alerts) | |
442 | angular.module('crmMailing').factory('crmMailingPreviewMgr', function (dialogService, crmMailingMgr, crmStatus) { | |
443 | return { | |
444 | // @param mode string one of 'html', 'text', or 'full' | |
445 | // @return Promise | |
446 | preview: function preview(mailing, mode) { | |
447 | var templates = { | |
fd9c35ce TO |
448 | html: '~/crmMailing/PreviewMgr/html.html', |
449 | text: '~/crmMailing/PreviewMgr/text.html', | |
450 | full: '~/crmMailing/PreviewMgr/full.html' | |
58dfba8d TO |
451 | }; |
452 | var result = null; | |
453 | var p = crmMailingMgr | |
454 | .preview(mailing) | |
455 | .then(function (content) { | |
b1fc510d | 456 | var options = CRM.utils.adjustDialogDefaults({ |
58dfba8d | 457 | autoOpen: false, |
58dfba8d TO |
458 | title: ts('Subject: %1', { |
459 | 1: content.subject | |
460 | }) | |
b1fc510d | 461 | }); |
58dfba8d TO |
462 | result = dialogService.open('previewDialog', templates[mode], content, options); |
463 | }); | |
46233df6 | 464 | crmStatus({start: ts('Previewing...'), success: ''}, p); |
58dfba8d TO |
465 | return result; |
466 | }, | |
467 | ||
468 | // @param to Object with either key "email" (string) or "gid" (int) | |
469 | // @return Promise | |
470 | sendTest: function sendTest(mailing, recipient) { | |
471 | var promise = crmMailingMgr.sendTest(mailing, recipient) | |
472 | .then(function (deliveryInfos) { | |
473 | var count = Object.keys(deliveryInfos).length; | |
474 | if (count === 0) { | |
475 | CRM.alert(ts('Could not identify any recipients. Perhaps the group is empty?')); | |
476 | } | |
477 | }) | |
478 | ; | |
479 | return crmStatus({start: ts('Sending...'), success: ts('Sent')}, promise); | |
480 | } | |
481 | }; | |
482 | }); | |
483 | ||
7173e315 TO |
484 | angular.module('crmMailing').factory('crmMailingStats', function (crmApi, crmLegacy) { |
485 | var statTypes = [ | |
78414681 SL |
486 | // {name: 'Recipients', title: ts('Intended Recipients'), searchFilter: '', eventsFilter: '&event=queue', reportType: 'detail', reportFilter: ''}, |
487 | {name: 'Delivered', title: ts('Successful Deliveries'), searchFilter: '&mailing_delivery_status=Y', eventsFilter: '&event=delivered', reportType: 'detail', reportFilter: '&delivery_status_value=successful'}, | |
488 | {name: 'Opened', title: ts('Tracked Opens'), searchFilter: '&mailing_open_status=Y', eventsFilter: '&event=opened', reportType: 'opened', reportFilter: ''}, | |
489 | {name: 'Unique Clicks', title: ts('Click-throughs'), searchFilter: '&mailing_click_status=Y', eventsFilter: '&event=click&distinct=1', reportType: 'clicks', reportFilter: ''}, | |
490 | // {name: 'Forward', title: ts('Forwards'), searchFilter: '&mailing_forward=1', eventsFilter: '&event=forward', reportType: 'detail', reportFilter: '&is_forwarded_value=1'}, | |
491 | // {name: 'Replies', title: ts('Replies'), searchFilter: '&mailing_reply_status=Y', eventsFilter: '&event=reply', reportType: 'detail', reportFilter: '&is_replied_value=1'}, | |
492 | {name: 'Bounces', title: ts('Bounces'), searchFilter: '&mailing_delivery_status=N', eventsFilter: '&event=bounce', reportType: 'bounce', reportFilter: ''}, | |
493 | {name: 'Unsubscribers', title: ts('Unsubscribes'), searchFilter: '&mailing_unsubscribe=1', eventsFilter: '&event=unsubscribe', reportType: 'detail', reportFilter: '&is_unsubscribed_value=1'}, | |
494 | // {name: 'OptOuts', title: ts('Opt-Outs'), searchFilter: '&mailing_optout=1', eventsFilter: '&event=optout', reportType: 'detail', reportFilter: ''} | |
7173e315 TO |
495 | ]; |
496 | ||
497 | return { | |
498 | getStatTypes: function() { | |
499 | return statTypes; | |
500 | }, | |
501 | ||
502 | /** | |
503 | * @param mailingIds object | |
504 | * List of mailing IDs ({a: 123, b: 456}) | |
505 | * @return Promise | |
506 | * List of stats for each mailing | |
507 | * ({a: ...object..., b: ...object...}) | |
508 | */ | |
509 | getStats: function(mailingIds) { | |
510 | var params = {}; | |
511 | angular.forEach(mailingIds, function(mailingId, name) { | |
b4486604 | 512 | params[name] = ['Mailing', 'stats', {mailing_id: mailingId, is_distinct: 0}]; |
7173e315 TO |
513 | }); |
514 | return crmApi(params).then(function(result) { | |
515 | var stats = {}; | |
516 | angular.forEach(mailingIds, function(mailingId, name) { | |
517 | stats[name] = result[name].values[mailingId]; | |
518 | }); | |
519 | return stats; | |
520 | }); | |
521 | }, | |
522 | ||
523 | /** | |
524 | * Determine the legacy URL for a report about a given mailing and stat. | |
525 | * | |
526 | * @param mailing object | |
527 | * @param statType object (see statTypes above) | |
528 | * @param view string ('search', 'event', 'report') | |
05381a97 | 529 | * @param returnPath string|null Return path (relative to Angular base) |
7173e315 TO |
530 | * @return string|null |
531 | */ | |
05381a97 | 532 | getUrl: function getUrl(mailing, statType, view, returnPath) { |
7173e315 TO |
533 | switch (view) { |
534 | case 'events': | |
05381a97 | 535 | var retParams = returnPath ? '&context=angPage&angPage=' + returnPath : ''; |
7173e315 | 536 | return crmLegacy.url('civicrm/mailing/report/event', |
05381a97 | 537 | 'reset=1&mid=' + mailing.id + statType.eventsFilter + retParams); |
7173e315 TO |
538 | case 'search': |
539 | return crmLegacy.url('civicrm/contact/search/advanced', | |
540 | 'force=1&mailing_id=' + mailing.id + statType.searchFilter); | |
78414681 | 541 | case 'report': |
b4fdf77a | 542 | var reportIds = CRM.crmMailing.reportIds; |
78414681 SL |
543 | return crmLegacy.url('civicrm/report/instance/' + reportIds[statType.reportType], |
544 | 'reset=1&mailing_id_value=' + mailing.id + statType.reportFilter); | |
7173e315 TO |
545 | default: |
546 | return null; | |
547 | } | |
548 | } | |
549 | }; | |
550 | }); | |
551 | ||
e908a393 TO |
552 | // crmMailingSimpleDirective is a template/factory-function for constructing very basic |
553 | // directives that accept a "mailing" argument. Please don't overload it. If one continues building | |
554 | // this, it risks becoming a second system that violates Angular architecture (and prevents one | |
555 | // from using standard Angular docs+plugins). So this really shouldn't do much -- it is really | |
556 | // only for simple directives. For something complex, suck it up and write 10 lines of boilerplate. | |
557 | angular.module('crmMailing').factory('crmMailingSimpleDirective', function ($q, crmMetadata, crmUiHelp) { | |
558 | return function crmMailingSimpleDirective(directiveName, templateUrl) { | |
559 | return { | |
560 | scope: { | |
561 | crmMailing: '@' | |
562 | }, | |
563 | templateUrl: templateUrl, | |
564 | link: function (scope, elm, attr) { | |
565 | scope.$parent.$watch(attr.crmMailing, function(newValue){ | |
566 | scope.mailing = newValue; | |
567 | }); | |
568 | scope.crmMailingConst = CRM.crmMailing; | |
569 | scope.ts = CRM.ts(null); | |
570 | scope.hs = crmUiHelp({file: 'CRM/Mailing/MailingUI'}); | |
571 | scope[directiveName] = attr[directiveName] ? scope.$parent.$eval(attr[directiveName]) : {}; | |
572 | $q.when(crmMetadata.getFields('Mailing'), function(fields) { | |
573 | scope.mailingFields = fields; | |
574 | }); | |
575 | } | |
576 | }; | |
577 | }; | |
578 | }); | |
579 | ||
7a646a01 | 580 | angular.module('crmMailing').factory('crmMailingCache', ['$cacheFactory', function($cacheFactory) { |
581 | return $cacheFactory('crmMailingCache'); | |
582 | }]); | |
583 | ||
8dfd5110 | 584 | })(angular, CRM.$, CRM._); |