ldc-legaulois/app/Http/Requests/Bo/SectionPages/UpdateSectionPageRequest.php

131 lines
4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Requests\Bo\SectionPages;
use App\Http\Requests\Bo\UpdateMiniCrudRequest;
use App\Traits\Requests\HasPicture;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
2026-09-25 16:25:31 +02:00
use Illuminate\Support\Facades\Gate;
class UpdateSectionPageRequest extends UpdateMiniCrudRequest
{
use HasPicture;
protected $modelName = 'sectionPage';
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::check('update', $this->route($this->modelName));
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$data = $this->input('data', []);
foreach ($data as $key => $item) {
2026-09-25 16:25:31 +02:00
if (! isset($item['value'])) {
if (empty($item['alt']) && empty($item['title'])) {
$data[$key] = ['alt' => null, 'title' => null, 'value' => null];
} else {
$data[$key] = ['value' => $item];
}
}
}
$this->merge([
2026-09-25 16:25:31 +02:00
'data' => $data,
]);
$this->normalizeFieldsConfig();
}
/**
* Set default validate config.
*/
private function normalizeFieldsConfig(): void
{
$sectionPage = $this->route($this->modelName);
2026-09-25 16:25:31 +02:00
if (! $sectionPage || ! $sectionPage->data) {
return;
}
$config = json_decode(json_encode($sectionPage->data), true);
foreach ($config as $key => $fieldConfig) {
2026-09-25 16:25:31 +02:00
if (is_array($fieldConfig) && ! array_key_exists('validate', $fieldConfig)) {
$config[$key]['validate'] = [];
}
}
$sectionPage->data = $config;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return array_merge(parent::rules(), [
'visibility' => 'nullable|boolean',
'data' => 'present|array',
'data.*' => 'required|array',
]);
}
protected function failedValidation(Validator $validator)
{
$errors = collect($validator->errors()->all())->join("\n");
session()->flash('error', 'Liste des erreurs :' . "\n" . $errors);
throw new HttpResponseException(
response()->json([
'message' => 'Erreur de validation.',
'errors' => $validator->errors(),
], 422)
);
}
public function attributes(): array
{
$attributes = [
'data.title' => 'Titre',
'data.description' => 'Description',
'data.media' => 'Media',
'data.media.value' => 'Media',
'data.media.alt' => 'Media Alt',
'data.media.title' => 'Media Title',
'data.media_picto.value' => 'Picto',
'data.media_picto.alt' => 'Picto Alt',
'data.media_picto.title' => 'Picto Title',
2026-06-11 10:47:10 +02:00
'data.media.value.video.title' => 'Video title',
'data.media.value.video.alt' => 'Video Alt',
];
for ($i = 1; $i <= 8; $i++) {
$attributes["data.title_{$i}.value"] = "Titre du point {$i}";
}
foreach (['left' => 'gauche', 'right' => 'droite', 'center' => 'centre'] as $key => $label) {
$attributes["data.title_{$key}.value"] = "Titre {$label}";
$attributes["data.description_{$key}.value"] = "Description {$label}";
$attributes["data.media_{$key}.value"] = "Picto {$label}";
$attributes["data.media_{$key}.alt"] = "Picto {$label} alt";
$attributes["data.media_{$key}.title"] = "Picto {$label} title";
}
2026-09-25 16:25:31 +02:00
$attributes['data.title_sec.value'] = "Titre de l'encart";
return $attributes;
}
}