| 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) { |
| 24 | scope.$eval(attr.onSend, { |
| 25 | preview: {recipient: recipient} |
| 26 | }); |
| 27 | }; |
| 28 | |
| 29 | scope.previewTestGroup = function(e) { |
| 30 | var $dialog = $(this); |
| 31 | $dialog.html('<div class="crm-loading-element"></div>').parent().find('button[data-op=yes]').prop('disabled', true); |
| 32 | $dialog.dialog('option', 'title', ts('Send to %1', {1: _.pluck(_.where(scope.crmMailingConst.groupNames, {id: scope.testGroup.gid}), 'title')[0]})); |
| 33 | CRM.api3('contact', 'get', { |
| 34 | group: scope.testGroup.gid, |
| 35 | options: {limit: 0}, |
| 36 | return: 'display_name,email' |
| 37 | }).done(function(data) { |
| 38 | var count = 0, |
| 39 | // Fixme: should this be in a template? |
| 40 | markup = '<ol>'; |
| 41 | _.each(data.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>'; |
| 46 | } |
| 47 | }); |
| 48 | markup += '</ol>'; |
| 49 | markup = '<h4>' + ts('A test message will be sent to %1 people:', {1: count}) + '</h4>' + markup; |
| 50 | if (!count) { |
| 51 | markup = '<div class="messages status"><div class="icon ui-icon-alert"></div> ' + |
| 52 | (data.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); |
| 59 | }); |
| 60 | }; |
| 61 | } |
| 62 | }; |
| 63 | }); |
| 64 | |
| 65 | })(angular, CRM.$, CRM._); |