Skip to content

Commit

Permalink
Merge pull request #18118 from artfulrobot/artfulrobot-msgtpl-disable…
Browse files Browse the repository at this point in the history
…-smarty

Add disable_smarty option to MessageTemplate.send API
  • Loading branch information
seamuslee001 authored Aug 24, 2020
2 parents f8682b5 + 56f523f commit d2fadae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
51 changes: 33 additions & 18 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ public static function sendTemplate($params) {
'isTest' => FALSE,
// filename of optional PDF version to add as attachment (do not include path)
'PDFFilename' => NULL,
// Disable Smarty?
'disableSmarty' => FALSE,
];
$params = array_merge($defaults, $params);

Expand Down Expand Up @@ -511,14 +513,17 @@ public static function sendTemplate($params) {
$contact = $contact[$contactID];
}

$mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], TRUE);
$mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE);
$mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE);
// When using Smarty we need to pass the $escapeSmarty parameter.
$escapeSmarty = !$params['disableSmarty'];

$mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], $escapeSmarty);
$mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], $escapeSmarty);
$mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], $escapeSmarty);

if ($contactID) {
$mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['subject'], FALSE, TRUE);
$mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
$mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE);
$mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['subject'], FALSE, $escapeSmarty);
$mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, $escapeSmarty);
$mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, $escapeSmarty);

$contactArray = [$contactID => $contact];
CRM_Utils_Hook::tokenValues($contactArray,
Expand All @@ -535,20 +540,30 @@ public static function sendTemplate($params) {
$mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE);
}

// strip whitespace from ends and turn into a single line
$mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}";
// 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']) {
// strip whitespace from ends and turn into a single line
$mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}";

// parse the three elements with Smarty
$smarty = CRM_Core_Smarty::singleton();
foreach ($params['tplParams'] as $name => $value) {
$smarty->assign($name, $value);
// parse the three elements with Smarty
$smarty = CRM_Core_Smarty::singleton();
foreach ($params['tplParams'] as $name => $value) {
$smarty->assign($name, $value);
}
foreach ([
'subject',
'text',
'html',
] as $elem) {
$mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}");
}
}
foreach ([
'subject',
'text',
'html',
] as $elem) {
$mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}");
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']));
}

// send the template, honouring the target user’s preferences (if any)
Expand Down
5 changes: 5 additions & 0 deletions api/v3/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,9 @@ function _civicrm_api3_message_template_send_spec(&$params) {
$params['pdf_filename']['title'] = 'PDF Filename';
$params['pdf_filename']['api.aliases'] = ['PDFFilename'];
$params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING;

$params['disable_smarty']['description'] = 'Disable Smarty. Normal CiviMail tokens are still supported. By default Smarty is enabled.';
$params['disable_smarty']['title'] = 'Disable Smarty';
$params['disable_smarty']['api.aliases'] = ['disableSmarty'];
$params['disable_smarty']['type'] = CRM_Utils_Type::T_BOOLEAN;
}

0 comments on commit d2fadae

Please sign in to comment.