(REF) Extract TestBanner as listener
authorTim Otten <totten@civicrm.org>
Fri, 23 Jun 2023 03:58:03 +0000 (20:58 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 23 Jun 2023 05:01:56 +0000 (22:01 -0700)
To r-run this, I used the following procedure:

* Take an arbitrary `Contribution` from example DB (eg id=100)
* Update in SQL (`SET is_test=1 WHERE id=100`)
* Send mailing with `cv api Contribution.sendconfirmation id=100`
* Check email log

CRM/Core/BAO/MessageTemplate.php
Civi/WorkflowMessage/TestBanner.php [new file with mode: 0644]

index 282d2ad7969b4d56256071f64bdf2747c6b8492c..87ab6c292cd2a01b04b2120d38df9ad6da775b28 100644 (file)
@@ -531,23 +531,11 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate implemen
       // https://github.com/civicrm/civicrm-core/pull/17180
       'groupName' => $groupName,
       'workflow' => $workflowName,
+      'isTest' => $isTest,
     ];
 
     CRM_Utils_Hook::alterMailContent($mailContent);
 
-    // add the test banner (if requested)
-    if ($isTest) {
-      $testText = MessageTemplate::get(FALSE)
-        ->setSelect(['msg_subject', 'msg_text', 'msg_html'])
-        ->addWhere('workflow_name', '=', 'test_preview')
-        ->addWhere('is_default', '=', TRUE)
-        ->execute()->first();
-
-      $mailContent['subject'] = $testText['msg_subject'] . $mailContent['subject'];
-      $mailContent['text'] = $testText['msg_text'] . $mailContent['text'];
-      $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testText['msg_html']}", $mailContent['html']);
-    }
-
     if (!empty($subjectOverride)) {
       CRM_Core_Error::deprecatedWarning('CRM_Core_BAO_MessageTemplate: $params[subject] is deprecated. Use $params[messageTemplate][msg_subject] instead.');
       $mailContent['subject'] = $subjectOverride;
diff --git a/Civi/WorkflowMessage/TestBanner.php b/Civi/WorkflowMessage/TestBanner.php
new file mode 100644 (file)
index 0000000..9480b3c
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\WorkflowMessage;
+
+use Civi\Api4\MessageTemplate;
+use Civi\Core\Service\AutoService;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * If someone sends an automated message for a test record (e.g. Contribution with `is_test=1`),
+ * then we add a banner to the automated message.
+ *
+ * @service
+ * @internal
+ */
+class TestBanner extends AutoService implements EventSubscriberInterface {
+
+  public static function getSubscribedEvents() {
+    return [
+      '&hook_civicrm_alterMailContent' => ['onAlterMailContent', -1000],
+    ];
+  }
+
+  public function onAlterMailContent(array &$mailContent): void {
+    // Only alter workflow-messages -- not CiviMail messages
+    if (!empty($mailContent['mailingID'])) {
+      return;
+    }
+
+    // Only alter test messages
+    if (empty($mailContent['isTest'])) {
+      return;
+    }
+
+    $testText = MessageTemplate::get(FALSE)
+      ->setSelect(['msg_subject', 'msg_text', 'msg_html'])
+      ->addWhere('workflow_name', '=', 'test_preview')
+      ->addWhere('is_default', '=', TRUE)
+      ->execute()->first();
+
+    $mailContent['subject'] = $testText['msg_subject'] . $mailContent['subject'];
+    $mailContent['text'] = $testText['msg_text'] . $mailContent['text'];
+    $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testText['msg_html']}", $mailContent['html']);
+  }
+
+}