Skip to content

Commit bc76e6f

Browse files
committed
Improve edit task template search, validation, and controller logic
1 parent c8bb951 commit bc76e6f

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

api/v1/editTaskTemplates/PKPEditTaskTemplateController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public function add(AddTaskTemplate $illuminateRequest): JsonResponse
9898
'include' => $validated['include'] ?? false,
9999
'description' => $validated['description'] ?? null,
100100
'dueInterval' => $validated['dueInterval'] ?? null,
101-
'type' => (int) $validated['type'],
102101
]);
103102

104103
$tpl->userGroups()->sync($validated['userGroupIds']);
@@ -144,11 +143,9 @@ public function getMany(Request $request): JsonResponse
144143
$collector->filterByTitleLike((string) $val);
145144
break;
146145
case 'type':
147-
if (is_numeric($val)) {
148-
$type = (int) $val;
149-
if (in_array($type, [EditorialTaskType::DISCUSSION->value, EditorialTaskType::TASK->value], true)) {
150-
$collector->filterByType($type);
151-
}
146+
$type = (int) $val;
147+
if (in_array($type, array_column(EditorialTaskType::cases(), 'value'), true)) {
148+
$collector->filterByType($type);
152149
}
153150
break;
154151
case 'stageId':
@@ -233,12 +230,17 @@ public function delete(Request $illuminateRequest): JsonResponse
233230
], Response::HTTP_NOT_FOUND);
234231
}
235232

233+
$resource = new TaskTemplateResource($template->load('userGroups'));
234+
236235
DB::transaction(function () use ($template) {
237236
// Pivot/settings rows cascade via FKs defined in migration
238237
$template->delete();
239238
});
240239

241-
return response()->json([], Response::HTTP_OK);
240+
return response()->json(
241+
$resource->toArray($illuminateRequest),
242+
Response::HTTP_OK
243+
);
242244
}
243245

244246
}

api/v1/editTaskTemplates/formRequests/UpdateTaskTemplate.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,32 @@ public function rules(): array
3232

3333
protected function prepareForValidation(): void
3434
{
35-
$stageId = $this->input('stageId', null);
36-
$type = $this->input('type', null);
37-
38-
$this->merge([
39-
'include' => $this->has('include')
40-
? filter_var($this->input('include'), FILTER_VALIDATE_BOOLEAN)
41-
: $this->input('include', null),
42-
43-
'userGroupIds' => $this->has('userGroupIds')
44-
? array_values(array_map('intval', (array) $this->input('userGroupIds', [])))
45-
: $this->input('userGroupIds', null),
46-
47-
'stageId' => $this->has('stageId')
48-
? (is_null($stageId) ? null : (int) $stageId)
49-
: $this->input('stageId', null),
50-
51-
'type' => $this->has('type')
52-
? (is_null($type) ? null : (int) $type)
53-
: $this->input('type', null),
54-
]);
35+
$stageId = $this->input('stageId');
36+
$type = $this->input('type');
37+
38+
$data = [];
39+
40+
if ($this->has('include')) {
41+
$data['include'] = filter_var($this->input('include'), FILTER_VALIDATE_BOOLEAN);
42+
}
43+
44+
if ($this->has('userGroupIds')) {
45+
$data['userGroupIds'] = array_values(
46+
array_map('intval', (array) $this->input('userGroupIds', []))
47+
);
48+
}
49+
50+
if ($this->has('stageId')) {
51+
$data['stageId'] = is_null($stageId) ? null : (int) $stageId;
52+
}
53+
54+
if ($this->has('type')) {
55+
$data['type'] = is_null($type) ? null : (int) $type;
56+
}
57+
58+
if ($data) {
59+
$this->merge($data);
60+
}
5561
}
5662

5763
}

api/v1/editTaskTemplates/resources/TaskTemplateResource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function toArray(Request $request)
3131
'include' => (bool) $this->include,
3232
'dueInterval' => $this->dueInterval,
3333
'description' => $this->description,
34-
'type' => (int) $this->type,
3534
'userGroups' => $this->whenLoaded(
3635
'userGroups',
3736
fn () => $this->userGroups

classes/editorialTask/Template.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,12 @@ public function scopeFilterByTitleLike(Builder $query, string $title): Builder
185185
if ($title === '') {
186186
return $query;
187187
}
188-
$needle = '%' . str_replace(['%', '_'], ['\\%', '\\_'], mb_strtolower($title)) . '%';
189-
return $query->whereRaw('LOWER(title) LIKE ?', [$needle]);
188+
189+
// escape LIKE wildcards in the user input, then wrap with %
190+
$needle = '%' . addcslashes($title, '%_') . '%';
191+
192+
// use LOWER() on both sides so DB applies the same case-folding
193+
return $query->whereRaw('LOWER(title) LIKE LOWER(?)', [$needle]);
190194
}
191195

192196
/**

0 commit comments

Comments
 (0)