Skip to content

Commit

Permalink
#10571 Remove unnecessary email form components and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Nov 25, 2024
1 parent 098ea45 commit a5f43dc
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 176 deletions.
15 changes: 11 additions & 4 deletions api/v1/emailTemplates/PKPEmailTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function add(Request $illuminateRequest): JsonResponse
$emailTemplate = Repo::emailTemplate()->newDataObject($params);
Repo::emailTemplate()->add($emailTemplate);

Repo::emailTemplate()->setEmailTemplateAccess($emailTemplate, $requestContext->getId(), $params['userGroupIds'], $params['isUnrestricted']);
Repo::emailTemplate()->setEmailTemplateAccess($emailTemplate, $requestContext->getId(), $params['assignedUserGroupIds'], $params['isUnrestricted']);

$emailTemplate = Repo::emailTemplate()->getByKey($emailTemplate->getData('contextId'), $emailTemplate->getData('key'));

Expand Down Expand Up @@ -247,7 +247,7 @@ public function edit(Request $illuminateRequest): JsonResponse

// If the user submitted an empty list (meaning all user groups were unchecked), the empty array is converted to null in the request's data.
// Convert back to an empty array.
$params['userGroupIds'] = $params['userGroupIds'] ?: [];
$params['assignedUserGroupIds'] = $params['assignedUserGroupIds'] ?: [];

$errors = Repo::emailTemplate()->validate(
$emailTemplate,
Expand All @@ -260,7 +260,7 @@ public function edit(Request $illuminateRequest): JsonResponse
}

Repo::emailTemplate()->edit($emailTemplate, $params);
Repo::emailTemplate()->setEmailTemplateAccess($emailTemplate, $requestContext->getId(), $params['userGroupIds'], $params['isUnrestricted']);
Repo::emailTemplate()->setEmailTemplateAccess($emailTemplate, $requestContext->getId(), $params['assignedUserGroupIds'], $params['isUnrestricted']);

$emailTemplate = Repo::emailTemplate()->getByKey(
// context ID is null if edited for the first time
Expand Down Expand Up @@ -290,7 +290,14 @@ public function delete(Request $illuminateRequest): JsonResponse

$props = Repo::emailTemplate()->getSchemaMap()->map($emailTemplate);
Repo::emailTemplate()->delete($emailTemplate);
Repo::emailTemplate()->deleteTemplateGroupAccess($requestContext->getId(), [$illuminateRequest->route('key')]);

// Default templates are not deleted - instead, their body and subject fields are reset.
// Only delete access group data for custom templates.
// Custom templates will have an alternateTo (which is the default)
if($emailTemplate->getData('alternateTo')) {
Repo::emailTemplate()->deleteTemplateGroupAccess($requestContext->getId(), [$illuminateRequest->route('key')]);
}

return response()->json($props, Response::HTTP_OK);
}

Expand Down
33 changes: 0 additions & 33 deletions classes/components/forms/FieldEmailTemplateUnrestricted.php

This file was deleted.

33 changes: 0 additions & 33 deletions classes/components/forms/FieldEmailTemplateUserGroupSettings.php

This file was deleted.

32 changes: 25 additions & 7 deletions classes/components/forms/emailTemplate/EmailTemplateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

namespace PKP\components\forms\emailTemplate;

use PKP\components\forms\FieldEmailTemplateUnrestricted;
use PKP\components\forms\FieldEmailTemplateUserGroupSettings;
use APP\core\Application;
use APP\facades\Repo;
use PKP\components\forms\FieldOptions;
use PKP\components\forms\FieldPreparedContent;
use PKP\components\forms\FieldText;
use PKP\components\forms\FormComponent;
use PKP\userGroup\UserGroup;

class EmailTemplateForm extends FormComponent
{
Expand All @@ -32,6 +34,14 @@ public function __construct(string $action, array $locales)
$this->method = 'POST';
$this->locales = $locales;

$userGroups = collect();
Repo::userGroup()->getCollector()
->filterByContextIds([Application::get()->getRequest()->getContext()->getId()])
->getMany()->each(fn (UserGroup $group) => $userGroups->add([
'value' => $group->getId(),
'label' => $group->getLocalizedName()
]));

$this->addField(new FieldText('name', [
'label' => __('common.name'),
'description' => __('manager.emailTemplate.name.description'),
Expand All @@ -48,14 +58,22 @@ public function __construct(string $action, array $locales)
'isMultilingual' => true,
'toolbar' => 'bold italic superscript subscript | link | blockquote bullist numlist',
'plugins' => 'paste,link,lists',
]))->addField(new FieldEmailTemplateUnrestricted('isUnrestricted', [
'type' => 'checkbox',
'label' => __('admin.workflow.email.userGroup.assign.unrestricted'),
'subNote' => __('admin.workflow.email.userGroup.unrestricted.template.note')
]))
->addField(new FieldEmailTemplateUserGroupSettings('userGroupIds', [
->addField(new FieldOptions('isUnrestricted', [
'label' => __('admin.workflow.email.userGroup.assign.unrestricted'),
'groupId' => 'isUnrestricted',
'description' => __('admin.workflow.email.userGroup.unrestricted.template.note'),
'type' => 'checkbox',
'options' => [
['value' => true, 'label' => __('admin.workflow.email.userGroup.assign.unrestricted')],
],
'value' => true
]))
->addField(new FieldOptions('assignedUserGroupIds', [
'label' => __('admin.workflow.email.userGroup.allowed'),
'type' => 'checkbox',
'value' => [],
'options' => $userGroups
]));
}
}
6 changes: 3 additions & 3 deletions classes/decision/steps/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ protected function getEmailTemplates(): array
->getCollector($context->getId())
->alternateTo([$this->mailable::getEmailTemplateKey()])
->getMany()
->each(function (EmailTemplate $e) use ($context, $request, $emailTemplates) {
if (Repo::emailTemplate()->isTemplateAccessibleToUser($request->getUser(), $e, $context->getId())) {
$emailTemplates->add($e);
->each(function (EmailTemplate $template) use ($context, $request, $emailTemplates) {
if (Repo::emailTemplate()->isTemplateAccessibleToUser($request->getUser(), $template, $context->getId())) {
$emailTemplates->add($template);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ 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.
* @param bool $recordTemplateGroupAccess - If true, records the templates as unrestricted. For versions 3.6 or higher, this value should be set to true when calling `installEmailTemplates`.
* 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.
*
Expand Down
30 changes: 27 additions & 3 deletions classes/emailTemplate/EmailTemplateAccessGroup.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
<?php

/**
* @file classes/emailTemplate/EmailTemplateAccessGroup.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class EmailTemplateAccessGroup
*
* @ingroup emailTemplate
*
* @brief Eloquent model for email template user group access
*/

namespace PKP\emailTemplate;

use Eloquence\Behaviours\HasCamelCasing;
use Eloquence\Database\Model;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class EmailTemplateAccessGroup extends Model
{
use HasCamelCasing;

public $timestamps = false;
protected $primaryKey = 'email_template_user_group_access_id';
protected $table = 'email_template_user_group_access';
protected $fillable = ['userGroupId', 'contextId','emailKey'];
protected $fillable = ['userGroupId', 'contextId', 'emailKey'];


/**
* Scope a query to only include email template access records for email templates with specific keys.
*/
public function scopeWithEmailKey(Builder $query, ?array $keys): Builder
{
return $query->when(!empty($keys), function ($query) use ($keys) {
return $query->whereIn('email_key', $keys);
});
}

/**
* Scope a query to only include email template access records that are related to a specific context ID.
*/
public function scopeWithContextId(Builder $query, int $contextId): Builder
{
return $query->where('context_id', $contextId);
}

/**
* Scope a query to only include email template access records for specific user group IDs.
*/
public function scopeWithGroupIds(Builder $query, array $ids): Builder
{
return $query->whereIn('user_group_id', $ids);
Expand Down
30 changes: 13 additions & 17 deletions classes/emailTemplate/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ public function validate(?EmailTemplate $object, array $props, Context $context)
});
}

// If groupIds were passed to limit email access, check that the user groups exists within the context
if (isset($props['userGroupIds'])) {
// If assignedUserGroupIds were passed to limit email access, check that the user groups exists within the context
if (isset($props['assignedUserGroupIds'])) {
$validator->after(function () use ($validator, $props, $context) {
$existingGroupIds = Repo::userGroup()->getCollector()
->filterByContextIds([$context->getId()])
->filterByUserGroupIds($props['userGroupIds'])->getIds()->toArray();
->filterByUserGroupIds($props['assignedUserGroupIds'])->getIds()->toArray();

if (!empty(array_diff($existingGroupIds, $props['userGroupIds']))) {
$validator->errors()->add('userGroupIds', __('api.emailTemplates.404.userGroupIds'));
if (!empty(array_diff($existingGroupIds, $props['assignedUserGroupIds']))) {
$validator->errors()->add('assignedUserGroupIds', __('api.emailTemplates.404.userGroupIds'));
}
});
}
Expand Down Expand Up @@ -224,16 +224,14 @@ public function restoreDefaults($contextId): array
$this->delete($emailTemplate);
});


$this->dao->installAlternateEmailTemplates($contextId);
Repo::emailTemplate()->restoreTemplateUserGroupAccess($contextId, $deletedKeys);
Hook::call('EmailTemplate::restoreDefaults', [&$deletedKeys, $contextId]);
return $deletedKeys;
}


/***
* Gets the IDs of the user groups assigned to an email template
* Gets the IDs of the user groups assigned to an email template.
*/
public function getAssignedGroupsIds(string $templateKey, int $contextId): array
{
Expand All @@ -246,7 +244,7 @@ public function getAssignedGroupsIds(string $templateKey, int $contextId): array
}

/***
* Checks if an Email Template is unrestricted
* Checks if an Email Template is unrestricted.
*/
public function isTemplateUnrestricted(string $templateKey, int $contextId): bool
{
Expand All @@ -256,9 +254,8 @@ public function isTemplateUnrestricted(string $templateKey, int $contextId): boo
->first();
}


/**
* Checks if an email template is accessible to a user. A template is accessible if it is assigned to a user group that the user belongs to or if the template is unrestricted
* Checks if an email template is accessible to a user. A template is accessible if it is assigned to a user group that the user belongs to or if the template is unrestricted.
*/
public function isTemplateAccessibleToUser(User $user, EmailTemplate $template, int $contextId): bool
{
Expand Down Expand Up @@ -300,7 +297,7 @@ public function filterTemplatesByUserAccess(Enumerable $templates, User $user, i
}

/***
* Internal method used to assign user group IDs to an email template
* Internal method used to assign user group IDs to an email template.
*/
private function updateTemplateAccessGroups(EmailTemplate $emailTemplate, array $newUserGroupIds, int $contextId): void
{
Expand All @@ -327,7 +324,8 @@ private function updateTemplateAccessGroups(EmailTemplate $emailTemplate, array
}

/**
* Pass empty array in $userGroupIds to delete all existing user groups for a template
* Sets the restrictions for an email template.
* Pass empty array in $userGroupIds to delete all existing user groups for a template.
*/
public function setEmailTemplateAccess(EmailTemplate $emailTemplate, int $contextId, ?array $userGroupIds, ?bool $isUnrestricted): void
{
Expand All @@ -340,7 +338,6 @@ public function setEmailTemplateAccess(EmailTemplate $emailTemplate, int $contex
}
}


/**
* Mark an email template as unrestricted or not.
* An unrestricted email template is available to all user groups.
Expand Down Expand Up @@ -372,17 +369,16 @@ public function markTemplateAsUnrestricted(string $emailKey, bool $isUnrestricte
}
}


/**
* Deletes all user group access for an email
* 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
* Deletes all user group access for an email template and set unrestricted to their default.
*/
public function restoreTemplateUserGroupAccess(int $contextId, array $emailKeys)
{
Expand Down
Loading

0 comments on commit a5f43dc

Please sign in to comment.