Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Further extract the portion of sendTemplate that relates to rendering #19522

Merged
merged 1 commit into from
Feb 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,29 +425,7 @@ public static function sendTemplate($params) {
$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;
Expand Down Expand Up @@ -706,4 +684,41 @@ protected static function parseThroughSmarty(array $mailContent, $tplParams): ar
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;
}

}