, 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, holder?: string|null, scope?: string|null, sort?: string|null, limit?: int|null} $options Filter * nach Säule, Materialart, Eignung und Nutzerkreis, Sortierung (name, neu, preis) und Höchstzahl * @return list> */ public function cards(string $term = '', array $options = []): array { return $this->arrange($this->all($term), $options); } /** * Alle Kacheln zu einem Suchbegriff, ungefiltert und in der Reihenfolge der Quellen. * * @return list> */ public function all(string $term = ''): 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)); $pillarMap = $this->publishedPillars($composition->pillars); $pillars = array_values($pillarMap); $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'] = array_keys($pillarMap); $card['kind_id'] = $composition->editorial_content_kind_id !== null ? (int) $composition->editorial_content_kind_id : null; $card['holders'] = $summaries[$composition->id]['holders'] ?? []; $card['scopes'] = $summaries[$composition->id]['scopes'] ?? []; 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; $pillarMap = $this->publishedPillars($article->pillars); $pillars = array_values($pillarMap); $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'] = array_keys($pillarMap); $card['kind_id'] = $kind !== null ? (int) $kind->id : null; $card['since'] = $this->markers->firstOffered($offerIds)?->getTimestamp() ?? 0; $card['holders'] = $articleSummaries[$article->id]['holders'] ?? []; $card['scopes'] = $articleSummaries[$article->id]['scopes'] ?? []; 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)); $card['holders'] = $bundleSummaries[$bundle->id]['holders'] ?? []; $card['scopes'] = $bundleSummaries[$bundle->id]['scopes'] ?? []; if ($this->matches($term, $bundle->name.' '.$names)) { $cards[] = $card; } } return $cards; } /** * 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> */ public 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); } $holder = (string) ($options['holder'] ?? ''); $scope = (string) ($options['scope'] ?? ''); if ($holder !== '') { $cards = array_filter($cards, fn (array $card): bool => in_array($holder, $card['holders'] ?? [], true)); } if ($scope !== '') { $cards = array_filter($cards, fn (array $card): bool => in_array($scope, $card['scopes'] ?? [], true)); } $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; } public const HOLDERS = ['person' => 'Privatpersonen', 'organisation' => 'Organisationen und Vereine']; public const SCOPES = [LicenceScope::SINGLE => 'Einzellizenz', LicenceScope::MULTI => 'Mehrfachlizenz']; /** * Die Auswahl, die ein Besucher bei diesen Kacheln hat: nur Werte, die es gibt, und nur Auswahlfelder mit mindestens * zwei Werten (sonst filtert nichts). * * @param list> $cards * @return array{pillar: array, kind: array, holder: array, scope: array} */ public function facets(array $cards): array { $pillars = []; $kinds = []; $holders = []; $scopes = []; foreach ($cards as $card) { foreach (array_values($card['pillar_ids'] ?? []) as $index => $id) { $pillars[(int) $id] = (string) ($card['pillars'][$index] ?? ''); } if (($card['kind_id'] ?? null) !== null && ($card['kind_label'] ?? '') !== '') { $kinds[(int) $card['kind_id']] = (string) $card['kind_label']; } foreach ($card['holders'] ?? [] as $holder) { if (isset(self::HOLDERS[$holder])) { $holders[$holder] = self::HOLDERS[$holder]; } } foreach ($card['scopes'] ?? [] as $scope) { if (isset(self::SCOPES[$scope])) { $scopes[$scope] = self::SCOPES[$scope]; } } } asort($pillars); asort($kinds); $facets = ['pillar' => $pillars, 'kind' => $kinds, 'holder' => $holders, 'scope' => $scopes]; return array_map(fn (array $values): array => count($values) >= 2 ? $values : [], $facets); } /** * Die Produktliste für Shopseite und Widget: Suche, Auswahl des Besuchers (Säule, Materialart, Eignung, * Nutzerkreis), Reihenfolge, Ansicht und Seiten. Alles kommt aus dem Abfragetext und wird gegen feste Werte * geprüft; unbekannte Werte entfallen. * * @param array $query Abfrage des Besuchers (suche, saeule, art, eignung, kreis, sort, ansicht, seite) * @param array $fixed Vorgaben der Seite (pillar, kind, sort, limit, view, filters) * @return array */ public function listing(array $query, array $fixed = []): array { $text = fn (string $key): string => isset($query[$key]) && (is_string($query[$key]) || is_int($query[$key])) ? trim((string) $query[$key]) : ''; $filters = (bool) ($fixed['filters'] ?? true); $term = mb_substr($text('suche'), 0, 100); $base = $this->arrange($this->all($term), array_intersect_key($fixed, ['pillar' => 1, 'kind' => 1])); $facets = $filters ? $this->facets($base) : ['pillar' => [], 'kind' => [], 'holder' => [], 'scope' => []]; $pick = function (string $key, string $facet) use ($text, $facets, $filters): int|string|null { $value = $text($key); return $filters && $value !== '' && array_key_exists($value, $facets[$facet]) ? (ctype_digit($value) ? (int) $value : $value) : null; }; $chosen = [ 'pillar' => $pick('saeule', 'pillar'), 'kind' => $pick('art', 'kind'), 'holder' => $pick('eignung', 'holder'), 'scope' => $pick('kreis', 'scope'), ]; $sort = $filters && isset(self::SORTS[$text('sort')]) ? $text('sort') : ($fixed['sort'] ?? null); $view = $filters && in_array($text('ansicht'), ['kacheln', 'liste'], true) ? $text('ansicht') : (string) ($fixed['view'] ?? 'kacheln'); $cards = $this->arrange($base, $chosen + ['sort' => $sort, 'limit' => $fixed['limit'] ?? null]); $perPage = max(1, (int) config('shop.per_page', 12)); $pages = max(1, (int) ceil(count($cards) / $perPage)); $page = $filters ? min($pages, max(1, (int) $text('seite'))) : 1; $shown = $filters ? array_slice($cards, ($page - 1) * $perPage, $perPage) : $cards; return [ 'cards' => $shown, 'total' => count($cards), 'page' => $page, 'pages' => $filters ? $pages : 1, 'facets' => $facets, 'chosen' => $chosen, 'sort' => $sort, 'view' => $view, 'term' => $term, 'filters' => $filters, ]; } /** * @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, 'holders' => [], 'scopes' => [], ]; } 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 array Kennung => Name */ private function publishedPillars(Collection $pillars): array { return $pillars->filter(fn (Pillar $pillar): bool => $pillar->published_at !== null)->mapWithKeys(fn (Pillar $pillar): array => [(int) $pillar->id => (string) $pillar->name])->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]; } }