Merge pull request #17283 from agh1/stop-icon-png
[civicrm-core.git] / ang / crmMailing / ViewRecipCtrl.js
CommitLineData
6b8bd380
TO
1(function(angular, $, _) {
2
7b05fbd8 3 angular.module('crmMailing').controller('ViewRecipCtrl', function ViewRecipCtrl($scope) {
58008837
SL
4 var mids = [];
5 var gids = [];
6 var groupNames = [];
7 var mailings = [];
8 var civimailings = [];
9 var civimails = [];
10
11 function getGroupNames(mailing) {
12 if (-1 == mailings.indexOf(mailing.id)) {
13 mailings.push(mailing.id);
14 _.each(mailing.recipients.groups.include, function(id) {
15 if (-1 == gids.indexOf(id)) {
16 gids.push(id);
17 }
18 });
19 _.each(mailing.recipients.groups.exclude, function(id) {
20 if (-1 == gids.indexOf(id)) {
21 gids.push(id);
22 }
23 });
24 _.each(mailing.recipients.groups.base, function(id) {
25 if (-1 == gids.indexOf(id)) {
26 gids.push(id);
27 }
28 });
29 if (!_.isEmpty(gids)) {
30 CRM.api3('Group', 'get', {'id': {"IN": gids}}).then(function(result) {
31 _.each(result.values, function(grp) {
32 if (_.isEmpty(_.where(groupNames, {id: parseInt(grp.id)}))) {
33 groupNames.push({id: parseInt(grp.id), title: grp.title, is_hidden: grp.is_hidden});
34 }
35 });
36 CRM.crmMailing.groupNames = groupNames;
37 $scope.$parent.crmMailingConst.groupNames = groupNames;
38 });
39 }
40 }
41 }
42
43 function getCiviMails(mailing) {
44 if (-1 == civimailings.indexOf(mailing.id)) {
45 civimailings.push(mailing.id);
46 _.each(mailing.recipients.mailings.include, function(id) {
47 if (-1 == mids.indexOf(id)) {
48 mids.push(id);
49 }
50 });
51 _.each(mailing.recipients.mailings.exclude, function(id) {
52 if (-1 == mids.indexOf(id)) {
53 mids.push(id);
54 }
55 });
56 if (!_.isEmpty(mids)) {
57 CRM.api3('Mailing', 'get', {'id': {"IN": mids}}).then(function(result) {
58 _.each(result.values, function(mail) {
59 if (_.isEmpty(_.where(civimails, {id: parseInt(mail.id)}))) {
60 civimails.push({id: parseInt(mail.id), name: mail.label});
61 }
62 });
63 CRM.crmMailing.civiMails = civimails;
64 $scope.$parent.crmMailingConst.civiMails = civimails;
65 });
66 }
67 }
68 }
69
6b8bd380
TO
70 $scope.getIncludesAsString = function(mailing) {
71 var first = true;
72 var names = '';
58008837
SL
73 if (_.isEmpty(CRM.crmMailing.groupNames)) {
74 getGroupNames(mailing);
75 }
76 if (_.isEmpty(CRM.crmMailing.civiMails)) {
77 getCiviMails(mailing);
78 }
6b8bd380 79 _.each(mailing.recipients.groups.include, function(id) {
58008837 80 var group = _.where(CRM.crmMailing.groupNames, {id: parseInt(id)});
836bf6b7
SL
81 if (group.length) {
82 if (!first) {
83 names = names + ', ';
84 }
85 names = names + group[0].title;
86 first = false;
87 }
6b8bd380
TO
88 });
89 _.each(mailing.recipients.mailings.include, function(id) {
58008837 90 var oldMailing = _.where(CRM.crmMailing.civiMails, {id: parseInt(id)});
836bf6b7
SL
91 if (oldMailing.length) {
92 if (!first) {
93 names = names + ', ';
94 }
95 names = names + oldMailing[0].name;
96 first = false;
97 }
6b8bd380
TO
98 });
99 return names;
100 };
101 $scope.getExcludesAsString = function(mailing) {
102 var first = true;
103 var names = '';
104 _.each(mailing.recipients.groups.exclude, function(id) {
58008837 105 var group = _.where(CRM.crmMailing.groupNames, {id: parseInt(id)});
836bf6b7
SL
106 if (group.length) {
107 if (!first) {
108 names = names + ', ';
109 }
110 names = names + group[0].title;
111 first = false;
112 }
6b8bd380
TO
113 });
114 _.each(mailing.recipients.mailings.exclude, function(id) {
58008837 115 var oldMailing = _.where(CRM.crmMailing.civiMails, {id: parseInt(id)});
836bf6b7
SL
116 if (oldMailing.length) {
117 if (!first) {
118 names = names + ', ';
119 }
120 names = names + oldMailing[0].name;
121 first = false;
122 }
6b8bd380
TO
123 });
124 return names;
125 };
126 });
127
128})(angular, CRM.$, CRM._);