From d2a39a85c3adb07dd44d39cff123b7c675895dec Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 20 Feb 2022 17:36:19 -0500 Subject: [PATCH] SearchKit - Add CiviMail integration --- .../Action/SearchDisplay/GetSearchTasks.php | 10 +++ .../crmSearchBatchRunner.component.js | 9 ++- .../crmSearchTaskMailing.ctrl.js | 73 +++++++++++++++++++ .../crmSearchTasks/crmSearchTaskMailing.html | 34 +++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js create mode 100644 ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php index da0c5d61d1..b0ded1f45e 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php @@ -120,6 +120,16 @@ class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction { ], ]; } + if (\CRM_Core_Component::isEnabled('CiviMail') && ( + \CRM_Core_Permission::access('CiviMail') || !$this->checkPermissions || + (\CRM_Mailing_Info::workflowEnabled() && \CRM_Core_Permission::check('create mailings')) + )) { + $tasks[$entity['name']]['contact.mailing'] = [ + 'title' => E::ts('Email - schedule/send via CiviMail'), + 'uiDialog' => ['templateUrl' => '~/crmSearchTasks/crmSearchTaskMailing.html'], + 'icon' => 'fa-paper-plane', + ]; + } } if ($entity['name'] === 'Contribution') { diff --git a/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js b/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js index 6b910d825a..88c90f7f4b 100644 --- a/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js +++ b/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js @@ -27,6 +27,9 @@ EST_BATCH_TIME = 5; this.$onInit = function() { + if (ctrl.action === 'create') { + ctrl.ids = [0]; + } totalBatches = Math.ceil(ctrl.ids.length / BATCH_SIZE); runBatch(); }; @@ -50,7 +53,7 @@ records.push(record); }); }); - } else { + } else if (ctrl.action !== 'create') { // For other batch actions (update, delete), add supplied ids to the where clause params.where = params.where || []; params.where.push([ctrl.idField || 'id', 'IN', ctrl.ids.slice(ctrl.first, ctrl.last)]); @@ -60,7 +63,9 @@ stopIncrementer(); ctrl.progress = Math.floor(100 * ++currentBatch / totalBatches); if (ctrl.last >= ctrl.ids.length) { - $timeout(ctrl.success, 500); + $timeout(function() { + ctrl.success({result: result}); + }, 500); } else { runBatch(); } diff --git a/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js new file mode 100644 index 0000000000..a5f14bb303 --- /dev/null +++ b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js @@ -0,0 +1,73 @@ +(function(angular, $, _) { + "use strict"; + + angular.module('crmSearchTasks').controller('crmSearchTaskMailing', function($scope, crmApi4, searchTaskBaseTrait) { + var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), + // Combine this controller with model properties (ids, entity, entityInfo) and searchTaskBaseTrait + ctrl = angular.extend(this, $scope.model, searchTaskBaseTrait), + templateTypes; + + this.entityTitle = this.getEntityTitle(); + + // This option is needed to determine whether the mailing will be handled by CiviMail or Mosaico + crmApi4({ + templateTypes: ['Mailing', 'getFields', { + loadOptions: ['id'], + where: [['name', '=', 'template_type']] + }, ['options']], + recipientCount: ['Contact', 'get', { + select: ['row_count'], + join: [['Email AS email', 'INNER', ['id', '=', 'email.contact_id']]], + where: [['id', 'IN', ctrl.ids], ['do_not_email', '!=', true], ['is_opt_out', '!=', true], ['email.on_hold', '=', 0]], + groupBy: ['id'] + }] + }).then(function(results) { + templateTypes = results.templateTypes[0]; + ctrl.recipientCount = results.recipientCount.count; + }); + + this.submit = function() { + var contacts = _.transform(ctrl.ids, function(records, cid) { + records.push({contact_id: cid}); + }); + ctrl.start({ + values: { + title: 'Hidden Group ' + Date.now(), + is_hidden: true, + 'group_type:name': ['Mailing List'], + }, + chain: { + contacts: ['GroupContact', 'save', { + defaults: {group_id: '$id'}, + records: contacts + }], + mailing: ['Mailing', 'create', { + values: { + name: ctrl.name, + template_type: templateTypes[0].id + } + }, 0], + mailingGroup: ['MailingGroup', 'create', { + values: { + group_type: 'Include', + 'entity_table:name': 'Group', + entity_id: '$id', + mailing_id: '$mailing.id' + }, + }, 0] + } + }); + }; + + + this.onSuccess = function(result) { + window.location = CRM.url('civicrm/a#/mailing/' + result[0].mailing.id); + }; + + this.onError = function() { + CRM.alert(ts('An error occurred while attempting to create mailing.'), ts('Error'), 'error'); + this.cancel(); + }; + + }); +})(angular, CRM.$, CRM._); diff --git a/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html new file mode 100644 index 0000000000..f03ae5d909 --- /dev/null +++ b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html @@ -0,0 +1,34 @@ +
+
+
+

{{:: ts('Compose and send a mass-mailing to the %1 selected contacts (you will be able to add or exclude additional groups of contacts in the next step).', {1: $ctrl.ids.length}) }}

+
+ + +
+
+
+ + {{:: ts('Checking recipients...') }} +
+
+ + {{:: ts('None of the selected contacts are eligible to receive mailings (due to lack of email address or unsubscribe status).') }} +
+
+ + {{:: ts('%1 of the selected contacts cannot receive mailings (due to lack of email address or unsubscribe status).', {1: $ctrl.ids.length - $ctrl.recipientCount}) }} +
+
+ + {{:: ts('All of the selected contacts have active email addresses.') }} +
+
+
+
{{:: ts('Creating mailing...') }}
+ +
+ + + +
-- 2.25.1