From fc7fef2125e9c1041b4d60b1518c46a7bc8662aa Mon Sep 17 00:00:00 2001 From: Neil Drumm Date: Thu, 8 Aug 2013 04:08:18 +0000 Subject: [PATCH] CRM-12930 Execute Smarty templates before token replacement --- CRM/Core/BAO/ActionSchedule.php | 15 +++++------ CRM/Core/BAO/MessageTemplates.php | 43 ++++++++++++++----------------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index f1ec99fa3e..97d296a2e6 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -385,6 +385,13 @@ WHERE cas.entity_value = $id AND $body_text = CRM_Utils_String::htmlToText($body_html); } + // Execute Smarty templates before token replacement, so templates do not + // interfere and we hit the compiled template cache. + $smarty = CRM_Core_Smarty::singleton(); + $body_text = $smarty->fetch('string:' . $body_text); + $body_html = $smarty->fetch('string:' . $body_html); + $body_subject = $smarty->fetch('string:' . $messageSubject); + $params = array(array('contact_id', '=', $contactId, 0, 0)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); @@ -423,12 +430,6 @@ WHERE cas.entity_value = $id AND $html = $body_html; $text = $body_text; - $smarty = CRM_Core_Smarty::singleton(); - foreach (array( - 'text', 'html') as $elem) { - $$elem = $smarty->fetch("string:{$$elem}"); - } - $matches = array(); preg_match_all('/(?fetch("string:{$messageSubject}"); - // set up the parameters for CRM_Utils_Mail::send $mailParams = array( 'groupName' => 'Scheduled Reminder Sender', diff --git a/CRM/Core/BAO/MessageTemplates.php b/CRM/Core/BAO/MessageTemplates.php index 83af344c51..0d4c1b332a 100644 --- a/CRM/Core/BAO/MessageTemplates.php +++ b/CRM/Core/BAO/MessageTemplates.php @@ -166,6 +166,13 @@ class CRM_Core_BAO_MessageTemplates extends CRM_Core_DAO_MessageTemplates { $body_text = CRM_Utils_String::htmlToText($body_html); } + // Execute Smarty templates before token replacement, so templates do not + // interfere and we hit the compiled template cache. + $smarty = CRM_Core_Smarty::singleton(); + $body_text = $smarty->fetch('string:' . $body_text); + $body_html = $smarty->fetch('string:' . $body_html); + $body_subject = $smarty->fetch('string:' . $body_subject); + $params = array(array('contact_id', '=', $contactId, 0, 0)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); @@ -217,20 +224,12 @@ class CRM_Core_BAO_MessageTemplates extends CRM_Core_DAO_MessageTemplates { $html = $body_html; $text = $body_text; - $smarty = CRM_Core_Smarty::singleton(); - foreach (array( - 'text', 'html') as $elem) { - $$elem = $smarty->fetch("string:{$$elem}"); - } - // do replacements in message subject $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, false, $tokens); $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, true, $tokens); $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens, true); $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, true); - $messageSubject = $smarty->fetch("string:{$messageSubject}"); - // set up the parameters for CRM_Utils_Mail::send $mailParams = array( 'groupName' => 'Scheduled Reminder Sender', @@ -389,6 +388,19 @@ class CRM_Core_BAO_MessageTemplates extends CRM_Core_DAO_MessageTemplates { $testDao->free(); } + // Strip whitespace from ends and turn into a single line. + $subject = "{strip}$subject{/strip}"; + + // Parse the three elements with Smarty. + $smarty = CRM_Core_Smarty::singleton(); + foreach ($params['tplParams'] as $name => $value) { + $smarty->assign($name, $value); + } + foreach (array( + 'subject', 'text', 'html') as $elem) { + $$elem = $smarty->fetch("string:{$$elem}"); + } + // replace tokens in the three elements (in subject as if it was the text body) $domain = CRM_Core_BAO_Domain::getDomain(); $hookTokens = array(); @@ -451,21 +463,6 @@ class CRM_Core_BAO_MessageTemplates extends CRM_Core_DAO_MessageTemplates { $html = CRM_Utils_Token::replaceHookTokens($html, $contact, $categories, TRUE); } - // strip whitespace from ends and turn into a single line - $subject = "{strip}$subject{/strip}"; - - // parse the three elements with Smarty - - - $smarty = CRM_Core_Smarty::singleton(); - foreach ($params['tplParams'] as $name => $value) { - $smarty->assign($name, $value); - } - foreach (array( - 'subject', 'text', 'html') as $elem) { - $$elem = $smarty->fetch("string:{$$elem}"); - } - // send the template, honouring the target user’s preferences (if any) $sent = FALSE; -- 2.25.1