Skip to content

pkp/pkp-lib#11125 Fix: Copyediting Stage, Discussion tags do not populate [main] #11180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* @file classes/migration/upgrade/v3_4_0/I11125_UpdateEmailTemplateVariables.php
*
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2000-2025 John Willinsky
Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2000-2025 John Willinsky
* Copyright (c) 2025 Simon Fraser University
* Copyright (c) 2025 John Willinsky

* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I11125_UpdateEmailTemplateVariables
*
* @brief Migration to update Email Template variable names
*/

namespace PKP\migration\upgrade\v3_4_0;

use Illuminate\Support\Facades\DB;

abstract class I11125_UpdateEmailTemplateVariables extends \PKP\migration\Migration
{
public function up(): void
{
// Update template variables
$this->renameTemplateVariables($this->oldToNewVariablesMap());
}

public function down(): void
{
$newToOldVariableMap = array_map(function ($variablesMap) {
return array_flip($variablesMap);
}, $this->oldToNewVariablesMap());

$this->renameTemplateVariables($newToOldVariableMap);
Comment on lines +29 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note... At the moment, given the number of migrations without a down(), I think it's a bit pointless to code it, we need to have a well defined policy. I had started a discussion here: #9061

}

/**
* Replaces email template variables in templates' subject and body
*/
protected function renameTemplateVariables(array $oldNewVariablesMap): void
{
foreach ($oldNewVariablesMap as $emailKey => $variablesMap) {
$existingVariables = [];
$replacementsVariables = [];

foreach ($variablesMap as $key => $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
foreach ($variablesMap as $key => $value) {
foreach ($variablesMap as $oldName => $newName) {

$existingVariables[] = '/\{\$' . $key . '\}/';
$replacementsVariables[] = '{$' . $value . '}';
}

// Default templates
$data = DB::table('email_templates_default_data')->where('email_key', $emailKey)->get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how many rows this and the next table might have (some installations out there have many journals), but such simple string replacements can be done with SQL, then you won't need to download all the record set and the operation should complete faster.


$data->each(function (object $entry) use ($existingVariables, $replacementsVariables) {
$subject = preg_replace($existingVariables, $replacementsVariables, $entry->subject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's a simple string, then you could use the str_replace() instead of a regular expression.

$body = preg_replace($existingVariables, $replacementsVariables, $entry->body);
DB::table('email_templates_default_data')
->where('email_key', $entry->{'email_key'})
->where('locale', $entry->{'locale'})
->update(['subject' => $subject, 'body' => $body]);
});

// Custom templates
$customData = DB::table('email_templates')->where('email_key', $emailKey)->get();
$customData->each(function (object $customEntry) use ($existingVariables, $replacementsVariables) {
$emailSettingsRows = DB::table('email_templates_settings')->where('email_id', $customEntry->{'email_id'})->get();
foreach ($emailSettingsRows as $emailSettingsRow) {
$value = preg_replace($existingVariables, $replacementsVariables, $emailSettingsRow->{'setting_value'});
DB::table('email_templates_settings')
->where('email_id', $emailSettingsRow->{'email_id'})
->where('locale', $emailSettingsRow->{'locale'})
->where('setting_name', $emailSettingsRow->{'setting_name'})
->update(['setting_value' => $value]);
}
});
}
}

/**
* @return array [email_key => [old_variable => new_variable]]
*/
abstract protected function oldToNewVariablesMap(): array;
}