From 055fce992cef55f5c092855c78b50ccf502008e3 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Mon, 22 Aug 2022 16:09:31 -0400 Subject: [PATCH] update all is_reserved templates each time --- CRM/Upgrade/Form.php | 7 ++ CRM/Upgrade/Incremental/MessageTemplates.php | 77 ++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index 7f4903ec44..9839582e16 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -581,6 +581,13 @@ SET version = '$version' $queue->createItem($task, ['weight' => 0]); } + $task = new CRM_Queue_Task( + ['CRM_Upgrade_Incremental_MessageTemplates', 'updateReservedAndMaybeDefaultTemplates'], + [], + "Update all reserved message templates" + ); + $queue->createItem($task, ['weight' => 990]); + $task = new CRM_Queue_Task( ['CRM_Upgrade_Form', 'doCoreFinish'], [$rev, $latestVer, $latestVer, $postUpgradeMessageFile], diff --git a/CRM/Upgrade/Incremental/MessageTemplates.php b/CRM/Upgrade/Incremental/MessageTemplates.php index e85585a596..dca70ec0ca 100644 --- a/CRM/Upgrade/Incremental/MessageTemplates.php +++ b/CRM/Upgrade/Incremental/MessageTemplates.php @@ -528,4 +528,81 @@ class CRM_Upgrade_Incremental_MessageTemplates { } } + /** + * Make sure *all* reserved ones get updated. Might be inefficient because we either already updated or + * there were no changes to a given template, but there's only about 30. + * This runs near the final steps of the upgrade, otherwise the earlier checks that run during the + * individual revisions wouldn't accurately be checking against the right is_reserved version to see + * if it had changed. + * @todo - do we still need those earlier per-version runs? e.g. the token replacement functions should still work as-is? + * + * @param CRM_Queue_TaskContext $ctx + * @return bool + */ + public static function updateReservedAndMaybeDefaultTemplates(CRM_Queue_TaskContext $ctx): bool { + // This has to come first otherwise it would be checking against is_reserved we already updated. + $uneditedTemplates = self::getUneditedTemplates(); + + $dao = CRM_Core_DAO::executeQuery("SELECT id, workflow_id, workflow_name FROM civicrm_msg_template WHERE is_reserved=1"); + while ($dao->fetch()) { + foreach (['html', 'text', 'subject'] as $type) { + $content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $dao->workflow_name . '_' . $type . '.tpl')); + if ($content) { + CRM_Core_DAO::executeQuery( + "UPDATE civicrm_msg_template SET msg_{$type} = %1 WHERE id = %2", [ + 1 => [$content, 'String'], + 2 => [$dao->id, 'Integer'], + ] + ); + + // If the same workflow_id and type appears in our list of unedited templates, update it too. + // There's probably a more efficient way to look this up but simple for now. + foreach ($uneditedTemplates as $uneditedTemplate) { + if ($uneditedTemplate['workflow_id'] === $dao->workflow_id && $uneditedTemplate['type'] === $type) { + CRM_Core_DAO::executeQuery( + "UPDATE civicrm_msg_template SET msg_{$type} = %1 WHERE id = %2", [ + 1 => [$content, 'String'], + 2 => [$uneditedTemplate['id'], 'Integer'], + ] + ); + break; + } + } + } + } + } + return TRUE; + } + + /** + * Get all the is_default templates that are the unchanged from their is_reserved counterpart, which + * at the time this runs was the version shipped with core when it was last changed. + * + * @todo have pulled this out since want to re-use it to get the preUpgrade message right. Currently + * it always tells you all the ones in the hardcoded per-version list have been customized. + * + * @return array + */ + public static function getUneditedTemplates(): array { + $templates = []; + foreach (['html', 'text', 'subject'] as $type) { + $dao = CRM_Core_DAO::executeQuery(" + SELECT default_template.id, default_template.workflow_id FROM civicrm_msg_template reserved + LEFT JOIN civicrm_msg_template default_template + ON reserved.workflow_id = default_template.workflow_id + WHERE reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id + AND reserved.msg_{$type} = default_template.msg_{$type} + "); + while ($dao->fetch()) { + // Note the same id can appear multiple times, e.g. you might change the html but not the subject. + $templates[] = [ + 'id' => $dao->id, + 'type' => $type, + 'workflow_id' => $dao->workflow_id, + ]; + } + } + return $templates; + } + } -- 2.25.1