Implemented a block editor for changing the layout of the page
This commit is contained in:
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);
|
||||
|
||||
Reference in New Issue
Block a user