Files
laraundfelix/app/Domain/Catalog/EditorialVocabulary.php
T
LarsandClaude Sonnet 5.5 3e2fd872c2 Führe Dokumente mit Kapiteln, Fassung, Seite und PDF mit Inhaltsverzeichnis ein (ADR-51)
Ein Dokument ist eine Komposition der Art document, ein Kapitel ein Text mit der
automatischen Materialart Dokumentkapitel; die Ebene steht in der Rolle. Der
Aufbaueditor bekommt den Kapitelarbeitsplatz (Übersicht, Ordnen als Block,
offene Eingaben bleiben beim Wechsel erhalten, Speichern als Entwurf,
Bereitstellen als Dokumentfassung). Module und Programme binden eine Fassung
über den gemeinsamen Picker ein. Mitglieder lesen sie mit anklickbarem
Inhaltsverzeichnis und laden ein PDF mit Inhaltsverzeichnis, Seitenzahlen,
Tabellen und Bildern. Ein älterer Stand liefert die gebundene Fassung weiter.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 12:23:18 +01:00

299 lines
11 KiB
PHP

<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/**
* Eine Regelquelle für redaktionelle Stammdaten (FACH-56 bis FACH-58): Welche fachlichen Materialarten und welche
* Einordnungswerte Editor, Filter und Picker anbieten.
*
* - Neu wählbar sind nur aktive Werte; Materialarten zusätzlich nur im zulässigen Objektkontext.
* - Vorhandene Zuordnungen bleiben sichtbar und erhalten, auch wenn der Wert inaktiv ist oder sein Kontext geändert
* wurde; sie sind gekennzeichnet.
* - Filter finden auch Objekte mit inaktiven Werten; ein gewählter Filter bleibt erhalten.
*/
final class EditorialVocabulary
{
public const INACTIVE_SUFFIX = ' (inaktiv)';
public const OUT_OF_CONTEXT_SUFFIX = ' (hier nicht vorgesehen)';
/**
* Objektkontext eines Fachobjekts nach dem Fachmodell, null wenn es keine Materialart trägt.
*/
public function contextOf(Model $subject): ?string
{
if ($subject instanceof ContentElement) {
return 'medium';
}
if ($subject instanceof LicenseArticle) {
return 'lizenzartikel';
}
if ($subject instanceof Composition) {
return self::compositionContext($subject->kind instanceof CompositionKind ? $subject->kind : CompositionKind::tryFrom((string) $subject->kind));
}
return null;
}
public static function compositionContext(?CompositionKind $kind): ?string
{
return match ($kind) {
CompositionKind::Kurs => 'programm',
CompositionKind::Uebung => 'modul',
CompositionKind::Exercise => 'uebung',
CompositionKind::Document => 'dokument',
default => null,
};
}
/**
* Auswahl der Materialart in einem Editor: aktive, im Kontext zulässige Arten in Pflegereihenfolge, dazu die
* bereits zugeordnete Art mit Kennzeichnung, falls sie inaktiv oder hier nicht vorgesehen ist.
*
* @return array<int, string>
*/
public function kindOptions(string $context, ?int $currentId = null): array
{
$options = [];
foreach (EditorialContentKind::query()->orderBy('sort_order')->orderBy('id')->get() as $kind) {
$offered = $kind->is_active && $kind->allows($context);
if ($offered) {
$options[$kind->id] = (string) $kind->label;
} elseif ($currentId !== null && $kind->id === $currentId) {
$options[$kind->id] = $this->kindLabel($kind, $context);
}
}
return $options;
}
/**
* Prüft eine neue Materialart serverseitig. Die bereits gespeicherte Art bleibt immer zulässig.
*/
public function kindError(string $context, ?int $newId, ?int $storedId): ?string
{
if ($newId === null || $newId === $storedId) {
return null;
}
$kind = EditorialContentKind::query()->find($newId);
if (! $kind instanceof EditorialContentKind) {
return 'Diese fachliche Materialart gibt es nicht mehr.';
}
if (! $kind->is_active) {
return 'Die fachliche Materialart „'.$kind->label.'“ ist deaktiviert und kann nicht neu gewählt werden.';
}
if (! $kind->allows($context)) {
return 'Die fachliche Materialart „'.$kind->label.'“ ist für '.(EditorialContentKind::CONTEXTS[$context] ?? $context).' nicht vorgesehen.';
}
return null;
}
/**
* Filteroptionen der Materialart für einen Bestand: im Kontext zulässige Arten und Arten, die dort tatsächlich
* vorkommen (gekennzeichnet), dazu ein bereits gewählter Filter.
*
* @return array<string, string>
*/
public function kindFilterOptions(string $context, string $selected = ''): array
{
$used = $this->usedKindIds($context);
$options = [];
foreach (EditorialContentKind::query()->orderBy('sort_order')->orderBy('id')->get() as $kind) {
$show = ($kind->allows($context) && $kind->is_active) || in_array($kind->id, $used, true) || (string) $kind->id === $selected;
if ($show) {
$options[(string) $kind->id] = $this->kindLabel($kind, $context);
}
}
return $options;
}
public function kindLabel(EditorialContentKind $kind, string $context): string
{
// Die automatische Art hat bewusst keinen wählbaren Kontext; das ist kein Mangel der Zuordnung.
if ($kind->isAutomatic() && $kind->is_active) {
return (string) $kind->label;
}
return (string) $kind->label
.(! $kind->is_active ? self::INACTIVE_SUFFIX : (! $kind->allows($context) ? self::OUT_OF_CONTEXT_SUFFIX : ''));
}
/**
* Materialarten für die Mehrfachauswahl im Medienmanager: im Kontext zulässige Arten, Arten, die bei Medien
* tatsächlich vorkommen, und die automatische Art „Übungsabschnitt“. Die Reihenfolge folgt der Pflege, die
* automatische Art steht zuletzt. „Ohne Materialart“ ergänzt der Medienmanager.
*
* @param list<int> $selectedIds bereits gewählte Arten bleiben sichtbar
* @return list<array{id: int, label: string, automatic: bool}>
*/
public function mediumKindChoices(array $selectedIds = []): array
{
$used = $this->usedKindIds('medium');
$regular = [];
$automatic = [];
foreach (EditorialContentKind::query()->orderBy('sort_order')->orderBy('id')->get() as $kind) {
$id = (int) $kind->id;
$show = $kind->isAutomatic()
|| ($kind->allows('medium') && $kind->is_active)
|| in_array($id, $used, true)
|| in_array($id, $selectedIds, true);
if (! $show) {
continue;
}
$row = ['id' => $id, 'label' => $this->kindLabel($kind, 'medium'), 'automatic' => $kind->isAutomatic()];
if ($kind->isAutomatic()) {
$automatic[] = $row;
} else {
$regular[] = $row;
}
}
return [...$regular, ...$automatic];
}
/**
* @return list<int>
*/
private function usedKindIds(string $context): array
{
$query = match ($context) {
'medium' => DB::table('content_elements'),
'lizenzartikel' => DB::table('license_articles'),
'programm' => DB::table('compositions')->where('kind', CompositionKind::Kurs->value),
'modul' => DB::table('compositions')->where('kind', CompositionKind::Uebung->value),
'uebung' => DB::table('compositions')->where('kind', CompositionKind::Exercise->value),
'dokument' => DB::table('compositions')->where('kind', CompositionKind::Document->value),
default => null,
};
return $query === null ? [] : $query->whereNotNull('editorial_content_kind_id')->distinct()
->pluck('editorial_content_kind_id')->map(fn ($id): int => (int) $id)->all();
}
/**
* Einordnung im Editor: je angebotener Dimension aktive Werte und bereits zugeordnete inaktive Werte (gekennzeichnet).
*
* @param list<int> $assignedIds
* @return list<array{label: string, values: list<array{id: int, label: string, inactive: bool}>}>
*/
public function dimensionGroups(Model $subject, array $assignedIds): array
{
$groups = [];
foreach (EditorialDimension::forSubject($subject) as $dimension) {
$values = [];
foreach ($dimension->values as $value) {
if ($value->is_active || in_array((int) $value->id, $assignedIds, true)) {
$values[] = [
'id' => (int) $value->id,
'label' => (string) $value->label.($value->is_active ? '' : self::INACTIVE_SUFFIX),
'inactive' => ! $value->is_active,
];
}
}
if ($values !== []) {
$groups[] = ['label' => (string) $dimension->label, 'values' => $values];
}
}
return $groups;
}
/**
* Filtergruppen der Einordnung: aktive Werte, verwendete inaktive Werte (gekennzeichnet) und gewählte Filter.
*
* @param list<int> $selectedIds
* @return list<array{label: string, values: list<array{id: int, label: string}>}>
*/
public function dimensionFilterGroups(array $selectedIds = []): array
{
$used = DB::table('identity_dimension_values')->distinct()->pluck('editorial_dimension_value_id')->map(fn ($id): int => (int) $id)->all();
$groups = [];
foreach (EditorialDimension::query()->with('values')->orderBy('label')->get() as $dimension) {
if ($dimension->slug === EditorialDimension::PILLAR_SLUG) {
continue;
}
$values = [];
foreach ($dimension->values as $value) {
$id = (int) $value->id;
if ($value->is_active || in_array($id, $used, true) || in_array($id, $selectedIds, true)) {
$values[] = ['id' => $id, 'label' => (string) $value->label.($value->is_active ? '' : self::INACTIVE_SUFFIX)];
}
}
if ($values !== []) {
$groups[] = ['label' => (string) $dimension->label, 'values' => $values];
}
}
return $groups;
}
/**
* Öffentliche Filter der Mitgliederbibliothek: nur aktive Werte, ein bereits gewählter Wert bleibt sichtbar.
*
* @param list<int> $selectedIds
* @return Collection<int, EditorialDimension>
*/
public function memberFilterDimensions(array $selectedIds = []): Collection
{
$dimensions = EditorialDimension::query()->with('values')->where('slug', '!=', EditorialDimension::PILLAR_SLUG)->orderBy('label')->get();
foreach ($dimensions as $dimension) {
$dimension->setRelation('values', $dimension->values
->filter(fn (EditorialDimensionValue $value): bool => $value->is_active || in_array((int) $value->id, $selectedIds, true))
->values());
}
return $dimensions->filter(fn (EditorialDimension $dimension): bool => $dimension->values->isNotEmpty())->values();
}
/**
* Für replaceOffered: Werte, die bei diesem Fachobjekt gesetzt oder entfernt werden dürfen. Aktive Werte der
* angebotenen Dimensionen und die bereits zugeordneten inaktiven (der Editor zeigt sie gekennzeichnet an).
*
* @param list<int> $assignedIds
* @return list<int>
*/
public function editableValueIds(Model $subject, array $assignedIds): array
{
$ids = [];
foreach (EditorialDimension::forSubject($subject) as $dimension) {
foreach ($dimension->values as $value) {
if ($value->is_active || in_array((int) $value->id, $assignedIds, true)) {
$ids[] = (int) $value->id;
}
}
}
return $ids;
}
}