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

@@ -8,6 +8,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StorePreregistrationPageRequest;
use App\Http\Requests\Admin\UpdatePreregistrationPageRequest;
use App\Models\PreregistrationPage;
use App\Services\PreregistrationPageBlockWriter;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -17,8 +18,9 @@ use Illuminate\View\View;
class PageController extends Controller
{
public function __construct()
{
public function __construct(
private readonly PreregistrationPageBlockWriter $blockWriter
) {
$this->authorizeResource(PreregistrationPage::class, 'page');
}
@@ -47,31 +49,32 @@ class PageController extends Controller
public function store(StorePreregistrationPageRequest $request): RedirectResponse
{
$validated = $request->validated();
$background = $request->file('background_image');
$logo = $request->file('logo_image');
unset($validated['background_image'], $validated['logo_image']);
$validated['slug'] = (string) Str::uuid();
$validated['user_id'] = $request->user()->id;
$validated['heading'] = $validated['title'];
$validated['intro_text'] = null;
$validated['phone_enabled'] = false;
$validated['background_image'] = null;
$validated['logo_image'] = null;
$page = DB::transaction(function () use ($validated, $background, $logo): PreregistrationPage {
$page = PreregistrationPage::create($validated);
$paths = [];
if ($background !== null) {
$paths['background_image'] = $background->store("preregister/pages/{$page->id}", 'public');
}
if ($logo !== null) {
$paths['logo_image'] = $logo->store("preregister/pages/{$page->id}", 'public');
}
if ($paths !== []) {
$page->update($paths);
$page = DB::transaction(function () use ($validated, $request): PreregistrationPage {
$page = PreregistrationPage::query()->create($validated);
$this->blockWriter->seedDefaultBlocks($page);
$page = $page->fresh(['blocks']);
$bgFile = $request->file('page_background');
if ($bgFile !== null && $bgFile->isValid()) {
$path = $bgFile->store("preregister/pages/{$page->id}", 'public');
$page->update(['background_image' => $path]);
}
return $page->fresh();
return $page->fresh(['blocks']);
});
$page->syncLegacyContentColumnsFromBlocks();
return redirect()
->route('admin.pages.index')
->route('admin.pages.edit', $page)
->with('status', __('Page created. Public URL: :url', ['url' => url('/r/'.$page->slug)]));
}
@@ -82,44 +85,61 @@ class PageController extends Controller
public function edit(PreregistrationPage $page): View
{
$page->load(['blocks' => fn ($q) => $q->orderBy('sort_order')]);
return view('admin.pages.edit', compact('page'));
}
public function update(UpdatePreregistrationPageRequest $request, PreregistrationPage $page): RedirectResponse
{
$validated = $request->validated();
$background = $request->file('background_image');
$logo = $request->file('logo_image');
unset($validated['background_image'], $validated['logo_image']);
/** @var array<string|int, array<string, mixed>> $blocks */
$blocks = $validated['blocks'];
unset($validated['blocks']);
DB::transaction(function () use ($validated, $background, $logo, $page): void {
if ($background !== null) {
if ($page->background_image !== null) {
Storage::disk('public')->delete($page->background_image);
DB::transaction(function () use ($validated, $blocks, $request, $page): void {
$disk = Storage::disk('public');
if ($request->boolean('remove_page_background')) {
if (is_string($page->background_image) && $page->background_image !== '') {
$disk->delete($page->background_image);
}
$validated['background_image'] = $background->store("preregister/pages/{$page->id}", 'public');
$validated['background_image'] = null;
}
if ($logo !== null) {
if ($page->logo_image !== null) {
Storage::disk('public')->delete($page->logo_image);
$bgFile = $request->file('page_background');
if ($bgFile !== null && $bgFile->isValid()) {
if (is_string($page->background_image) && $page->background_image !== '') {
$disk->delete($page->background_image);
}
$validated['logo_image'] = $logo->store("preregister/pages/{$page->id}", 'public');
$validated['background_image'] = $bgFile->store("preregister/pages/{$page->id}", 'public');
}
$page->update($validated);
$this->blockWriter->replaceBlocks($page, $blocks, $request);
});
$page->fresh(['blocks'])->syncLegacyContentColumnsFromBlocks();
return redirect()
->route('admin.pages.index')
->route('admin.pages.edit', $page)
->with('status', __('Page updated.'));
}
public function destroy(PreregistrationPage $page): RedirectResponse
{
DB::transaction(function () use ($page): void {
if ($page->background_image !== null) {
$page->load('blocks');
foreach ($page->blocks as $block) {
if ($block->type !== 'image') {
continue;
}
$path = data_get($block->content, 'image');
if (is_string($path) && $path !== '') {
Storage::disk('public')->delete($path);
}
}
if ($page->background_image !== null && $page->background_image !== '') {
Storage::disk('public')->delete($page->background_image);
}
if ($page->logo_image !== null) {
if ($page->logo_image !== null && $page->logo_image !== '') {
Storage::disk('public')->delete($page->logo_image);
}
$page->delete();

View File

@@ -70,7 +70,7 @@ class SubscriberController extends Controller
->orderBy('created_at')
->get();
$phoneEnabled = $page->phone_enabled;
$phoneEnabled = $page->isPhoneFieldEnabledForSubscribers();
return response()->streamDownload(function () use ($subscribers, $phoneEnabled): void {
$handle = fopen('php://output', 'w');

View File

@@ -6,15 +6,31 @@ namespace App\Http\Controllers;
use App\Http\Requests\SubscribePublicPageRequest;
use App\Jobs\SyncSubscriberToMailwizz;
use App\Models\PageBlock;
use App\Models\PreregistrationPage;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\View\View;
class PublicPageController extends Controller
{
public function show(PreregistrationPage $publicPage): View
{
return view('public.page', ['page' => $publicPage]);
$publicPage->load(['blocks' => fn ($q) => $q->orderBy('sort_order')]);
$pageState = $this->resolvePageState($publicPage);
$blocksToRender = $this->filterBlocksForPageState($publicPage, $pageState);
$subscriberCount = $publicPage->subscribers()->count();
$title = $publicPage->headlineForMeta();
return view('public.page', [
'page' => $publicPage,
'pageState' => $pageState,
'blocksToRender' => $blocksToRender,
'subscriberCount' => $subscriberCount,
'title' => $title,
]);
}
public function subscribe(SubscribePublicPageRequest $request, PreregistrationPage $publicPage): JsonResponse
@@ -45,4 +61,35 @@ class PublicPageController extends Controller
'message' => $publicPage->thank_you_message ?? __('Thank you for registering!'),
]);
}
private function resolvePageState(PreregistrationPage $page): string
{
if ($page->isBeforeStart()) {
return 'countdown';
}
if ($page->isExpired()) {
return 'expired';
}
return 'active';
}
/**
* @return Collection<int, PageBlock>
*/
private function filterBlocksForPageState(PreregistrationPage $page, string $pageState): Collection
{
return $page->visibleBlocks()
->orderBy('sort_order')
->get()
->filter(function (PageBlock $block) use ($pageState): bool {
return match ($pageState) {
'countdown' => in_array($block->type, ['hero', 'countdown', 'image'], true),
'expired' => in_array($block->type, ['hero', 'image', 'cta_banner'], true),
default => true,
};
})
->values();
}
}

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();
}
}

View File

@@ -22,6 +22,7 @@ class SubscribePublicPageRequest extends FormRequest
{
/** @var PreregistrationPage $page */
$page = $this->route('publicPage');
$page->loadMissing('blocks');
$emailRule = (new Email)
->rfcCompliant()
@@ -31,7 +32,7 @@ class SubscribePublicPageRequest extends FormRequest
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'max:255', $emailRule],
'phone' => $page->phone_enabled
'phone' => $page->isPhoneFieldEnabledForSubscribers()
? ['nullable', 'string', 'regex:/^[0-9]{8,15}$/']
: ['nullable', 'string', 'max:255'],
];
@@ -73,7 +74,7 @@ class SubscribePublicPageRequest extends FormRequest
/** @var PreregistrationPage $page */
$page = $this->route('publicPage');
$phone = $this->input('phone');
if (! $page->phone_enabled) {
if (! $page->isPhoneFieldEnabledForSubscribers()) {
$this->merge(['phone' => null]);
return;