From 6f4976a2337981b94ac01000a30ee3498b23169e Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 4 Feb 2021 13:51:45 +1300 Subject: [PATCH] [REF] Further extract the portion of sendTemplate that relates to rendering This is primary for the purposes of simplification at this stage --- CRM/Core/BAO/MessageTemplate.php | 61 ++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index 70358b0702..9ba60e2003 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -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; + } + } -- 2.25.1