2026-06-04 14:20:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Bo\BlockPage;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Block\BlockContentTypeEnum;
|
|
|
|
|
use App\Enums\BlockTypeEnum;
|
|
|
|
|
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;
|
2026-06-04 14:20:18 +02:00
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
|
|
|
|
|
class UpdateBlockRequest extends UpdateMiniCrudRequest
|
|
|
|
|
{
|
|
|
|
|
use HasPicture;
|
|
|
|
|
|
|
|
|
|
protected $modelName = 'block';
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2026-06-04 14:20:18 +02:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
{
|
2026-09-25 16:25:31 +02:00
|
|
|
$data = $this->input('data', []);
|
|
|
|
|
$block = $this->route('block');
|
2026-06-11 10:47:10 +02:00
|
|
|
$fields = json_decode(json_encode($block->data), true);
|
2026-06-04 14:20:18 +02:00
|
|
|
if ($block->type === BlockTypeEnum::redirection) {
|
|
|
|
|
$data = $this->processBlockData($data, $fields);
|
2026-06-22 15:11:31 +02:00
|
|
|
if (isset($data['article_id']['value']) && is_array($data['article_id']['value'])) {
|
2026-09-25 16:25:31 +02:00
|
|
|
$article = $data['article_id']['value'][0] ?? null;
|
|
|
|
|
$data['article_id']['value'] = $article['id'] ?? null;
|
2026-06-22 15:11:31 +02:00
|
|
|
}
|
2026-06-11 10:47:10 +02:00
|
|
|
} else {
|
|
|
|
|
foreach ($fields as $key => $field) {
|
|
|
|
|
if (isset($data[$key])) {
|
|
|
|
|
$submitted = $data[$key];
|
|
|
|
|
|
|
|
|
|
if (is_array($submitted)) {
|
|
|
|
|
$fields[$key] = array_merge($field, $submitted);
|
|
|
|
|
} else {
|
|
|
|
|
$fields[$key]['value'] = $submitted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$data = $fields;
|
2026-06-04 14:20:18 +02:00
|
|
|
}
|
|
|
|
|
$this->merge(['data' => $data]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
$parentRules = parent::rules();
|
2026-09-25 16:25:31 +02:00
|
|
|
$block = $this->route('block');
|
2026-06-04 14:20:18 +02:00
|
|
|
|
|
|
|
|
$rules = [
|
2026-09-25 16:25:31 +02:00
|
|
|
'data' => 'present|array',
|
2026-06-04 14:20:18 +02:00
|
|
|
'data.*' => 'required|array',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($block->type === BlockTypeEnum::redirection) {
|
2026-06-11 10:47:10 +02:00
|
|
|
$rules['data.block_type.value'] = ['required', Rule::in(BlockContentTypeEnum::toValues())];
|
2026-09-25 16:25:31 +02:00
|
|
|
$rules = array_merge($rules, $this->resolveRedirectionRules());
|
2026-06-04 14:20:18 +02:00
|
|
|
}
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2026-06-04 14:20:18 +02:00
|
|
|
return array_merge($parentRules, $rules);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function attributes(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2026-09-25 16:25:31 +02:00
|
|
|
'data.cta_url.value' => 'redirection',
|
|
|
|
|
'data.cta_label.value' => 'du bouton',
|
|
|
|
|
'data.block_type.value' => 'type de bloc',
|
|
|
|
|
'data.article_id.value' => 'article lié',
|
|
|
|
|
'data.title.value' => 'titre',
|
|
|
|
|
'data.position.value' => 'position',
|
2026-06-11 10:47:10 +02:00
|
|
|
'data.picture.value' => 'pictogramme',
|
|
|
|
|
'data.picture.alt' => 'pictogramme Alt',
|
|
|
|
|
'data.picture.title' => 'pictogramme Title',
|
2026-06-04 14:20:18 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize input data and apply showWhen visibility logic for redirection blocks.
|
|
|
|
|
*
|
2026-09-25 16:25:31 +02:00
|
|
|
* @param array $data The raw input data from the request.
|
|
|
|
|
* @param array $fields The block field configuration (including showWhen rules).
|
2026-06-04 14:20:18 +02:00
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
|
|
|
*/
|
|
|
|
|
private function processBlockData(array $data, array $fields): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $key => $item) {
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! isset($item['value'])) {
|
2026-06-04 14:20:18 +02:00
|
|
|
$data[$key] = ['value' => $item];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($fields as $fieldName => $fieldConfig) {
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! isset($fieldConfig['showWhen'])) {
|
2026-06-04 14:20:18 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$shouldShow = true;
|
|
|
|
|
foreach ($fieldConfig['showWhen'] as $dependentField => $allowedValues) {
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! in_array($data[$dependentField]['value'] ?? null, (array) $allowedValues)) {
|
2026-06-04 14:20:18 +02:00
|
|
|
$shouldShow = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! $shouldShow) {
|
2026-06-04 14:20:18 +02:00
|
|
|
$data[$fieldName] = ['validate' => 'nullable', 'required' => false, 'value' => null];
|
|
|
|
|
} else {
|
|
|
|
|
$data[$fieldName]['required'] = true;
|
|
|
|
|
unset($data[$fieldName]['validate']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2026-06-04 14:20:18 +02:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve validation rules for redirection block fields dynamically.
|
|
|
|
|
*
|
|
|
|
|
* builds a rules array based on:
|
|
|
|
|
* - The block's field config (validate key) as fallback.
|
|
|
|
|
* - The field's required flag set during prepareForValidation to either
|
|
|
|
|
* enforce "required" or replace it with "nullable".
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
|
|
|
*/
|
|
|
|
|
private function resolveRedirectionRules(): array
|
|
|
|
|
{
|
2026-09-25 16:25:31 +02:00
|
|
|
$data = $this->input('data', []);
|
2026-06-04 14:20:18 +02:00
|
|
|
$parentRules = parent::rules();
|
2026-09-25 16:25:31 +02:00
|
|
|
$block = $this->route('block');
|
|
|
|
|
$config = json_decode(json_encode($block->data), true);
|
2026-06-04 14:20:18 +02:00
|
|
|
|
|
|
|
|
$rules = [];
|
|
|
|
|
|
|
|
|
|
foreach ($data as $fieldName => $item) {
|
|
|
|
|
if ($fieldName === 'block_type' || $fieldName === 'position') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$isRequired = $item['required'] ?? true;
|
|
|
|
|
|
|
|
|
|
$baseRules = $parentRules["data.{$fieldName}.value"]
|
|
|
|
|
?? $config[$fieldName]['validate']
|
|
|
|
|
?? [];
|
|
|
|
|
|
|
|
|
|
if (is_string($baseRules)) {
|
|
|
|
|
$baseRules = explode('|', $baseRules);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($baseRules)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! $isRequired) {
|
|
|
|
|
$baseRules = array_filter($baseRules, fn ($r) => $r !== 'required');
|
2026-06-04 14:20:18 +02:00
|
|
|
$baseRules[] = 'nullable';
|
2026-09-25 16:25:31 +02:00
|
|
|
$baseRules = array_values($baseRules);
|
2026-06-04 14:20:18 +02:00
|
|
|
} else {
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! in_array('required', $baseRules)) {
|
2026-06-04 14:20:18 +02:00
|
|
|
array_unshift($baseRules, 'required');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rules["data.{$fieldName}.value"] = $baseRules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
|
}
|
|
|
|
|
}
|