Hinweise werden zu Bannern mit Website, Erscheinungsbildern, Prioritaet und optionalem Sonderangebot (Text, Termin und Link aus dem Angebot, sichtbar nur solange es beworben wird). Sonderangebote lassen sich mit einem Klick als Banner zeigen; die Liste nennt das heute sichtbare Banner. Banner-Texte der Erscheinungsbilder werden zu saisonalen Bannern. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
4.2 KiB
PHP
122 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Models\Concerns\BelongsToTenant;
|
||
use App\Tenancy\TenantContext;
|
||
use Carbon\CarbonInterface;
|
||
use Database\Factories\AppearanceFactory;
|
||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
||
/**
|
||
* A seasonal look of a site (e.g. summer, winter, Christmas, New Year's Eve): colour tokens and texts change,
|
||
* photos and banners made for it (appearance_ids) are shown – the page structure does not (sections may be limited to
|
||
* appearances, see SectionRegistry). Offers are advertised by their own rules (OfferShowcase). The period recurs every year ("MM-DD" to "MM-DD",
|
||
* inclusive; a period may run over the turn of the year). The default appearance applies when none matches.
|
||
*/
|
||
#[Fillable(['site_id', 'name', 'starts_md', 'ends_md', 'priority', 'is_default', 'is_active', 'tokens', 'overrides'])]
|
||
class Appearance extends Model
|
||
{
|
||
/** @use HasFactory<AppearanceFactory> */
|
||
use BelongsToTenant, HasFactory;
|
||
|
||
/**
|
||
* Texts an appearance can replace, with their admin labels. Sections read them through Theme::text().
|
||
*/
|
||
public const OVERRIDABLE = [
|
||
'hero.eyebrow' => 'Titelbereich: kleine Überschrift',
|
||
'hero.title' => 'Titelbereich: Überschrift',
|
||
'hero.text' => 'Titelbereich: Text',
|
||
'cta.title' => 'Abschluss: Überschrift',
|
||
'cta.text' => 'Abschluss: Text',
|
||
];
|
||
|
||
/**
|
||
* Get the attributes that should be cast.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'is_default' => 'boolean',
|
||
'is_active' => 'boolean',
|
||
'tokens' => 'array',
|
||
'overrides' => 'array',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return BelongsTo<Site, $this>
|
||
*/
|
||
public function site(): BelongsTo
|
||
{
|
||
return $this->belongsTo(Site::class);
|
||
}
|
||
|
||
/**
|
||
* Names by id of the landlord's appearances, loaded once per request and landlord.
|
||
*
|
||
* @return array<int, string>
|
||
*/
|
||
public static function names(): array
|
||
{
|
||
$tenantId = app(TenantContext::class)->id();
|
||
|
||
// once() keys its cache by the variables the closure uses, so each landlord gets its own entry.
|
||
return once(fn (): array => self::query()->when($tenantId, fn ($query) => $query->where('tenant_id', $tenantId))->pluck('name', 'id')->all());
|
||
}
|
||
|
||
/**
|
||
* Choices for "Zeigen in" at the photos, e.g. "Winter · 01.11.–31.03."; with several websites prefixed by the website.
|
||
*
|
||
* @return array<int, string>
|
||
*/
|
||
public static function photoOptions(): array
|
||
{
|
||
$appearances = self::query()->with('site')->orderByDesc('is_default')->orderBy('starts_md')->orderBy('name')->get();
|
||
$severalSites = $appearances->pluck('site_id')->unique()->count() > 1;
|
||
|
||
return $appearances->mapWithKeys(fn (Appearance $appearance): array => [
|
||
$appearance->id => ($severalSites ? $appearance->site?->name.': ' : '').$appearance->periodLabel(),
|
||
])->all();
|
||
}
|
||
|
||
/**
|
||
* "Winter · 01.11.–31.03.", "Standard · wenn kein Zeitraum passt".
|
||
*/
|
||
public function periodLabel(): string
|
||
{
|
||
if ($this->starts_md === null || $this->ends_md === null) {
|
||
return $this->name.($this->is_default ? ' · wenn kein Zeitraum passt' : '');
|
||
}
|
||
|
||
$day = fn (string $monthDay): string => substr($monthDay, 3, 2).'.'.substr($monthDay, 0, 2).'.';
|
||
|
||
return $this->name.' · '.$day($this->starts_md).'–'.$day($this->ends_md);
|
||
}
|
||
|
||
public function covers(CarbonInterface $date): bool
|
||
{
|
||
if ($this->starts_md === null || $this->ends_md === null) {
|
||
return false;
|
||
}
|
||
|
||
$day = $date->format('m-d');
|
||
|
||
return $this->starts_md <= $this->ends_md
|
||
? $day >= $this->starts_md && $day <= $this->ends_md
|
||
: $day >= $this->starts_md || $day <= $this->ends_md;
|
||
}
|
||
|
||
public function override(string $key): ?string
|
||
{
|
||
$value = data_get($this->overrides, $key);
|
||
|
||
return filled($value) ? (string) $value : null;
|
||
}
|
||
}
|