forked from sean-xiao-zhao7/pkp-orcidprofile
-
Notifications
You must be signed in to change notification settings - Fork 51
/
OrcidProfileEmailDataMigration.php
125 lines (104 loc) · 3.88 KB
/
OrcidProfileEmailDataMigration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @file OrcidProfileDataMigration.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2000-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class OrcidProfileEmailDataMigration
*
* @brief Migrations for the plugin's email templates
*/
namespace APP\plugins\generic\orcidProfile;
use Exception;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use PKP\db\DAORegistry;
use PKP\db\XMLDAO;
use PKP\facades\Locale;
use PKP\install\Installer;
class OrcidProfileEmailDataMigration extends Migration
{
protected Installer $installer;
private OrcidProfilePlugin $plugin;
public function __construct(Installer $installer, OrcidProfilePlugin $plugin)
{
$this->installer = $installer;
$this->plugin = $plugin;
}
/**
* Run the migrations.
*/
public function up(): void
{
if ($this->installer) {
$version = $this->installer->getCurrentVersion();
if (in_array($version->getProduct(), ['ojs2', 'ops'])) {
if ($version->compare('3.4.0.0') < 0
&& $this->installer->getNewVersion()->compare('3.4.0.0') >= 0) {
$this->migrateEmailTemplatesName();
}
} elseif ($version->getProduct() == 'orcidProfile') {
$thresholdVersion = '1.3.4.1';
$versionDao = DAORegistry::getDAO('VersionDAO');
$installedPluginVersion = $versionDao->getCurrentVersion();
if ($version->compare($thresholdVersion) > 0) {
// initial installation of the plugin or an upgrade from a plugin below threshold level.
if (!$installedPluginVersion or $installedPluginVersion < $thresholdVersion) {
$this->migrateEmailTemplatesName();
}
}
}
}
}
/**
* Adds name to the email template
* Execute only during upgrade to 3.4
*/
public function migrateEmailTemplatesName(): void
{
$xmlDao = new XMLDAO();
$data = $xmlDao->parseStruct($this->plugin->getInstallEmailTemplatesFile(), ['email']);
if (!isset($data['email'])) {
throw new Exception('Unable to find <email> entries in ' . $this->plugin->getInstallEmailTemplatesFile());
}
$locales = json_decode(DB::table('site')->value('installed_locales'));
foreach ($data['email'] as $entry) {
$attrs = $entry['attributes'];
$name = $attrs['name'] ?? null;
$emailKey = $attrs['key'];
if (!$name) {
throw new Exception('Failed to install email template ' . $emailKey . ' due to missing name');
}
$previous = Locale::getMissingKeyHandler();
Locale::setMissingKeyHandler(fn (string $key): string => '');
foreach ($locales as $locale) {
$translatedName = $name ? __($name, [], $locale) : $attrs['key'];
DB::table('email_templates_default_data')
->where('email_key', $emailKey)
->where('locale', $locale)
->update(['name' => $translatedName]);
}
Locale::setMissingKeyHandler($previous);
}
}
/**
* Reverse the migrations
*/
public function down(): void
{
$xmlDao = new XMLDAO();
$data = $xmlDao->parseStruct($this->plugin->getInstallEmailTemplatesFile(), ['email']);
if (!isset($data['email'])) {
return;
}
foreach ($data['email'] as $entry) {
$attrs = $entry['attributes'];
$emailKey = $attrs['key'];
DB::table('email_templates_default_data')
->where('email_key', $emailKey)
->update(['name' => '']);
}
}
}