249 lines
9.3 KiB
PHP
249 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms\Catalog;
|
|
|
|
use App\Domain\Catalog\CompositionStructureReader;
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use App\Domain\Catalog\StructureNode;
|
|
use App\Domain\Media\ThumbnailRef;
|
|
|
|
/**
|
|
* Lesesicht „Medien und Materialien“ für Premium-Karten (Website-CMS-Zielarchitektur 6.2).
|
|
*
|
|
* Auswahl in der Redaktion: jedes nicht archivierte Inhaltselement mit mindestens einer nicht gesperrten Fassung.
|
|
*
|
|
* Öffentliche Sichtbarkeit (Übergangsregel, solange öffentliche Fachobjektprofile nach ADR-29 fehlen):
|
|
* Ein Medium hat keine eigene öffentliche Seite. Öffentlich genannt wird es heute nur als Bestandteil der gezeigten
|
|
* Fassung eines öffentlichen Programms (Programmseite /programme/{id}). Genau dann erscheint auch die Karte
|
|
* öffentlich, mit Verweis auf diese Programmseite, und nur mit bereitgestellter Fassung. Traineranleitungen erscheinen
|
|
* nie öffentlich (vertraulich für Trainerinnen und Trainer).
|
|
*
|
|
* Öffentlich gelesen werden Anzeigetitel (sonst Arbeitstitel), Kurzbeschreibung, Inhaltsart und Ausgabeart. Nie:
|
|
* Beschreibung, Einsatzhinweis, Lizenzvermerk, Dateien, Lehrtexte, Ausgabeadressen. Das Vorschaubild (Filmstandbild)
|
|
* der Bibliothek ist nicht für die Website freigegeben; es erscheint nur in der Redaktionsvorschau.
|
|
* Eine Karte spielt nichts ab, bietet keinen Download und gibt kein Medium frei.
|
|
*/
|
|
class PublicMaterialReader
|
|
{
|
|
/** Inhaltsarten, die nie öffentlich auf einer Karte erscheinen. */
|
|
public const CONFIDENTIAL_KINDS = ['traineranleitung'];
|
|
|
|
/** @var array<int, array{id: int, title: string}>|null Element-ID => erstes öffentliches Programm */
|
|
private ?array $publicIndex = null;
|
|
|
|
public function __construct(
|
|
private readonly PublicCatalogReader $catalog,
|
|
private readonly CompositionStructureReader $structure,
|
|
) {}
|
|
|
|
public function isSelectable(ContentElement $element): bool
|
|
{
|
|
return $element->archived_at === null
|
|
&& $element->versions()->where('status', '!=', ContentVersionStatus::Gesperrt->value)->exists();
|
|
}
|
|
|
|
public function isReady(ContentElement $element): bool
|
|
{
|
|
return $element->versions()->where('status', ContentVersionStatus::Bereit->value)->exists();
|
|
}
|
|
|
|
/**
|
|
* Öffentliches Programm, in dessen gezeigter Fassung das Element vorkommt, sonst null.
|
|
*
|
|
* @return array{id: int, title: string}|null
|
|
*/
|
|
public function publicProgramFor(ContentElement $element): ?array
|
|
{
|
|
return $this->publicIndex()[$element->id] ?? null;
|
|
}
|
|
|
|
public function isPublic(ContentElement $element): bool
|
|
{
|
|
return $this->isSelectable($element)
|
|
&& $this->isReady($element)
|
|
&& ! in_array($element->editorialContentKind?->slug, self::CONFIDENTIAL_KINDS, true)
|
|
&& $this->publicProgramFor($element) !== null;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
public function search(?string $term, int $limit = 25): array
|
|
{
|
|
$term = trim((string) $term);
|
|
|
|
$candidates = ContentElement::query()
|
|
->with('editorialContentKind')
|
|
->whereNull('archived_at')
|
|
->whereHas('versions', fn ($query) => $query->where('status', '!=', ContentVersionStatus::Gesperrt->value))
|
|
->when($term !== '', function ($query) use ($term): void {
|
|
$like = '%'.mb_strtolower($term).'%';
|
|
$query->where(fn ($inner) => $inner
|
|
->whereRaw('LOWER(working_title) LIKE ?', [$like])
|
|
->orWhereRaw('LOWER(COALESCE(display_title, \'\')) LIKE ?', [$like]));
|
|
})
|
|
->orderBy('working_title')
|
|
->limit($limit)
|
|
->get();
|
|
|
|
$options = [];
|
|
foreach ($candidates as $element) {
|
|
$options[$element->id] = $this->optionLabel($element);
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
public function labelFor(mixed $id): ?string
|
|
{
|
|
$element = is_numeric($id) ? ContentElement::query()->with('editorialContentKind')->find((int) $id) : null;
|
|
|
|
return $element === null ? null : $this->optionLabel($element);
|
|
}
|
|
|
|
/**
|
|
* Anzeigedaten für eine Karte. Öffentlich null, wenn das Element nicht öffentlich genannt wird.
|
|
*/
|
|
public function subject(mixed $id, bool $includeNonPublic): ?CardSubject
|
|
{
|
|
$element = is_numeric($id) ? ContentElement::query()->with('editorialContentKind')->find((int) $id) : null;
|
|
|
|
if ($element === null || ! $this->isSelectable($element)) {
|
|
return null;
|
|
}
|
|
|
|
$public = $this->isPublic($element);
|
|
|
|
if (! $public && ! $includeNonPublic) {
|
|
return null;
|
|
}
|
|
|
|
$program = $this->publicProgramFor($element);
|
|
$output = self::outputLabel((string) $element->output_class);
|
|
$film = $element->output_class === 'film';
|
|
$editorialKind = $element->editorialContentKind?->label;
|
|
$summary = filled($element->summary) ? trim((string) $element->summary) : null;
|
|
|
|
$meta = [$output];
|
|
if ($program !== null) {
|
|
$meta[] = 'Im Programm „'.$program['title'].'“';
|
|
}
|
|
if ($film) {
|
|
$meta[] = 'Vollständiger Film nur mit Lizenz im Mitgliederbereich';
|
|
}
|
|
|
|
$missing = [];
|
|
if ($summary === null) {
|
|
$missing[] = 'Keine Kurzbeschreibung gepflegt (Medium bearbeiten → Kurzbeschreibung).';
|
|
}
|
|
if (! filled($element->display_title)) {
|
|
$missing[] = 'Kein Anzeigetitel gepflegt; die Karte zeigt den Arbeitstitel.';
|
|
}
|
|
|
|
$previewImage = $includeNonPublic ? ThumbnailRef::url('editorial.card', 'element', (int) $element->id, $element->thumbnail_path) : null;
|
|
|
|
return new CardSubject(
|
|
title: (string) ($element->display_title ?: $element->working_title),
|
|
summary: $summary,
|
|
kind: filled($editorialKind) ? (string) $editorialKind : $output,
|
|
badge: $output,
|
|
meta: $meta,
|
|
url: $public && $program !== null ? route('public.program', $program['id']) : null,
|
|
public: $public,
|
|
notice: $public ? null : $this->notice($element, $program),
|
|
visual: $film ? 'film' : 'material',
|
|
visualLabel: $output,
|
|
previewImage: $previewImage,
|
|
missing: $missing,
|
|
style: $film ? 'media' : 'material',
|
|
);
|
|
}
|
|
|
|
/** Bezeichnung der Ausgabeart, wie in der Redaktionsstruktur (CompositionRelease::outputLabel). */
|
|
public static function outputLabel(string $outputClass): string
|
|
{
|
|
return match ($outputClass) {
|
|
'film' => 'Film',
|
|
'audio' => 'Hörstück',
|
|
'bild' => 'Bild',
|
|
'document', 'pdf' => 'Dokument',
|
|
default => 'Text',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param array{id: int, title: string}|null $program
|
|
*/
|
|
private function notice(ContentElement $element, ?array $program): string
|
|
{
|
|
$reason = match (true) {
|
|
in_array($element->editorialContentKind?->slug, self::CONFIDENTIAL_KINDS, true) => 'Traineranleitungen erscheinen nie öffentlich.',
|
|
! $this->isReady($element) => 'Das Medium hat noch keine bereitgestellte Fassung.',
|
|
$program === null => 'Das Medium gehört zu keinem öffentlich gezeigten Programm. Medien haben noch keine eigene öffentliche Freigabe (öffentliches Profil, ADR-29).',
|
|
default => 'Das Medium ist nicht öffentlich.',
|
|
};
|
|
|
|
return 'Nur in der Redaktionsvorschau sichtbar: '.$reason;
|
|
}
|
|
|
|
private function optionLabel(ContentElement $element): string
|
|
{
|
|
$visibility = $this->isPublic($element)
|
|
? 'öffentlich über Programmseite'
|
|
: ($this->isReady($element) ? 'nur Redaktionsvorschau' : 'Entwurf, nur Redaktionsvorschau');
|
|
$kind = $element->editorialContentKind?->label ?: self::outputLabel((string) $element->output_class);
|
|
|
|
return (string) ($element->display_title ?: $element->working_title).' · '.$kind.' · '.$visibility;
|
|
}
|
|
|
|
/**
|
|
* Index aller Elemente, die in der gezeigten Fassung eines öffentlichen Programms vorkommen (auch verschachtelt).
|
|
*
|
|
* @return array<int, array{id: int, title: string}>
|
|
*/
|
|
private function publicIndex(): array
|
|
{
|
|
if ($this->publicIndex !== null) {
|
|
return $this->publicIndex;
|
|
}
|
|
|
|
$index = [];
|
|
|
|
foreach ($this->catalog->publicPrograms() as $program) {
|
|
$version = $this->catalog->shownVersion($program);
|
|
|
|
if ($version === null) {
|
|
continue;
|
|
}
|
|
|
|
$entry = ['id' => (int) $program->id, 'title' => (string) ($program->display_title ?: $program->designation)];
|
|
|
|
foreach ($this->elementIds($this->structure->tree($version)) as $elementId) {
|
|
$index[$elementId] ??= $entry;
|
|
}
|
|
}
|
|
|
|
return $this->publicIndex = $index;
|
|
}
|
|
|
|
/**
|
|
* @param list<StructureNode> $nodes
|
|
* @return list<int>
|
|
*/
|
|
private function elementIds(array $nodes): array
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($nodes as $node) {
|
|
if ($node->contentElementId !== null) {
|
|
$ids[] = (int) $node->contentElementId;
|
|
}
|
|
|
|
array_push($ids, ...$this->elementIds($node->children));
|
|
}
|
|
|
|
return $ids;
|
|
}
|
|
}
|