Implemented a block editor for changing the layout of the page
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
162
app/Http/Requests/Admin/PageBlockContentValidator.php
Normal file
162
app/Http/Requests/Admin/PageBlockContentValidator.php
Normal 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 => [],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -144,7 +144,7 @@ class SyncSubscriberToMailwizz implements ShouldBeUnique, ShouldQueue
|
||||
string $listUid
|
||||
): void {
|
||||
$page = $subscriber->preregistrationPage;
|
||||
$data = $this->buildBasePayload($subscriber, $config, (bool) $page->phone_enabled);
|
||||
$data = $this->buildBasePayload($subscriber, $config, $page->isPhoneFieldEnabledForSubscribers());
|
||||
$tagField = $config->tag_field;
|
||||
$tagValue = $config->tag_value;
|
||||
if ($tagField !== null && $tagField !== '' && $tagValue !== null && $tagValue !== '') {
|
||||
@@ -162,7 +162,7 @@ class SyncSubscriberToMailwizz implements ShouldBeUnique, ShouldQueue
|
||||
string $subscriberUid
|
||||
): void {
|
||||
$page = $subscriber->preregistrationPage;
|
||||
$data = $this->buildBasePayload($subscriber, $config, (bool) $page->phone_enabled);
|
||||
$data = $this->buildBasePayload($subscriber, $config, $page->isPhoneFieldEnabledForSubscribers());
|
||||
|
||||
$tagField = $config->tag_field;
|
||||
$tagValue = $config->tag_value;
|
||||
|
||||
56
app/Models/PageBlock.php
Normal file
56
app/Models/PageBlock.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PageBlock extends Model
|
||||
{
|
||||
public const TYPES = [
|
||||
'hero',
|
||||
'image',
|
||||
'benefits',
|
||||
'social_proof',
|
||||
'form',
|
||||
'countdown',
|
||||
'text',
|
||||
'cta_banner',
|
||||
'divider',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'preregistration_page_id',
|
||||
'type',
|
||||
'content',
|
||||
'sort_order',
|
||||
'is_visible',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'content' => 'array',
|
||||
'is_visible' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function preregistrationPage(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PreregistrationPage::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Blade dynamic component name (kebab-case where needed).
|
||||
*/
|
||||
public function bladeComponentName(): string
|
||||
{
|
||||
return match ($this->type) {
|
||||
'social_proof' => 'blocks.social-proof',
|
||||
'cta_banner' => 'blocks.cta-banner',
|
||||
default => 'blocks.'.$this->type,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,13 @@ class PreregistrationPage extends Model
|
||||
'thank_you_message',
|
||||
'expired_message',
|
||||
'ticketshop_url',
|
||||
'post_submit_redirect_url',
|
||||
'start_date',
|
||||
'end_date',
|
||||
'phone_enabled',
|
||||
'background_image',
|
||||
'background_overlay_color',
|
||||
'background_overlay_opacity',
|
||||
'logo_image',
|
||||
'is_active',
|
||||
];
|
||||
@@ -61,6 +64,84 @@ class PreregistrationPage extends Model
|
||||
return $this->hasMany(Subscriber::class);
|
||||
}
|
||||
|
||||
public function blocks(): HasMany
|
||||
{
|
||||
return $this->hasMany(PageBlock::class)->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function visibleBlocks(): HasMany
|
||||
{
|
||||
return $this->blocks()->where('is_visible', true);
|
||||
}
|
||||
|
||||
public function getBlockByType(string $type): ?PageBlock
|
||||
{
|
||||
if ($this->relationLoaded('blocks')) {
|
||||
return $this->blocks->firstWhere('type', $type);
|
||||
}
|
||||
|
||||
return $this->blocks()->where('type', $type)->first();
|
||||
}
|
||||
|
||||
public function getFormBlock(): ?PageBlock
|
||||
{
|
||||
return $this->getBlockByType('form');
|
||||
}
|
||||
|
||||
public function getHeroBlock(): ?PageBlock
|
||||
{
|
||||
return $this->getBlockByType('hero');
|
||||
}
|
||||
|
||||
/**
|
||||
* Phone field is shown and validated when enabled in the form block (falls back to legacy column).
|
||||
*/
|
||||
public function isPhoneFieldEnabledForSubscribers(): bool
|
||||
{
|
||||
$form = $this->getBlockByType('form');
|
||||
if ($form !== null) {
|
||||
return (bool) data_get($form->content, 'fields.phone.enabled', false);
|
||||
}
|
||||
|
||||
return (bool) $this->phone_enabled;
|
||||
}
|
||||
|
||||
public function headlineForMeta(): string
|
||||
{
|
||||
$hero = $this->getHeroBlock();
|
||||
if ($hero !== null) {
|
||||
$headline = data_get($hero->content, 'headline');
|
||||
if (is_string($headline) && $headline !== '') {
|
||||
return $headline;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->heading;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps legacy DB columns aligned with hero/form blocks until those columns are dropped.
|
||||
*/
|
||||
public function syncLegacyContentColumnsFromBlocks(): void
|
||||
{
|
||||
$this->load('blocks');
|
||||
$hero = $this->getHeroBlock();
|
||||
$form = $this->getFormBlock();
|
||||
$updates = [];
|
||||
if ($hero !== null) {
|
||||
$c = $hero->content ?? [];
|
||||
$updates['heading'] = is_string($c['headline'] ?? null) ? $c['headline'] : $this->heading;
|
||||
$sub = $c['subheadline'] ?? null;
|
||||
$updates['intro_text'] = is_string($sub) ? $sub : null;
|
||||
}
|
||||
if ($form !== null) {
|
||||
$updates['phone_enabled'] = (bool) data_get($form->content, 'fields.phone.enabled', false);
|
||||
}
|
||||
if ($updates !== []) {
|
||||
$this->update($updates);
|
||||
}
|
||||
}
|
||||
|
||||
public function mailwizzConfig(): HasOne
|
||||
{
|
||||
return $this->hasOne(MailwizzConfig::class);
|
||||
|
||||
251
app/Services/PreregistrationPageBlockWriter.php
Normal file
251
app/Services/PreregistrationPageBlockWriter.php
Normal file
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\PageBlock;
|
||||
use App\Models\PreregistrationPage;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class PreregistrationPageBlockWriter
|
||||
{
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $blocks
|
||||
*/
|
||||
public function replaceBlocks(PreregistrationPage $page, array $blocks, Request $request): void
|
||||
{
|
||||
DB::transaction(function () use ($page, $blocks, $request): void {
|
||||
$page->load('blocks');
|
||||
$oldPaths = $this->collectBlockImagePaths($page);
|
||||
$page->blocks()->delete();
|
||||
|
||||
$orderedKeys = array_keys($blocks);
|
||||
usort($orderedKeys, function (string|int $a, string|int $b) use ($blocks): int {
|
||||
/** @var array<string, mixed> $rowA */
|
||||
$rowA = $blocks[$a];
|
||||
/** @var array<string, mixed> $rowB */
|
||||
$rowB = $blocks[$b];
|
||||
|
||||
return ((int) ($rowA['sort_order'] ?? 0)) <=> ((int) ($rowB['sort_order'] ?? 0));
|
||||
});
|
||||
|
||||
$allNewPaths = [];
|
||||
foreach ($orderedKeys as $blockKey) {
|
||||
/** @var array<string, mixed> $blockRow */
|
||||
$blockRow = $blocks[$blockKey];
|
||||
$type = (string) $blockRow['type'];
|
||||
/** @var array<string, mixed> $content */
|
||||
$content = is_array($blockRow['content'] ?? null) ? $blockRow['content'] : [];
|
||||
if ($type === 'text' && array_key_exists('body', $content) && is_string($content['body'])) {
|
||||
$content['body'] = ltrim($content['body']);
|
||||
}
|
||||
if ($type === 'image') {
|
||||
$content = $this->mergeImageBlockFiles($page, (string) $blockKey, $content, $request);
|
||||
}
|
||||
$allNewPaths = array_merge($allNewPaths, $this->pathsFromImageTypeContent($type, $content));
|
||||
|
||||
PageBlock::query()->create([
|
||||
'preregistration_page_id' => $page->id,
|
||||
'type' => $type,
|
||||
'content' => $content,
|
||||
'sort_order' => (int) ($blockRow['sort_order'] ?? 0),
|
||||
'is_visible' => (bool) ($blockRow['is_visible'] ?? true),
|
||||
]);
|
||||
}
|
||||
|
||||
$allNewPaths = array_values(array_unique($allNewPaths));
|
||||
foreach (array_diff($oldPaths, $allNewPaths) as $orphan) {
|
||||
if (is_string($orphan) && $orphan !== '') {
|
||||
Storage::disk('public')->delete($orphan);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function seedDefaultBlocks(PreregistrationPage $page): void
|
||||
{
|
||||
$rows = [
|
||||
[
|
||||
'type' => 'hero',
|
||||
'sort_order' => 0,
|
||||
'is_visible' => true,
|
||||
'content' => [
|
||||
'headline' => $page->title,
|
||||
'subheadline' => '',
|
||||
'eyebrow_text' => '',
|
||||
'eyebrow_style' => 'badge',
|
||||
'text_alignment' => 'center',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$next = 1;
|
||||
if ($page->start_date->isFuture()) {
|
||||
$rows[] = [
|
||||
'type' => 'countdown',
|
||||
'sort_order' => $next,
|
||||
'is_visible' => true,
|
||||
'content' => [
|
||||
'target_datetime' => $page->start_date->toIso8601String(),
|
||||
'title' => 'De pre-registratie opent over:',
|
||||
'expired_action' => 'reload',
|
||||
'style' => 'large',
|
||||
'show_labels' => true,
|
||||
'labels' => [
|
||||
'days' => 'dagen',
|
||||
'hours' => 'uren',
|
||||
'minutes' => 'minuten',
|
||||
'seconds' => 'seconden',
|
||||
],
|
||||
],
|
||||
];
|
||||
$next++;
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
'type' => 'benefits',
|
||||
'sort_order' => $next,
|
||||
'is_visible' => true,
|
||||
'content' => [
|
||||
'title' => 'Waarom voorregistreren?',
|
||||
'items' => [
|
||||
['icon' => 'ticket', 'text' => 'Exclusieve korting op tickets'],
|
||||
['icon' => 'clock', 'text' => 'Eerder toegang tot de ticketshop'],
|
||||
],
|
||||
'layout' => 'list',
|
||||
'max_columns' => 2,
|
||||
],
|
||||
];
|
||||
$next++;
|
||||
|
||||
$rows[] = [
|
||||
'type' => 'social_proof',
|
||||
'sort_order' => $next,
|
||||
'is_visible' => true,
|
||||
'content' => [
|
||||
'template' => 'Al {count} bezoekers aangemeld!',
|
||||
'min_count' => 10,
|
||||
'show_animation' => true,
|
||||
'style' => 'pill',
|
||||
],
|
||||
];
|
||||
$next++;
|
||||
|
||||
$rows[] = [
|
||||
'type' => 'form',
|
||||
'sort_order' => $next,
|
||||
'is_visible' => true,
|
||||
'content' => [
|
||||
'title' => 'Registreer nu',
|
||||
'description' => '',
|
||||
'button_label' => 'Registreer nu!',
|
||||
'button_color' => '#F47B20',
|
||||
'button_text_color' => '#FFFFFF',
|
||||
'fields' => [
|
||||
'first_name' => [
|
||||
'enabled' => true,
|
||||
'required' => true,
|
||||
'label' => 'Voornaam',
|
||||
'placeholder' => 'Je voornaam',
|
||||
],
|
||||
'last_name' => [
|
||||
'enabled' => true,
|
||||
'required' => true,
|
||||
'label' => 'Achternaam',
|
||||
'placeholder' => 'Je achternaam',
|
||||
],
|
||||
'email' => [
|
||||
'enabled' => true,
|
||||
'required' => true,
|
||||
'label' => 'E-mailadres',
|
||||
'placeholder' => 'je@email.nl',
|
||||
],
|
||||
'phone' => [
|
||||
'enabled' => false,
|
||||
'required' => false,
|
||||
'label' => 'Mobiel',
|
||||
'placeholder' => '+31 6 12345678',
|
||||
],
|
||||
],
|
||||
'show_field_icons' => true,
|
||||
'privacy_text' => 'Door je te registreren ga je akkoord met onze privacyverklaring.',
|
||||
'privacy_url' => '',
|
||||
],
|
||||
];
|
||||
|
||||
$page->blocks()->createMany($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function collectBlockImagePaths(PreregistrationPage $page): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach ($page->blocks as $b) {
|
||||
if ($b->type !== 'image') {
|
||||
continue;
|
||||
}
|
||||
$paths = array_merge($paths, $this->pathsFromImageTypeContent('image', is_array($b->content) ? $b->content : []));
|
||||
}
|
||||
|
||||
return array_values(array_unique($paths));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $content
|
||||
* @return list<string>
|
||||
*/
|
||||
private function pathsFromImageTypeContent(string $type, array $content): array
|
||||
{
|
||||
if ($type !== 'image') {
|
||||
return [];
|
||||
}
|
||||
$p = data_get($content, 'image');
|
||||
if (is_string($p) && $p !== '') {
|
||||
return [$p];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $content
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function mergeImageBlockFiles(PreregistrationPage $page, string $blockKey, array $content, Request $request): array
|
||||
{
|
||||
$disk = Storage::disk('public');
|
||||
$dir = "preregister/pages/{$page->id}";
|
||||
|
||||
$link = $content['link_url'] ?? null;
|
||||
$content['link_url'] = is_string($link) && trim($link) !== '' ? trim($link) : null;
|
||||
|
||||
if ($request->boolean("blocks.$blockKey.remove_block_image")) {
|
||||
$prev = $content['image'] ?? null;
|
||||
if (is_string($prev) && $prev !== '') {
|
||||
$disk->delete($prev);
|
||||
}
|
||||
$content['image'] = null;
|
||||
}
|
||||
|
||||
$file = $request->file("blocks.$blockKey.block_image");
|
||||
if ($this->isValidUpload($file)) {
|
||||
if (isset($content['image']) && is_string($content['image']) && $content['image'] !== '') {
|
||||
$disk->delete($content['image']);
|
||||
}
|
||||
$content['image'] = $file->store($dir, 'public');
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function isValidUpload(?UploadedFile $file): bool
|
||||
{
|
||||
return $file !== null && $file->isValid();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user