, markers: list} */ class ShopCatalog { public function __construct( private readonly ProductOptions $products, private readonly PublicCatalogReader $catalog, private readonly EditorialProfileReader $profiles, private readonly PresentationMedia $presentation, private readonly ShopMarkers $markers, ) {} public const SORTS = ['name' => 'Alphabetisch', 'neu' => 'Neueste zuerst', 'preis' => 'Preis aufsteigend']; /** * @param array{pillar?: int|null, kind?: int|null, sort?: string|null, limit?: int|null} $options Filter nach Säule * und Materialart, Sortierung (name, neu, preis) und Höchstzahl * @return list> */ public function cards(string $term = '', array $options = []): array { $term = mb_strtolower(mb_substr(trim($term), 0, 100)); $cards = []; // Altbestand: Angebote mit mehreren Positionen auf einer freigegebenen Programmseite. $summaries = $this->products->summaries(); foreach (Composition::query()->with(['editorialContentKind', 'pillars'])->whereIn('id', array_keys($summaries))->orderBy('designation')->get() as $composition) { if ($this->catalog->shownVersion($composition) === null) { continue; } $title = (string) ($composition->display_title ?: $composition->designation); $teaser = (string) ($composition->teaser ?: FormattedText::plain($composition->summary)); $pillars = $this->pillarNames($composition->pillars); $card = $this->card('c:'.$composition->id, 'programm', route('public.program', $composition), $title, $teaser, $summaries[$composition->id], 'Programm', null, $pillars, [], $this->image($composition)); $card['pillar_ids'] = $composition->pillars->pluck('id')->map(fn ($id): int => (int) $id)->all(); $card['kind_id'] = $composition->editorial_content_kind_id !== null ? (int) $composition->editorial_content_kind_id : null; if ($this->matches($term, $title.' '.$teaser.' '.FormattedText::plain($composition->summary).' '.$composition->search_terms.' '.implode(' ', $pillars))) { $cards[] = $card; } } // Lizenzartikel mit kaufbarer Option. $articleSummaries = $this->products->articleSummaries(); foreach (LicenseArticle::query()->with(['editorialContentKind', 'pillars', 'currentRelease'])->whereIn('id', array_keys($articleSummaries))->whereNull('archived_at')->orderBy('code')->get() as $article) { $profile = $this->profiles->read($article, 'kachel'); $title = $this->products->articleTitle($article); $teaser = (string) ($profile->value('teaser') ?: $profile->value('summary')); $kind = $article->editorialContentKind; $pillars = $this->pillarNames($article->pillars); $offerIds = array_map(fn ($offer): int => (int) $offer->id, $this->products->offersOfArticle($article)); $card = $this->card( 'a:'.$article->id, 'artikel', route('shop.article', $article), $title, $teaser, $articleSummaries[$article->id], (string) ($kind?->label ?? ''), $this->symbol($kind?->symbol), $pillars, $this->markers->visible($article, $offerIds), $this->image($article), ); $card['pillar_ids'] = $article->pillars->pluck('id')->map(fn ($id): int => (int) $id)->all(); $card['kind_id'] = $kind !== null ? (int) $kind->id : null; $card['since'] = $this->markers->firstOffered($offerIds)?->getTimestamp() ?? 0; if ($this->matches($term, $title.' '.$teaser.' '.$profile->value('summary').' '.$profile->searchTerms.' '.($kind?->label ?? '').' '.implode(' ', $pillars))) { $cards[] = $card; } } // Pakete mit eigener Produktseite. $bundleSummaries = $this->products->bundleSummaries(); foreach (Bundle::query()->whereIn('id', array_keys($bundleSummaries))->orderBy('name')->get() as $bundle) { $contents = $this->products->bundleContents($this->products->forBundle($bundle)); $names = implode(', ', array_column($contents, 'title')); $teaser = 'Paket mit '.count($contents).' '.(count($contents) === 1 ? 'Inhalt' : 'Inhalten').($names !== '' ? ': '.$names : ''); $card = $this->card('b:'.$bundle->id, 'paket', route('public.package', $bundle), (string) $bundle->name, $teaser, $bundleSummaries[$bundle->id], 'Paket', null, [], [], $this->image($bundle)); if ($this->matches($term, $bundle->name.' '.$names)) { $cards[] = $card; } } return $this->arrange($cards, $options); } /** * Filter, Sortierung und Höchstzahl der Kacheln (Widget und Shopseite). Ohne Angabe bleibt die Reihenfolge der * Datenquelle. * * @param list> $cards * @param array $options * @return list> */ private function arrange(array $cards, array $options): array { $pillar = isset($options['pillar']) ? (int) $options['pillar'] : 0; $kind = isset($options['kind']) ? (int) $options['kind'] : 0; if ($pillar > 0) { $cards = array_filter($cards, fn (array $card): bool => in_array($pillar, $card['pillar_ids'] ?? [], true)); } if ($kind > 0) { $cards = array_filter($cards, fn (array $card): bool => ($card['kind_id'] ?? null) === $kind); } $cards = array_values($cards); match ($options['sort'] ?? null) { 'name' => usort($cards, fn (array $a, array $b): int => strcmp(mb_strtolower($a['title']), mb_strtolower($b['title']))), 'neu' => usort($cards, fn (array $a, array $b): int => [$b['since'] ?? 0, mb_strtolower($a['title'])] <=> [$a['since'] ?? 0, mb_strtolower($b['title'])]), 'preis' => usort($cards, fn (array $a, array $b): int => [$a['from_cents'], mb_strtolower($a['title'])] <=> [$b['from_cents'], mb_strtolower($b['title'])]), default => null, }; $limit = isset($options['limit']) ? (int) $options['limit'] : 0; return $limit > 0 ? array_slice($cards, 0, $limit) : $cards; } /** * @param array{from_cents: int, count: int} $summary * @param list $pillars * @param list $markers * @param array{url: string, alt: string}|null $image * @return array */ private function card(string $key, string $kind, string $url, string $title, string $teaser, array $summary, string $kindLabel, ?string $symbol, array $pillars, array $markers, ?array $image): array { return [ 'key' => $key, 'kind' => $kind, 'url' => $url, 'title' => $title, 'teaser' => $teaser, 'from' => Money::format($summary['from_cents']), 'from_cents' => (int) $summary['from_cents'], 'count' => (int) $summary['count'], 'image' => $image, 'kind_label' => $kindLabel, 'symbol' => $symbol, 'pillars' => $pillars, 'markers' => $markers, 'pillar_ids' => [], 'kind_id' => null, 'since' => 0, ]; } private function matches(string $term, string $haystack): bool { return $term === '' || str_contains(mb_strtolower($haystack), $term); } /** * Nur veröffentlichte Säulen sind öffentlich (Säulenverwaltung). * * @param Collection $pillars * @return list */ private function pillarNames(Collection $pillars): array { return $pillars->filter(fn (Pillar $pillar): bool => $pillar->published_at !== null)->map(fn (Pillar $pillar): string => (string) $pillar->name)->values()->all(); } /** Ein Symbol zeigt nur, was die Symbolbibliothek kennt (die Auswahl wird an der Materialart geprüft, hier noch einmal). */ private function symbol(?string $key): ?string { return is_string($key) && $key !== '' && array_key_exists($key, SymbolCatalog::options()) ? $key : null; } /** * @return array{url: string, alt: string}|null */ private function image(Model $subject): ?array { $presentation = $this->presentation->coverPresentation($subject); return $presentation === null ? null : ['url' => $presentation->url, 'alt' => $presentation->alt]; } }