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

dev/core#2303 Extract function to load the messageContent for a template #19409

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
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
129 changes: 78 additions & 51 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public static function sendReminder($contactId, $email, $messageTemplateID, $fro
$returnProperties[$value] = 1;
}
}
list($details) = CRM_Utils_Token::getTokenDetails([$contactId],
[$details] = CRM_Utils_Token::getTokenDetails([$contactId],
$returnProperties,
NULL, NULL, FALSE,
$tokens,
Expand Down Expand Up @@ -413,56 +413,11 @@ public static function sendTemplate($params) {
}

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

if (!$params['valueName'] && !$params['messageTemplateID']) {
throw new CRM_Core_Exception(ts("Message template's option value or ID missing."));
}

$apiCall = MessageTemplate::get(FALSE)
->addSelect('msg_subject', 'msg_text', 'msg_html', 'pdf_format_id', 'id')
->addWhere('is_default', '=', 1);

if ($params['messageTemplateID']) {
$apiCall->addWhere('id', '=', (int) $params['messageTemplateID']);
}
else {
$apiCall->addWhere('workflow_name', '=', $params['valueName']);
}
$messageTemplate = $apiCall->execute()->first();

if (empty($messageTemplate['id'])) {
if ($params['messageTemplateID']) {
throw new CRM_Core_Exception(ts('No such message template: id=%1.', [1 => $params['messageTemplateID']]));
}
throw new CRM_Core_Exception(ts('No message template with workflow name %2.', [2 => $params['valueName']]));
}

$mailContent = [
'subject' => $messageTemplate['msg_subject'],
'text' => $messageTemplate['msg_text'],
'html' => $messageTemplate['msg_html'],
'format' => $messageTemplate['pdf_format_id'],
// Group name is a deprecated parameter. At some point it will not be passed out.
// https://github.com/civicrm/civicrm-core/pull/17180
'groupName' => $params['groupName'] ?? NULL,
'valueName' => $params['valueName'],
'messageTemplateID' => $params['messageTemplateID'],
];

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
FROM civicrm_msg_template mt
WHERE workflow_name = 'test_preview' AND mt.is_default = 1";
$testDao = CRM_Core_DAO::executeQuery($query);
$testDao->fetch();

$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']);
if (!is_int($params['messageTemplateID']) && !is_null($params['messageTemplateID'])) {
CRM_Core_Error::deprecatedWarning('message template id should be an integer');
$params['messageTemplateID'] = (int) $params['messageTemplateID'];
}
$mailContent = self::loadTemplate((string) $params['valueName'], $params['isTest'], $params['messageTemplateID'] ?? NULL, $params['groupName'] ?? '');

// Overwrite subject from form field
if (!empty($params['subject'])) {
Expand Down Expand Up @@ -506,7 +461,7 @@ public static function sendTemplate($params) {
// effectively comment out this next (performance-expensive) line
// but unfortunately testing is a bit think on the ground to that needs to
// be added.
list($contact) = CRM_Utils_Token::getTokenDetails($contactParams,
[$contact] = CRM_Utils_Token::getTokenDetails($contactParams,
$returnProperties,
FALSE, FALSE, NULL,
CRM_Utils_Token::flattenTokens($tokens),
Expand Down Expand Up @@ -640,4 +595,76 @@ protected static function getWorkflowNameIdMap() {
])->fetchMap('name', 'id');
}

/**
* Load the specified template.
*
* @param string $workflowName
* @param bool $isTest
* @param int|null $messageTemplateID
* @param string $groupName
*
* @return array
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected static function loadTemplate(string $workflowName, bool $isTest, int $messageTemplateID = NULL, $groupName = NULL): array {
if (!$workflowName && !$messageTemplateID) {
throw new CRM_Core_Exception(ts("Message template's option value or ID missing."));
}

$apiCall = MessageTemplate::get(FALSE)
->addSelect('msg_subject', 'msg_text', 'msg_html', 'pdf_format_id', 'id')
->addWhere('is_default', '=', 1);

if ($messageTemplateID) {
$apiCall->addWhere('id', '=', (int) $messageTemplateID);
}
else {
$apiCall->addWhere('workflow_name', '=', $workflowName);
}
$messageTemplate = $apiCall->execute()->first();
if (empty($messageTemplate['id'])) {
if ($messageTemplateID) {
throw new CRM_Core_Exception(ts('No such message template: id=%1.', [1 => $messageTemplateID]));
}
throw new CRM_Core_Exception(ts('No message template with workflow name %2.', [2 => $workflowName]));
}

$mailContent = [
'subject' => $messageTemplate['msg_subject'],
'text' => $messageTemplate['msg_text'],
'html' => $messageTemplate['msg_html'],
'format' => $messageTemplate['pdf_format_id'],
// Workflow name is the field in the message templates table that denotes the
// workflow the template is used for. This is intended to eventually
// replace the non-standard option value/group implementation - see
// https://github.com/civicrm/civicrm-core/pull/17227 and the longer
// discussion on https://github.com/civicrm/civicrm-core/pull/17180
'workflow_name' => $workflowName,
// Note messageTemplateID is the id but when present we also know it was specifically requested.
'messageTemplateID' => $messageTemplateID,
// Group name & valueName are deprecated parameters. At some point it will not be passed out.
// https://github.com/civicrm/civicrm-core/pull/17180
'groupName' => $groupName,
'valueName' => $workflowName,
];

CRM_Utils_Hook::alterMailContent($mailContent);

// add the test banner (if requested)
if ($isTest) {
$testText = MessageTemplate::get(FALSE)
->setSelect(['msg_subject', 'msg_text', 'msg_html'])
->addWhere('workflow_name', '=', 'test_preview')
->addWhere('is_default', '=', TRUE)
->execute()->first();

$mailContent['subject'] = $testText['msg_subject'] . $mailContent['subject'];
$mailContent['text'] = $testText['msg_text'] . $mailContent['text'];
$mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testText['msg_html']}", $mailContent['html']);
}

return $mailContent;
}

}
10 changes: 7 additions & 3 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2422,23 +2422,27 @@ public function testRepeatTransactionLineItems() {

/**
* Test repeat contribution successfully creates is_test transaction.
*
* @throws \CRM_Core_Exception
*/
public function testRepeatTransactionIsTest() {
public function testRepeatTransactionIsTest(): void {
$this->_params['is_test'] = 1;
$originalContribution = $this->setUpRepeatTransaction(['is_test' => 1], 'single');

$this->callAPISuccess('contribution', 'repeattransaction', [
'original_contribution_id' => $originalContribution['id'],
'contribution_status_id' => 'Completed',
'trxn_id' => uniqid(),
'trxn_id' => '1234',
]);
$this->callAPISuccessGetCount('Contribution', ['contribution_test' => 1], 2);
}

/**
* Test repeat contribution passed in status.
*
* @throws \CRM_Core_Exception
*/
public function testRepeatTransactionPassedInStatus() {
public function testRepeatTransactionPassedInStatus(): void {
$originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');

$this->callAPISuccess('contribution', 'repeattransaction', [
Expand Down