Skip to content

Commit

Permalink
Reverse Render->send to Sent->render
Browse files Browse the repository at this point in the history
We have a gazillion tests on this code ...
  • Loading branch information
eileenmcnaughton authored and totten committed Sep 28, 2021
1 parent b40adb2 commit fd0e4bd
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,32 +262,6 @@ public static function revert($id) {
* @see sendTemplate()
*/
public static function renderTemplate($params) {
$forbidden = ['from', 'toName', 'toEmail', 'cc', 'bcc', 'replyTo'];
$intersect = array_intersect($forbidden, array_keys($params));
if (!empty($intersect)) {
throw new \CRM_Core_Exception(sprintf("renderTemplate() received forbidden fields (%s)",
implode(',', $intersect)));
}

$mailContent = [];
// sendTemplate has had an obscure feature - if you omit `toEmail`, then it merely renders.
// At some point, we may want to invert the relation between renderTemplate/sendTemplate, but for now this is a smaller patch.
[$sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']] = static::sendTemplate($params);
return $mailContent;
}

/**
* Send an email from the specified template based on an array of params.
*
* @param array $params
* A string-keyed array of function params, see function body for details.
*
* @return array
* Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public static function sendTemplate($params) {
$modelDefaults = [
// instance of WorkflowMessageInterface, containing a list of data to provide to the message-template
'model' => NULL,
Expand Down Expand Up @@ -355,17 +329,25 @@ public static function sendTemplate($params) {
$rendered['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $rendered['subject']));
}
$nullSet = ['subject' => NULL, 'text' => NULL, 'html' => NULL];
$mailContent = array_merge($nullSet, $mailContent, $rendered);
return array_merge($nullSet, $mailContent, $rendered);
}

/**
* Send an email from the specified template based on an array of params.
*
* @param array $params
* A string-keyed array of function params, see function body for details.
*
* @return array
* Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public static function sendTemplate($params) {
$params = array_merge($params, self::renderTemplate($params));
// send the template, honouring the target user’s preferences (if any)
$sent = FALSE;

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

if ($params['toEmail']) {
if (!empty($params['toEmail'])) {
// @todo - consider whether we really should be loading
// this based on 'the first email in the db that matches'.
// when we likely have the contact id. OTOH people probably barely
Expand All @@ -388,7 +370,7 @@ public static function sendTemplate($params) {
if (empty($params['attachments'])) {
$params['attachments'] = [];
}
$params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']);
$params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $params['format']);
}
$pdf_filename = '';
if ($config->doNotAttachPDFReceipt &&
Expand All @@ -412,7 +394,7 @@ public static function sendTemplate($params) {
}
}

return [$sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']];
return [$sent, $params['subject'], $params['text'], $params['html']];
}

/**
Expand Down

0 comments on commit fd0e4bd

Please sign in to comment.