CRM-15578 - crmMailing2 - Extract crmMailingMgr service
[civicrm-core.git] / js / angular-crmMailing2-services.js
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
8 crmMailing2.factory('crmMailingMgr', function($q, crmApi) {
9 var pickDefaultMailComponent = function(type) {
10 var mcs = _.where(CRM.crmMailing.headerfooterList, {
11 component_type:type,
12 is_default: "1"
13 });
14 return (mcs.length >= 1) ? mcs[0].id : null;
15 };
16
17 return {
18 // @param scalar idExpr a number or the literal string 'new'
19 // @return Promise|Object Mailing (per APIv3)
20 getOrCreate: function (idExpr) {
21 return (idExpr == 'new') ? this.create() : this.get(idExpr);
22 },
23 // @return Promise Mailing (per APIv3)
24 get: function (id) {
25 return crmApi('Mailing', 'getsingle', {id: id}).then(function(mailing){
26 return crmApi('MailingGroup', 'get', {mailing_id: id}).then(function(groupResult){
27 mailing.groups = {include: [], exclude: []};
28 mailing.mailings = {include: [], exclude: []};
29 _.each(groupResult.values, function(mailingGroup) {
30 var bucket = (mailingGroup.entity_table == 'civicrm_group') ? 'groups' : 'mailings';
31 mailing[bucket][mailingGroup.group_type].push(mailingGroup.entity_id);
32 });
33 return mailing;
34 });
35 });
36 },
37 // @return Object Mailing (per APIv3)
38 create: function () {
39 return {
40 name: "revert this", // fixme
41 campaign_id: null,
42 from: _.where(CRM.crmMailing.fromAddress, {is_default: "1"})[0].label,
43 replyto_email: "",
44 subject: "For {contact.display_name}", // fixme
45 dedupe_email: "1",
46 groups: {include: [2], exclude: [4]}, // fixme
47 mailings: {include: [], exclude: []},
48 body_html: "<b>Hello</b> {contact.display_name}", // fixme
49 body_text: "Hello {contact.display_name}", // fixme
50 footer_id: null, // pickDefaultMailComponent('Footer'),
51 header_id: null, // pickDefaultMailComponent('Header'),
52 visibility: "Public Pages",
53 url_tracking: "1",
54 dedupe_email: "1",
55 forward_replies: "0",
56 auto_responder: "0",
57 open_tracking: "1",
58 override_verp: "1",
59 optout_id: pickDefaultMailComponent('OptOut'),
60 reply_id: pickDefaultMailComponent('Reply'),
61 resubscribe_id: pickDefaultMailComponent('Resubscribe'),
62 unsubscribe_id: pickDefaultMailComponent('Unsubscribe')
63 };
64 },
65
66 // @param mailing Object (per APIv3)
67 // @param int previewLimit
68 // @return Promise for a list of recipients (mailing_id, contact_id, api.contact.getvalue, api.email.getvalue)
69 previewRecipients: function(mailing, previewLimit) {
70 // To get list of recipients, we tentatively save the mailing and
71 // get the resulting recipients -- then rollback any changes.
72 var params = _.extend({}, mailing, {
73 options: {force_rollback: 1},
74 'api.MailingRecipients.get': {
75 mailing_id: '$value.id',
76 options: { limit: previewLimit },
77 'api.contact.getvalue': {'return': 'display_name'},
78 'api.email.getvalue': {'return': 'email'}
79 }
80 });
81 return crmApi('Mailing', 'create', params).then(function(recipResult){
82 return recipResult.values[recipResult.id]['api.MailingRecipients.get'].values;
83 });
84 }
85 };
86 });
87
88 })(angular, CRM.$, CRM._);