CI Feature / test (push) Successful in 6m4s
- ShopCatalog::listing mit Auswahl nach Thema, Materialart, Eignung und Nutzerkreis, Reihenfolge, Ansicht und Seiten - Listenansicht shop/zeile, gemeinsame Liste fuer Shopseite und Widget (Variante Liste, Einstellung Auswahl fuer Besucher) - Werte aus dem Abfragetext werden gegen vorhandene Werte geprueft, Felder als Liste und Zahlen ausserhalb des Bereichs sind harmlos - Suchfeld heisst nach dem Umbau Zurücksetzen statt Suche zuruecksetzen (Test bewusst angepasst) - ADR-57 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
334 lines
16 KiB
PHP
334 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Commerce;
|
|
|
|
use App\Domain\Catalog\Bundle;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\EditorialProfileReader;
|
|
use App\Domain\Catalog\FormattedText;
|
|
use App\Domain\Catalog\LicenceScope;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\Pillar;
|
|
use App\Domain\Catalog\PresentationMedia;
|
|
use App\Domain\Cms\Catalog\PublicCatalogReader;
|
|
use App\Support\Brand\SymbolCatalog;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Die Produkte des Shops als Kacheln: eine Datenquelle für die Shopseite (und später das Widget).
|
|
*
|
|
* Gelistet wird, was mindestens eine kaufbare Option hat (Artikel, Pakete mit eigener Seite, Altbestand mit
|
|
* Programmseite). Jede Kachel trägt nur öffentlich Zeigbares: Titel und Kurztext aus der Redaktion, das Titelbild nur mit
|
|
* Website-Freigabe, Säulen nur, wenn sie veröffentlicht sind, die Materialart mit ihrem Symbol, die Marker und den
|
|
* niedrigsten Preis. Nichts davon begründet eine Berechtigung (ADR-52, ADR-54).
|
|
*
|
|
* @phpstan-type Card array{key: string, kind: string, url: string, title: string, teaser: string, from: string, from_cents: int, count: int, image: ?array{url: string, alt: string}, kind_label: string, symbol: ?string, pillars: list<string>, markers: list<array{key: string, label: string}>}
|
|
*/
|
|
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<array<string, mixed>>
|
|
*/
|
|
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<array<string, mixed>>
|
|
*/
|
|
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<array<string, mixed>> $cards
|
|
* @param array<string, mixed> $options
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
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<array<string, mixed>> $cards
|
|
* @return array{pillar: array<int, string>, kind: array<int, string>, holder: array<string, string>, scope: array<string, string>}
|
|
*/
|
|
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<string, mixed> $query Abfrage des Besuchers (suche, saeule, art, eignung, kreis, sort, ansicht, seite)
|
|
* @param array<string, mixed> $fixed Vorgaben der Seite (pillar, kind, sort, limit, view, filters)
|
|
* @return array<string, mixed>
|
|
*/
|
|
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<string> $pillars
|
|
* @param list<array{key: string, label: string}> $markers
|
|
* @param array{url: string, alt: string}|null $image
|
|
* @return array<string, mixed>
|
|
*/
|
|
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<int, Pillar> $pillars
|
|
* @return array<int, string> 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];
|
|
}
|
|
}
|