Skip to content

Commit

Permalink
Merge pull request #8718 from alifrumin/CRM-19099
Browse files Browse the repository at this point in the history
CRM-19099: Adding altermailparams and altermailcontent hooks to receipts
  • Loading branch information
monishdeb authored Jul 29, 2016
2 parents 2d328a5 + eff0a94 commit 2b6622c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
68 changes: 37 additions & 31 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ public static function sendTemplate($params) {
);
$params = array_merge($defaults, $params);

CRM_Utils_Hook::alterMailParams($params, 'messageTemplate');

if ((!$params['groupName'] ||
!$params['valueName']
) &&
Expand Down Expand Up @@ -399,12 +401,16 @@ public static function sendTemplate($params) {
}
}

$subject = $dao->subject;
$text = $dao->text;
$html = $dao->html;
$format = $dao->format;
$mailContent = array(
'subject' => $dao->subject,
'text' => $dao->text,
'html' => $dao->html,
'format' => $dao->format,
);
$dao->free();

CRM_Utils_Hook::alterMailContent($mailContent);

// add the test banner (if requested)
if ($params['isTest']) {
$query = "SELECT msg_subject subject, msg_text text, msg_html html
Expand All @@ -415,19 +421,19 @@ public static function sendTemplate($params) {
$testDao = CRM_Core_DAO::executeQuery($query);
$testDao->fetch();

$subject = $testDao->subject . $subject;
$text = $testDao->text . $text;
$html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
$mailContent['subject'] = $testDao->subject . $mailContent['subject'];
$mailContent['text'] = $testDao->text . $mailContent['text'];
$mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']);
$testDao->free();
}

// replace tokens in the three elements (in subject as if it was the text body)
$domain = CRM_Core_BAO_Domain::getDomain();
$hookTokens = array();
$mailing = new CRM_Mailing_BAO_Mailing();
$mailing->subject = $subject;
$mailing->body_text = $text;
$mailing->body_html = $html;
$mailing->subject = $mailContent['subject'];
$mailing->body_text = $mailContent['text'];
$mailing->body_html = $mailContent['html'];
$tokens = $mailing->getTokens();
CRM_Utils_Hook::tokens($hookTokens);
$categories = array_keys($hookTokens);
Expand Down Expand Up @@ -470,14 +476,14 @@ public static function sendTemplate($params) {
$contact = $contact[$contactID];
}

$subject = CRM_Utils_Token::replaceDomainTokens($subject, $domain, FALSE, $tokens['text'], TRUE);
$text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text'], TRUE);
$html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
$mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], 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);

if ($contactID) {
$subject = CRM_Utils_Token::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
$text = CRM_Utils_Token::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
$html = CRM_Utils_Token::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
$mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], 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);

$contactArray = array($contactID => $contact);
CRM_Utils_Hook::tokenValues($contactArray,
Expand All @@ -489,34 +495,34 @@ public static function sendTemplate($params) {
);
$contact = $contactArray[$contactID];

$subject = CRM_Utils_Token::replaceHookTokens($subject, $contact, $categories, TRUE);
$text = CRM_Utils_Token::replaceHookTokens($text, $contact, $categories, TRUE);
$html = CRM_Utils_Token::replaceHookTokens($html, $contact, $categories, TRUE);
$mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE);
$mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE);
$mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE);
}

// strip whitespace from ends and turn into a single line
$subject = "{strip}$subject{/strip}";
$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);
}
foreach (array(
'subject',
'text',
'html',
) as $elem) {
$$elem = $smarty->fetch("string:{$$elem}");
'subject',
'text',
'html',
) as $elem) {
$mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}");
}

// send the template, honouring the target user’s preferences (if any)
$sent = FALSE;

// create the params array
$params['subject'] = $subject;
$params['text'] = $text;
$params['html'] = $html;
$params['subject'] = $mailContent['subject'];
$params['text'] = $mailContent['text'];
$params['html'] = $mailContent['html'];

if ($params['toEmail']) {
$contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
Expand All @@ -538,7 +544,7 @@ public static function sendTemplate($params) {
if (empty($params['attachments'])) {
$params['attachments'] = array();
}
$params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $format);
$params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']);
}
$pdf_filename = '';
if ($config->doNotAttachPDFReceipt &&
Expand All @@ -548,7 +554,7 @@ public static function sendTemplate($params) {
if (empty($params['attachments'])) {
$params['attachments'] = array();
}
$params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $format);
$params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']);
if (isset($params['tplParams']['email_comment'])) {
$params['html'] = $params['tplParams']['email_comment'];
$params['text'] = strip_tags($params['tplParams']['email_comment']);
Expand All @@ -562,7 +568,7 @@ public static function sendTemplate($params) {
}
}

return array($sent, $subject, $text, $html);
return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']);
}

}
2 changes: 1 addition & 1 deletion CRM/Utils/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function send(&$params) {
}

// first call the mail alter hook
CRM_Utils_Hook::alterMailParams($params);
CRM_Utils_Hook::alterMailParams($params, 'singleEmail');

// check if any module has aborted mail sending
if (!empty($params['abortMailSend']) || empty($params['toEmail'])) {
Expand Down

0 comments on commit 2b6622c

Please sign in to comment.