Merge pull request #17614 from demeritcowboy/xmlproc-acttypes-more
[civicrm-core.git] / ang / crmMailing / BlockPreview.js
CommitLineData
b396fc59
TO
1(function(angular, $, _) {
2 // example: <div crm-mailing-block-preview crm-mailing="myMailing" on-preview="openPreview(myMailing, preview.mode)" on-send="sendEmail(myMailing,preview.recipient)">
3 // note: the directive defines a variable called "preview" with any inputs supplied by the user (e.g. the target recipient for an example mailing)
4
5 angular.module('crmMailing').directive('crmMailingBlockPreview', function(crmUiHelp) {
6 return {
7 templateUrl: '~/crmMailing/BlockPreview.html',
8 link: function(scope, elm, attr) {
9 scope.$watch(attr.crmMailing, function(newValue) {
10 scope.mailing = newValue;
11 });
12 scope.crmMailingConst = CRM.crmMailing;
13 scope.ts = CRM.ts(null);
14 scope.hs = crmUiHelp({file: 'CRM/Mailing/MailingUI'});
15 scope.testContact = {email: CRM.crmMailing.defaultTestEmail};
16 scope.testGroup = {gid: null};
17
18 scope.doPreview = function(mode) {
19 scope.$eval(attr.onPreview, {
20 preview: {mode: mode}
21 });
22 };
23 scope.doSend = function doSend(recipient) {
93b33e81 24 recipient = JSON.parse(JSON.stringify(recipient).replace(/\,\s/g, ','));
b396fc59
TO
25 scope.$eval(attr.onSend, {
26 preview: {recipient: recipient}
27 });
28 };
29
30 scope.previewTestGroup = function(e) {
31 var $dialog = $(this);
32 $dialog.html('<div class="crm-loading-element"></div>').parent().find('button[data-op=yes]').prop('disabled', true);
786d2085
CW
33 CRM.api3({
34 contact: ['contact', 'get', {group: scope.testGroup.gid, options: {limit: 0}, return: 'display_name,email'}],
35 group: ['group', 'getsingle', {id: scope.testGroup.gid, return: 'title'}]
36 }).done(function(data) {
37 $dialog.dialog('option', 'title', ts('Send to %1', {1: data.group.title}));
38 var count = 0,
39 // Fixme: should this be in a template?
40 markup = '<ol>';
41 _.each(data.contact.values, function(row) {
42 // Fixme: contact api doesn't seem capable of filtering out contacts with no email, so we're doing it client-side
43 if (row.email) {
44 count++;
45 markup += '<li>' + row.display_name + ' - ' + row.email + '</li>';
b396fc59
TO
46 }
47 });
786d2085
CW
48 markup += '</ol>';
49 markup = '<h4>' + ts('A test message will be sent to %1 people:', {1: count}) + '</h4>' + markup;
50 if (!count) {
13a3d214 51 markup = '<div class="messages status"><i class="crm-i fa-exclamation-triangle" aria-hidden="true"></i> ' +
786d2085
CW
52 (data.contact.count ? ts('None of the contacts in this group have an email address.') : ts('Group is empty.')) +
53 '</div>';
54 }
55 $dialog
56 .html(markup)
57 .trigger('crmLoad')
58 .parent().find('button[data-op=yes]').prop('disabled', !count);
b396fc59
TO
59 });
60 };
61 }
62 };
63 });
64
65})(angular, CRM.$, CRM._);