Files
laraundfelix/app/Domain/Cms/Catalog/PublicCatalogReader.php
LarsandCursor f29ea58fd5
CI Feature / test (push) Successful in 54s
Deploy Test / test (push) Successful in 47s
Deploy Test / deploy (push) Successful in 24s
Ergänze die gemeinsame redaktionelle Basis an den Fachobjekten.
Texte, Materialart und Präsentationsmedien bleiben am Medium, Modul, Programm und Lizenzartikel und werden von Verwaltung und Karten aus derselben Quelle gelesen, ohne Fassung oder Lizenz zu verändern.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 07:54:36 +01:00

350 lines
13 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\ContentVersionStatus;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\EditorialProfileReader;
use App\Domain\Media\ThumbnailRef;
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])
->orWhereRaw('LOWER(COALESCE(teaser, \'\')) LIKE ?', [$like])
->orWhereRaw('LOWER(COALESCE(search_terms, \'\')) 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;
}
/**
* Für die Programm- oder Modulkarte wählbar: Programm oder Modul, nicht archiviert, mit mindestens einer
* bereitgestellten Fassung. Anders als bei den Katalogkarten ist keine Anzeigefreigabe nötig, damit die Redaktion
* eine Karte vorbereiten kann; öffentlich gilt unverändert isPublic().
*/
public function isCardSelectable(Composition $composition): bool
{
return $composition->archived_at === null
&& in_array($composition->editorialContentKind?->slug, self::SELECTABLE_KINDS, true)
&& $this->latestReadyVersion($composition) !== null;
}
/**
* @return array<int, string>
*/
public function cardSearch(?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])
->orWhereRaw('LOWER(COALESCE(teaser, \'\')) LIKE ?', [$like])
->orWhereRaw('LOWER(COALESCE(search_terms, \'\')) LIKE ?', [$like]));
})
->orderBy('designation')
->limit($limit * 3)
->get();
$options = [];
foreach ($candidates as $composition) {
if (! $this->isCardSelectable($composition)) {
continue;
}
$options[$composition->id] = $this->cardOptionLabel($composition);
if (count($options) >= $limit) {
break;
}
}
return $options;
}
public function cardLabelFor(mixed $id): ?string
{
$composition = is_numeric($id) ? Composition::query()->with('editorialContentKind')->find((int) $id) : null;
return $composition === null ? null : $this->cardOptionLabel($composition);
}
/**
* Anzeigedaten für eine Programm- oder Modulkarte (Premium-Karte).
*
* - Öffentlich nur Programme mit Anzeigefreigabe (dieselbe Regel wie /programme), mit Programmseite als Ziel.
* Module haben keine öffentliche Seitenart (ADR-29) und erscheinen nur in der Redaktionsvorschau, ohne Link.
* - Text: Kurzbeschreibung, sonst das Lernziel der gezeigten (öffentlich) bzw. neuesten bereitgestellten Fassung;
* das Lernziel steht auf der öffentlichen Programmseite bereits öffentlich.
* - Zusatzangaben: Zahl der Module, Dauer. Kein Preis, kein Angebot, keine Kaufaktion; eine Karte ist kein
* Lizenzartikel. Das Vorschaubild der Bibliothek erscheint nur in der Redaktionsvorschau.
*/
public function subject(mixed $id, bool $includeNonPublic): ?CardSubject
{
$composition = is_numeric($id) ? Composition::query()->with('editorialContentKind')->find((int) $id) : null;
if ($composition === null || ! $this->isCardSelectable($composition)) {
return null;
}
$shown = $this->shownVersion($composition);
$public = $shown !== null && $composition->archived_at === null;
if (! $public && ! $includeNonPublic) {
return null;
}
$version = $shown ?? $this->latestReadyVersion($composition);
$program = $composition->editorialContentKind?->slug === 'programm';
$kind = (string) ($composition->editorialContentKind?->label ?? ($program ? 'Programm' : 'Modul'));
$profile = app(EditorialProfileReader::class)->read($composition, 'kachel');
$summary = $profile->value('teaser') ?: $profile->value('summary');
$goal = filled($version?->learning_goal) ? trim((string) $version->learning_goal) : null;
$missing = [];
if ($summary === null) {
$missing[] = $goal !== null
? 'Keine Kurzbeschreibung gepflegt; die Karte zeigt das Lernziel.'
: 'Weder Kurzbeschreibung noch Lernziel gepflegt.';
}
$meta = [];
if ($version !== null) {
$modules = $version->members()
->whereNotNull('nested_composition_version_id')
->with('nestedCompositionVersion.composition.editorialContentKind')
->get()
->filter(fn ($member): bool => $member->nestedCompositionVersion?->composition?->editorialContentKind?->slug === 'modul')
->count();
if ($program && $modules > 0) {
$meta[] = $modules === 1 ? '1 Modul' : $modules.' Module';
}
if (is_numeric($version->duration_minutes) && (int) $version->duration_minutes > 0) {
$meta[] = 'Dauer '.(int) $version->duration_minutes.' Min.';
} else {
$missing[] = 'Keine Dauer gepflegt.';
}
}
$notice = null;
if (! $public) {
$notice = 'Nur in der Redaktionsvorschau sichtbar: '.($program
? 'Das Programm hat keine Anzeigefreigabe.'
: 'Für Module gibt es noch keine öffentliche Seitenart (öffentliches Profil, ADR-29); die Karte erhält deshalb keinen Link.');
}
return new CardSubject(
title: $profile->value('display_title') ?: $this->title($composition),
summary: $summary ?? $goal,
kind: $kind,
badge: null,
meta: $meta,
url: $public ? route('public.program', $composition) : null,
public: $public,
notice: $notice,
visual: 'catalog',
visualLabel: $kind,
previewImage: $includeNonPublic ? ThumbnailRef::url('editorial.card', 'komposition', (int) $composition->id, $composition->thumbnail_path) : null,
missing: $missing,
);
}
private function latestReadyVersion(Composition $composition): ?CompositionVersion
{
return $composition->versions()
->where('status', ContentVersionStatus::Bereit->value)
->orderByDesc('number')
->first();
}
private function cardOptionLabel(Composition $composition): string
{
$visibility = match (true) {
$this->isPublic($composition) => 'öffentlich sichtbar',
$composition->editorialContentKind?->slug === 'modul' => 'Modul, nur Redaktionsvorschau',
default => 'ohne Anzeigefreigabe, nur Redaktionsvorschau',
};
return $this->title($composition).' · '.($composition->editorialContentKind?->label ?? '').' · '.$visibility;
}
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;
}
}