Files
RomhackPlaza/app/Http/Requests/StoreEntryRequest.php
2026-06-09 11:45:59 +02:00

159 lines
6.3 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Rules\PublicFileExists;
use App\Rules\XfUserExists;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class StoreEntryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$entry = $this->route('entry');
if( $entry )
return $this->user()->can('update', $entry);
return $this->user()->can('create', '\App\Models\Entry');
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function prepareForValidation(): void
{
$newGameTitle = trim((string) $this->input('new-game-title', ''));
$newGamePlatform = $this->input('new-game-platform');
$newGameGenre = $this->input('new-game-genre');
$this->merge([
'game_id' => $this->input('game_id') !== '' ? $this->input('game_id') : null,
'new-game-title' => $newGameTitle !== '' ? $newGameTitle : null,
'new-game-platform' => $newGamePlatform !== '' ? $newGamePlatform : null,
'new-game-genre' => $newGameGenre !== '' ? $newGameGenre : null,
'gallery' => $this->input('gallery') !== '' ? $this->input('gallery') : null,
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$isEdit = (bool) $this->route('entry');
$rules = [];
$section = $this->route('section');
$rules['files_uuid'] = 'array|required|min:1';
$rules['files_uuid.*'] = 'string';
if( $isEdit ){
$rules['files_state'] = 'array|required|min:1';
$rules['files_state.*'] = 'string|in:public,private,archived';
}
if( section_must_not_be( ['translations','homebrew'], $section ) ){
$rules['entry_title'] = "required|string|max:255";
} else {
$rules['entry_title'] = "nullable|string|max:255";
}
if( section_must_be( 'romhacks', $section ) ){
$rules['modifications'] = 'array|required|min:1';
$rules['modifications.*'] = 'integer|exists:modifications,id';
}
$rules['version'] = 'required|string|max:50';
$rules['release-date'] = 'required|date';
$rules['status'] = 'required|integer|exists:statuses,id';
$rules['description'] = 'required|string';
if( section_must_be( ['romhacks', 'translations' ], $section ) ){
$rules['game_id'] = 'required_without:new-game-title|nullable|integer|exists:games,id';
$rules['new-game-title'] = 'required_without:game_id|nullable|string|max:255';
$rules['new-game-platform'] = 'required_with:new-game-title|nullable|integer|exists:platforms,id';
$rules['new-game-genre'] = 'required_with:new-game-title|integer|nullable|exists:genres,id';
}
$rules['hashes'] = 'array|required|min:1';
$rules['hashes.*.filename'] = 'required|string|max:512';
$rules['hashes.*.hash_crc32'] = 'required|string|max:512';
$rules['hashes.*.hash_sha1'] = 'required|string|max:512';
$rules['hashes.*.verified'] = 'required|string|max:512';
$rules['languages'] = 'array|required|min:1';
$rules['languages.*'] = 'integer|exists:languages,id';
$rules['main-image'] = [ 'required', 'string', new PublicFileExists ];
$rules['gallery'] = 'array|required|min:1';
$rules['gallery.*'] = [ 'string', new PublicFileExists ];
$rules['authors'] = 'array|required_without:new-authors|min:1';
$rules['authors.*'] = 'integer|exists:authors,id';
$rules['new-authors'] = 'array|required_without:authors|min:1';
$rules['new-authors.*'] = 'string|max:255';
$rules['staff_credits'] = 'nullable|json';
$rules['release_site'] = 'nullable|url|max:500';
$rules['youtube_video'] = 'nullable|url|max:500';
if( $isEdit ){
$ss = 'draft,pending,published';
if( \Auth::user()->can('moderate', $this->route('entry')) && \Auth::user()->can('view-hidden', $this->route('entry')) )
$ss .= ',hidden';
if(\Auth::user()->can('moderate', $this->route('entry')) && \Auth::user()->can('view-locked', $this->route('entry')) )
$ss .= ',locked';
$rules['submit-state'] = 'required|in:' . $ss;
} else {
if( $this->user()->can('skip-queue', '\App\Models\Entry') ){
$rules['submit-state'] = 'required|string|in:draft,pending,published';
} else {
$rules['submit-state'] = 'required|string|in:draft,pending';
}
}
if( $isEdit ){
$rules['files_metadata'] = 'array|nullable';
$rules['files_metadata.*.online_patcher'] = 'nullable|boolean';
$rules['files_metadata.*.secondary_online_patcher'] = 'nullable|boolean|required_with:files_metadata.*.online_patcher';
}
if( $isEdit && $this->user()->can('moderate', $this->route('entry') ) ){
$rules['staff_comment'] = 'nullable|string';
$rules['owner_user_id'] = [ 'required', 'integer', new XfUserExists ];
$rules['comments_thread_id'] = 'nullable|integer';
$rules['featured'] = 'nullable|boolean';
}
return $rules;
}
public function messages(): array
{
return [
'entry_title.required' => 'Please provide an entry title.',
'slug.unique' => 'An entry with this title already exists. Please choose another title.',
'file_ids.required' => 'Please upload at least one file.',
'file_ids.min' => 'Please upload at least one file.',
'hashes.required' => 'Please add at least one hash.',
'hashes.min' => 'Please add at least one hash.',
'gallery.required' => 'Please add at least one screenshot.',
'gallery.min' => 'Please add at least one screenshot.',
'new-game-platform.required_without' => 'Please choose a platform for the new game.',
'new-game-genre.required_without' => 'Please choose a genre for the new game.',
];
}
}