Skip to content

Commit d2e9586

Browse files
committedNov 25, 2024
#10571 Remove unnecessary email form components and clean up code
1 parent 80068ac commit d2e9586

18 files changed

+125
-176
lines changed
 

‎api/v1/emailTemplates/PKPEmailTemplateController.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function add(Request $illuminateRequest): JsonResponse
205205
$emailTemplate = Repo::emailTemplate()->newDataObject($params);
206206
Repo::emailTemplate()->add($emailTemplate);
207207

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

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

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

248248
// 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.
249249
// Convert back to an empty array.
250-
$params['userGroupIds'] = $params['userGroupIds'] ?: [];
250+
$params['assignedUserGroupIds'] = $params['assignedUserGroupIds'] ?: [];
251251

252252
$errors = Repo::emailTemplate()->validate(
253253
$emailTemplate,
@@ -260,7 +260,7 @@ public function edit(Request $illuminateRequest): JsonResponse
260260
}
261261

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

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

291291
$props = Repo::emailTemplate()->getSchemaMap()->map($emailTemplate);
292292
Repo::emailTemplate()->delete($emailTemplate);
293-
Repo::emailTemplate()->deleteTemplateGroupAccess($requestContext->getId(), [$illuminateRequest->route('key')]);
293+
294+
// Default templates are not deleted - instead, their body and subject fields are reset.
295+
// Only delete access group data for custom templates.
296+
// Custom templates will have an alternateTo (which is the default)
297+
if($emailTemplate->getData('alternateTo')) {
298+
Repo::emailTemplate()->deleteTemplateGroupAccess($requestContext->getId(), [$illuminateRequest->route('key')]);
299+
}
300+
294301
return response()->json($props, Response::HTTP_OK);
295302
}
296303

‎classes/components/forms/FieldEmailTemplateUnrestricted.php

-33
This file was deleted.

‎classes/components/forms/FieldEmailTemplateUserGroupSettings.php

-33
This file was deleted.

‎classes/components/forms/emailTemplate/EmailTemplateForm.php

+25-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
namespace PKP\components\forms\emailTemplate;
1717

18-
use PKP\components\forms\FieldEmailTemplateUnrestricted;
19-
use PKP\components\forms\FieldEmailTemplateUserGroupSettings;
18+
use APP\core\Application;
19+
use APP\facades\Repo;
20+
use PKP\components\forms\FieldOptions;
2021
use PKP\components\forms\FieldPreparedContent;
2122
use PKP\components\forms\FieldText;
2223
use PKP\components\forms\FormComponent;
24+
use PKP\userGroup\UserGroup;
2325

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

37+
$userGroups = collect();
38+
Repo::userGroup()->getCollector()
39+
->filterByContextIds([Application::get()->getRequest()->getContext()->getId()])
40+
->getMany()->each(fn (UserGroup $group) => $userGroups->add([
41+
'value' => $group->getId(),
42+
'label' => $group->getLocalizedName()
43+
]));
44+
3545
$this->addField(new FieldText('name', [
3646
'label' => __('common.name'),
3747
'description' => __('manager.emailTemplate.name.description'),
@@ -48,14 +58,22 @@ public function __construct(string $action, array $locales)
4858
'isMultilingual' => true,
4959
'toolbar' => 'bold italic superscript subscript | link | blockquote bullist numlist',
5060
'plugins' => 'paste,link,lists',
51-
]))->addField(new FieldEmailTemplateUnrestricted('isUnrestricted', [
52-
'type' => 'checkbox',
53-
'label' => __('admin.workflow.email.userGroup.assign.unrestricted'),
54-
'subNote' => __('admin.workflow.email.userGroup.unrestricted.template.note')
5561
]))
56-
->addField(new FieldEmailTemplateUserGroupSettings('userGroupIds', [
62+
->addField(new FieldOptions('isUnrestricted', [
63+
'label' => __('admin.workflow.email.userGroup.assign.unrestricted'),
64+
'groupId' => 'isUnrestricted',
65+
'description' => __('admin.workflow.email.userGroup.unrestricted.template.note'),
5766
'type' => 'checkbox',
67+
'options' => [
68+
['value' => true, 'label' => __('admin.workflow.email.userGroup.assign.unrestricted')],
69+
],
70+
'value' => true
71+
]))
72+
->addField(new FieldOptions('assignedUserGroupIds', [
5873
'label' => __('admin.workflow.email.userGroup.allowed'),
74+
'type' => 'checkbox',
75+
'value' => [],
76+
'options' => $userGroups
5977
]));
6078
}
6179
}

‎classes/decision/steps/Email.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ protected function getEmailTemplates(): array
135135
->getCollector($context->getId())
136136
->alternateTo([$this->mailable::getEmailTemplateKey()])
137137
->getMany()
138-
->each(function (EmailTemplate $e) use ($context, $request, $emailTemplates) {
139-
if (Repo::emailTemplate()->isTemplateAccessibleToUser($request->getUser(), $e, $context->getId())) {
140-
$emailTemplates->add($e);
138+
->each(function (EmailTemplate $template) use ($context, $request, $emailTemplates) {
139+
if (Repo::emailTemplate()->isTemplateAccessibleToUser($request->getUser(), $template, $context->getId())) {
140+
$emailTemplates->add($template);
141141
}
142142
});
143143
}

‎classes/emailTemplate/DAO.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function getMainEmailTemplatesFilename()
235235
* skipping others
236236
* @param bool $skipExisting If true, do not install email templates
237237
* that already exist in the database
238-
* @param bool $recordTemplateGroupAccess - If true, records the templates as unrestricted.
238+
* @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`.
239239
* By default, it is set to false to ensure compatibility with older processes (e.g., migrations)
240240
* where the `email_template_user_group_access` table may not exist at the time of execution.
241241
*

‎classes/emailTemplate/EmailTemplateAccessGroup.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
11
<?php
22

3+
/**
4+
* @file classes/emailTemplate/EmailTemplateAccessGroup.php
5+
*
6+
* Copyright (c) 2014-2024 Simon Fraser University
7+
* Copyright (c) 2000-2024 John Willinsky
8+
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
9+
*
10+
* @class EmailTemplateAccessGroup
11+
*
12+
* @ingroup emailTemplate
13+
*
14+
* @brief Eloquent model for email template user group access
15+
*/
16+
317
namespace PKP\emailTemplate;
418

519
use Eloquence\Behaviours\HasCamelCasing;
6-
use Eloquence\Database\Model;
7-
use Illuminate\Contracts\Database\Eloquent\Builder;
20+
use Illuminate\Database\Eloquent\Builder;
21+
use Illuminate\Database\Eloquent\Model;
822

923
class EmailTemplateAccessGroup extends Model
1024
{
1125
use HasCamelCasing;
26+
1227
public $timestamps = false;
1328
protected $primaryKey = 'email_template_user_group_access_id';
1429
protected $table = 'email_template_user_group_access';
15-
protected $fillable = ['userGroupId', 'contextId','emailKey'];
30+
protected $fillable = ['userGroupId', 'contextId', 'emailKey'];
1631

1732

33+
/**
34+
* Scope a query to only include email template access records for email templates with specific keys.
35+
*/
1836
public function scopeWithEmailKey(Builder $query, ?array $keys): Builder
1937
{
2038
return $query->when(!empty($keys), function ($query) use ($keys) {
2139
return $query->whereIn('email_key', $keys);
2240
});
2341
}
2442

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

51+
/**
52+
* Scope a query to only include email template access records for specific user group IDs.
53+
*/
3054
public function scopeWithGroupIds(Builder $query, array $ids): Builder
3155
{
3256
return $query->whereIn('user_group_id', $ids);

‎classes/emailTemplate/Repository.php

+13-17
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ public function validate(?EmailTemplate $object, array $props, Context $context)
131131
});
132132
}
133133

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

141-
if (!empty(array_diff($existingGroupIds, $props['userGroupIds']))) {
142-
$validator->errors()->add('userGroupIds', __('api.emailTemplates.404.userGroupIds'));
141+
if (!empty(array_diff($existingGroupIds, $props['assignedUserGroupIds']))) {
142+
$validator->errors()->add('assignedUserGroupIds', __('api.emailTemplates.404.userGroupIds'));
143143
}
144144
});
145145
}
@@ -224,16 +224,14 @@ public function restoreDefaults($contextId): array
224224
$this->delete($emailTemplate);
225225
});
226226

227-
228227
$this->dao->installAlternateEmailTemplates($contextId);
229228
Repo::emailTemplate()->restoreTemplateUserGroupAccess($contextId, $deletedKeys);
230229
Hook::call('EmailTemplate::restoreDefaults', [&$deletedKeys, $contextId]);
231230
return $deletedKeys;
232231
}
233232

234-
235233
/***
236-
* Gets the IDs of the user groups assigned to an email template
234+
* Gets the IDs of the user groups assigned to an email template.
237235
*/
238236
public function getAssignedGroupsIds(string $templateKey, int $contextId): array
239237
{
@@ -246,7 +244,7 @@ public function getAssignedGroupsIds(string $templateKey, int $contextId): array
246244
}
247245

248246
/***
249-
* Checks if an Email Template is unrestricted
247+
* Checks if an Email Template is unrestricted.
250248
*/
251249
public function isTemplateUnrestricted(string $templateKey, int $contextId): bool
252250
{
@@ -256,9 +254,8 @@ public function isTemplateUnrestricted(string $templateKey, int $contextId): boo
256254
->first();
257255
}
258256

259-
260257
/**
261-
* 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
258+
* 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.
262259
*/
263260
public function isTemplateAccessibleToUser(User $user, EmailTemplate $template, int $contextId): bool
264261
{
@@ -300,7 +297,7 @@ public function filterTemplatesByUserAccess(Enumerable $templates, User $user, i
300297
}
301298

302299
/***
303-
* Internal method used to assign user group IDs to an email template
300+
* Internal method used to assign user group IDs to an email template.
304301
*/
305302
private function updateTemplateAccessGroups(EmailTemplate $emailTemplate, array $newUserGroupIds, int $contextId): void
306303
{
@@ -327,7 +324,8 @@ private function updateTemplateAccessGroups(EmailTemplate $emailTemplate, array
327324
}
328325

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

343-
344341
/**
345342
* Mark an email template as unrestricted or not.
346343
* An unrestricted email template is available to all user groups.
@@ -372,17 +369,16 @@ public function markTemplateAsUnrestricted(string $emailKey, bool $isUnrestricte
372369
}
373370
}
374371

375-
376372
/**
377-
* Deletes all user group access for an email
373+
* Deletes all user group access for an email.
378374
*/
379375
public function deleteTemplateGroupAccess(int $contextId, array $emailKey): void
380376
{
381377
EmailTemplateAccessGroup::where('context_id', $contextId)->whereIn('email_key', $emailKey)->delete();
382378
}
383379

384380
/**
385-
* Deletes all user group access for an email template and set unrestricted to their default
381+
* Deletes all user group access for an email template and set unrestricted to their default.
386382
*/
387383
public function restoreTemplateUserGroupAccess(int $contextId, array $emailKeys)
388384
{

0 commit comments

Comments
 (0)