From fd0e4bde38e14a19b44254649f6e931347675223 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 25 Sep 2021 12:32:30 +1200 Subject: [PATCH] Reverse Render->send to Sent->render We have a gazillion tests on this code ... --- CRM/Core/BAO/MessageTemplate.php | 54 +++++++++++--------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index 802a3f822f..fa3bea928b 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -262,32 +262,6 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { * @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, @@ -355,17 +329,25 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { $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 @@ -388,7 +370,7 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { 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 && @@ -412,7 +394,7 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { } } - return [$sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']]; + return [$sent, $params['subject'], $params['text'], $params['html']]; } /** -- 2.25.1