1 (function(angular
, $, _
) {
4 angular
.module('crmSearchTasks').controller('crmSearchTaskMailing', function($scope
, crmApi4
, searchTaskBaseTrait
) {
5 var ts
= $scope
.ts
= CRM
.ts('org.civicrm.search_kit'),
6 // Combine this controller with model properties (ids, entity, entityInfo) and searchTaskBaseTrait
7 ctrl
= angular
.extend(this, $scope
.model
, searchTaskBaseTrait
),
10 this.entityTitle
= this.getEntityTitle();
12 // This option is needed to determine whether the mailing will be handled by CiviMail or Mosaico
14 templateTypes
: ['Mailing', 'getFields', {
16 where
: [['name', '=', 'template_type']]
18 recipientCount
: ['Contact', 'get', {
19 select
: ['row_count'],
20 join
: [['Email AS email', 'INNER', ['id', '=', 'email.contact_id']]],
21 where
: [['id', 'IN', ctrl
.ids
], ['do_not_email', '!=', true], ['is_opt_out', '!=', true], ['email.on_hold', '=', 0]],
24 }).then(function(results
) {
25 templateTypes
= results
.templateTypes
[0];
26 ctrl
.recipientCount
= results
.recipientCount
.count
;
29 this.submit = function() {
30 var contacts
= _
.transform(ctrl
.ids
, function(records
, cid
) {
31 records
.push({contact_id
: cid
});
35 title
: 'Hidden Group ' + Date
.now(),
37 'group_type:name': ['Mailing List'],
40 contacts
: ['GroupContact', 'save', {
41 defaults
: {group_id
: '$id'},
44 mailing
: ['Mailing', 'create', {
47 template_type
: templateTypes
[0].id
50 mailingGroup
: ['MailingGroup', 'create', {
52 group_type
: 'Include',
53 'entity_table:name': 'Group',
55 mailing_id
: '$mailing.id'
63 this.onSuccess = function(result
) {
64 window
.location
= CRM
.url('civicrm/a#/mailing/' + result
[0].mailing
.id
);
67 this.onError = function() {
68 CRM
.alert(ts('An error occurred while attempting to create mailing.'), ts('Error'), 'error');
73 })(angular
, CRM
.$, CRM
._
);