Skip to content

Commit d80399b

Browse files
committed
#11125 Add migration script to update email template variables
1 parent 8d8bcd1 commit d80399b

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* @file classes/migration/upgrade/v3_5_0/I11125_UpdateEmailTemplateVariables.php
5+
*
6+
* Copyright (c) 2025 Simon Fraser University
7+
* Copyright (c) 2025 John Willinsky
8+
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
9+
*
10+
* @class I11125_UpdateEmailTemplateVariables
11+
*
12+
* @brief Migration to update Email Template variable names
13+
*/
14+
15+
namespace PKP\migration\upgrade\v3_5_0;
16+
17+
use Illuminate\Support\Facades\DB;
18+
use PKP\install\DowngradeNotSupportedException;
19+
20+
abstract class I11125_UpdateEmailTemplateVariables extends \PKP\migration\Migration
21+
{
22+
public function up(): void
23+
{
24+
// Update template variables
25+
$this->renameTemplateVariables($this->oldToNewVariablesMap());
26+
}
27+
28+
public function down(): void
29+
{
30+
throw new DowngradeNotSupportedException();
31+
}
32+
33+
/**
34+
* Replaces email template variables in templates' subject and body
35+
*/
36+
protected function renameTemplateVariables(array $oldNewVariablesMap): void
37+
{
38+
foreach ($oldNewVariablesMap as $emailKey => $variablesMap) {
39+
$settingsTableReplaceExpression = 'setting_value';
40+
$defaultTableBodyReplaceExpression = 'body';
41+
$defaultTableSubjectReplaceExpression = 'subject';
42+
43+
foreach ($variablesMap as $oldName => $newName) {
44+
$existingVariable = "'{\${$oldName}}'";
45+
$replacementsVariable = "'{\${$newName}}'";
46+
47+
$settingsTableReplaceExpression = "REPLACE({$settingsTableReplaceExpression}, {$existingVariable}, {$replacementsVariable})";
48+
$defaultTableBodyReplaceExpression = "REPLACE({$defaultTableBodyReplaceExpression}, {$existingVariable}, {$replacementsVariable})";
49+
$defaultTableSubjectReplaceExpression = "REPLACE({$defaultTableSubjectReplaceExpression}, {$existingVariable}, {$replacementsVariable})";
50+
}
51+
52+
// Default templates SQL
53+
$defaultTemplatesSql = "
54+
UPDATE email_templates_default_data
55+
SET
56+
subject = {$defaultTableSubjectReplaceExpression},
57+
body = {$defaultTableBodyReplaceExpression}
58+
WHERE email_key = ?
59+
";
60+
61+
// Custom templates SQL
62+
$customTemplatesSql = "
63+
UPDATE email_templates_settings
64+
SET setting_value = {$settingsTableReplaceExpression}
65+
WHERE email_id IN (
66+
SELECT email_id
67+
FROM email_templates
68+
WHERE email_key = ?
69+
)";
70+
71+
DB::update($defaultTemplatesSql, [$emailKey]);
72+
DB::update($customTemplatesSql, [$emailKey]);
73+
}
74+
}
75+
76+
/**
77+
* @return array [email_key => [old_variable => new_variable]]
78+
*/
79+
abstract protected function oldToNewVariablesMap(): array;
80+
}

0 commit comments

Comments
 (0)