From 3cb222d07012c523262ffdaca19e4908c2ff0741 Mon Sep 17 00:00:00 2001 From: Taslan Graham Date: Tue, 19 Nov 2024 17:56:04 -0500 Subject: [PATCH] Add flag for to increase method usage compatibility --- classes/emailTemplate/DAO.php | 27 +++++++++++-------- .../I10403_EmailTemplateUserGroupAccess.php | 1 - classes/plugins/Plugin.php | 2 +- tools/installEmailTemplate.php | 4 ++- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/classes/emailTemplate/DAO.php b/classes/emailTemplate/DAO.php index 7759a287ea8..b863172f6a7 100644 --- a/classes/emailTemplate/DAO.php +++ b/classes/emailTemplate/DAO.php @@ -235,13 +235,16 @@ public function getMainEmailTemplatesFilename() * skipping others * @param bool $skipExisting If true, do not install email templates * that already exist in the database - * + * @param bool $recordTemplateGroupAccess - If true, records the templates as unrestricted. + * By default, it is set to false to ensure compatibility with older processes (e.g., migrations) + * where the `email_template_user_group_access` table may not exist at the time of execution. */ public function installEmailTemplates( string $templatesFile, array $locales = [], ?string $emailKey = null, - bool $skipExisting = false + bool $skipExisting = false, + bool $recordTemplateGroupAccess = false ): bool { $xmlDao = new XMLDAO(); $data = $xmlDao->parseStruct($templatesFile, ['email']); @@ -282,16 +285,18 @@ public function installEmailTemplates( } } - // Default to true if `isUnrestricted` is not set. - $isUnrestricted = $attrs['isUnrestricted'] ?? '1'; + if ($recordTemplateGroupAccess) { + // Default to true if `isUnrestricted` is not set. + $isUnrestricted = $attrs['isUnrestricted'] ?? '1'; - if ($isUnrestricted !== '1' && $isUnrestricted !== '0') { - throw new Exception('Invalid value given for the `isUnrestricted` attribute on the ' . $attrs['key'] . ' template.'); - } + if ($isUnrestricted !== '1' && $isUnrestricted !== '0') { + throw new Exception('Invalid value given for the `isUnrestricted` attribute on the ' . $attrs['key'] . ' template.'); + } - $contextIds = app()->get('context')->getIds(); - foreach ($contextIds as $contextId) { - Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$isUnrestricted, $contextId); + $contextIds = app()->get('context')->getIds(); + foreach ($contextIds as $contextId) { + Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$isUnrestricted, $contextId); + } } } return true; @@ -421,7 +426,7 @@ public function installAlternateEmailTemplates(int $contextId, ?string $emailKey 'Tried to install email template as an alternate to `' . $alternateTo . '`, but no default template exists with this key. Installing ' . $alternateTo . ' email template first', E_USER_WARNING ); - $this->installEmailTemplates(Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(), [], $alternateTo); + $this->installEmailTemplates(Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(), [], $alternateTo, false, true); } DB::table($this->table)->insert([ diff --git a/classes/migration/upgrade/V3_6_0/I10403_EmailTemplateUserGroupAccess.php b/classes/migration/upgrade/V3_6_0/I10403_EmailTemplateUserGroupAccess.php index 2bbe3b1c98a..3189ff423ef 100644 --- a/classes/migration/upgrade/V3_6_0/I10403_EmailTemplateUserGroupAccess.php +++ b/classes/migration/upgrade/V3_6_0/I10403_EmailTemplateUserGroupAccess.php @@ -5,7 +5,6 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; -use PKP\context\Context; use PKP\emailTemplate\EmailTemplateAccessGroup; use PKP\migration\Migration; diff --git a/classes/plugins/Plugin.php b/classes/plugins/Plugin.php index 62818b997c5..2bc36d07dd2 100644 --- a/classes/plugins/Plugin.php +++ b/classes/plugins/Plugin.php @@ -579,7 +579,7 @@ public function installEmailTemplates($hookName, $args) } // Localized data is needed by the email installation $this->addLocaleData(); - $status = Repo::emailTemplate()->dao->installEmailTemplates($this->getInstallEmailTemplatesFile(), $locales, null, true); + $status = Repo::emailTemplate()->dao->installEmailTemplates($this->getInstallEmailTemplatesFile(), $locales, null, true, true); if ($status === false) { // The template file seems to be invalid. diff --git a/tools/installEmailTemplate.php b/tools/installEmailTemplate.php index 633af8971fa..02e03b10188 100644 --- a/tools/installEmailTemplate.php +++ b/tools/installEmailTemplate.php @@ -70,7 +70,9 @@ public function execute() Repo::emailTemplate()->dao->installEmailTemplates( Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(), $locales, - $this->_emailKey + $this->_emailKey, + false, + true ); } }