Skip to content

Commit

Permalink
Add feature to attach evidence as URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Feb 16, 2025
1 parent e6a6b16 commit 1adffe6
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 49 deletions.
51 changes: 40 additions & 11 deletions app/Http/Controllers/BanWardenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class BanWardenController extends Controller
{
Expand Down Expand Up @@ -268,8 +269,7 @@ public function indexAlts(PlayerPunishment $playerPunishment)
return $players;
}


public function showEvidence(PlayerPunishment $playerPunishment, $evidence, Request $request)
public function showMediaEvidence(PlayerPunishment $playerPunishment, $evidence, Request $request)
{
$this->authorize('viewEvidence', $playerPunishment);

Expand All @@ -289,12 +289,17 @@ public function createEvidence(PlayerPunishment $playerPunishment, Request $requ
$evidenceMaxSizeKb = config('minetrax.banwarden.evidence_max_size_kb');
$evidenceMaxCount = config('minetrax.banwarden.evidence_max_count');
$request->validate([
'type' => 'required|string|in:media,url',
'file' => [
'required',
'required_if:type,media',
'file',
'mimes:' . $evidenceAllowedMimetypes,
'max:' . $evidenceMaxSizeKb,
]
],
'url' => [
'required_if:type,url',
'url',
],
]);

$previousEvidences = $playerPunishment->evidences;
Expand All @@ -303,22 +308,46 @@ public function createEvidence(PlayerPunishment $playerPunishment, Request $requ
->with(['toast' => ['type' => 'error', 'title' => __('Upload Failed'), 'body' => __('You can only upload up to :count evidence files', ['count' => $evidenceMaxCount])]]);
}

$playerPunishment->addMediaFromRequest('file')->toMediaCollection('punishment-evidence');
if ($request->input('type') === 'media') {
$playerPunishment->addMediaFromRequest('file')->toMediaCollection('punishment-evidence');
} else {
$playerPunishment->evidence_urls = array_merge($playerPunishment->evidence_urls ?? [], [
[
'id' => Str::uuid(),
'url' => $request->input('url'),
]
]);
$playerPunishment->save();
}

return redirect()->back()
->with(['toast' => ['type' => 'success', 'title' => __('Upload Successful'), 'body' => __('Evidence uploaded successfully')]]);
}

public function deleteEvidence(PlayerPunishment $playerPunishment, $evidence)
public function deleteEvidence(PlayerPunishment $playerPunishment, Request $request)
{
$this->authorize('deleteEvidence', PlayerPunishment::class);

$media = $playerPunishment->getMedia('punishment-evidence')->find($evidence);
if (!$media) {
abort(404);
}
$request->validate([
'evidence' => 'required|string',
'type' => 'required|string|in:media,url',
]);

$media->delete();
$evidence = $request->input('evidence');
$type = $request->input('type');

if ($type == 'media') {
$media = $playerPunishment->getMedia('punishment-evidence')->find($evidence);
if (!$media) {
abort(404);
}
$media->delete();
} else {
$playerPunishment->evidence_urls = array_filter($playerPunishment->evidence_urls, function ($url) use ($evidence) {
return $url['id'] !== $evidence;
});
$playerPunishment->save();
}

return redirect()->back()
->with(['toast' => ['type' => 'success', 'title' => __('Delete Successful'), 'body' => __('Evidence deleted successfully')]]);
Expand Down
31 changes: 30 additions & 1 deletion app/Models/PlayerPunishment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PlayerPunishment extends BaseModel implements HasMedia
'end_at' => 'datetime',
'removed_at' => 'datetime',
'is_ipban' => 'boolean',
'evidence_urls' => 'array',
];

protected $appends = [
Expand All @@ -45,7 +46,35 @@ public function registerMediaCollections(): void

public function getEvidencesAttribute()
{
return $this->getMedia('punishment-evidence');
$mediaFiles = $this->getMedia('punishment-evidence');
$urls = $this->evidence_urls ?? [];

return collect($mediaFiles)->map(fn($mediaFile) => [
'type' => 'media',
'data' => $mediaFile,
])->concat(
collect($urls)->map(fn($url) => [
'type' => 'url',
'data' => $url,
])
);
}

public function scopeEvidences($query, $flag)
{
if ($flag) {
// where has media or evidence_urls is not null
return $query->where(function ($query) {
$query->whereHas('media')
->orWhereNotNull('evidence_urls');
});
} else {
// where has no media and evidence_urls is null
return $query->where(function ($query) {
$query->whereDoesntHave('media')
->whereNull('evidence_urls');
});
}
}

public function scopeStatus($query, $status)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('player_punishments', function (Blueprint $table) {
$table->json('evidence_urls')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('player_punishments', function (Blueprint $table) {
$table->dropColumn('evidence_urls');
});
}
};
Loading

0 comments on commit 1adffe6

Please sign in to comment.