Files
preregister/app/Models/PreregistrationPage.php
bert.hausmans 89931b817d feat(admin): Weeztix setup wizard, integration status badges
- Summary view when Weeztix is configured; edits only via 3-step wizard
- Step 1: reuse or replace OAuth client ID/secret; callback URL shown
- Step 2: OAuth connect (resume wizard after callback when started from wizard)
- Step 3: coupon, prefix, usage; finishing exits wizard
- PreregistrationPage: mailwizz/weeztix integration status helpers
- Pages index: Integrations column with MW/WZ badges; edit page: status cards

Made-with: Cursor
2026-04-05 11:12:10 +02:00

260 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
class PreregistrationPage extends Model
{
use HasFactory;
protected $fillable = [
'slug',
'user_id',
'title',
'heading',
'intro_text',
'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',
'background_fixed',
'logo_image',
'is_active',
];
protected function casts(): array
{
return [
'start_date' => 'datetime',
'end_date' => 'datetime',
'phone_enabled' => 'boolean',
'background_fixed' => 'boolean',
'is_active' => 'boolean',
];
}
/**
* Route model binding uses 'slug' instead of 'id'.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function subscribers(): HasMany
{
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;
}
/**
* When the form block marks the phone field as required (only applies if phone is enabled).
*/
public function isPhoneFieldRequiredForSubscribers(): bool
{
$form = $this->getBlockByType('form');
if ($form !== null) {
return (bool) data_get($form->content, 'fields.phone.required', false);
}
return false;
}
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);
}
public function weeztixConfig(): HasOne
{
return $this->hasOne(WeeztixConfig::class);
}
public function isBeforeStart(): bool
{
return Carbon::now()->lt($this->start_date);
}
public function isActive(): bool
{
$now = Carbon::now();
return $now->gte($this->start_date) && $now->lte($this->end_date);
}
public function isExpired(): bool
{
return Carbon::now()->gt($this->end_date);
}
public function scopeActive(Builder $query): Builder
{
return $query->where('is_active', true);
}
/**
* Lifecycle label for admin tables: before registration opens, open window, or after end.
*/
public function statusKey(): string
{
if ($this->isBeforeStart()) {
return 'before_start';
}
if ($this->isExpired()) {
return 'expired';
}
return 'active';
}
/**
* Mailwizz setup depth for admin UI (API key + list + email field = ready to sync).
*
* @return 'none'|'partial'|'ready'
*/
public function mailwizzIntegrationStatus(): string
{
$c = $this->mailwizzConfig;
if ($c === null) {
return 'none';
}
$key = $c->api_key;
if (! is_string($key) || $key === '') {
return 'partial';
}
if (! is_string($c->list_uid) || $c->list_uid === '' || ! is_string($c->field_email) || $c->field_email === '') {
return 'partial';
}
return 'ready';
}
/**
* Weeztix setup depth for admin UI.
*
* @return 'none'|'credentials'|'connected'|'ready'
*/
public function weeztixIntegrationStatus(): string
{
$c = $this->weeztixConfig;
if ($c === null) {
return 'none';
}
$hasClient = is_string($c->client_id) && $c->client_id !== '';
if (! $hasClient) {
return 'none';
}
if (! $c->is_connected) {
return 'credentials';
}
if (! is_string($c->coupon_guid) || $c->coupon_guid === '') {
return 'connected';
}
return 'ready';
}
}