diff --git a/api/v1/emailTemplates/PKPEmailTemplateController.php b/api/v1/emailTemplates/PKPEmailTemplateController.php index 3e83f21461a..723d5445d81 100644 --- a/api/v1/emailTemplates/PKPEmailTemplateController.php +++ b/api/v1/emailTemplates/PKPEmailTemplateController.php @@ -290,7 +290,7 @@ public function delete(Request $illuminateRequest): JsonResponse $props = Repo::emailTemplate()->getSchemaMap()->map($emailTemplate); Repo::emailTemplate()->delete($emailTemplate); - + Repo::emailTemplate()->deleteTemplateGroupAccess($requestContext->getId(), [$illuminateRequest->route('key')]); return response()->json($props, Response::HTTP_OK); } diff --git a/classes/emailTemplate/DAO.php b/classes/emailTemplate/DAO.php index 12bbfebe3f7..469ab30354e 100644 --- a/classes/emailTemplate/DAO.php +++ b/classes/emailTemplate/DAO.php @@ -360,34 +360,6 @@ 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 * @@ -494,4 +466,37 @@ protected function getUniqueKey(EmailTemplate $emailTemplate): string return $key; } + + + /** + * Sets email template's unrestricted status to their defaults + */ + public function setTemplateDefaultUnrestirctedSetting(int $contextId, ?array $emailKeys = null) + { + $xmlDao = new XMLDAO(); + $data = $xmlDao->parseStruct($this->getMainEmailTemplatesFilename(), ['email']); + + if (!isset($data['email'])) { + return false; + } + + foreach ($data['email'] as $entry) { + $attrs = $entry['attributes']; + + if ($emailKeys !== null && !in_array($attrs['key'], $emailKeys)) { + continue; + } + + // 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.'); + } + + Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$isUnrestricted, $contextId); + } + + return true; + } } diff --git a/classes/emailTemplate/Repository.php b/classes/emailTemplate/Repository.php index 746cc5e7038..373cad7d150 100644 --- a/classes/emailTemplate/Repository.php +++ b/classes/emailTemplate/Repository.php @@ -13,6 +13,7 @@ namespace PKP\emailTemplate; +use APP\core\Application; use APP\emailTemplate\DAO; use APP\facades\Repo; use Illuminate\Support\Collection; @@ -195,10 +196,11 @@ public function delete(EmailTemplate $emailTemplate) /** * Delete a collection of email templates */ - public function deleteMany(Collector $collector) + public function deleteMany(Collector $collector): void { foreach ($collector->getMany() as $emailTemplate) { $this->delete($emailTemplate); + $this->deleteTemplateGroupAccess(Application::get()->getRequest()->getContext()->getId(), $emailTemplate); } } @@ -217,11 +219,14 @@ public function restoreDefaults($contextId): array ->getMany(); $deletedKeys = []; - $results->each(function ($emailTemplate) use ($deletedKeys) { + $results->each(function ($emailTemplate) use ($contextId, &$deletedKeys) { $deletedKeys[] = $emailTemplate->getData('key'); $this->delete($emailTemplate); }); + + $this->dao->installAlternateEmailTemplates($contextId); + Repo::emailTemplate()->restoreTemplateUserGroupAccess($contextId, $deletedKeys); Hook::call('EmailTemplate::restoreDefaults', [&$deletedKeys, $contextId]); return $deletedKeys; } @@ -366,4 +371,24 @@ public function markTemplateAsUnrestricted(string $emailKey, bool $isUnrestricte ->delete(); } } + + + /** + * Deletes all user group access for an email + */ + public function deleteTemplateGroupAccess(int $contextId, array $emailKey): void + { + EmailTemplateAccessGroup::where('context_id', $contextId)->whereIn('email_key', $emailKey)->delete(); + } + + /** + * Deletes all user group access for an email template and set unrestricted to their default + */ + public function restoreTemplateUserGroupAccess(int $contextId, array $emailKeys) + { + if (!empty($emailKeys)) { + $this->deleteTemplateGroupAccess($contextId, $emailKeys); + return $this->dao->setTemplateDefaultUnrestirctedSetting($contextId, $emailKeys); + } + } } diff --git a/classes/mail/Repository.php b/classes/mail/Repository.php index c6b199e6c1e..71b193cb226 100644 --- a/classes/mail/Repository.php +++ b/classes/mail/Repository.php @@ -28,6 +28,7 @@ use PKP\mail\mailables\SubmissionSavedForLater; use PKP\mail\traits\Configurable; use PKP\plugins\Hook; +use PKP\userGroup\UserGroup; class Repository { @@ -101,6 +102,14 @@ public function summarizeMailable(string $class): array $dataDescriptions = $class::getDataDescriptions(); ksort($dataDescriptions); + $userGroups = collect(); + Repo::userGroup()->getCollector() + ->filterByContextIds([Application::get()->getRequest()->getContext()->getId()]) + ->getMany()->each(fn (UserGroup $group) => $userGroups->add([ + 'id' => $group->getId(), + 'name' => $group->getLocalizedName() + ])); + return [ '_href' => Application::get()->getRequest()->getDispatcher()->url( Application::get()->getRequest(), @@ -117,6 +126,7 @@ public function summarizeMailable(string $class): array 'supportsTemplates' => $class::getSupportsTemplates(), 'toRoleIds' => $class::getToRoleIds(), 'canAssignUserGroupToTemplates' => $this->isGroupsAssignableToTemplates($class), + 'assignableTemplateUserGroups' => $userGroups ]; } diff --git a/classes/services/PKPContextService.php b/classes/services/PKPContextService.php index 6a24bd4278d..7a0ceb223c6 100644 --- a/classes/services/PKPContextService.php +++ b/classes/services/PKPContextService.php @@ -561,7 +561,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()); + Repo::emailTemplate()->dao->setTemplateDefaultUnrestirctedSetting($context->getId()); // Load all plugins so they can hook in and add their installation settings PluginRegistry::loadAllPlugins();