Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(REF) MessageTemplate - Reverse renderTemplate()<=>sendTemplate() call #21610

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,32 +262,22 @@ 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;
[$mailContent, $params] = self::renderTemplateRaw($params);
return CRM_Utils_Array::subset($mailContent, ['subject', 'text', 'html']);
}

/**
* Send an email from the specified template based on an array of params.
* Render a message template.
*
* @param array $params
* A string-keyed array of function params, see function body for details.
*
* Mixed render parameters. See sendTemplate() for more 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
* Tuple of [$mailContent, $updatedParams].
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @see sendTemplate()
*/
public static function sendTemplate($params) {
protected static function renderTemplateRaw($params) {
$modelDefaults = [
// instance of WorkflowMessageInterface, containing a list of data to provide to the message-template
'model' => NULL,
Expand Down Expand Up @@ -356,16 +346,31 @@ public static function sendTemplate($params) {
}
$nullSet = ['subject' => NULL, 'text' => NULL, 'html' => NULL];
$mailContent = array_merge($nullSet, $mailContent, $rendered);
return [$mailContent, $params];
}

// send the template, honouring the target user’s preferences (if any)
$sent = FALSE;
/**
* 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) {
[$mailContent, $params] = self::renderTemplateRaw($params);

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

if ($params['toEmail']) {
// send the template, honouring the target user’s preferences (if any)
$sent = FALSE;
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 Down