From 29666ca3082ceeffdec0fde4e26b2ff471962e78 Mon Sep 17 00:00:00 2001 From: Taslan Graham Date: Wed, 13 Nov 2024 22:24:05 -0500 Subject: [PATCH] pkp/pkp-lib#10571 Make default templates unrestricted on Context creation --- classes/emailTemplate/DAO.php | 28 ++++++++++++++++++++++++++ classes/services/PKPContextService.php | 1 + 2 files changed, 29 insertions(+) diff --git a/classes/emailTemplate/DAO.php b/classes/emailTemplate/DAO.php index 1763e6f7f7c..cf6dce96712 100644 --- a/classes/emailTemplate/DAO.php +++ b/classes/emailTemplate/DAO.php @@ -343,6 +343,34 @@ public function installEmailTemplateLocaleData( return true; } + + /** + * Registers default templates as unrestricted for the specified context ID. + */ + public function registerDefaultTemplatesAsUnrestricted(int $contextId): void + { + $templates = Repo::emailTemplate()->getCollector($contextId)->getMany()->all(); + $keys = array_map(fn (EmailTemplate $template) => $template->getData('key'), $templates); + + $existingUnrestrictedTemplates = EmailTemplateAccessGroup::withContextId($contextId) + ->whereNull('user_group_id') + ->select('email_key') + ->pluck('email_key')->all(); + + // Identify templates to add as unrestricted + $defaultKeysToRegister = array_diff($keys, $existingUnrestrictedTemplates); + + $data = array_map(function ($key) use ($contextId) { + return [ + 'email_key' => $key, + 'context_id' => $contextId, + 'user_group_id' => null + ]; + }, $defaultKeysToRegister); + + EmailTemplateAccessGroup::insert($data); + } + /** * Installs the "extra" email templates for a context * diff --git a/classes/services/PKPContextService.php b/classes/services/PKPContextService.php index cf65fe33eee..9723773c3c0 100644 --- a/classes/services/PKPContextService.php +++ b/classes/services/PKPContextService.php @@ -584,6 +584,7 @@ public function add($context, $request) $navigationMenuDao->installSettings($context->getId(), 'registry/navigationMenus.xml'); Repo::emailTemplate()->dao->installAlternateEmailTemplates($context->getId()); + Repo::emailTemplate()->dao->registerDefaultTemplatesAsUnrestricted($context->getId()); // Load all plugins so they can hook in and add their installation settings PluginRegistry::loadAllPlugins();