Files
laraundfelix/app/Domain/Cms/Catalog/PublicCatalogReader.php
T
LarsandClaude Opus 5.5 127ea86817
CI Feature / test (push) Successful in 46s
Deploy Test / test (push) Successful in 39s
Deploy Test / deploy (push) Successful in 29s
Integriere die Premium-Karten v1.7 als Themen-, Medien- und Programmkarte.
- Drei Widget-Arten (topic_card, media_card, program_card) auf gemeinsamer Basis, eine Karte je Platz im
  bestehenden Abschnittsraster; kein zweites Kartenraster.
- Titel, Kurzbeschreibung, Art, Zusatzangaben und Linkziel kommen zur Laufzeit aus Thema, Inhaltselement
  bzw. Programm/Modul; im CMS stehen nur Kennungen, Bild, Bildausschnitt, Beschriftung und Hinweis.
- Medienkarten sind öffentlich nur als Bestandteil eines öffentlichen Programms (PublicMaterialReader),
  Module nur in der Redaktionsvorschau; keine Bibliotheksbilder, kein Preis, keine Kaufaktion.
- Gleiche Höhe je Zeile über die gemeinsame Höhe „fill“; Raster 1 + 1 + 2 und 2 + 1 + 1 ergänzt.
- Tests zu Raster- und Widgetliste bewusst um die neuen Raster und Arten erweitert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 13:25:06 +01:00

224 lines
8.0 KiB
PHP

<?php
namespace App\Domain\Cms\Catalog;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\EditorialContentKind;
use Illuminate\Support\Collection;
/**
* Lesesicht „Öffentlicher Katalog“ für Website und CMS (Website-CMS-Zielarchitektur 5.1, 6.2).
*
* Übergangsregel, solange öffentliche Fachobjektprofile (ADR-29) fehlen:
* Öffentlich gezeigt wird nur, was die Website bereits heute öffentlich zeigt, nämlich Kompositionen
* der Inhaltsart „programm“ mit einer zur Anzeige freigegebenen Fassung (Route /programme).
* Module haben noch keine öffentliche Seitenart; sie sind im Editor wählbar und erscheinen nur in
* der geschützten Redaktionsvorschau, nie auf der öffentlichen Seite.
*
* Gelesen werden nur Anzeigetitel, Kurzbeschreibung und Inhaltsart, nie Dateien, Lehrtexte oder
* Vorschaubilder der Bibliothek (diese sind geschützt, Paket 6).
*/
class PublicCatalogReader
{
/** Inhaltsarten mit öffentlicher Seitenart. */
public const PUBLIC_KINDS = ['programm'];
/** Inhaltsarten, die ein Katalog-Widget referenzieren darf. */
public const SELECTABLE_KINDS = ['programm', 'modul'];
public function __construct(private readonly CompositionDisplayRelease $releases) {}
/**
* @return Collection<int, Composition>
*/
public function publicPrograms(): Collection
{
$kind = EditorialContentKind::query()->where('slug', 'programm')->first();
if ($kind === null) {
return collect();
}
return Composition::query()
->where('editorial_content_kind_id', $kind->id)
->orderBy('designation')
->get()
->filter(fn (Composition $composition): bool => $this->shownVersion($composition) !== null)
->values();
}
/**
* Gezeigte Fassung einer öffentlich gezeigten Komposition, sonst null.
*/
public function shownVersion(Composition $composition): ?CompositionVersion
{
if (! in_array($composition->editorialContentKind?->slug, self::PUBLIC_KINDS, true)) {
return null;
}
return $this->releases->highestReleasedVersion($composition);
}
public function isPublic(Composition $composition): bool
{
return $this->shownVersion($composition) !== null;
}
/**
* Wählbar sind Programme und Module mit zur Anzeige freigegebener Fassung, die nicht archiviert sind.
*/
public function isSelectable(Composition $composition): bool
{
return $composition->archived_at === null
&& in_array($composition->editorialContentKind?->slug, self::SELECTABLE_KINDS, true)
&& $this->releases->highestReleasedVersion($composition) !== null;
}
/**
* Suchtreffer für die Auswahl im Editor: Kennung => Bezeichnung mit Sichtbarkeitshinweis.
*
* @return array<int, string>
*/
public function search(?string $term, int $limit = 25): array
{
$term = trim((string) $term);
$candidates = Composition::query()
->with('editorialContentKind')
->whereNull('archived_at')
->whereHas('editorialContentKind', fn ($query) => $query->whereIn('slug', self::SELECTABLE_KINDS))
->when($term !== '', function ($query) use ($term): void {
$like = '%'.mb_strtolower($term).'%';
$query->where(fn ($inner) => $inner
->whereRaw('LOWER(designation) LIKE ?', [$like])
->orWhereRaw('LOWER(COALESCE(display_title, \'\')) LIKE ?', [$like]));
})
->orderBy('designation')
->limit($limit * 3)
->get();
$options = [];
foreach ($candidates as $composition) {
if (! $this->isSelectable($composition)) {
continue;
}
$options[$composition->id] = $this->optionLabel($composition);
if (count($options) >= $limit) {
break;
}
}
return $options;
}
public function labelFor(mixed $id): ?string
{
$composition = is_numeric($id) ? Composition::query()->with('editorialContentKind')->find((int) $id) : null;
return $composition === null ? null : $this->optionLabel($composition);
}
/**
* Löst Verweise in Anzeigeeinträge auf. Öffentlich entfallen nicht öffentliche Objekte stillschweigend.
*
* @param list<array{type?: mixed, id?: mixed}> $refs
* @return list<CatalogEntry>
*/
public function entries(array $refs, bool $includeNonPublic): array
{
$ids = [];
foreach ($refs as $ref) {
if (is_array($ref) && ($ref['type'] ?? null) === 'composition' && is_numeric($ref['id'] ?? null)) {
$ids[] = (int) $ref['id'];
}
}
if ($ids === []) {
return [];
}
$compositions = Composition::query()->with('editorialContentKind')->whereIn('id', $ids)->get()->keyBy('id');
$entries = [];
foreach (array_unique($ids) as $id) {
$composition = $compositions->get($id);
if ($composition === null || ! $this->isSelectable($composition)) {
continue;
}
$public = $this->isPublic($composition);
if (! $public && ! $includeNonPublic) {
continue;
}
$entries[] = new CatalogEntry(
id: $composition->id,
title: $this->title($composition),
summary: filled($composition->summary) ? (string) $composition->summary : null,
kindLabel: (string) ($composition->editorialContentKind?->label ?? ''),
url: $public ? route('public.program', $composition) : null,
public: $public,
);
}
return $entries;
}
/**
* Anzeigedaten für eine Programm- oder Modulkarte (Premium-Karte). Dieselbe Regel wie entries():
* Programme mit Anzeigefreigabe öffentlich mit Programmseite, Module nur in der Redaktionsvorschau.
* Die Art („Programm“ oder „Modul“) ist immer benannt. Kein Preis, kein Angebot, keine Kaufaktion.
*/
public function subject(mixed $id, bool $includeNonPublic): ?CardSubject
{
$entry = $this->entries([['type' => 'composition', 'id' => $id]], $includeNonPublic)[0] ?? null;
if ($entry === null) {
return null;
}
$meta = [];
$composition = Composition::query()->find($entry->id);
$version = $composition !== null ? $this->shownVersion($composition) : null;
if ($version !== null) {
$nested = $version->members()->whereNotNull('nested_composition_version_id')->with('nestedCompositionVersion.composition.editorialContentKind')->get();
$modules = $nested->filter(fn ($member): bool => $member->nestedCompositionVersion?->composition?->editorialContentKind?->slug === 'modul')->count();
if ($modules > 0) {
$meta[] = $modules === 1 ? '1 Modul' : $modules.' Module';
}
}
return new CardSubject(
title: $entry->title,
summary: $entry->summary,
kind: $entry->kindLabel,
badge: null,
meta: $meta,
url: $entry->url,
public: $entry->public,
notice: $entry->public ? null : 'Nur in der Redaktionsvorschau sichtbar: Für Module gibt es noch keine öffentliche Seitenart (öffentliches Profil, ADR-29).',
);
}
private function title(Composition $composition): string
{
return (string) ($composition->display_title ?: $composition->designation);
}
private function optionLabel(Composition $composition): string
{
$visibility = $this->isPublic($composition) ? 'öffentlich sichtbar' : 'nur Redaktionsvorschau';
return $this->title($composition).' · '.($composition->editorialContentKind?->label ?? '').' · '.$visibility;
}
}