Skip to content

Commit

Permalink
update all is_reserved templates each time
Browse files Browse the repository at this point in the history
  • Loading branch information
demeritcowboy committed Aug 23, 2022
1 parent 71f0493 commit 7343723
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CRM/Upgrade/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,13 @@ public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFi
$queue->createItem($task, ['weight' => 0]);
}

$task = new CRM_Queue_Task(
['CRM_Upgrade_Incremental_MessageTemplates', 'updateReservedTemplates'],
[$latestVer],
"Update all reserved message templates"
);
$queue->createItem($task, ['weight' => 990]);

$task = new CRM_Queue_Task(
['CRM_Upgrade_Form', 'doCoreFinish'],
[$rev, $latestVer, $latestVer, $postUpgradeMessageFile],
Expand Down
32 changes: 32 additions & 0 deletions CRM/Upgrade/Incremental/MessageTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,38 @@ public function getUpgradeMessages() {
return $messages;
}

/**
* Make sure *all* reserved ones got updated. Might be inefficient because we either already updated or
* there were no changes to a given template, but there's only about 30.
* Note this has to come after the is_default in-use templates are checked to see if they're different
* from the is_reserved version, otherwise that check can't tell which ones the site has changed.
*
* @param CRM_Queue_TaskContext $ctx
* @param string $version
* @return bool
*/
public static function updateReservedTemplates(CRM_Queue_TaskContext $ctx, $version) {
if (version_compare($version, '5.26', '<')) {
// We can't do this before workflow_name was introduced. Strictly speaking we don't care until 5.54.
return TRUE;
}
$dao = CRM_Core_DAO::executeQuery("SELECT 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'],
]
);
}
}
}
return TRUE;
}

/**
* Update message templates.
*/
Expand Down

0 comments on commit 7343723

Please sign in to comment.