Skip to content

Commit 6b7fc8b

Browse files
committed
#11885 Refactor edit task templates API and validation rules
1 parent 25889c2 commit 6b7fc8b

File tree

5 files changed

+41
-103
lines changed

5 files changed

+41
-103
lines changed

api/v1/editTaskTemplates/PKPEditTaskTemplateController.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PKP\security\authorization\CanAccessSettingsPolicy;
3030
use PKP\security\authorization\ContextAccessPolicy;
3131
use PKP\security\Role;
32+
use PKP\editorialTask\enums\EditorialTaskType;
3233
use PKP\API\v1\editTaskTemplates\formRequests\UpdateTaskTemplate;
3334

3435
class PKPEditTaskTemplateController extends PKPBaseController
@@ -144,7 +145,7 @@ public function getMany(Request $request): JsonResponse
144145
case 'type':
145146
if (is_numeric($val)) {
146147
$type = (int) $val;
147-
if ($type === Template::TYPE_DISCUSSION || $type === Template::TYPE_TASK) {
148+
if (in_array($type, [EditorialTaskType::DISCUSSION->value, EditorialTaskType::TASK->value], true)) {
148149
$collector->filterByType($type);
149150
}
150151
}
@@ -186,22 +187,33 @@ public function update(UpdateTaskTemplate $request): JsonResponse
186187
$id = (int) $request->route('templateId');
187188

188189
$template = Template::query()
189-
->where('context_id', $contextId)
190-
->findOrFail($id);
190+
->byContextId($contextId)
191+
->find($id);
192+
193+
if (!$template) {
194+
return response()->json([
195+
'error' => __('api.404.resourceNotFound'),
196+
], Response::HTTP_NOT_FOUND);
197+
}
191198

192199
$data = $request->validated();
193200

194201
DB::transaction(function () use ($template, $data) {
195-
$updates = [];
196-
if (array_key_exists('stageId', $data)) $updates['stage_id'] = (int) $data['stageId'];
197-
if (array_key_exists('title', $data)) $updates['title'] = $data['title'];
198-
if (array_key_exists('include', $data)) $updates['include'] = (bool) $data['include'];
199-
if (array_key_exists('emailTemplateKey', $data)) $updates['email_template_key'] = $data['emailTemplateKey'];
200-
if (array_key_exists('type', $data)) $updates['type'] = (int) $data['type'];
201-
202-
if ($updates) {
203-
$template->update($updates);
204-
}
202+
$updates = collect($data)
203+
->only(['stageId', 'title', 'include', 'type', 'description', 'dueInterval'])
204+
->mapWithKeys(function ($v, $k) {
205+
return match ($k) {
206+
'stageId' => ['stageId' => (int) $v],
207+
'include' => ['include' => (bool) $v],
208+
'type' => ['type' => (int) $v],
209+
'dueInterval' => ['dueInterval' => $v],
210+
default => [$k => $v], // 'title', 'description'
211+
};
212+
})
213+
->all();
214+
215+
$template->update($updates);
216+
205217
if (array_key_exists('userGroupIds', $data)) {
206218
$template->userGroups()->sync($data['userGroupIds']);
207219
}
@@ -222,7 +234,7 @@ public function delete(Request $illuminateRequest): JsonResponse
222234
$id = (int) $illuminateRequest->route('templateId');
223235

224236
$template = Template::query()
225-
->where('context_id', $contextId)
237+
->byContextId($contextId)
226238
->find($id);
227239

228240
if (!$template) {

api/v1/editTaskTemplates/formRequests/AddTaskTemplate.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ public function rules(): array
3434

3535
// build a list of allowed stage IDs from values
3636
$stages = Application::getApplicationStages();
37-
$stageIds = array_values(array_unique(array_filter(
38-
array_map('intval', array_merge(array_keys((array)$stages), array_values((array)$stages))),
39-
fn ($id) => $id > 0
40-
)));
37+
$stageIds = array_values(array_unique(array_map('intval', array_values((array) $stages))));
38+
4139

4240
return [
4341
'type' => ['required', Rule::in(array_column(EditorialTaskType::cases(), 'value'))],
@@ -46,7 +44,6 @@ public function rules(): array
4644
'include' => ['boolean'],
4745
'dueInterval' => ['sometimes', 'nullable', 'string', Rule::in(array_column(EditorialTaskDueInterval::cases(), 'value'))],
4846
'description' => ['sometimes', 'nullable', 'string'],
49-
'type' => ['required','integer','in:1,2'],
5047
'userGroupIds' => ['required', 'array', 'min:1'],
5148
'userGroupIds.*' => [
5249
'integer',

api/v1/editTaskTemplates/formRequests/UpdateTaskTemplate.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use APP\core\Application;
66
use Illuminate\Foundation\Http\FormRequest;
77
use Illuminate\Validation\Rule;
8+
use PKP\editorialTask\enums\EditorialTaskDueInterval;
9+
use PKP\editorialTask\enums\EditorialTaskType;
810

911
class UpdateTaskTemplate extends FormRequest
1012
{
@@ -19,27 +21,18 @@ public function rules(): array
1921

2022
// Build allowed stage IDs (values)
2123
$stages = Application::getApplicationStages();
22-
$stageIds = array_values(array_unique(array_filter(
23-
array_map('intval', array_merge(array_keys((array)$stages), array_values((array)$stages))),
24-
fn ($id) => $id > 0
25-
)));
26-
27-
// Context-scoped email keys
28-
$emailKeys = \APP\facades\Repo::emailTemplate()
29-
->getCollector($contextId)
30-
->getMany()
31-
->map(fn ($t) => $t->getData('key'))
32-
->filter()
33-
->values()
34-
->all();
24+
$stageIds = array_values(array_unique(array_map('intval', array_values((array) $stages))));
3525

3626
return [
3727
// all fields are optional but if present must validate
3828
'stageId' => ['sometimes', 'integer', Rule::in($stageIds)],
3929
'title' => ['sometimes', 'string', 'max:255'],
4030
'include' => ['sometimes', 'boolean'],
41-
'emailTemplateKey' => ['sometimes', 'nullable', 'string', 'max:255', Rule::in($emailKeys)],
42-
'type' => ['sometimes','integer','in:1,2'],
31+
'description' => ['sometimes', 'nullable', 'string'],
32+
'dueInterval' => ['sometimes', 'nullable', 'string',
33+
Rule::in(array_column(EditorialTaskDueInterval::cases(), 'value'))
34+
],
35+
'type' => ['sometimes', Rule::in(array_column(EditorialTaskType::cases(), 'value'))],
4336

4437
'userGroupIds' => ['sometimes', 'array', 'min:1'],
4538
'userGroupIds.*' => [
@@ -75,11 +68,4 @@ protected function prepareForValidation(): void
7568
]);
7669
}
7770

78-
protected function passedValidation(): void
79-
{
80-
if ($this->has('emailTemplateKey')) {
81-
$key = $this->input('emailTemplateKey');
82-
$this->merge(['emailTemplateKey' => is_string($key) ? trim($key) : $key]);
83-
}
84-
}
8571
}

classes/editorialTask/Template.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class Template extends Model
3838

3939
public $timestamps = true;
4040

41-
public const TYPE_DISCUSSION = 1;
42-
public const TYPE_TASK = 2;
4341

4442
// columns on edit_task_templates
4543
protected $fillable = [
@@ -143,14 +141,6 @@ public function scopeFilterByInclude(Builder $query, bool $include): Builder
143141
return $query->where('include', $include);
144142
}
145143

146-
/**
147-
* Scope: filter by email_template_key
148-
*/
149-
public function scopeFilterByEmailTemplateKey(Builder $query, string $key): Builder
150-
{
151-
return $query->where('email_template_key', $key);
152-
}
153-
154144
/**
155145
* Creates a new task from a template
156146
*/
@@ -202,8 +192,6 @@ public function scopeFilterByTitleLike(Builder $query, string $title): Builder
202192
* free-text/ words search across:
203193
* title column
204194
* name, description
205-
* email_template_key column
206-
*
207195
*/
208196
public function scopeFilterBySearch(Builder $query, string $phrase): Builder
209197
{
@@ -218,18 +206,18 @@ public function scopeFilterBySearch(Builder $query, string $phrase): Builder
218206
return $query;
219207
}
220208

221-
$settingsTable = $this->getSettingsTable(); // 'edit_task_template_settings'
222-
$pk = $this->getKeyName(); // 'edit_task_template_id'
223-
$selfTable = $this->getTable(); // 'edit_task_templates'
209+
$settingsTable = $this->getSettingsTable();
210+
$pk = $this->getKeyName();
211+
$selfTable = $this->getTable();
224212

225213
return $query->where(function (Builder $outer) use ($tokens, $settingsTable, $pk, $selfTable) {
226214
foreach ($tokens as $tok) {
227215
// escape % and _
228-
$like = '%' . str_replace(['%', '_'], ['\\%', '\\_'], mb_strtolower($tok)) . '%';
216+
$like = '%' . str_replace(['%', '_'], ['\\%', '\\_'], mb_strtolower($tok, 'UTF-8')) . '%';
229217

230218
$outer->where(function (Builder $q) use ($like, $settingsTable, $pk, $selfTable) {
231219
$q->whereRaw('LOWER(title) LIKE ?', [$like])
232-
->orWhereRaw('LOWER(email_template_key) LIKE ?', [$like])
220+
->orWhereRaw('LOWER(description) LIKE ?', [$like])
233221
->orWhereExists(function ($sub) use ($like, $settingsTable, $pk, $selfTable) {
234222
$sub->select(DB::raw(1))
235223
->from($settingsTable . ' as ets')

classes/migration/upgrade/v3_6_0/I11885_AddTypeToEditTaskTemplates.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)