Skip to content

Commit

Permalink
Add function to update message tokens during upgrade
Browse files Browse the repository at this point in the history
In conjunction with civicrm#20867
  • Loading branch information
eileenmcnaughton committed Jul 19, 2021
1 parent 8b1dce5 commit 2bbf23c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
1 change: 0 additions & 1 deletion CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
'invoice_date' => $invoiceDate,
'dueDate' => $dueDate,
'notes' => $invoiceNotes,
'display_name' => $contribution->_relatedObjects['contact']->display_name,
'lineItem' => $lineItem,
'dataArray' => $dataArray,
'refundedStatusId' => $refundedStatusId,
Expand Down
16 changes: 16 additions & 0 deletions CRM/Upgrade/Incremental/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ public static function updateMessageTemplate(&$message, $version) {
$messageObj->updateTemplates();
}

/**
* Updated a message token within a template.
*
* @param string $workflowName
* @param string $old
* @param string $new
* @param $version
*
* @return bool
*/
public static function updateMessageToken(string $workflowName, string $old, string $new, $version):bool {
$messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version);
$messageObj->replaceTokenInTemplate($workflowName, $old, $new);
return TRUE;
}

/**
* @param $message
* @param $latestVer
Expand Down
49 changes: 49 additions & 0 deletions CRM/Upgrade/Incremental/MessageTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,55 @@ public function getTemplatesToUpdate() {
return $return;
}

/**
* Replace a token with the new preferred option.
*
* @param string $workflowName
* @param string $old
* @param string $new
*/
public function replaceTokenInTemplate(string $workflowName, string $old, string $new): void {
$oldToken = '{' . $old . '}';
$newToken = '{' . $new . '}';
CRM_Core_DAO::executeQuery("UPDATE civicrm_msg_template
SET
msg_text = REPLACE(msg_text, '$oldToken', '$newToken'),
msg_subject = REPLACE(msg_subject, '$oldToken', '$newToken'),
msg_html = REPLACE(msg_html, '$oldToken', '$newToken')
WHERE workflow_name = '$workflowName'
");
}

/**
* Get warnings for users if the replaced string is still present.
*
* This might be the case when used in an IF and for now we will recommend
* manual intervention.
*
* @param string $workflowName
* @param string $old
* @param string $new
*
* @return string
*/
public function getMessageTemplateWarning(string $workflowName, string $old, string $new) {
if (CRM_Core_DAO::singleValueQuery("
SELECT COUNT(*)
FROM civicrm_msg_template
WHERE workflow_name = '$workflowName'
AND msg_html LIKE '%$old%'
OR msg_subject LIKE '%$old%'
OR civicrm_msg_template.msg_text LIKE '%$old%'
")) {
return ts('Please review your %1 message template and remove references to the token %2 as it has been replaced by %3', [
1 => $workflowName,
2 => '{' . $old . '}',
3 => '{' . $new . '}',
]);
}
return '';
}

/**
* Get the upgrade messages.
*/
Expand Down
25 changes: 13 additions & 12 deletions CRM/Upgrade/Incremental/php/FiveFortyOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU
* an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
*/
public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
$templateUpgrader = new CRM_Upgrade_Incremental_MessageTemplates($rev);
$postUpgradeMessage .= $templateUpgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$displayName', 'contact.display_name');
// Example: Generate a post-upgrade message.
// if ($rev == '5.12.34') {
// $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
Expand All @@ -55,18 +57,17 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
* (change the x in the function name):
*/

// /**
// * Upgrade function.
// *
// * @param string $rev
// */
// public function upgrade_5_0_x($rev) {
// $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
// $this->addTask('Do the foo change', 'taskFoo', ...);
// // Additional tasks here...
// // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
// // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
// }
/**
* Upgrade function.
*
* @param string $rev
*/
public function upgrade_5_41_alpha1($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Replace legacy displayName smarty token in Invoice workflow template',
'updateMessageToken', ['contribution_invoice_receipt', '$displayName', 'contact.display_name']
);
}

// public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) {
// return TRUE;
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Civi\Api4\MessageTemplate;

/**
* Class CRM_UF_Page_ProfileEditorTest
* @group headless
Expand Down Expand Up @@ -37,6 +39,31 @@ public function testMessageTemplateUpgrade() {
}
}

/**
* Test that a string replacement in a message template can be done.
*
* @throws \API_Exception
*/
public function testMessageTemplateStringReplace(): void {
MessageTemplate::update()->setValues(['msg_html' => '{$displayName}'])->addWhere(
'workflow_name', '=', 'contribution_invoice_receipt'
)->execute();
$upgrader = new CRM_Upgrade_Incremental_MessageTemplates('5.41.0');
$messages = $upgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$displayName', 'contact.display_name');
$this->assertEquals('Please review your contribution_invoice_receipt message template and remove references to the token {$displayName} as it has been replaced by {contact.display_name}', $messages);
$upgrader->replaceTokenInTemplate('contribution_invoice_receipt', '$displayName', 'contact.display_name');
$templates = MessageTemplate::get()->addSelect('msg_html')
->addWhere(
'workflow_name', '=', 'contribution_invoice_receipt'
)->execute();
foreach ($templates as $template) {
$this->assertEquals('{contact.display_name}', $template['msg_html']);
}
$messages = $upgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$displayName', 'contact.display_name');
$this->assertEquals('', $messages);
$this->revertTemplateToReservedTemplate('contribution_invoice_receipt');
}

/**
* Test message upgrade process only edits the default if the template is customised.
*/
Expand Down

0 comments on commit 2bbf23c

Please sign in to comment.