|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file classes/migration/upgrade/v3_4_0/I11125_UpdateEmailTemplateVariables.php |
| 5 | + * |
| 6 | + * Copyright (c) 2014-2025 Simon Fraser University |
| 7 | + * Copyright (c) 2000-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_4_0; |
| 16 | + |
| 17 | +use Illuminate\Support\Facades\DB; |
| 18 | + |
| 19 | +abstract class I11125_UpdateEmailTemplateVariables extends \PKP\migration\Migration |
| 20 | +{ |
| 21 | + public function up(): void |
| 22 | + { |
| 23 | + // Update template variables |
| 24 | + $this->renameTemplateVariables($this->oldToNewVariablesMap()); |
| 25 | + } |
| 26 | + |
| 27 | + public function down(): void |
| 28 | + { |
| 29 | + $newToOldVariableMap = array_map(function ($variablesMap) { |
| 30 | + return array_flip($variablesMap); |
| 31 | + }, $this->oldToNewVariablesMap()); |
| 32 | + |
| 33 | + $this->renameTemplateVariables($newToOldVariableMap); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Replaces email template variables in templates' subject and body |
| 38 | + */ |
| 39 | + protected function renameTemplateVariables(array $oldNewVariablesMap): void |
| 40 | + { |
| 41 | + foreach ($oldNewVariablesMap as $emailKey => $variablesMap) { |
| 42 | + $existingVariables = []; |
| 43 | + $replacementsVariables = []; |
| 44 | + |
| 45 | + foreach ($variablesMap as $key => $value) { |
| 46 | + $existingVariables[] = '/\{\$' . $key . '\}/'; |
| 47 | + $replacementsVariables[] = '{$' . $value . '}'; |
| 48 | + } |
| 49 | + |
| 50 | + // Default templates |
| 51 | + $data = DB::table('email_templates_default_data')->where('email_key', $emailKey)->get(); |
| 52 | + |
| 53 | + $data->each(function (object $entry) use ($existingVariables, $replacementsVariables) { |
| 54 | + $subject = preg_replace($existingVariables, $replacementsVariables, $entry->subject); |
| 55 | + $body = preg_replace($existingVariables, $replacementsVariables, $entry->body); |
| 56 | + DB::table('email_templates_default_data') |
| 57 | + ->where('email_key', $entry->{'email_key'}) |
| 58 | + ->where('locale', $entry->{'locale'}) |
| 59 | + ->update(['subject' => $subject, 'body' => $body]); |
| 60 | + }); |
| 61 | + |
| 62 | + // Custom templates |
| 63 | + $customData = DB::table('email_templates')->where('email_key', $emailKey)->get(); |
| 64 | + $customData->each(function (object $customEntry) use ($existingVariables, $replacementsVariables) { |
| 65 | + $emailSettingsRows = DB::table('email_templates_settings')->where('email_id', $customEntry->{'email_id'})->get(); |
| 66 | + foreach ($emailSettingsRows as $emailSettingsRow) { |
| 67 | + $value = preg_replace($existingVariables, $replacementsVariables, $emailSettingsRow->{'setting_value'}); |
| 68 | + DB::table('email_templates_settings') |
| 69 | + ->where('email_id', $emailSettingsRow->{'email_id'}) |
| 70 | + ->where('locale', $emailSettingsRow->{'locale'}) |
| 71 | + ->where('setting_name', $emailSettingsRow->{'setting_name'}) |
| 72 | + ->update(['setting_value' => $value]); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array [email_key => [old_variable => new_variable]] |
| 80 | + */ |
| 81 | + abstract protected function oldToNewVariablesMap(): array; |
| 82 | +} |
0 commit comments