CRM-16142 - Variable scope fixes
[civicrm-core.git] / js / angular-crmMailing.js
CommitLineData
030dce01 1(function (angular, $, _) {
030dce01 2
88e9e883 3 angular.module('crmMailing', [
8443385d 4 'crmUtil', 'crmAttachment', 'crmAutosave', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService'
882e1558 5 ]);
030dce01 6
7801d9b5
TO
7 // Time to wait before triggering AJAX update to recipients list
8 var RECIPIENTS_DEBOUNCE_MS = 100;
b73e0c53 9 var RECIPIENTS_PREVIEW_LIMIT = 10000;
7801d9b5 10
c0e89319
TO
11 var APPROVAL_STATUSES = {'Approved': 1, 'Rejected': 2, 'None': 3};
12
88e9e883 13 angular.module('crmMailing').config([
f4f103fa 14 '$routeProvider',
030dce01 15 function ($routeProvider) {
96ac27bd 16 $routeProvider.when('/mailing', {
030dce01
TO
17 template: '<div></div>',
18 controller: 'ListMailingsCtrl'
19 });
b7c3beb2
TO
20
21 var editorPaths = {
ef5d18a1
TO
22 '': '~/crmMailing/edit.html',
23 '/unified': '~/crmMailing/edit-unified.html',
24 '/unified2': '~/crmMailing/edit-unified2.html',
25 '/wizard': '~/crmMailing/edit-wizard.html'
b7c3beb2
TO
26 };
27 angular.forEach(editorPaths, function(editTemplate, pathSuffix) {
c0e89319
TO
28 if (CRM && CRM.crmMailing && CRM.crmMailing.workflowEnabled) {
29 editTemplate = '~/crmMailing/edit-workflow.html'; // override
30 }
b7c3beb2
TO
31 $routeProvider.when('/mailing/new' + pathSuffix, {
32 template: '<p>' + ts('Initializing...') + '</p>',
33 controller: 'CreateMailingCtrl',
34 resolve: {
35 selectedMail: function(crmMailingMgr) {
36 var m = crmMailingMgr.create();
37 return crmMailingMgr.save(m);
38 }
f4f103fa 39 }
b7c3beb2
TO
40 });
41 $routeProvider.when('/mailing/:id' + pathSuffix, {
42 templateUrl: editTemplate,
43 controller: 'EditMailingCtrl',
44 resolve: {
45 selectedMail: function($route, crmMailingMgr) {
46 return crmMailingMgr.get($route.current.params.id);
f2cdd789
TO
47 },
48 attachments: function($route, CrmAttachments) {
49 var attachments = new CrmAttachments(function () {
50 return {entity_table: 'civicrm_mailing', entity_id: $route.current.params.id};
51 });
52 return attachments.load();
b7c3beb2 53 }
f4f103fa 54 }
b7c3beb2 55 });
030dce01
TO
56 });
57 }
58 ]);
59
416abe87 60 angular.module('crmMailing').controller('ListMailingsCtrl', ['crmLegacy', 'crmNavigator', function ListMailingsCtrl(crmLegacy, crmNavigator) {
030dce01
TO
61 // We haven't implemented this in Angular, but some users may get clever
62 // about typing URLs, so we'll provide a redirect.
416abe87
PH
63 var new_url = crmLegacy.url('civicrm/mailing/browse/unscheduled', {reset: 1, scheduled: 'false'});
64 crmNavigator.redirect(new_url);
65 }]);
030dce01 66
b7c3beb2
TO
67 angular.module('crmMailing').controller('CreateMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location) {
68 // Transition URL "/mailing/new/foo" => "/mailing/123/foo"
69 var parts = $location.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard"
70 parts[2] = selectedMail.id;
71 $location.path(parts.join('/'));
72 $location.replace();
73 });
74
e08a61f9 75 angular.module('crmMailing').controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmStatus, attachments, crmMailingPreviewMgr, crmBlocker, CrmAutosaveCtrl, $timeout) {
27e690c2 76 $scope.mailing = selectedMail;
f2cdd789 77 $scope.attachments = attachments;
27e690c2 78 $scope.crmMailingConst = CRM.crmMailing;
c0e89319 79 $scope.checkPerm = CRM.checkPerm;
27e690c2 80
5d8901af 81 var ts = $scope.ts = CRM.ts(null);
bdd3f781 82 var block = $scope.block = crmBlocker();
c0e89319 83 var myAutosave = null;
27e690c2 84
86c3a327
TO
85 $scope.isSubmitted = function isSubmitted() {
86 return _.size($scope.mailing.jobs) > 0;
87 };
88
c0e89319
TO
89 // usage: approve('Approved')
90 $scope.approve = function approve(status, options) {
91 $scope.mailing.approval_status_id = APPROVAL_STATUSES[status];
92 return myAutosave.suspend($scope.submit(options));
93 };
94
58dfba8d
TO
95 // @return Promise
96 $scope.previewMailing = function previewMailing(mailing, mode) {
97 return crmMailingPreviewMgr.preview(mailing, mode);
98 };
99
100 // @return Promise
101 $scope.sendTest = function sendTest(mailing, attachments, recipient) {
102 var savePromise = crmMailingMgr.save(mailing)
103 .then(function () {
104 return attachments.save();
b7c3beb2 105 });
bdd3f781 106 return block(crmStatus({start: ts('Saving...'), success: ''}, savePromise)
58dfba8d
TO
107 .then(function () {
108 crmMailingPreviewMgr.sendTest(mailing, recipient);
bdd3f781 109 }));
58dfba8d
TO
110 };
111
705c61e9 112 // @return Promise
c0e89319
TO
113 $scope.submit = function submit(options) {
114 options = options || {};
c9ae63bb
TO
115 if (block.check() || $scope.crmMailing.$invalid) {
116 return;
117 }
118
db083bf0 119 var promise = crmMailingMgr.save($scope.mailing)
86c3a327
TO
120 .then(function () {
121 // pre-condition: the mailing exists *before* saving attachments to it
122 return $scope.attachments.save();
123 })
124 .then(function () {
125 return crmMailingMgr.submit($scope.mailing);
126 })
127 .then(function () {
c0e89319
TO
128 if (!options.stay) {
129 $scope.leave('scheduled');
130 }
86c3a327
TO
131 })
132 ;
bdd3f781 133 return block(crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, promise));
2d36e6bc 134 };
58dfba8d 135
705c61e9
TO
136 // @return Promise
137 $scope.save = function save() {
bdd3f781 138 return block(crmStatus(null,
db083bf0
TO
139 crmMailingMgr
140 .save($scope.mailing)
141 .then(function () {
142 // pre-condition: the mailing exists *before* saving attachments to it
143 return $scope.attachments.save();
144 })
bdd3f781 145 ));
2d36e6bc 146 };
58dfba8d 147
705c61e9
TO
148 // @return Promise
149 $scope.delete = function cancel() {
bdd3f781 150 return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')},
f286acec 151 crmMailingMgr.delete($scope.mailing)
b0797ac3 152 .then(function () {
650a6ffc 153 $scope.leave('unscheduled');
b0797ac3 154 })
bdd3f781 155 ));
2d36e6bc 156 };
58dfba8d 157
b0797ac3 158 // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
650a6ffc 159 $scope.leave = function leave(listingScreen) {
b0797ac3
TO
160 switch (listingScreen) {
161 case 'archive':
162 window.location = CRM.url('civicrm/mailing/browse/archived', {
163 reset: 1
164 });
165 break;
166 case 'scheduled':
167 window.location = CRM.url('civicrm/mailing/browse/scheduled', {
168 reset: 1,
169 scheduled: 'true'
170 });
171 break;
172 case 'unscheduled':
8acf0123 173 /* falls through */
b0797ac3
TO
174 default:
175 window.location = CRM.url('civicrm/mailing/browse/unscheduled', {
176 reset: 1,
177 scheduled: 'false'
178 });
179 }
650a6ffc 180 };
e08a61f9 181
c0e89319 182 myAutosave = new CrmAutosaveCtrl({
e08a61f9
TO
183 save: $scope.save,
184 saveIf: function() {
185 return true;
186 },
187 model: function() {
188 return [$scope.mailing, $scope.attachments.getAutosaveSignature()];
189 },
190 form: function() {
191 return $scope.crmMailing;
192 }
193 });
194 $timeout(myAutosave.start);
195 $scope.$on('$destroy', myAutosave.stop);
030dce01
TO
196 });
197
deea897f
TO
198 angular.module('crmMailing').controller('ViewRecipCtrl', function EditRecipCtrl($scope) {
199 $scope.getIncludesAsString = function(mailing) {
47bacc20
TO
200 var first = true;
201 var names = '';
deea897f 202 _.each(mailing.recipients.groups.include, function (id) {
f4f103fa
TO
203 if (!first) {
204 names = names + ', ';
205 }
206 var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
47bacc20
TO
207 names = names + group[0].title;
208 first = false;
209 });
deea897f 210 _.each(mailing.recipients.mailings.include, function (id) {
f4f103fa
TO
211 if (!first) {
212 names = names + ', ';
213 }
214 var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
47bacc20
TO
215 names = names + oldMailing[0].name;
216 first = false;
217 });
218 return names;
219 };
deea897f 220 $scope.getExcludesAsString = function (mailing) {
47bacc20
TO
221 var first = true;
222 var names = '';
deea897f 223 _.each(mailing.recipients.groups.exclude, function (id) {
f4f103fa
TO
224 if (!first) {
225 names = names + ', ';
226 }
227 var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
47bacc20
TO
228 names = names + group[0].title;
229 first = false;
230 });
deea897f 231 _.each(mailing.recipients.mailings.exclude, function (id) {
f4f103fa
TO
232 if (!first) {
233 names = names + ', ';
234 }
235 var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
47bacc20
TO
236 names = names + oldMailing[0].name;
237 first = false;
238 });
239 return names;
240 };
deea897f
TO
241 });
242
243 // Controller for the edit-recipients fields (
244 // WISHLIST: Move most of this to a (cache-enabled) service
245 // Scope members:
246 // - [input] mailing: object
247 // - [output] recipients: array of recipient records
248 angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata) {
249 var ts = $scope.ts = CRM.ts(null);
5d544a77
TO
250
251 $scope.isMailingList = function isMailingList(group) {
252 var GROUP_TYPE_MAILING_LIST = '2';
253 return _.contains(group.group_type, GROUP_TYPE_MAILING_LIST);
254 };
255
deea897f
TO
256 $scope.recipients = null;
257 $scope.getRecipientsEstimate = function () {
258 var ts = $scope.ts;
259 if ($scope.recipients === null) {
260 return ts('(Estimating)');
261 }
262 if ($scope.recipients.length === 0) {
263 return ts('No recipients');
264 }
265 if ($scope.recipients.length === 1) {
266 return ts('~1 recipient');
267 }
268 if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT) {
269 return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT});
270 }
271 return ts('~%1 recipients', {1: $scope.recipients.length});
272 };
47bacc20 273
7801d9b5
TO
274 // We monitor four fields -- use debounce so that changes across the
275 // four fields can settle-down before AJAX.
276 var refreshRecipients = _.debounce(function () {
277 $scope.$apply(function () {
278 $scope.recipients = null;
c0e89319 279 if (!$scope.mailing) return;
8dfd5110
TO
280 crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function (recipients) {
281 $scope.recipients = recipients;
b73e0c53 282 });
7801d9b5
TO
283 });
284 }, RECIPIENTS_DEBOUNCE_MS);
9cd4f489
TO
285 $scope.$watchCollection("mailing.recipients.groups.include", refreshRecipients);
286 $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients);
287 $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients);
288 $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients);
7801d9b5 289
4dd19229 290 $scope.previewRecipients = function previewRecipients() {
7801d9b5
TO
291 var model = {
292 recipients: $scope.recipients
293 };
b1fc510d 294 var options = CRM.utils.adjustDialogDefaults({
7801d9b5 295 autoOpen: false,
7801d9b5
TO
296 title: ts('Preview (%1)', {
297 1: $scope.getRecipientsEstimate()
52f515c6 298 })
b1fc510d 299 });
ef5d18a1 300 dialogService.open('recipDialog', '~/crmMailing/dialog/recipients.html', model, options);
7801d9b5 301 };
b8b2e316
TO
302
303 // Open a dialog for editing the advanced recipient options.
304 $scope.editOptions = function editOptions(mailing) {
b1fc510d 305 var options = CRM.utils.adjustDialogDefaults({
b8b2e316 306 autoOpen: false,
b8b2e316 307 title: ts('Edit Options')
b1fc510d 308 });
08baf3ec
TO
309 $q.when(crmMetadata.getFields('Mailing')).then(function(fields) {
310 var model = {
311 fields: fields,
312 mailing: mailing
313 };
314 dialogService.open('previewComponentDialog', '~/crmMailing/dialog/recipientOptions.html', model, options);
315 });
b8b2e316 316 };
7801d9b5
TO
317 });
318
319 // Controller for the "Preview Recipients" dialog
320 // Note: Expects $scope.model to be an object with properties:
321 // - recipients: array of contacts
88e9e883 322 angular.module('crmMailing').controller('PreviewRecipCtrl', function ($scope) {
5d8901af 323 $scope.ts = CRM.ts(null);
7801d9b5 324 });
493eb47a 325
493eb47a
TO
326 // Controller for the "Preview Mailing" dialog
327 // Note: Expects $scope.model to be an object with properties:
328 // - "subject"
329 // - "body_html"
330 // - "body_text"
870cbdbb 331 angular.module('crmMailing').controller('PreviewMailingDialogCtrl', function PreviewMailingDialogCtrl($scope) {
5d8901af 332 $scope.ts = CRM.ts(null);
493eb47a
TO
333 });
334
b8b2e316 335 // Controller for the "Recipients: Edit Options" dialog
3cf58cc3
TO
336 // Note: Expects $scope.model to be an object with properties:
337 // - "mailing" (APIv3 mailing object)
338 // - "fields" (list of fields)
b8b2e316
TO
339 angular.module('crmMailing').controller('EditRecipOptionsDialogCtrl', function EditRecipOptionsDialogCtrl($scope) {
340 $scope.ts = CRM.ts(null);
341 });
342
47bacc20
TO
343 // Controller for the "Preview Mailing Component" segment
344 // which displays header/footer/auto-responder
d376f33e 345 angular.module('crmMailing').controller('PreviewComponentCtrl', function PreviewComponentCtrl($scope, dialogService) {
5d8901af 346 var ts = $scope.ts = CRM.ts(null);
52f515c6 347
47bacc20 348 $scope.previewComponent = function previewComponent(title, componentId) {
f4f103fa 349 var component = _.where(CRM.crmMailing.headerfooterList, {id: "" + componentId});
47bacc20
TO
350 if (!component || !component[0]) {
351 CRM.alert(ts('Invalid component ID (%1)', {
352 1: componentId
353 }));
354 return;
355 }
b1fc510d 356 var options = CRM.utils.adjustDialogDefaults({
47bacc20 357 autoOpen: false,
47bacc20 358 title: title // component[0].name
b1fc510d 359 });
ef5d18a1 360 dialogService.open('previewComponentDialog', '~/crmMailing/dialog/previewComponent.html', component[0], options);
47bacc20
TO
361 };
362 });
363
d376f33e 364 // Controller for the "Preview Mailing Component" dialog
47bacc20
TO
365 // Note: Expects $scope.model to be an object with properties:
366 // - "name"
367 // - "subject"
368 // - "body_html"
369 // - "body_text"
d376f33e 370 angular.module('crmMailing').controller('PreviewComponentDialogCtrl', function PreviewComponentDialogCtrl($scope) {
5d8901af 371 $scope.ts = CRM.ts(null);
47bacc20
TO
372 });
373
744bebee 374 // Controller for the in-place msg-template management
870cbdbb 375 angular.module('crmMailing').controller('MsgTemplateCtrl', function MsgTemplateCtrl($scope, crmMsgTemplates, dialogService) {
5d8901af 376 var ts = $scope.ts = CRM.ts(null);
744bebee
TO
377 $scope.crmMsgTemplates = crmMsgTemplates;
378
379 // @return Promise MessageTemplate (per APIv3)
74263d6b 380 $scope.saveTemplate = function saveTemplate(mailing) {
744bebee 381 var model = {
74263d6b 382 selected_id: mailing.msg_template_id,
744bebee
TO
383 tpl: {
384 msg_title: '',
74263d6b
TO
385 msg_subject: mailing.subject,
386 msg_text: mailing.body_text,
387 msg_html: mailing.body_html
744bebee
TO
388 }
389 };
b1fc510d 390 var options = CRM.utils.adjustDialogDefaults({
744bebee 391 autoOpen: false,
744bebee 392 title: ts('Save Template')
b1fc510d 393 });
ef5d18a1 394 return dialogService.open('saveTemplateDialog', '~/crmMailing/dialog/saveTemplate.html', model, options)
f4f103fa 395 .then(function (item) {
74263d6b 396 mailing.msg_template_id = item.id;
744bebee
TO
397 return item;
398 });
399 };
400
401 // @param int id
402 // @return Promise
74263d6b 403 $scope.loadTemplate = function loadTemplate(mailing, id) {
744bebee 404 return crmMsgTemplates.get(id).then(function (tpl) {
74263d6b
TO
405 mailing.subject = tpl.msg_subject;
406 mailing.body_text = tpl.msg_text;
407 mailing.body_html = tpl.msg_html;
744bebee
TO
408 });
409 };
410 });
411
412 // Controller for the "Save Message Template" dialog
413 // Scope members:
414 // - [input] "model": Object
415 // - "selected_id": int
416 // - "tpl": Object
417 // - "msg_subject": string
418 // - "msg_text": string
419 // - "msg_html": string
88e9e883 420 angular.module('crmMailing').controller('SaveMsgTemplateDialogCtrl', function SaveMsgTemplateDialogCtrl($scope, crmMsgTemplates, dialogService) {
5d8901af 421 var ts = $scope.ts = CRM.ts(null);
744bebee
TO
422 $scope.saveOpt = {mode: '', newTitle: ''};
423 $scope.selected = null;
424
425 $scope.save = function save() {
426 var tpl = _.extend({}, $scope.model.tpl);
427 switch ($scope.saveOpt.mode) {
428 case 'add':
429 tpl.msg_title = $scope.saveOpt.newTitle;
430 break;
431 case 'update':
432 tpl.id = $scope.selected.id;
433 tpl.msg_title = $scope.selected.msg_title;
434 break;
435 default:
436 throw 'SaveMsgTemplateDialogCtrl: Unrecognized mode: ' + $scope.saveOpt.mode;
437 }
438 return crmMsgTemplates.save(tpl)
439 .then(function (item) {
440 CRM.status(ts('Saved'));
441 return item;
442 });
443 };
444
445 function scopeApply(f) {
446 return function () {
447 var args = arguments;
448 $scope.$apply(function () {
449 f.apply(args);
450 });
451 };
452 }
453
454 function init() {
455 crmMsgTemplates.get($scope.model.selected_id).then(
456 function (tpl) {
457 $scope.saveOpt.mode = 'update';
458 $scope.selected = tpl;
459 },
460 function () {
461 $scope.saveOpt.mode = 'add';
462 $scope.selected = null;
463 }
464 );
465 // When using dialogService with a button bar, the major button actions
466 // need to be registered with the dialog widget (and not embedded in
467 // the body of the dialog).
269d44f5
CW
468 var buttons = [
469 {
470 text: ts('Save'),
471 icons: {primary: 'ui-icon-check'},
472 click: function () {
473 $scope.save().then(function (item) {
474 dialogService.close('saveTemplateDialog', item);
475 });
476 }
477 },
478 {
479 text: ts('Cancel'),
480 icons: {primary: 'ui-icon-close'},
481 click: function () {
482 dialogService.cancel('saveTemplateDialog');
483 }
484 }
485 ];
744bebee
TO
486 dialogService.setButtons('saveTemplateDialog', buttons);
487 }
f4f103fa 488
744bebee
TO
489 setTimeout(scopeApply(init), 0);
490 });
2d06b3b6 491
87391d86
TO
492 angular.module('crmMailing').controller('EmailAddrCtrl', function EmailAddrCtrl($scope, crmFromAddresses, crmUiAlert) {
493 var ts = CRM.ts(null);
494 function changeAlert(winnerField, loserField) {
495 crmUiAlert({
496 title: ts('Conflict'),
497 text: ts('The "%1" option conflicts with the "%2" option. The "%2" option has been disabled.', {
498 1: winnerField,
499 2: loserField
500 })
501 });
502 }
503
0a993c89 504 $scope.crmFromAddresses = crmFromAddresses;
87391d86
TO
505 $scope.checkReplyToChange = function checkReplyToChange(mailing) {
506 if (!_.isEmpty(mailing.replyto_email) && mailing.override_verp == '0') {
507 mailing.override_verp = '1';
16f12592 508 changeAlert(ts('Reply-To'), ts('Track Replies'));
87391d86
TO
509 }
510 };
511 $scope.checkVerpChange = function checkVerpChange(mailing) {
512 if (!_.isEmpty(mailing.replyto_email) && mailing.override_verp == '0') {
513 mailing.replyto_email = '';
514 changeAlert(ts('Track Replies'), ts('Reply-To'));
515 }
516 };
0a993c89 517 });
21fb26b8
TO
518
519 var lastEmailTokenAlert = null;
89f476c4 520 angular.module('crmMailing').controller('EmailBodyCtrl', function EmailBodyCtrl($scope, crmMailingMgr, crmUiAlert, $timeout) {
21fb26b8
TO
521 var ts = CRM.ts(null);
522
89f476c4
TO
523 // ex: if (!hasAllTokens(myMailing, 'body_text)) alert('Oh noes!');
524 $scope.hasAllTokens = function hasAllTokens(mailing, field) {
21fb26b8
TO
525 return _.isEmpty(crmMailingMgr.findMissingTokens(mailing, field));
526 };
527
89f476c4
TO
528 // ex: checkTokens(myMailing, 'body_text', 'insert:body_text')
529 // ex: checkTokens(myMailing, '*')
530 $scope.checkTokens = function checkTokens(mailing, field, insertEvent) {
21fb26b8
TO
531 if (lastEmailTokenAlert) {
532 lastEmailTokenAlert.close();
533 }
89f476c4
TO
534 var missing, insertable;
535 if (field == '*') {
536 insertable = false;
537 missing = angular.extend({},
538 crmMailingMgr.findMissingTokens(mailing, 'body_html'),
539 crmMailingMgr.findMissingTokens(mailing, 'body_text')
540 );
541 } else {
542 insertable = !_.isEmpty(insertEvent);
543 missing = crmMailingMgr.findMissingTokens(mailing, field);
544 }
545 if (!_.isEmpty(missing)) {
546 lastEmailTokenAlert = crmUiAlert({
547 type: 'error',
548 title: ts('Required tokens'),
549 templateUrl: '~/crmMailing/dialog/tokenAlert.html',
550 scope: angular.extend($scope.$new(), {
551 insertable: insertable,
552 insertToken: function(token) {
553 $timeout(function(){
554 $scope.$broadcast(insertEvent, '{' + token + '}');
555 $timeout(function(){
556 checkTokens(mailing, field, insertEvent);
557 });
558 });
559 },
560 missing: missing
561 })
21fb26b8 562 });
21fb26b8 563 }
d03e0d13 564 };
21fb26b8 565 });
720c658b
TO
566
567 angular.module('crmMailing').controller('EditUnsubGroupCtrl', function EditUnsubGroupCtrl($scope) {
568 // CRM.crmMailing.groupNames is a global constant - since it doesn't change, we can digest & cache.
569 var mandatoryIds = [];
570 _.each(CRM.crmMailing.groupNames, function(grp){
571 if (grp.is_hidden == "1") {
572 mandatoryIds.push(parseInt(grp.id));
573 }
574 });
575
576 $scope.isUnsubGroupRequired = function isUnsubGroupRequired(mailing) {
577 return _.intersection(mandatoryIds, mailing.recipients.groups.include).length > 0;
578 };
579 });
030dce01 580})(angular, CRM.$, CRM._);