Zeige auf der Shopkachel Titelbild, Saeulen, Materialart mit Symbol und Marker
- ShopCatalog als gemeinsame Datenquelle der Kacheln, shop/kachel als Partial - Titelbild nur mit Website-Freigabe (PresentationMedia::coverPresentation), schmueckend mit leerem alt - Symbol je Materialart (Stammdaten), Marker Neu, Aktualisiert, Beliebt mit Einstellung am Artikel - Migration 2026_12_01_100000, rein ergaenzend - ADR-54, Plan, DEPLOYMENT.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
db3a6ba779
commit
0ac70884cc
18 files changed
+991
-88
No files matched your search
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Commerce;
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Domain\Catalog\LicenseArticle;
|
||||
use App\Domain\Catalog\OfferVersion;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Produktmarker „Neu“, „Aktualisiert“ und „Beliebt“ an einer Kachel (ADR-54).
|
||||
*
|
||||
* „Neu“ und „Aktualisiert“ folgen automatisch den Daten (erstes Angebot als Kauf, neuer Stand), jeweils für eine feste
|
||||
* Zahl Tage (`shop.marker_days`); die Redaktion kann sie auch fest an- oder abschalten. „Beliebt“ setzt die Redaktion
|
||||
* von Hand, solange es keine Verkaufszahlen gibt. Ein Marker trägt immer sein Wort, nie nur Farbe oder Symbol.
|
||||
*/
|
||||
class ShopMarkers
|
||||
{
|
||||
public const KEYS = ['neu' => 'Neu', 'aktualisiert' => 'Aktualisiert', 'beliebt' => 'Beliebt'];
|
||||
|
||||
/** Erlaubte Einstellungen je Marker; der erste Wert ist die Vorgabe. */
|
||||
public const CHOICES = [
|
||||
'neu' => ['auto' => 'Automatisch (zeitlich begrenzt nach dem ersten Angebot)', 'an' => 'Immer anzeigen', 'aus' => 'Nie anzeigen'],
|
||||
'aktualisiert' => ['auto' => 'Automatisch (zeitlich begrenzt nach einem neuen Stand)', 'an' => 'Immer anzeigen', 'aus' => 'Nie anzeigen'],
|
||||
'beliebt' => ['aus' => 'Nicht anzeigen', 'an' => 'Anzeigen'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Die Einstellungen des Artikels mit den Vorgaben für fehlende oder ungültige Werte.
|
||||
*
|
||||
* @return array{neu: string, aktualisiert: string, beliebt: string}
|
||||
*/
|
||||
public function settings(LicenseArticle $article): array
|
||||
{
|
||||
$stored = is_array($article->shop_markers) ? $article->shop_markers : [];
|
||||
$settings = [];
|
||||
|
||||
foreach (self::CHOICES as $key => $choices) {
|
||||
$value = $stored[$key] ?? null;
|
||||
$settings[$key] = is_string($value) && array_key_exists($value, $choices) ? $value : array_key_first($choices);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $choices
|
||||
*/
|
||||
public function save(LicenseArticle $article, array $choices): void
|
||||
{
|
||||
$settings = [];
|
||||
|
||||
foreach (self::CHOICES as $key => $allowed) {
|
||||
$value = $choices[$key] ?? array_key_first($allowed);
|
||||
|
||||
if (! is_string($value) || ! array_key_exists($value, $allowed)) {
|
||||
throw new DomainRuleException('Der Marker „'.self::KEYS[$key].'“ hat eine unzulässige Einstellung.');
|
||||
}
|
||||
|
||||
$settings[$key] = $value;
|
||||
}
|
||||
|
||||
$article->shop_markers = $settings;
|
||||
$article->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Die anzuzeigenden Marker in fester Reihenfolge.
|
||||
*
|
||||
* @param list<int> $offerIds Angebote dieses Artikels (Optionen)
|
||||
* @return list<array{key: string, label: string}>
|
||||
*/
|
||||
public function visible(LicenseArticle $article, array $offerIds, ?Carbon $now = null): array
|
||||
{
|
||||
$now ??= now();
|
||||
$settings = $this->settings($article);
|
||||
$days = max(1, (int) config('shop.marker_days', 30));
|
||||
$limit = $now->copy()->subDays($days);
|
||||
|
||||
$isNew = match ($settings['neu']) {
|
||||
'an' => true,
|
||||
'aus' => false,
|
||||
default => $this->firstOffered($offerIds)?->greaterThanOrEqualTo($limit) ?? false,
|
||||
};
|
||||
|
||||
$isUpdated = match ($settings['aktualisiert']) {
|
||||
'an' => true,
|
||||
'aus' => false,
|
||||
default => ! $isNew && $this->updatedAt($article)?->greaterThanOrEqualTo($limit) === true,
|
||||
};
|
||||
|
||||
$markers = [];
|
||||
|
||||
foreach (['neu' => $isNew, 'aktualisiert' => $isUpdated, 'beliebt' => $settings['beliebt'] === 'an'] as $key => $shown) {
|
||||
if ($shown) {
|
||||
$markers[] = ['key' => $key, 'label' => self::KEYS[$key]];
|
||||
}
|
||||
}
|
||||
|
||||
return $markers;
|
||||
}
|
||||
|
||||
/** Zeitpunkt der ersten Fassung eines Angebots als Kauf. */
|
||||
private function firstOffered(array $offerIds): ?Carbon
|
||||
{
|
||||
if ($offerIds === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$first = OfferVersion::query()->whereIn('offer_id', $offerIds)->where('grant_kind', 'kauf')->min('created_at');
|
||||
|
||||
return $first === null ? null : Carbon::parse($first);
|
||||
}
|
||||
|
||||
/** Zeitpunkt des neuesten Stands, sofern es nicht der erste ist. */
|
||||
private function updatedAt(LicenseArticle $article): ?Carbon
|
||||
{
|
||||
$release = $article->currentRelease;
|
||||
|
||||
return $release !== null && (int) $release->number > 1 && $release->created_at !== null ? Carbon::parse($release->created_at) : null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user