Skip to content

Commit

Permalink
improve filter multi field to support relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Dec 21, 2024
1 parent eecc952 commit ec26f95
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 185 deletions.
5 changes: 4 additions & 1 deletion app/Http/Controllers/Admin/CommandQueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function index()
];

$commandQueues = QueryBuilder::for(CommandQueue::class)
->with(['server:id,name,hostname', 'player:id,uuid,username'])
->with(['server:id,name,hostname', 'player:id,uuid,username,skin_texture_id', 'user:id,name,username,profile_photo_path'])
->allowedFilters([
...$fields,
'server.name',
Expand All @@ -60,6 +60,9 @@ public function index()
'output',
'tag',
'player_uuid',
'player.username',
'user.username',
'user.name',
])),
])
->allowedSorts($fields)
Expand Down
48 changes: 34 additions & 14 deletions app/Http/Controllers/Admin/RecruitmentSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ public function indexOpen(Request $request)
'user.name',
'lastActor.name',
'lastCommentor.name',
AllowedFilter::custom('q', new FilterMultipleFields(['id', 'data', 'status'])),
AllowedFilter::custom('q', new FilterMultipleFields([
'id',
'data',
'status',
'user.name',
'user.username',
'lastActor.name',
'lastActor.username',
'lastCommentor.name',
'lastCommentor.username',
])),
])
->allowedSorts($fields)
->defaultSort('-updated_at')
Expand Down Expand Up @@ -142,7 +152,17 @@ public function indexClosed(Request $request)
'user.name',
'lastActor.name',
'lastCommentor.name',
AllowedFilter::custom('q', new FilterMultipleFields(['id', 'data', 'status'])),
AllowedFilter::custom('q', new FilterMultipleFields([
'id',
'data',
'status',
'user.name',
'user.username',
'lastActor.name',
'lastActor.username',
'lastCommentor.name',
'lastCommentor.username',
])),
])
->allowedSorts($fields)
->defaultSort('-updated_at')
Expand All @@ -167,17 +187,17 @@ public function show(Request $request, RecruitmentSubmission $submission)
'lastActor:id,name,username',
]);
$submission['i_can_act'] = $request->user()->can('actOn', $submission)
&& in_array($submission->status, [
RecruitmentSubmissionStatus::PENDING,
RecruitmentSubmissionStatus::INPROGRESS,
RecruitmentSubmissionStatus::ONHOLD,
]);
&& in_array($submission->status, [
RecruitmentSubmissionStatus::PENDING,
RecruitmentSubmissionStatus::INPROGRESS,
RecruitmentSubmissionStatus::ONHOLD,
]);
$submission['i_can_delete'] = $request->user()->can('delete', $submission)
&& in_array($submission->status, [
RecruitmentSubmissionStatus::APPROVED,
RecruitmentSubmissionStatus::REJECTED,
RecruitmentSubmissionStatus::WITHDRAWN,
]);
&& in_array($submission->status, [
RecruitmentSubmissionStatus::APPROVED,
RecruitmentSubmissionStatus::REJECTED,
RecruitmentSubmissionStatus::WITHDRAWN,
]);
$submission['i_can_send_message'] = in_array($submission->status, [
RecruitmentSubmissionStatus::PENDING,
RecruitmentSubmissionStatus::INPROGRESS,
Expand All @@ -204,7 +224,7 @@ public function act(Request $request, RecruitmentSubmission $submission)
$this->authorize('actOn', $submission);

$request->validate([
'action' => 'required|in:'.implode(',', [
'action' => 'required|in:' . implode(',', [
RecruitmentSubmissionStatus::INPROGRESS,
RecruitmentSubmissionStatus::ONHOLD,
RecruitmentSubmissionStatus::APPROVED,
Expand Down Expand Up @@ -251,7 +271,7 @@ public function postMessage(Request $request, RecruitmentSubmission $submission)
{
$request->validate([
'message' => 'required|max:2000',
'type' => ['required', 'in:'.implode(',', [CommentType::RECRUITMENT_STAFF_WHISPER, CommentType::RECRUITMENT_STAFF_MESSAGE])],
'type' => ['required', 'in:' . implode(',', [CommentType::RECRUITMENT_STAFF_WHISPER, CommentType::RECRUITMENT_STAFF_MESSAGE])],
]);

$comment = $submission->comment($request->message, $request->type);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/BanWardenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function index()
'creator_username',
'remover_username',
'remover_uuid',
'victimPlayer.username',
])),
])
->allowedSorts($fields)
Expand Down
33 changes: 31 additions & 2 deletions app/Queries/Filters/FilterMultipleFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace App\Queries\Filters;

use App\Utils\Helpers\Helper;
use Illuminate\Support\Str;
use Spatie\QueryBuilder\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;

class FilterMultipleFields implements Filter
{
public function __construct(public $fields) {}
public function __construct(public $fields)
{
}

public function __invoke(Builder $query, $value, string $property): Builder
{
Expand All @@ -17,10 +21,35 @@ public function __invoke(Builder $query, $value, string $property): Builder
$query->where(function ($query) use ($fields, $value) {
foreach ($fields as $field) {
$val = Helper::escapeLike($value);
$query->orWhere($field, 'LIKE', "%" . $val . "%");
// Handle Relation Field
if ($this->isRelationProperty($query, $field)) {
$query->orWhereHas(Str::before($field, '.'), function ($query) use ($field, $val) {
$query->where(Str::after($field, '.'), 'LIKE', "%" . $val . "%");
});
continue;
} else {
// Handle No Relation Field
$query->orWhere($field, 'LIKE', "%" . $val . "%");
}
}
});

return $query;
}


private function isRelationProperty(Builder $query, string $property): bool
{
if (!Str::contains($property, '.')) {
return false;
}

$firstRelationship = explode('.', $property)[0];

if (!method_exists($query->getModel(), $firstRelationship)) {
return false;
}

return is_a($query->getModel()->{$firstRelationship}(), Relation::class);
}
}
Loading

0 comments on commit ec26f95

Please sign in to comment.