From f774d43647963fcbad9d66c0ca9c0ba8d3a0b223 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 20 Nov 2026 21:18:03 +0100 Subject: [PATCH] =?UTF-8?q?F=C3=BChre=20die=20zentrale=20Pflege=20redaktio?= =?UTF-8?q?neller=20Stammdaten=20ein?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/Console/Commands/SeedLibraryReview.php | 2 +- app/Domain/Catalog/EditorialContentKind.php | 60 +- app/Domain/Catalog/EditorialDimension.php | 8 +- .../Catalog/EditorialDimensionValue.php | 10 +- app/Domain/Catalog/EditorialVocabulary.php | 252 +++++++ app/Domain/Catalog/IdentityDimension.php | 11 +- .../Pages/RedaktionelleStammdaten.php | 671 ++++++++++++++++++ .../Compositions/Pages/EditComposition.php | 21 +- .../Pages/EditContentElement.php | 16 +- .../Pages/ListContentElements.php | 6 +- .../Pages/ViewContentElement.php | 31 +- .../Pages/ViewLicenseArticle.php | 37 +- app/Filament/Support/ChoosesMedia.php | 38 +- app/Filament/Support/ListsCompositions.php | 18 +- .../Support/ManagesEditorialSurface.php | 2 +- app/Filament/Support/MediaLibrary.php | 13 +- .../Controllers/Member/LibraryController.php | 4 +- ...00000_editorial_master_data_management.php | 72 ++ .../filament/stammdaten-verwendung.blade.php | 24 + resources/views/filament/stammdaten.blade.php | 27 + .../filament/verwaltung/einordnung.blade.php | 2 +- .../filament/verwaltung/picker.blade.php | 23 +- .../views/widgets/catalog_cards.blade.php | 2 +- tests/Feature/RedaktionelleStammdatenTest.php | 284 ++++++++ 24 files changed, 1543 insertions(+), 91 deletions(-) create mode 100644 app/Domain/Catalog/EditorialVocabulary.php create mode 100644 app/Filament/Pages/RedaktionelleStammdaten.php create mode 100644 database/migrations/2026_11_20_200000_editorial_master_data_management.php create mode 100644 resources/views/filament/stammdaten-verwendung.blade.php create mode 100644 resources/views/filament/stammdaten.blade.php create mode 100644 tests/Feature/RedaktionelleStammdatenTest.php diff --git a/app/Console/Commands/SeedLibraryReview.php b/app/Console/Commands/SeedLibraryReview.php index f989547..93408f0 100644 --- a/app/Console/Commands/SeedLibraryReview.php +++ b/app/Console/Commands/SeedLibraryReview.php @@ -67,7 +67,7 @@ private function createFixture( $kind = EditorialContentKind::query()->where('slug', 'programm')->first(); if ($kind === null) { - $this->error('Die Inhaltsart Programm fehlt.'); + $this->error('Die fachliche Materialart Programm fehlt.'); return self::FAILURE; } diff --git a/app/Domain/Catalog/EditorialContentKind.php b/app/Domain/Catalog/EditorialContentKind.php index 87ddede..2c12e3a 100644 --- a/app/Domain/Catalog/EditorialContentKind.php +++ b/app/Domain/Catalog/EditorialContentKind.php @@ -2,17 +2,49 @@ namespace App\Domain\Catalog; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +/** + * Fachliche Materialart (FACH-56 bis FACH-58). Die Bezeichnung ist pflegbar, die Kennung (slug) bleibt nach dem Anlegen + * fest. Zulässige Objektkontexte bestimmen, wo eine Materialart neu gewählt werden kann; vorhandene Zuordnungen bleiben + * immer erhalten (siehe EditorialVocabulary). + */ class EditorialContentKind extends Model { + /** Objektkontexte aus dem Fachmodell: Medium (ContentElement), Modul und Programm (Composition), Lizenzartikel. */ + public const CONTEXTS = [ + 'medium' => 'Medium', + 'modul' => 'Modul', + 'programm' => 'Programm', + 'lizenzartikel' => 'Lizenzartikel', + ]; + + /** + * Kennungen, auf die der Code fachlich verweist: öffentliche Programmseiten und das Anlegen von Programmen und + * Modulen (programm, modul), Vertraulichkeit von Traineranleitungen (traineranleitung). Ihre Bezeichnung bleibt + * pflegbar; deaktivieren, Kontexte ändern und löschen sind gesperrt. + */ + public const SYSTEM_SLUGS = ['programm', 'modul', 'traineranleitung']; + protected $fillable = [ 'slug', 'label', 'sort_order', + 'is_active', + 'contexts', ]; + protected function casts(): array + { + return [ + 'is_active' => 'boolean', + 'contexts' => 'array', + 'sort_order' => 'integer', + ]; + } + public function compositions(): HasMany { return $this->hasMany(Composition::class); @@ -23,11 +55,37 @@ public function contentElements(): HasMany return $this->hasMany(ContentElement::class); } + public function licenseArticles(): HasMany + { + return $this->hasMany(LicenseArticle::class); + } + /** + * @param Builder $query + */ + public function scopeActive(Builder $query): void + { + $query->where('is_active', true); + } + + public function allows(string $context): bool + { + return in_array($context, (array) ($this->contexts ?? []), true); + } + + public function isSystem(): bool + { + return in_array($this->slug, self::SYSTEM_SLUGS, true); + } + + /** + * Alle Materialarten in Pflegereihenfolge (ohne Kontext- oder Aktivfilter). Für Auswahlstellen gilt + * EditorialVocabulary. + * * @return array */ public static function selectOptions(): array { - return static::query()->orderBy('sort_order')->pluck('label', 'id')->all(); + return static::query()->orderBy('sort_order')->orderBy('id')->pluck('label', 'id')->all(); } } diff --git a/app/Domain/Catalog/EditorialDimension.php b/app/Domain/Catalog/EditorialDimension.php index 3cd3860..fe177f0 100644 --- a/app/Domain/Catalog/EditorialDimension.php +++ b/app/Domain/Catalog/EditorialDimension.php @@ -8,11 +8,15 @@ class EditorialDimension extends Model { + /** Gespiegelte Säulen; gepflegt werden Säulen über die Säulenverwaltung, nicht als Stammdaten. */ + public const PILLAR_SLUG = 'saeule'; + protected $fillable = ['slug', 'label']; + /** Werte in Pflegereihenfolge. */ public function values(): HasMany { - return $this->hasMany(EditorialDimensionValue::class); + return $this->hasMany(EditorialDimensionValue::class)->orderBy('sort_order')->orderBy('id'); } /** @@ -26,7 +30,7 @@ public static function forSubject(Model $subject): Collection $query = static::query()->with('values')->orderBy('label'); if ($subject instanceof ContentElement) { - $query->where('slug', '!=', 'saeule'); + $query->where('slug', '!=', self::PILLAR_SLUG); } return $query->get(); diff --git a/app/Domain/Catalog/EditorialDimensionValue.php b/app/Domain/Catalog/EditorialDimensionValue.php index a7bb7e9..e646517 100644 --- a/app/Domain/Catalog/EditorialDimensionValue.php +++ b/app/Domain/Catalog/EditorialDimensionValue.php @@ -7,7 +7,15 @@ class EditorialDimensionValue extends Model { - protected $fillable = ['editorial_dimension_id', 'slug', 'label']; + protected $fillable = ['editorial_dimension_id', 'slug', 'label', 'sort_order', 'is_active']; + + protected function casts(): array + { + return [ + 'is_active' => 'boolean', + 'sort_order' => 'integer', + ]; + } public function dimension(): BelongsTo { diff --git a/app/Domain/Catalog/EditorialVocabulary.php b/app/Domain/Catalog/EditorialVocabulary.php new file mode 100644 index 0000000..efd7dea --- /dev/null +++ b/app/Domain/Catalog/EditorialVocabulary.php @@ -0,0 +1,252 @@ +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 + */ + 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 + */ + 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 + */ + 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 $assignedIds + * @return list}> + */ + 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 $selectedIds + * @return list}> + */ + 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 $selectedIds + * @return Collection + */ + 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 $assignedIds + * @return list + */ + 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; + } +} diff --git a/app/Domain/Catalog/IdentityDimension.php b/app/Domain/Catalog/IdentityDimension.php index 6b41e89..43801f8 100644 --- a/app/Domain/Catalog/IdentityDimension.php +++ b/app/Domain/Catalog/IdentityDimension.php @@ -57,11 +57,20 @@ public function matches(Model $subject, array $valueIds): bool * Ersetzt nur die Werte der Dimensionen, die dieses Fachobjekt anbietet. * Nicht angebotene Zuordnungen, etwa gespiegelte Säulen am Inhaltselement, bleiben. * + * Inaktive Werte (Stammdatenpflege) sind nicht neu wählbar. Bereits zugeordnete inaktive Werte zeigt der Editor + * gekennzeichnet an; sie bleiben, solange sie mitgeschickt werden, und werden nur bei ausdrücklicher Abwahl entfernt. + * * @param list $valueIds */ public function replaceOffered(Model $subject, array $valueIds): void { - $offeredIds = EditorialDimension::offeredValueIds($subject); + $assigned = IdentityDimensionValue::query() + ->where('subject_type', $subject->getMorphClass()) + ->where('subject_id', $subject->getKey()) + ->pluck('editorial_dimension_value_id') + ->map(fn ($id): int => (int) $id) + ->all(); + $offeredIds = app(EditorialVocabulary::class)->editableValueIds($subject, $assigned); $ids = array_values(array_intersect( array_values(array_unique(array_map(intval(...), $valueIds))), $offeredIds, diff --git a/app/Filament/Pages/RedaktionelleStammdaten.php b/app/Filament/Pages/RedaktionelleStammdaten.php new file mode 100644 index 0000000..7c17949 --- /dev/null +++ b/app/Filament/Pages/RedaktionelleStammdaten.php @@ -0,0 +1,671 @@ +>|null */ + private ?array $verwendungCache = null; + + public static function canAccess(): bool + { + return self::darfPflegen(); + } + + public static function darfPflegen(): bool + { + return ContentElementResource::editor(); + } + + public function mount(): void + { + if (! array_key_exists($this->gruppe, $this->gruppen())) { + $this->gruppe = 'materialart'; + } + } + + public function getSubheading(): string|Htmlable|null + { + return 'Auswahlwerte für Redaktion, Filter und Picker. Bezeichnungen sind änderbar, Zuordnungen bleiben erhalten; verwendete Werte werden deaktiviert statt gelöscht.'; + } + + public function getMaxContentWidth(): Width|string|null + { + return Width::Full; + } + + /** + * @return array Schlüssel => Bezeichnung + */ + public function gruppen(): array + { + $gruppen = ['materialart' => 'Fachliche Materialarten']; + $dimensionen = EditorialDimension::query()->where('slug', '!=', EditorialDimension::PILLAR_SLUG)->get() + ->sortBy(fn (EditorialDimension $d): string => sprintf('%02d', array_search($d->slug, self::DIMENSION_ORDER, true) === false ? 99 : array_search($d->slug, self::DIMENSION_ORDER, true)).$d->label); + + foreach ($dimensionen as $dimension) { + $gruppen[$dimension->slug] = (string) $dimension->label; + } + + return $gruppen; + } + + public function zeigeGruppe(string $gruppe): void + { + if (array_key_exists($gruppe, $this->gruppen())) { + $this->gruppe = $gruppe; + $this->verwendungCache = null; + $this->resetTable(); + } + } + + public function saeulenUrl(): string + { + return PillarResource::getUrl('index'); + } + + private function istMaterialart(): bool + { + return $this->gruppe === 'materialart'; + } + + private function dimension(): ?EditorialDimension + { + return $this->istMaterialart() ? null : EditorialDimension::query()->where('slug', $this->gruppe)->first(); + } + + // --------------------------------------------------------------------------------------------------------------- + // Tabelle + // --------------------------------------------------------------------------------------------------------------- + + public function table(Table $table): Table + { + $materialart = $this->istMaterialart(); + + return $table + ->query(function (): Builder { + if ($this->istMaterialart()) { + return EditorialContentKind::query(); + } + + return EditorialDimensionValue::query()->where('editorial_dimension_id', $this->dimension()?->id ?? 0); + }) + ->defaultSort('sort_order') + ->reorderable('sort_order') + ->reorderRecordsTriggerAction(fn (Action $action, bool $isReordering): Action => $action + ->button() + ->label($isReordering ? 'Sortieren beenden' : 'Per Ziehen sortieren')) + ->paginated(false) + ->searchPlaceholder('Bezeichnung oder Kennung') + ->columns(array_values(array_filter([ + TextColumn::make('label') + ->label('Bezeichnung') + ->searchable(['label', 'slug']) + ->weight('semibold') + ->description(fn (Model $record): string => 'Kennung: '.$record->slug + .($record instanceof EditorialContentKind && $record->isSystem() ? ' · vom System verwendet' : '')), + $materialart ? TextColumn::make('contexts') + ->label('Zulässig für') + ->state(fn (EditorialContentKind $record): array => array_values(array_map( + fn (string $context): string => EditorialContentKind::CONTEXTS[$context] ?? $context, + (array) ($record->contexts ?? []), + ))) + ->badge() + ->color('gray') : null, + TextColumn::make('is_active') + ->label('Status') + ->formatStateUsing(fn (bool $state): string => $state ? 'Aktiv' : 'Inaktiv') + ->badge() + ->color(fn (bool $state): string => $state ? 'success' : 'gray'), + TextColumn::make('verwendung') + ->label('Verwendung') + ->state(fn (Model $record): string => $this->verwendungText($record)) + ->action($this->verwendungAction()->name('verwendungZeigen')), + ]))) + ->filters([ + SelectFilter::make('is_active') + ->label('Status') + ->options(['1' => 'Aktiv', '0' => 'Inaktiv']), + ]) + ->recordActions([ + Action::make('hoch') + ->label('Nach oben') + ->icon('heroicon-m-arrow-up') + ->iconButton() + ->color('gray') + ->authorize(fn (): bool => self::darfPflegen()) + ->disabled(fn (Model $record): bool => $this->nachbar($record, -1) === null) + ->action(fn (Model $record) => $this->verschiebe($record, -1)), + Action::make('runter') + ->label('Nach unten') + ->icon('heroicon-m-arrow-down') + ->iconButton() + ->color('gray') + ->authorize(fn (): bool => self::darfPflegen()) + ->disabled(fn (Model $record): bool => $this->nachbar($record, 1) === null) + ->action(fn (Model $record) => $this->verschiebe($record, 1)), + $this->bearbeitenAction(), + ActionGroup::make([ + $this->verwendungAction(), + $this->umschaltenAction(), + $this->loeschenAction(), + ])->label('Weitere')->icon('heroicon-m-chevron-down')->button()->size('sm')->color('gray'), + ]) + ->emptyStateHeading('Noch keine Werte') + ->emptyStateDescription('Mit „Wert anlegen“ entsteht der erste Wert dieser Gruppe.'); + } + + protected function getHeaderActions(): array + { + return [ + Action::make('anlegen') + ->label('Wert anlegen') + ->icon('heroicon-o-plus') + ->authorize(fn (): bool => self::darfPflegen()) + ->modalHeading(fn (): string => 'Neuer Wert: '.($this->gruppen()[$this->gruppe] ?? '')) + ->schema(fn (): array => $this->formular(null)) + ->fillForm(fn (): array => ['contexts' => ['medium']]) + ->modalSubmitActionLabel('Anlegen') + ->action(function (array $data): void { + $this->anlegen($data); + }), + ]; + } + + // --------------------------------------------------------------------------------------------------------------- + // Formular, Anlegen, Bearbeiten + // --------------------------------------------------------------------------------------------------------------- + + /** + * @return list + */ + private function formular(?Model $record): array + { + $felder = [ + TextInput::make('label') + ->label('Bezeichnung') + ->required() + ->maxLength(120) + ->helperText($record !== null + ? 'Die Kennung „'.$record->slug.'“ bleibt; vorhandene Zuordnungen behalten den Wert und zeigen die neue Bezeichnung.' + : 'Die Kennung entsteht aus der Bezeichnung und bleibt danach fest.'), + ]; + + if ($this->istMaterialart()) { + $system = $record instanceof EditorialContentKind && $record->isSystem(); + $felder[] = CheckboxList::make('contexts') + ->label('Zulässig für') + ->options(EditorialContentKind::CONTEXTS) + ->required() + ->live() + ->columns(['default' => 2, 'md' => 4]) + ->disabled($system) + ->dehydrated(! $system) + ->helperText($system + ? 'Diese Materialart verwendet das System; ihre Kontexte bleiben fest.' + : 'Nur hier wird die Materialart neu angeboten. Programm und Modul gehören zu den Objektarten Programm und Modul, nicht zu Medien.'); + + if ($record instanceof EditorialContentKind) { + $felder[] = Placeholder::make('konflikt') + ->label('Bestehende Zuordnungen') + ->visible(fn (Get $get): bool => $this->kontextKonflikte($record, (array) $get('contexts')) !== []) + ->content(fn (Get $get): HtmlString => new HtmlString('' + .e(implode(' ', $this->kontextKonflikte($record, (array) $get('contexts')))) + .' Diese Zuordnungen bleiben erhalten und werden dort als „hier nicht vorgesehen“ gekennzeichnet; neu wählbar ist die Materialart dort nicht mehr.')); + $felder[] = Checkbox::make('konflikt_bestaetigt') + ->label('Ich habe die bestehenden Zuordnungen geprüft.') + ->accepted() + ->visible(fn (Get $get): bool => $this->kontextKonflikte($record, (array) $get('contexts')) !== []); + } + } + + return $felder; + } + + /** + * @return list Meldungen je entferntem Kontext mit Verwendung + */ + private function kontextKonflikte(EditorialContentKind $kind, array $neu): array + { + $verwendung = $this->verwendung($kind); + $meldungen = []; + + foreach ((array) ($kind->contexts ?? []) as $context) { + if (! in_array($context, $neu, true) && ($verwendung[$context] ?? 0) > 0) { + $meldungen[] = (EditorialContentKind::CONTEXTS[$context] ?? $context).': '.$verwendung[$context].' '.($verwendung[$context] === 1 ? 'Zuordnung' : 'Zuordnungen').'.'; + } + } + + return $meldungen; + } + + private function bearbeitenAction(): Action + { + return Action::make('bearbeiten') + ->label('Bearbeiten') + ->icon('heroicon-o-pencil-square') + ->button() + ->size('sm') + ->color('gray') + ->authorize(fn (): bool => self::darfPflegen()) + ->modalHeading(fn (Model $record): string => '„'.$record->label.'“ bearbeiten') + ->schema(fn (Model $record): array => $this->formular($record)) + ->fillForm(fn (Model $record): array => [ + 'label' => $record->label, + 'contexts' => $record instanceof EditorialContentKind ? (array) ($record->contexts ?? []) : [], + ]) + ->modalSubmitActionLabel('Speichern') + ->action(function (Model $record, array $data): void { + $label = trim((string) $data['label']); + $this->pruefeBezeichnung($label, $record); + $record->label = $label; + + if ($record instanceof EditorialContentKind && ! $record->isSystem() && isset($data['contexts'])) { + $record->contexts = $this->kontexte((array) $data['contexts']); + } + + $record->save(); + Notification::make()->success()->title('Gespeichert')->body('Die neue Bezeichnung gilt sofort in Redaktion, Filtern und Picker.')->send(); + }); + } + + private function anlegen(array $data): void + { + $label = trim((string) $data['label']); + $this->pruefeBezeichnung($label, null); + + if ($this->istMaterialart()) { + EditorialContentKind::query()->create([ + 'label' => $label, + 'slug' => $this->freieKennung($label, EditorialContentKind::query()), + 'sort_order' => (int) EditorialContentKind::query()->max('sort_order') + 10, + 'is_active' => true, + 'contexts' => $this->kontexte((array) ($data['contexts'] ?? [])), + ]); + } else { + $dimension = $this->dimension(); + abort_if($dimension === null, 404); + $werte = EditorialDimensionValue::query()->where('editorial_dimension_id', $dimension->id); + EditorialDimensionValue::query()->create([ + 'editorial_dimension_id' => $dimension->id, + 'label' => $label, + 'slug' => $this->freieKennung($label, clone $werte), + 'sort_order' => (int) (clone $werte)->max('sort_order') + 10, + 'is_active' => true, + ]); + } + + $this->verwendungCache = null; + Notification::make()->success()->title('Angelegt: „'.$label.'“')->body('Der Wert ist sofort in der passenden Redaktion wählbar.')->send(); + } + + private function pruefeBezeichnung(string $label, ?Model $ausser): void + { + $query = $this->istMaterialart() + ? EditorialContentKind::query() + : EditorialDimensionValue::query()->where('editorial_dimension_id', $this->dimension()?->id ?? 0); + + $doppelt = $query->whereRaw('LOWER(label) = ?', [mb_strtolower($label)]) + ->when($ausser !== null, fn (Builder $q) => $q->whereKeyNot($ausser->getKey())) + ->exists(); + + if ($label === '' || $doppelt) { + Notification::make()->danger()->title($label === '' ? 'Die Bezeichnung fehlt.' : 'Diese Bezeichnung gibt es in der Gruppe schon.')->send(); + throw new Halt; + } + } + + /** + * @return list + */ + private function kontexte(array $kontexte): array + { + $erlaubt = array_keys(EditorialContentKind::CONTEXTS); + + return array_values(array_filter($erlaubt, fn (string $context): bool => in_array($context, $kontexte, true))); + } + + private function freieKennung(string $label, Builder $query): string + { + $basis = Str::slug($label) ?: 'wert'; + $kennung = $basis; + $nummer = 2; + + while ((clone $query)->where('slug', $kennung)->exists()) { + $kennung = $basis.'-'.$nummer++; + } + + return $kennung; + } + + // --------------------------------------------------------------------------------------------------------------- + // Aktiv, Löschen, Reihenfolge + // --------------------------------------------------------------------------------------------------------------- + + private function umschaltenAction(): Action + { + return Action::make('umschalten') + ->label(fn (Model $record): string => $record->is_active ? 'Deaktivieren' : 'Reaktivieren') + ->icon(fn (Model $record): string => $record->is_active ? 'heroicon-o-eye-slash' : 'heroicon-o-eye') + ->authorize(fn (): bool => self::darfPflegen()) + ->hidden(fn (Model $record): bool => $record instanceof EditorialContentKind && $record->isSystem()) + ->requiresConfirmation() + ->modalHeading(fn (Model $record): string => '„'.$record->label.'“ '.($record->is_active ? 'deaktivieren?' : 'reaktivieren?')) + ->modalDescription(fn (Model $record): string => $record->is_active + ? 'Der Wert wird für neue Zuordnungen nicht mehr angeboten. '.$this->verwendungSatz($record).' Filter finden diese Objekte weiterhin.' + : 'Der Wert wird wieder für neue Zuordnungen angeboten.') + ->modalSubmitActionLabel(fn (Model $record): string => $record->is_active ? 'Deaktivieren' : 'Reaktivieren') + ->action(function (Model $record): void { + abort_if($record instanceof EditorialContentKind && $record->isSystem(), 403); + $record->is_active = ! $record->is_active; + $record->save(); + }); + } + + private function loeschenAction(): Action + { + return Action::make('loeschen') + ->label('Löschen') + ->icon('heroicon-o-trash') + ->color('danger') + ->authorize(fn (): bool => self::darfPflegen()) + ->visible(fn (Model $record): bool => $this->loeschbar($record)) + ->requiresConfirmation() + ->modalHeading(fn (Model $record): string => '„'.$record->label.'“ löschen?') + ->modalDescription('Der Wert ist nirgends zugeordnet und wird endgültig entfernt.') + ->modalSubmitActionLabel('Löschen') + ->action(function (Model $record): void { + // Erneut prüfen: Zwischenzeitlich entstandene Zuordnungen würden die Datenbank sonst still lösen. + if (! $this->loeschbar($record)) { + Notification::make()->danger()->title('Nicht gelöscht: Der Wert ist inzwischen zugeordnet. Deaktivieren Sie ihn stattdessen.')->send(); + + return; + } + + $record->delete(); + $this->verwendungCache = null; + }); + } + + private function loeschbar(Model $record): bool + { + if ($record instanceof EditorialContentKind && $record->isSystem()) { + return false; + } + + return array_sum($this->verwendung($record, frisch: true)) === 0; + } + + private function nachbar(Model $record, int $richtung): ?Model + { + $liste = $this->geordnet(); + $index = $liste->search(fn (Model $m): bool => $m->is($record)); + + return $index === false ? null : $liste->get($index + $richtung); + } + + private function verschiebe(Model $record, int $richtung): void + { + $liste = $this->geordnet()->values(); + $index = $liste->search(fn (Model $m): bool => $m->is($record)); + + if ($index === false || $liste->get($index + $richtung) === null) { + return; + } + + $neu = $liste->all(); + [$neu[$index], $neu[$index + $richtung]] = [$neu[$index + $richtung], $neu[$index]]; + + DB::transaction(function () use ($neu): void { + foreach (array_values($neu) as $position => $eintrag) { + if ((int) $eintrag->sort_order !== ($position + 1) * 10) { + $eintrag->sort_order = ($position + 1) * 10; + $eintrag->save(); + } + } + }); + } + + /** + * @return Collection + */ + private function geordnet(): Collection + { + $query = $this->istMaterialart() + ? EditorialContentKind::query() + : EditorialDimensionValue::query()->where('editorial_dimension_id', $this->dimension()?->id ?? 0); + + return $query->orderBy('sort_order')->orderBy('id')->get()->values(); + } + + // --------------------------------------------------------------------------------------------------------------- + // Verwendung + // --------------------------------------------------------------------------------------------------------------- + + /** + * Zuordnungen je Objektkontext. + * + * @return array + */ + public function verwendung(Model $record, bool $frisch = false): array + { + if ($frisch || $this->verwendungCache === null) { + $this->verwendungCache = $this->istMaterialart() ? $this->materialartVerwendung() : $this->einordnungVerwendung(); + } + + return $this->verwendungCache[(int) $record->getKey()] ?? []; + } + + private function verwendungText(Model $record): string + { + $teile = []; + + foreach ($this->verwendung($record) as $context => $anzahl) { + if ($anzahl > 0) { + $teile[] = $anzahl.' '.$this->einheit($context, $anzahl); + } + } + + return $teile === [] ? 'Nicht verwendet' : implode(' · ', $teile); + } + + private function verwendungSatz(Model $record): string + { + $text = $this->verwendungText($record); + + return $text === 'Nicht verwendet' + ? 'Er ist derzeit nirgends zugeordnet.' + : 'Bestehende Zuordnungen ('.$text.') bleiben erhalten und sichtbar.'; + } + + private function einheit(string $context, int $anzahl): string + { + return match ($context) { + 'medium' => $anzahl === 1 ? 'Medium' : 'Medien', + 'modul' => $anzahl === 1 ? 'Modul' : 'Module', + 'programm' => $anzahl === 1 ? 'Programm' : 'Programme', + 'lizenzartikel' => 'Lizenzartikel', + default => $anzahl === 1 ? 'Kombination' : 'Kombinationen', + }; + } + + /** + * @return array> + */ + private function materialartVerwendung(): array + { + $ergebnis = []; + + foreach (DB::table('content_elements')->whereNotNull('editorial_content_kind_id')->groupBy('editorial_content_kind_id')->selectRaw('editorial_content_kind_id as id, count(*) as anzahl')->get() as $zeile) { + $ergebnis[(int) $zeile->id]['medium'] = (int) $zeile->anzahl; + } + + foreach (DB::table('compositions')->whereNotNull('editorial_content_kind_id')->groupBy('editorial_content_kind_id', 'kind')->selectRaw('editorial_content_kind_id as id, kind, count(*) as anzahl')->get() as $zeile) { + $context = EditorialVocabulary::compositionContext(CompositionKind::tryFrom((string) $zeile->kind)) ?? 'kombination'; + $ergebnis[(int) $zeile->id][$context] = ($ergebnis[(int) $zeile->id][$context] ?? 0) + (int) $zeile->anzahl; + } + + foreach (DB::table('license_articles')->whereNotNull('editorial_content_kind_id')->groupBy('editorial_content_kind_id')->selectRaw('editorial_content_kind_id as id, count(*) as anzahl')->get() as $zeile) { + $ergebnis[(int) $zeile->id]['lizenzartikel'] = (int) $zeile->anzahl; + } + + return $ergebnis; + } + + /** + * @return array> + */ + private function einordnungVerwendung(): array + { + $ergebnis = []; + $zeilen = DB::table('identity_dimension_values') + ->leftJoin('compositions', function ($join): void { + $join->on('compositions.id', '=', 'identity_dimension_values.subject_id') + ->where('identity_dimension_values.subject_type', (new Composition)->getMorphClass()); + }) + ->groupBy('identity_dimension_values.editorial_dimension_value_id', 'identity_dimension_values.subject_type', 'compositions.kind') + ->selectRaw('identity_dimension_values.editorial_dimension_value_id as id, identity_dimension_values.subject_type as typ, compositions.kind as kind, count(*) as anzahl') + ->get(); + + foreach ($zeilen as $zeile) { + $context = match ($zeile->typ) { + (new ContentElement)->getMorphClass() => 'medium', + (new LicenseArticle)->getMorphClass() => 'lizenzartikel', + default => EditorialVocabulary::compositionContext(CompositionKind::tryFrom((string) $zeile->kind)) ?? 'kombination', + }; + $ergebnis[(int) $zeile->id][$context] = ($ergebnis[(int) $zeile->id][$context] ?? 0) + (int) $zeile->anzahl; + } + + return $ergebnis; + } + + private function verwendungAction(): Action + { + return Action::make('verwendung') + ->label('Verwendung') + ->icon('heroicon-m-list-bullet') + ->authorize(fn (): bool => self::darfPflegen()) + ->modalHeading(fn (Model $record): string => 'Verwendung von „'.$record->label.'“') + ->modalContent(fn (Model $record) => view('filament.stammdaten-verwendung', $this->verwendungListe($record))) + ->modalSubmitAction(false) + ->modalCancelActionLabel('Schließen'); + } + + /** + * Zugeordnete Objekte mit Links in ihre Bearbeitung und gefilterte Bestandslisten. + * + * @return array{zeilen: list, mehr: int, filter: list} + */ + public function verwendungListe(Model $record): array + { + $grenze = 40; + $zeilen = []; + $filter = []; + + if ($record instanceof EditorialContentKind) { + $medien = ContentElement::query()->where('editorial_content_kind_id', $record->id); + $kompositionen = Composition::query()->where('editorial_content_kind_id', $record->id); + $artikel = LicenseArticle::query()->where('editorial_content_kind_id', $record->id); + if ((clone $medien)->exists()) { + $filter[] = ['label' => 'Im Medienmanager filtern', 'url' => ContentElementResource::getUrl('index', ['inhaltsart' => $record->id])]; + } + } else { + $zuordnung = fn (string $klasse) => DB::table('identity_dimension_values') + ->where('editorial_dimension_value_id', $record->getKey()) + ->where('subject_type', (new $klasse)->getMorphClass()) + ->pluck('subject_id'); + $medien = ContentElement::query()->whereKey($zuordnung(ContentElement::class)); + $kompositionen = Composition::query()->whereKey($zuordnung(Composition::class)); + $artikel = LicenseArticle::query()->whereKey($zuordnung(LicenseArticle::class)); + if ((clone $medien)->exists()) { + $filter[] = ['label' => 'Im Medienmanager filtern', 'url' => ContentElementResource::getUrl('index', ['einordnung' => $record->getKey()])]; + } + if ((clone $kompositionen)->exists()) { + $filter[] = ['label' => 'Programme und Module filtern', 'url' => EditorialDesk::getUrl(['pillar' => $record->getKey()])]; + } + } + + $gesamt = (clone $medien)->count() + (clone $kompositionen)->count() + (clone $artikel)->count(); + + foreach ($medien->orderBy('working_title')->limit($grenze)->get() as $element) { + $zeilen[] = ['art' => 'Medium', 'titel' => (string) ($element->display_title ?: $element->working_title), 'url' => ContentElementResource::getUrl('view', ['record' => $element])]; + } + + foreach ($kompositionen->orderBy('designation')->limit($grenze)->get() as $composition) { + $context = EditorialVocabulary::compositionContext($composition->kind instanceof CompositionKind ? $composition->kind : CompositionKind::tryFrom((string) $composition->kind)); + $zeilen[] = ['art' => EditorialContentKind::CONTEXTS[$context ?? ''] ?? 'Kombination', 'titel' => (string) ($composition->display_title ?: $composition->designation), 'url' => EditorialCatalog::editUrl($composition)]; + } + + foreach ($artikel->orderBy('code')->limit($grenze)->get() as $article) { + $zeilen[] = ['art' => 'Lizenzartikel', 'titel' => trim($article->code.' '.($article->display_title ?: $article->working_title)), 'url' => LicenseArticleResource::getUrl('view', ['record' => $article])]; + } + + return ['zeilen' => $zeilen, 'mehr' => max(0, $gesamt - count($zeilen)), 'filter' => $filter]; + } +} diff --git a/app/Filament/Resources/Compositions/Pages/EditComposition.php b/app/Filament/Resources/Compositions/Pages/EditComposition.php index b92d7fa..5fe04cf 100644 --- a/app/Filament/Resources/Compositions/Pages/EditComposition.php +++ b/app/Filament/Resources/Compositions/Pages/EditComposition.php @@ -13,7 +13,7 @@ use App\Domain\Catalog\ContentService; use App\Domain\Catalog\ContentVersionStatus; use App\Domain\Catalog\DomainRuleException; -use App\Domain\Catalog\EditorialDimension; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\EditorialWorkingDraft; use App\Domain\Catalog\IdentityDimension; use App\Domain\Catalog\IdentityDimensionValue; @@ -708,21 +708,10 @@ public function usageLines(): array */ public function dimensionGroups(): array { - $groups = []; - - foreach (EditorialDimension::forSubject($this->getRecord()) as $dimension) { - $values = []; - - foreach ($dimension->values as $value) { - $values[] = ['id' => $value->id, 'label' => $value->label]; - } - - if ($values !== []) { - $groups[] = ['label' => $dimension->label, 'values' => $values]; - } - } - - return $groups; + return app(EditorialVocabulary::class)->dimensionGroups($this->getRecord(), IdentityDimensionValue::query() + ->where('subject_type', $this->getRecord()->getMorphClass()) + ->where('subject_id', $this->getRecord()->getKey()) + ->pluck('editorial_dimension_value_id')->map(fn ($id): int => (int) $id)->all()); } /** diff --git a/app/Filament/Resources/ContentElements/Pages/EditContentElement.php b/app/Filament/Resources/ContentElements/Pages/EditContentElement.php index ad57a38..d062061 100644 --- a/app/Filament/Resources/ContentElements/Pages/EditContentElement.php +++ b/app/Filament/Resources/ContentElements/Pages/EditContentElement.php @@ -7,8 +7,7 @@ use App\Domain\Catalog\ContentElementVersion; use App\Domain\Catalog\ContentService; use App\Domain\Catalog\DomainRuleException; -use App\Domain\Catalog\EditorialContentKind; -use App\Domain\Catalog\EditorialDimension; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\IdentityDimension; use App\Domain\Catalog\IdentityDimensionValue; use App\Domain\Catalog\IdentityProfile; @@ -74,8 +73,8 @@ public function form(Schema $schema): Schema ->helperText('Erscheint in der Bibliothek. Leer lässt den Arbeitstitel stehen. Speichern legt keine Fassung an.'), Textarea::make('summary')->label('Kurzbeschreibung')->rows(3), Select::make('editorial_content_kind_id') - ->label('Inhaltsart') - ->options(fn (): array => EditorialContentKind::selectOptions()) + ->label('Fachliche Materialart') + ->options(fn (): array => app(EditorialVocabulary::class)->kindOptions('medium', $this->getRecord()->editorial_content_kind_id !== null ? (int) $this->getRecord()->editorial_content_kind_id : null)) ->nullable() ->helperText('Optional für Bibliothek und Website. Ändert weder die Ausgabeart noch eine Fassung.'), Placeholder::make('usage')->label('Verwendung')->content(function (): HtmlString { @@ -103,9 +102,12 @@ public function form(Schema $schema): Schema CheckboxList::make('dimension_ids')->label('Fachliche Einordnung') ->options(function (): array { $options = []; - foreach (EditorialDimension::forSubject($this->getRecord()) as $dimension) { - foreach ($dimension->values as $value) { - $options[$value->id] = $dimension->label.': '.$value->label; + foreach (app(EditorialVocabulary::class)->dimensionGroups($this->getRecord(), IdentityDimensionValue::query() + ->where('subject_type', $this->getRecord()->getMorphClass()) + ->where('subject_id', $this->getRecord()->getKey()) + ->pluck('editorial_dimension_value_id')->map(fn ($id): int => (int) $id)->all()) as $dimension) { + foreach ($dimension['values'] as $value) { + $options[$value['id']] = $dimension['label'].': '.$value['label']; } } diff --git a/app/Filament/Resources/ContentElements/Pages/ListContentElements.php b/app/Filament/Resources/ContentElements/Pages/ListContentElements.php index 87e0c5d..4cbde8c 100644 --- a/app/Filament/Resources/ContentElements/Pages/ListContentElements.php +++ b/app/Filament/Resources/ContentElements/Pages/ListContentElements.php @@ -2,7 +2,7 @@ namespace App\Filament\Resources\ContentElements\Pages; -use App\Domain\Catalog\EditorialContentKind; +use App\Domain\Catalog\EditorialVocabulary; use App\Filament\Resources\ContentElements\ContentElementResource; use App\Filament\Support\Bestand; use App\Filament\Support\MediaLibrary; @@ -121,7 +121,7 @@ public function pillarOptions(): array */ public function classificationOptions(): array { - return app(MediaLibrary::class)->classifications(); + return app(MediaLibrary::class)->classifications(ctype_digit($this->einordnung) ? [(int) $this->einordnung] : []); } /** @@ -129,7 +129,7 @@ public function classificationOptions(): array */ public function contentKindOptions(): array { - return EditorialContentKind::selectOptions(); + return app(EditorialVocabulary::class)->kindFilterOptions('medium', $this->inhaltsart); } public function filtersActive(): bool diff --git a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php index 3368aac..1d648a5 100644 --- a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php +++ b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php @@ -7,8 +7,7 @@ use App\Domain\Catalog\ContentService; use App\Domain\Catalog\ContentVersionStatus; use App\Domain\Catalog\DomainRuleException; -use App\Domain\Catalog\EditorialContentKind; -use App\Domain\Catalog\EditorialDimension; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\IdentityChange; use App\Domain\Catalog\IdentityDimension; use App\Domain\Catalog\IdentityDimensionValue; @@ -648,6 +647,19 @@ protected function saveRedaktionTexte(): bool return false; } + $kindError = app(EditorialVocabulary::class)->kindError( + 'medium', + filled($this->kindId) ? (int) $this->kindId : null, + $element->editorial_content_kind_id !== null ? (int) $element->editorial_content_kind_id : null, + ); + + if ($kindError !== null) { + $this->deskFeedback = $kindError; + $this->redaktionFehlerFeld = 'kindId'; + + return false; + } + $this->versionsBeforeSave = $element->versions()->count(); app(IdentityProfile::class)->fill($element, [ 'working_title' => $title, @@ -776,8 +788,8 @@ public function einordnungArt(): array { return [ 'model' => 'kindId', - 'options' => EditorialContentKind::selectOptions(), - 'hilfe' => 'Fachliche Materialart, unabhängig von der Dateiart.', + 'options' => app(EditorialVocabulary::class)->kindOptions('medium', $this->getRecord()->editorial_content_kind_id !== null ? (int) $this->getRecord()->editorial_content_kind_id : null), + 'hilfe' => 'Fachliche Materialart, unabhängig von der technischen Medienart.', ]; } @@ -793,11 +805,14 @@ public function einordnungGruppen(): array ->map(fn (Pillar $pillar): array => ['id' => (int) $pillar->id, 'label' => (string) $pillar->name])->all(), ]]; - foreach (EditorialDimension::forSubject($this->getRecord()) as $dimension) { + foreach (app(EditorialVocabulary::class)->dimensionGroups($this->getRecord(), IdentityDimensionValue::query() + ->where('subject_type', $this->getRecord()->getMorphClass()) + ->where('subject_id', $this->getRecord()->getKey()) + ->pluck('editorial_dimension_value_id')->map(fn ($id): int => (int) $id)->all()) as $dimension) { $groups[] = [ - 'legend' => (string) $dimension->label, + 'legend' => $dimension['label'], 'model' => 'dimensionIds', - 'values' => collect($dimension->values)->map(fn ($value): array => ['id' => (int) $value->id, 'label' => (string) $value->label])->all(), + 'values' => $dimension['values'], ]; } @@ -1014,7 +1029,7 @@ public function editorialHistory(): array 'thumbnail_alt' => 'Alternativtext', 'thumbnail_path' => 'Vorschaubild', 'thumbnail_source' => 'Herkunft des Vorschaubilds', - 'editorial_content_kind_id' => 'Inhaltsart', + 'editorial_content_kind_id' => 'Fachliche Materialart', ]; return IdentityChange::query() diff --git a/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php b/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php index b865732..9c17df4 100644 --- a/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php +++ b/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php @@ -8,9 +8,8 @@ use App\Domain\Catalog\CompositionVersion; use App\Domain\Catalog\ContentElement; use App\Domain\Catalog\ContentElementVersion; -use App\Domain\Catalog\EditorialContentKind; -use App\Domain\Catalog\EditorialDimension; use App\Domain\Catalog\EditorialProfileReader; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\IdentityDimension; use App\Domain\Catalog\IdentityDimensionValue; use App\Domain\Catalog\IdentityProfile; @@ -92,6 +91,19 @@ protected function saveRedaktionTexte(): bool $this->releaseBeforeSave = $article->current_release_id; $this->releaseCountBeforeSave = $article->releases()->count(); + $kindError = app(EditorialVocabulary::class)->kindError( + 'lizenzartikel', + filled($this->kindId) ? (int) $this->kindId : null, + $article->editorial_content_kind_id !== null ? (int) $article->editorial_content_kind_id : null, + ); + + if ($kindError !== null) { + $this->deskFeedback = $kindError; + $this->redaktionFehlerFeld = 'kindId'; + + return false; + } + app(IdentityProfile::class)->fill($article, [ 'working_title' => $this->workingTitle, 'display_title' => $this->displayTitle, @@ -248,7 +260,7 @@ public function einordnungArt(): array { return [ 'model' => 'kindId', - 'options' => EditorialContentKind::selectOptions(), + 'options' => app(EditorialVocabulary::class)->kindOptions('lizenzartikel', $this->getRecord()->editorial_content_kind_id !== null ? (int) $this->getRecord()->editorial_content_kind_id : null), 'hilfe' => 'Die Einordnung des Artikels ändert weder die Quelle noch den Preis.', ]; } @@ -277,21 +289,10 @@ private function sourceTitle(Model $source): string */ public function licenseDimensionGroups(): array { - $groups = []; - - foreach (EditorialDimension::forSubject($this->getRecord()) as $dimension) { - $values = []; - - foreach ($dimension->values as $value) { - $values[] = ['id' => $value->id, 'label' => $value->label]; - } - - if ($values !== []) { - $groups[] = ['label' => $dimension->label, 'values' => $values]; - } - } - - return $groups; + return app(EditorialVocabulary::class)->dimensionGroups($this->getRecord(), IdentityDimensionValue::query() + ->where('subject_type', $this->getRecord()->getMorphClass()) + ->where('subject_id', $this->getRecord()->getKey()) + ->pluck('editorial_dimension_value_id')->map(fn ($id): int => (int) $id)->all()); } private function fillLicenseEditorial(): void diff --git a/app/Filament/Support/ChoosesMedia.php b/app/Filament/Support/ChoosesMedia.php index 5565112..66ab025 100644 --- a/app/Filament/Support/ChoosesMedia.php +++ b/app/Filament/Support/ChoosesMedia.php @@ -4,6 +4,7 @@ use App\Domain\Catalog\ContentElement; use App\Domain\Catalog\ContentElementVersion; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Cms\Catalog\PublicMaterialReader; use App\Domain\Cms\Widgets\Support\WebsiteImage; use App\Http\Controllers\ContentPreviewController; @@ -26,6 +27,9 @@ trait ChoosesMedia public string $pickerDimension = ''; + /** Filter „Fachliche Materialart“ (Kennung), gleiche Regel wie Medieneditor und Medienfilter. */ + public string $pickerMaterialart = ''; + public string $pickerInspected = ''; public string $pickerSelected = ''; @@ -99,15 +103,16 @@ public function pickerResult(): array } if ($this->pickerPurpose === 'aufbau') { - return app(EditorialCatalog::class)->browse( + return $this->filterByMaterialart(app(EditorialCatalog::class)->browse( $this->pickerQuery, $this->pickerKind, $this->pickerDimension, $this->pickerExceptId(), - ); + )); } $result = app(EditorialCatalog::class)->presentationBrowse($this->pickerQuery, $this->pickerKind); + $result = $this->filterByMaterialart($result); // CMS: nur Bilder, die als Website-Bild wählbar sind (keine vertraulichen Arten, nicht archiviert). if ($this->pickerPurpose === 'website-bild') { @@ -159,6 +164,7 @@ public function pickerDetail(): ?array $row['released'] = $element->website_released_at !== null; $row['output_label'] = PublicMaterialReader::outputLabel((string) $element->output_class); $row['pillars'] = $element->pillars->pluck('name')->all(); + $row['material'] = $element->editorialContentKind?->label; $row['usage'] = app(CatalogReading::class)->elementUsage($element); } @@ -198,10 +204,38 @@ protected function resetPicker(string $purpose): void $this->pickerOpen = true; $this->pickerQuery = ''; $this->pickerDimension = ''; + $this->pickerMaterialart = ''; $this->pickerInspected = ''; $this->pickerSelected = ''; $this->pickerMarked = []; $this->replacingKey = null; $this->bindToVersion = false; } + + /** + * Fachliche Materialarten, nach denen der Picker Medien eingrenzen kann (Kontext Medium). + * + * @return array + */ + public function pickerMaterialarten(): array + { + return app(EditorialVocabulary::class)->kindFilterOptions('medium', $this->pickerMaterialart); + } + + /** + * @param array{items: list>, more: bool} $result + * @return array{items: list>, more: bool} + */ + private function filterByMaterialart(array $result): array + { + if (! ctype_digit($this->pickerMaterialart)) { + return $result; + } + + $ids = array_values(array_filter(array_map(fn (array $item): int => (int) ($item['element_id'] ?? 0), $result['items']))); + $passend = ContentElement::query()->whereKey($ids)->where('editorial_content_kind_id', (int) $this->pickerMaterialart)->pluck('id')->map(fn ($id): int => (int) $id)->all(); + $result['items'] = array_values(array_filter($result['items'], fn (array $item): bool => in_array((int) ($item['element_id'] ?? 0), $passend, true))); + + return $result; + } } diff --git a/app/Filament/Support/ListsCompositions.php b/app/Filament/Support/ListsCompositions.php index 3ae1495..bf4345c 100644 --- a/app/Filament/Support/ListsCompositions.php +++ b/app/Filament/Support/ListsCompositions.php @@ -5,7 +5,7 @@ use App\Domain\Catalog\Composition; use App\Domain\Catalog\CompositionKind; use App\Domain\Catalog\ContentVersionStatus; -use App\Domain\Catalog\EditorialDimension; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\IdentityDimensionValue; use App\Filament\Resources\ContentElements\ContentElementResource; use Filament\Actions\Action; @@ -111,21 +111,7 @@ public function bestand(): array */ public function dimensionFilters(): array { - $groups = []; - - foreach (EditorialDimension::query()->with('values')->orderBy('label')->get() as $dimension) { - $values = []; - - foreach ($dimension->values as $value) { - $values[] = ['id' => $value->id, 'label' => $value->label]; - } - - if ($values !== []) { - $groups[] = ['label' => $dimension->label, 'values' => $values]; - } - } - - return $groups; + return app(EditorialVocabulary::class)->dimensionFilterGroups(ctype_digit($this->pillar) ? [(int) $this->pillar] : []); } /** diff --git a/app/Filament/Support/ManagesEditorialSurface.php b/app/Filament/Support/ManagesEditorialSurface.php index 0404cb7..5746522 100644 --- a/app/Filament/Support/ManagesEditorialSurface.php +++ b/app/Filament/Support/ManagesEditorialSurface.php @@ -232,7 +232,7 @@ public function saveRedaktion(): void if (! $this->saveRedaktionTexte()) { $this->deskFeedbackError = true; - $this->redaktionTab = 'texte'; + $this->redaktionTab = $this->redaktionFehlerFeld === 'kindId' ? 'einordnung' : 'texte'; return; } diff --git a/app/Filament/Support/MediaLibrary.php b/app/Filament/Support/MediaLibrary.php index ffe6707..044645b 100644 --- a/app/Filament/Support/MediaLibrary.php +++ b/app/Filament/Support/MediaLibrary.php @@ -8,7 +8,7 @@ use App\Domain\Catalog\ContentElement; use App\Domain\Catalog\ContentElementVersion; use App\Domain\Catalog\ContentVersionStatus; -use App\Domain\Catalog\EditorialDimension; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\IdentityDimensionValue; use App\Domain\Catalog\Pillar; use App\Domain\Catalog\ReleaseContent; @@ -50,15 +50,16 @@ public function pillars(): array /** * @return list */ - public function classifications(): array + public function classifications(array $selectedIds = []): array { $options = []; - foreach (EditorialDimension::query()->with('values')->orderBy('label')->get() as $dimension) { - foreach ($dimension->values->sortBy('label') as $value) { + // Gleiche Regel wie Editor und Picker; verwendete inaktive Werte und gewählte Filter bleiben auffindbar. + foreach (app(EditorialVocabulary::class)->dimensionFilterGroups($selectedIds) as $dimension) { + foreach ($dimension['values'] as $value) { $options[] = [ - 'id' => $value->id, - 'label' => $dimension->label.': '.$value->label, + 'id' => $value['id'], + 'label' => $dimension['label'].': '.$value['label'], ]; } } diff --git a/app/Http/Controllers/Member/LibraryController.php b/app/Http/Controllers/Member/LibraryController.php index 10c310a..147fe63 100644 --- a/app/Http/Controllers/Member/LibraryController.php +++ b/app/Http/Controllers/Member/LibraryController.php @@ -6,7 +6,7 @@ use App\Domain\Catalog\Composition; use App\Domain\Catalog\ContentElement; use App\Domain\Catalog\ContentElementVersion; -use App\Domain\Catalog\EditorialDimension; +use App\Domain\Catalog\EditorialVocabulary; use App\Domain\Catalog\IdentityDimension; use App\Domain\Catalog\LicenseArticle; use App\Domain\Entitlement\DecideAccess; @@ -89,7 +89,7 @@ function (array $card) use ($dimensions, $dimensionIds): bool { 'sicht' => $sicht, 'nutzung' => $nutzung, 'dimensionIds' => $dimensionIds, - 'filterDimensions' => EditorialDimension::query()->with('values')->orderBy('label')->get(), + 'filterDimensions' => app(EditorialVocabulary::class)->memberFilterDimensions(array_map(intval(...), $dimensionIds)), 'libraryEmpty' => $untouched, ]); } diff --git a/database/migrations/2026_11_20_200000_editorial_master_data_management.php b/database/migrations/2026_11_20_200000_editorial_master_data_management.php new file mode 100644 index 0000000..7cb2699 --- /dev/null +++ b/database/migrations/2026_11_20_200000_editorial_master_data_management.php @@ -0,0 +1,72 @@ +boolean('is_active')->default(true)->after('sort_order'); + $table->json('contexts')->nullable()->after('is_active'); + }); + + Schema::table('editorial_dimension_values', function (Blueprint $table) { + $table->unsignedInteger('sort_order')->default(0)->after('label'); + $table->boolean('is_active')->default(true)->after('sort_order'); + }); + + foreach (DB::table('editorial_dimension_values')->orderBy('editorial_dimension_id')->orderBy('id')->get(['id', 'editorial_dimension_id']) as $index => $value) { + DB::table('editorial_dimension_values')->where('id', $value->id)->update(['sort_order' => ($index + 1) * 10]); + } + + $compositionContexts = ['kurs' => 'programm', 'uebung' => 'modul']; + + foreach (DB::table('editorial_content_kinds')->get(['id', 'slug']) as $kind) { + $contexts = match ($kind->slug) { + 'programm' => ['programm', 'lizenzartikel'], + 'modul' => ['modul', 'lizenzartikel'], + default => ['medium', 'lizenzartikel'], + }; + + if (DB::table('content_elements')->where('editorial_content_kind_id', $kind->id)->exists()) { + $contexts[] = 'medium'; + } + + foreach (DB::table('compositions')->where('editorial_content_kind_id', $kind->id)->distinct()->pluck('kind') as $compositionKind) { + if (isset($compositionContexts[$compositionKind])) { + $contexts[] = $compositionContexts[$compositionKind]; + } + } + + $order = ['medium', 'modul', 'programm', 'lizenzartikel']; + $contexts = array_values(array_filter($order, fn (string $context): bool => in_array($context, $contexts, true))); + + DB::table('editorial_content_kinds')->where('id', $kind->id)->update(['contexts' => json_encode($contexts)]); + } + } + + public function down(): void + { + Schema::table('editorial_dimension_values', function (Blueprint $table) { + $table->dropColumn(['sort_order', 'is_active']); + }); + + Schema::table('editorial_content_kinds', function (Blueprint $table) { + $table->dropColumn(['is_active', 'contexts']); + }); + } +}; diff --git a/resources/views/filament/stammdaten-verwendung.blade.php b/resources/views/filament/stammdaten-verwendung.blade.php new file mode 100644 index 0000000..e815d3a --- /dev/null +++ b/resources/views/filament/stammdaten-verwendung.blade.php @@ -0,0 +1,24 @@ +
+ @if ($zeilen === []) +

Dieser Wert ist derzeit nirgends zugeordnet.

+ @else + + @if ($mehr > 0) +

Und {{ $mehr }} weitere. Die gefilterten Listen zeigen alle.

+ @endif + @endif + @if ($filter !== []) +

+ @foreach ($filter as $link) + {{ $link['label'] }} + @endforeach +

+ @endif +
diff --git a/resources/views/filament/stammdaten.blade.php b/resources/views/filament/stammdaten.blade.php new file mode 100644 index 0000000..c57dfd3 --- /dev/null +++ b/resources/views/filament/stammdaten.blade.php @@ -0,0 +1,27 @@ + +
+
+
+ @foreach ($this->gruppen() as $wert => $label) + + @endforeach +
+ Fachliche Säulen @include('filament.verwaltung.icon', ['name' => 'extern']) +
+

+ @if ($this->gruppe === 'materialart') + Fachliche Materialart eines Mediums, Moduls, Programms oder Lizenzartikels – unabhängig von der technischen Medienart. „Zulässig für“ bestimmt, wo sie neu gewählt werden kann. + @else + Einordnung für Suche und Filter. Sie gibt nichts frei, erteilt keine Rechte und ist keine tatsächliche Verwendung. + @endif + Fachliche Säulen werden in der Säulenverwaltung gepflegt. +

+
+
+ {{ $this->table }} +
+
diff --git a/resources/views/filament/verwaltung/einordnung.blade.php b/resources/views/filament/verwaltung/einordnung.blade.php index bdcdc0b..5f00592 100644 --- a/resources/views/filament/verwaltung/einordnung.blade.php +++ b/resources/views/filament/verwaltung/einordnung.blade.php @@ -8,7 +8,7 @@ Steuert Suche und Filter. Sie gibt kein Medium frei, erteilt keine Rechte und ist keine tatsächliche Verwendung. @if ($art) -