Implemented a block editor for changing the layout of the page

This commit is contained in:
2026-04-04 01:17:05 +02:00
parent 0800f7664f
commit ff58e82497
41 changed files with 2706 additions and 298 deletions

View File

@@ -0,0 +1,162 @@
<?php
declare(strict_types=1);
namespace App\Http\Requests\Admin;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Support\Facades\Validator;
final class PageBlockContentValidator
{
/**
* @param array<int, array<string, mixed>> $blocks
*/
public static function validateBlocksArray(ValidatorContract $validator, array $blocks): void
{
$formCount = 0;
foreach ($blocks as $i => $block) {
if (! is_array($block)) {
$validator->errors()->add("blocks.$i", __('Ongeldig blok.'));
continue;
}
$type = $block['type'] ?? '';
if ($type === 'form') {
$formCount++;
}
$content = $block['content'] ?? [];
if (! is_array($content)) {
$validator->errors()->add("blocks.$i.content", __('Ongeldige blokinhoud.'));
continue;
}
$rules = self::rulesForType((string) $type);
if ($rules === []) {
$validator->errors()->add("blocks.$i.type", __('Onbekend bloktype.'));
continue;
}
$inner = Validator::make($content, $rules);
if ($inner->fails()) {
foreach ($inner->errors()->messages() as $field => $messages) {
foreach ($messages as $message) {
$validator->errors()->add("blocks.$i.content.$field", $message);
}
}
}
if ($type === 'form') {
foreach (['first_name', 'last_name', 'email'] as $coreField) {
if (! data_get($content, "fields.$coreField.enabled", true)) {
$validator->errors()->add(
"blocks.$i.content.fields.$coreField.enabled",
__('Voornaam, achternaam en e-mail moeten ingeschakeld blijven.')
);
}
}
}
}
if ($formCount > 1) {
$validator->errors()->add('blocks', __('Er mag maximaal één registratieformulier-blok zijn.'));
}
}
/**
* @return array<string, array<int, string|ValidationRule>>
*/
private static function rulesForType(string $type): array
{
$icons = 'ticket,clock,mail,users,star,heart,gift,music,shield,check';
return match ($type) {
'hero' => [
'headline' => ['required', 'string', 'max:255'],
'subheadline' => ['nullable', 'string', 'max:5000'],
'eyebrow_text' => ['nullable', 'string', 'max:255'],
'eyebrow_style' => ['nullable', 'string', 'in:badge,text,none'],
'text_alignment' => ['nullable', 'string', 'in:center,left,right'],
],
'image' => [
'image' => ['nullable', 'string', 'max:500'],
'link_url' => ['nullable', 'string', 'max:500'],
'alt' => ['nullable', 'string', 'max:255'],
'max_width_px' => ['nullable', 'integer', 'min:48', 'max:800'],
'text_alignment' => ['nullable', 'string', 'in:center,left,right'],
],
'benefits' => [
'title' => ['nullable', 'string', 'max:255'],
'items' => ['required', 'array', 'min:1'],
'items.*.icon' => ['required', 'string', "in:$icons"],
'items.*.text' => ['required', 'string', 'max:500'],
'layout' => ['nullable', 'string', 'in:list,grid'],
'max_columns' => ['nullable', 'integer', 'in:1,2,3'],
],
'social_proof' => [
'template' => ['required', 'string', 'max:255'],
'min_count' => ['required', 'integer', 'min:0'],
'show_animation' => ['sometimes', 'boolean'],
'style' => ['nullable', 'string', 'in:pill,badge,plain'],
],
'form' => [
'title' => ['nullable', 'string', 'max:255'],
'description' => ['nullable', 'string', 'max:2000'],
'button_label' => ['required', 'string', 'max:120'],
'button_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'button_text_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'show_field_icons' => ['sometimes', 'boolean'],
'privacy_text' => ['nullable', 'string', 'max:2000'],
'privacy_url' => ['nullable', 'string', 'max:500'],
'fields' => ['required', 'array'],
'fields.first_name.enabled' => ['sometimes', 'boolean'],
'fields.first_name.required' => ['sometimes', 'boolean'],
'fields.first_name.label' => ['nullable', 'string', 'max:120'],
'fields.first_name.placeholder' => ['nullable', 'string', 'max:255'],
'fields.last_name.enabled' => ['sometimes', 'boolean'],
'fields.last_name.required' => ['sometimes', 'boolean'],
'fields.last_name.label' => ['nullable', 'string', 'max:120'],
'fields.last_name.placeholder' => ['nullable', 'string', 'max:255'],
'fields.email.enabled' => ['sometimes', 'boolean'],
'fields.email.required' => ['sometimes', 'boolean'],
'fields.email.label' => ['nullable', 'string', 'max:120'],
'fields.email.placeholder' => ['nullable', 'string', 'max:255'],
'fields.phone.enabled' => ['sometimes', 'boolean'],
'fields.phone.required' => ['sometimes', 'boolean'],
'fields.phone.label' => ['nullable', 'string', 'max:120'],
'fields.phone.placeholder' => ['nullable', 'string', 'max:255'],
],
'countdown' => [
'target_datetime' => ['required', 'date'],
'title' => ['nullable', 'string', 'max:255'],
'expired_action' => ['required', 'string', 'in:hide,show_message,reload'],
'expired_message' => ['nullable', 'string', 'max:1000'],
'style' => ['nullable', 'string', 'in:large,compact'],
'show_labels' => ['sometimes', 'boolean'],
'labels' => ['nullable', 'array'],
'labels.days' => ['nullable', 'string', 'max:32'],
'labels.hours' => ['nullable', 'string', 'max:32'],
'labels.minutes' => ['nullable', 'string', 'max:32'],
'labels.seconds' => ['nullable', 'string', 'max:32'],
],
'text' => [
'title' => ['nullable', 'string', 'max:255'],
'body' => ['required', 'string', 'max:20000'],
'text_size' => ['nullable', 'string', 'in:sm,base,lg'],
'text_alignment' => ['nullable', 'string', 'in:center,left,right'],
],
'cta_banner' => [
'text' => ['required', 'string', 'max:500'],
'button_label' => ['required', 'string', 'max:120'],
'button_url' => ['required', 'string', 'url:http,https', 'max:500'],
'button_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'style' => ['nullable', 'string', 'in:inline,stacked'],
],
'divider' => [
'style' => ['required', 'string', 'in:line,dots,space_only'],
'spacing' => ['required', 'string', 'in:small,medium,large'],
],
default => [],
};
}
}

View File

@@ -19,7 +19,14 @@ class StorePreregistrationPageRequest extends FormRequest
protected function prepareForValidation(): void
{
$this->preparePreregistrationPageFields();
$this->preparePreregistrationPageSettings();
$opacity = $this->input('background_overlay_opacity');
if ($opacity === null || $opacity === '') {
$this->merge(['background_overlay_opacity' => 50]);
}
if ($this->input('background_overlay_color') === null || $this->input('background_overlay_color') === '') {
$this->merge(['background_overlay_color' => '#000000']);
}
}
/**
@@ -27,6 +34,6 @@ class StorePreregistrationPageRequest extends FormRequest
*/
public function rules(): array
{
return $this->preregistrationPageRules();
return $this->preregistrationPageSettingsRules();
}
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Http\Requests\Admin;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
class UpdatePreregistrationPageRequest extends FormRequest
@@ -23,7 +24,21 @@ class UpdatePreregistrationPageRequest extends FormRequest
protected function prepareForValidation(): void
{
$this->preparePreregistrationPageFields();
$this->preparePreregistrationPageSettings();
$blocks = $this->input('blocks');
if (! is_array($blocks)) {
return;
}
$merged = [];
foreach ($blocks as $key => $row) {
if (! is_array($row)) {
continue;
}
$row['is_visible'] = filter_var($row['is_visible'] ?? false, FILTER_VALIDATE_BOOLEAN);
$row['remove_block_image'] = filter_var($row['remove_block_image'] ?? false, FILTER_VALIDATE_BOOLEAN);
$merged[$key] = $row;
}
$this->merge(['blocks' => $merged]);
}
/**
@@ -31,6 +46,20 @@ class UpdatePreregistrationPageRequest extends FormRequest
*/
public function rules(): array
{
return $this->preregistrationPageRules();
return array_merge(
$this->preregistrationPageSettingsRules(),
$this->preregistrationPageBlocksRules(),
);
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
$blocks = $this->input('blocks');
if (! is_array($blocks)) {
return;
}
PageBlockContentValidator::validateBlocksArray($validator, $blocks);
});
}
}

View File

@@ -4,32 +4,50 @@ declare(strict_types=1);
namespace App\Http\Requests\Admin;
use App\Models\PageBlock;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\Rule;
trait ValidatesPreregistrationPageInput
{
/**
* @return array<string, array<int, ValidationRule|string>>
*/
protected function preregistrationPageRules(): array
protected function preregistrationPageSettingsRules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'heading' => ['required', 'string', 'max:255'],
'intro_text' => ['nullable', 'string'],
'thank_you_message' => ['nullable', 'string'],
'expired_message' => ['nullable', 'string'],
'ticketshop_url' => ['nullable', 'string', 'url:http,https', 'max:255'],
'post_submit_redirect_url' => ['nullable', 'string', 'url:http,https', 'max:500'],
'background_overlay_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'background_overlay_opacity' => ['nullable', 'integer', 'min:0', 'max:100'],
'page_background' => ['nullable', 'file', 'image', 'mimes:jpeg,png,jpg,webp', 'max:5120'],
'remove_page_background' => ['sometimes', 'boolean'],
'start_date' => ['required', 'date'],
'end_date' => ['required', 'date', 'after:start_date'],
'phone_enabled' => ['sometimes', 'boolean'],
'is_active' => ['sometimes', 'boolean'],
'background_image' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:5120'],
'logo_image' => ['nullable', 'file', 'mimes:jpeg,png,jpg,webp,svg', 'max:2048'],
];
}
protected function preparePreregistrationPageFields(): void
/**
* @return array<string, array<int, ValidationRule|string>>
*/
protected function preregistrationPageBlocksRules(): array
{
return [
'blocks' => ['required', 'array', 'min:1'],
'blocks.*.type' => ['required', 'string', Rule::in(PageBlock::TYPES)],
'blocks.*.sort_order' => ['required', 'integer', 'min:0', 'max:9999'],
'blocks.*.is_visible' => ['sometimes', 'boolean'],
'blocks.*.content' => ['required', 'array'],
'blocks.*.remove_block_image' => ['sometimes', 'boolean'],
'blocks.*.block_image' => ['nullable', 'file', 'mimes:jpeg,png,jpg,webp,svg', 'max:5120'],
];
}
protected function preparePreregistrationPageSettings(): void
{
$ticketshop = $this->input('ticketshop_url');
$ticketshopNormalized = null;
@@ -37,10 +55,39 @@ trait ValidatesPreregistrationPageInput
$ticketshopNormalized = trim($ticketshop);
}
$redirect = $this->input('post_submit_redirect_url');
$redirectNormalized = null;
if (is_string($redirect) && trim($redirect) !== '') {
$redirectNormalized = trim($redirect);
}
$opacity = $this->input('background_overlay_opacity');
$opacityInt = null;
if ($opacity !== null && $opacity !== '') {
$opacityInt = max(0, min(100, (int) $opacity));
}
$this->merge([
'phone_enabled' => $this->boolean('phone_enabled'),
'is_active' => $this->boolean('is_active'),
'remove_page_background' => $this->boolean('remove_page_background'),
'ticketshop_url' => $ticketshopNormalized,
'post_submit_redirect_url' => $redirectNormalized,
'background_overlay_opacity' => $opacityInt,
]);
}
/**
* @deprecated use preregistrationPageSettingsRules
*
* @return array<string, array<int, ValidationRule|string>>
*/
protected function preregistrationPageRules(): array
{
return array_merge($this->preregistrationPageSettingsRules(), $this->preregistrationPageBlocksRules());
}
protected function preparePreregistrationPageFields(): void
{
$this->preparePreregistrationPageSettings();
}
}