From: Eileen McNaughton Date: Fri, 16 Jul 2021 22:08:49 +0000 (+1200) Subject: Add function to update message tokens during upgrade X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8515e10c7bc0b2012acc6b9ca3ca7f8565d42e32;hp=403b0324b35193dc0aacaae9b7047cb86a5d1f74;p=civicrm-core.git Add function to update message tokens during upgrade In conjunction with https://github.com/civicrm/civicrm-core/pull/20867 --- diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index 64e903df88..8d3b16769e 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -374,7 +374,6 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { 'invoice_date' => $invoiceDate, 'dueDate' => $dueDate, 'notes' => $invoiceNotes, - 'display_name' => $contribution->_relatedObjects['contact']->display_name, 'lineItem' => $lineItem, 'dataArray' => $dataArray, 'refundedStatusId' => $refundedStatusId, diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index b51992a9a4..eda62c7956 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -229,7 +229,23 @@ class CRM_Upgrade_Incremental_Base { public static function updateMessageTemplates($ctx, $version) { $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version); $messageTemplateObject->updateTemplates(); + } + /** + * Updated a message token within a template. + * + * @param CRM_Queue_TaskContext $ctx + * @param string $workflowName + * @param string $old + * @param string $new + * @param $version + * + * @return bool + */ + public static function updateMessageToken($ctx, string $workflowName, string $old, string $new, $version):bool { + $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); + $messageObj->replaceTokenInTemplate($workflowName, $old, $new); + return TRUE; } /** diff --git a/CRM/Upgrade/Incremental/MessageTemplates.php b/CRM/Upgrade/Incremental/MessageTemplates.php index 441b068f1e..a783ffd023 100644 --- a/CRM/Upgrade/Incremental/MessageTemplates.php +++ b/CRM/Upgrade/Incremental/MessageTemplates.php @@ -270,6 +270,57 @@ class CRM_Upgrade_Incremental_MessageTemplates { 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. */ diff --git a/CRM/Upgrade/Incremental/php/FiveFortyOne.php b/CRM/Upgrade/Incremental/php/FiveFortyOne.php index c77a863a16..ad4d85154a 100644 --- a/CRM/Upgrade/Incremental/php/FiveFortyOne.php +++ b/CRM/Upgrade/Incremental/php/FiveFortyOne.php @@ -43,6 +43,8 @@ class CRM_Upgrade_Incremental_php_FiveFortyOne extends CRM_Upgrade_Incremental_B * 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', '$display_name', 'contact.display_name'); // Example: Generate a post-upgrade message. // if ($rev == '5.12.34') { // $postUpgradeMessage .= '

' . 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'."); @@ -55,18 +57,17 @@ class CRM_Upgrade_Incremental_php_FiveFortyOne extends CRM_Upgrade_Incremental_B * (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', '$display_name', 'contact.display_name', $rev + ); + } // public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) { // return TRUE; diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php index 0777030be0..72582c6c93 100644 --- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -1,5 +1,7 @@ setValues(['msg_html' => '{$display_name}'])->addWhere( + 'workflow_name', '=', 'contribution_invoice_receipt' + )->execute(); + $upgrader = new CRM_Upgrade_Incremental_MessageTemplates('5.41.0'); + $messages = $upgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$display_name', 'contact.display_name'); + $this->assertEquals('Please review your contribution_invoice_receipt message template and remove references to the token {$display_name} as it has been replaced by {contact.display_name}', $messages); + $upgrader->replaceTokenInTemplate('contribution_invoice_receipt', '$display_name', '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', '$display_name', 'contact.display_name'); + $this->assertEquals('', $messages); + $this->revertTemplateToReservedTemplate('contribution_invoice_receipt'); + } + /** * Test message upgrade process only edits the default if the template is customised. */