130 lines
4 KiB
PHP
130 lines
4 KiB
PHP
<?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;
|
|
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) {
|
|
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([
|
|
'data' => $data,
|
|
]);
|
|
|
|
$this->normalizeFieldsConfig();
|
|
}
|
|
|
|
/**
|
|
* Set default validate config.
|
|
*/
|
|
private function normalizeFieldsConfig(): void
|
|
{
|
|
$sectionPage = $this->route($this->modelName);
|
|
|
|
if (! $sectionPage || ! $sectionPage->data) {
|
|
return;
|
|
}
|
|
|
|
$config = json_decode(json_encode($sectionPage->data), true);
|
|
|
|
foreach ($config as $key => $fieldConfig) {
|
|
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',
|
|
'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";
|
|
}
|
|
|
|
$attributes['data.title_sec.value'] = "Titre de l'encart";
|
|
|
|
return $attributes;
|
|
}
|
|
}
|