[REF] Further extract the portion of sendTemplate that relates to rendering
authoreileen <emcnaughton@wikimedia.org>
Thu, 4 Feb 2021 00:51:45 +0000 (13:51 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 4 Feb 2021 00:51:45 +0000 (13:51 +1300)
This is primary for the purposes of simplification at this stage

CRM/Core/BAO/MessageTemplate.php

index 70358b0702d88056d096b9b2a56cd111e946fb90..9ba60e2003c9b3ec9691c5c891c1b5fccb32b050 100644 (file)
@@ -425,29 +425,7 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate {
       $mailContent['subject'] = $params['subject'];
     }
 
-    $tokens = self::getTokensToResolve($mailContent);
-
-    // When using Smarty we need to pass the $escapeSmarty parameter.
-    $escapeSmarty = !$params['disableSmarty'];
-
-    $mailContent = self::resolveDomainTokens($mailContent, $tokens, $escapeSmarty);
-
-    $contactID = $params['contactId'] ?? NULL;
-    if ($contactID) {
-      $mailContent = self::resolveContactTokens($contactID, $tokens, $mailContent, $escapeSmarty);
-    }
-
-    // Normally Smarty is run, but it can be disabled using the disableSmarty
-    // parameter, which may be useful for non-core uses of MessageTemplate.send
-    // In particular it helps with the mosaicomsgtpl extension.
-    if (!$params['disableSmarty']) {
-      $mailContent = self::parseThroughSmarty($mailContent, $params['tplParams']);
-    }
-    else {
-      // Since we're not relying on Smarty for this function, we DIY.
-      // strip whitespace from ends and turn into a single line
-      $mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject']));
-    }
+    $mailContent = self::renderMessageTemplate($mailContent, $params['disableSmarty'], $params['contactId'] ?? NULL, $params['tplParams']);
 
     // send the template, honouring the target user’s preferences (if any)
     $sent = FALSE;
@@ -706,4 +684,41 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate {
     return $mailContent;
   }
 
+  /**
+   * Render the message template, resolving tokens and smarty tokens.
+   *
+   * @param array $mailContent
+   * @param bool $disableSmarty
+   * @param int $contactID
+   * @param array $smartyAssigns
+   *
+   * @return array
+   * @throws \CRM_Core_Exception
+   */
+  protected static function renderMessageTemplate(array $mailContent, $disableSmarty, $contactID, $smartyAssigns): array {
+    $tokens = self::getTokensToResolve($mailContent);
+
+    // When using Smarty we need to pass the $escapeSmarty parameter.
+    $escapeSmarty = !$disableSmarty;
+
+    $mailContent = self::resolveDomainTokens($mailContent, $tokens, $escapeSmarty);
+
+    if ($contactID) {
+      $mailContent = self::resolveContactTokens($contactID, $tokens, $mailContent, $escapeSmarty);
+    }
+
+    // Normally Smarty is run, but it can be disabled using the disableSmarty
+    // parameter, which may be useful for non-core uses of MessageTemplate.send
+    // In particular it helps with the mosaicomsgtpl extension.
+    if (!$disableSmarty) {
+      $mailContent = self::parseThroughSmarty($mailContent, $smartyAssigns);
+    }
+    else {
+      // Since we're not relying on Smarty for this function, we DIY.
+      // strip whitespace from ends and turn into a single line
+      $mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject']));
+    }
+    return $mailContent;
+  }
+
 }