2023-03-31 17:38:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
2023-04-05 17:20:30 +02:00
|
|
|
namespace App\Http\Requests\Bo\Documents;
|
2023-03-31 17:38:44 +02:00
|
|
|
|
2023-04-05 17:20:30 +02:00
|
|
|
use App\Enums\Documents\LangEnum;
|
2023-10-04 15:51:07 +02:00
|
|
|
use App\Enums\Documents\TypeEnum;
|
2023-10-03 15:44:44 +02:00
|
|
|
use App\Models\Document;
|
2023-03-31 17:38:44 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2025-05-26 11:33:47 +02:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2023-10-04 15:51:07 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2025-05-26 11:33:47 +02:00
|
|
|
use Illuminate\Validation\Rule;
|
2025-05-20 10:16:12 +02:00
|
|
|
use Illuminate\Validation\Rules\Enum;
|
2025-05-26 11:46:46 +02:00
|
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
2023-03-31 17:38:44 +02:00
|
|
|
|
|
|
|
|
class StoreDocumentRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function authorize(): bool
|
2023-03-31 17:38:44 +02:00
|
|
|
{
|
2025-05-26 11:33:47 +02:00
|
|
|
return Gate::check('create', Document::class);
|
2023-03-31 17:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-13 08:34:54 +00:00
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
protected function prepareForValidation(): void
|
2023-07-13 08:34:54 +00:00
|
|
|
{
|
|
|
|
|
$this->merge([
|
2025-05-26 11:33:47 +02:00
|
|
|
'status' => $this->status ? true : false,
|
2023-07-13 08:34:54 +00:00
|
|
|
]);
|
2023-10-04 15:51:07 +02:00
|
|
|
// * Sanitize Path
|
|
|
|
|
if (!empty($this->path)) {
|
|
|
|
|
$filename = $this->path;
|
2025-05-26 11:46:46 +02:00
|
|
|
$refMethod = new \ReflectionMethod(FileHelper::class, 'sanitizeFileName');
|
2023-10-04 15:51:07 +02:00
|
|
|
$refMethod->setAccessible(true);
|
|
|
|
|
$filename = \pathinfo(
|
|
|
|
|
$refMethod->invoke(null, htmlspecialchars($filename), false),
|
|
|
|
|
\PATHINFO_BASENAME
|
|
|
|
|
);
|
|
|
|
|
$this->merge([
|
|
|
|
|
'path' => $filename
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-05-20 10:16:12 +02:00
|
|
|
if (Document::query()->where('id', $this->document_id)->exists() && $this->type) {
|
|
|
|
|
$this->merge([
|
|
|
|
|
'directory_id' => Document::query()->where('id', $this->document_id)->value('directory_id'),
|
2025-06-17 12:29:15 +02:00
|
|
|
'type' => TypeEnum::make($this->type, 'name')->name(),
|
2025-05-20 10:16:12 +02:00
|
|
|
]);
|
|
|
|
|
}
|
2023-07-13 08:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-31 17:38:44 +02:00
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function rules(): array
|
2023-03-31 17:38:44 +02:00
|
|
|
{
|
2023-10-04 15:51:07 +02:00
|
|
|
/** @var \Illuminate\Filesystem\FilesystemAdapter */
|
|
|
|
|
$disk = Storage::disk('docs');
|
2023-03-31 17:38:44 +02:00
|
|
|
return [
|
2023-10-03 15:44:44 +02:00
|
|
|
'directory_id' => 'required|integer',
|
2025-05-20 10:16:12 +02:00
|
|
|
'document_id' => 'sometimes:directory_id|nullable|integer',
|
|
|
|
|
'uuid' => [
|
2025-05-26 11:33:47 +02:00
|
|
|
'required',
|
|
|
|
|
'uuid',
|
2025-05-20 10:16:12 +02:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
2025-05-26 11:33:47 +02:00
|
|
|
function ($attribute, $value, $fail) use ($disk) {
|
|
|
|
|
if ($this->type and \array_column(TypeEnum::toArray(), 'name')) {
|
|
|
|
|
$extension = TypeEnum::make($this->type, 'name')->name();
|
2025-05-20 10:16:12 +02:00
|
|
|
$value = "{$value}.{$extension}";
|
2023-10-04 15:51:07 +02:00
|
|
|
}
|
2025-05-26 11:46:46 +02:00
|
|
|
if (!$disk->has(sprintf('%s/%s', \tenant()->getKey(), $value))) {
|
2023-10-04 15:51:07 +02:00
|
|
|
$path = $this->path ?? $this->value ?? 'not found';
|
|
|
|
|
$fail(\trans('Le fichier :filename n\'est pas présent dans le système de fichier', [
|
|
|
|
|
'filename' => $path
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
2025-05-20 10:16:12 +02:00
|
|
|
'title' => 'required|min:3|max:255',
|
2025-05-26 11:33:47 +02:00
|
|
|
'type' => ['required', 'string', Rule::in(array_column(TypeEnum::toArray(), 'name'))],
|
2025-05-20 10:16:12 +02:00
|
|
|
'path' => 'required|min:3|max:255',
|
2025-05-26 11:33:47 +02:00
|
|
|
'lang' => ['required', new Enum(LangEnum::class)],
|
2025-05-20 10:16:12 +02:00
|
|
|
'status' => 'required|boolean'
|
2023-03-31 17:38:44 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get custom attributes for validator errors.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function attributes(): array
|
2023-03-31 17:38:44 +02:00
|
|
|
{
|
|
|
|
|
return [
|
2025-05-26 11:33:47 +02:00
|
|
|
'directory_id' => \trans('models.classes.directory'),
|
|
|
|
|
'document_id' => \trans('models.classes.document'),
|
|
|
|
|
'title' => \trans('validation.attributes.title'),
|
|
|
|
|
'uuid' => \trans('models.classes.document'),
|
|
|
|
|
'type' => \trans('validation.custom.type'),
|
|
|
|
|
'path' => \trans('validation.custom.path'),
|
|
|
|
|
'lang' => \trans('validation.custom.lang'),
|
|
|
|
|
'status' => \trans('validation.custom.status'),
|
2023-03-31 17:38:44 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|