Files
laraundfelix/app/Domain/Catalog/EditorialVocabulary.php
T
LarsandClaude Opus 5.5 f774d43647
CI Feature / test (push) Successful in 1m12s
Deploy Test / test (push) Successful in 1m7s
Deploy Test / deploy (push) Successful in 23s
Führe die zentrale Pflege redaktioneller Stammdaten ein
FACH-56 bis FACH-58: Arbeitsplatz „Redaktionelle Stammdaten“ für fachliche
Materialarten, Altersbereich und Zielgruppe, Einsatzbereiche und
Verwendungszwecke (bestehende Tabellen; Säulen bleiben in ihrer Verwaltung).

- Migration (nur Ergänzungen): aktiv/inaktiv für Materialarten und Werte,
  Reihenfolge der Werte, zulässige Objektkontexte der Materialarten, einmalig
  aus dem Bestand abgeleitet; jede vorhandene Zuordnung bleibt zulässig.
- EditorialVocabulary als eine Regelquelle für Redaktion (Medium, Modul,
  Programm, Lizenzartikel), Medien- und Kompositionsfilter, Picker und
  Mitgliederbibliothek: neu wählbar nur aktive, kontextgerechte Werte;
  vorhandene inaktive Zuordnungen sichtbar und beim Speichern erhalten;
  serverseitige Prüfung der Materialart.
- Kennung fest, Bezeichnung pflegbar; verwendete Werte nur deaktivierbar;
  Systemkennungen (programm, modul, traineranleitung) geschützt.
- „Inhaltsart“ heißt durchgängig „Fachliche Materialart“; der Picker trennt
  Medienart und fachliche Materialart und filtert danach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 21:18:03 +01:00

253 lines
9.1 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',
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
{
return (string) $kind->label
.(! $kind->is_active ? self::INACTIVE_SUFFIX : (! $kind->allows($context) ? self::OUT_OF_CONTEXT_SUFFIX : ''));
}
/**
* @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),
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 = [], bool $withPillars = true): 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 (! $withPillars && $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')->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;
}
}