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 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' => '']); } } }