Files
laraundfelix/app/Filament/Support/ListsCompositions.php
T
LarsandClaude Sonnet 5.5 7c48a6480c
CI Feature / test (push) Successful in 1m34s
Deploy Test / test (push) Successful in 1m29s
Deploy Test / deploy (push) Successful in 29s
Vereinheitliche die fachlichen Säulen auf die zentrale Säulenverwaltung
Module, Programme und Lizenzartikel verwenden wie Medien und Seiten die Tabelle
pillars; die Kopie „Fachliche Säule“ in den redaktionellen Dimensionen wird nirgends
mehr angeboten, geändert oder gefiltert und bleibt nur als Verlauf erhalten.

- Migration (nur Ergänzungen): composition_pillar und license_article_pillar mit
  restrictOnDelete, pillar_id an den alten Dimensionswerten als dokumentierte
  Abbildung, einmalige wiederholbare Übernahme (LegacyPillarAdoption).
- Übernahme nur über die gespeicherte Kennung pfeiler-<Nummer> und nur direkte
  Zuordnungen; mehrdeutige, abgeleitete und verwaiste Fälle werden gemeldet, nicht
  geraten. Medien bleiben bei ihrer Zuordnung. Alte Zeilen bleiben unverändert.
  Befehl: php artisan luf:saeulen-uebernehmen [--dry-run].
- Editoren von Modul, Programm und Lizenzartikel wählen Säulen aus der Säulenverwaltung;
  PillarService::assign gilt für alle Objektarten, Löschschutz zählt alle Objektarten.
- Filter: Module und Programme, Aufbau-Picker und Mitgliederbibliothek nach der
  Säulenzuordnung; alte Adressen mit einem Säulenwert der Einordnung folgen der Säule.
- Abgeleitete Säulen (aus enthaltenen Medien) bleiben getrennt von direkten.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 10:11:42 +01:00

328 lines
12 KiB
PHP

<?php
namespace App\Filament\Support;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\EditorialVocabulary;
use App\Domain\Catalog\IdentityDimensionValue;
use App\Domain\Catalog\Pillar;
use App\Domain\Catalog\PillarFilterCompat;
use App\Filament\Resources\ContentElements\ContentElementResource;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Url;
/**
* Eine Liste für Programme und Module (Arbeitsplatz, „Module“, „Programme“): dieselben Zeilen, Filter und derselbe
* Anlegedialog. Seiten mit festem Typ setzen fixedKind(); der Arbeitsplatz zeigt beide Arten mit Umschalter.
*/
trait ListsCompositions
{
use ShowsBestand;
#[Url]
public string $query = '';
#[Url]
public string $kindFilter = 'alle';
#[Url]
public string $pillar = '';
/** Fachliche Säule (zentrale Säulenverwaltung), direkt zugeordnet. */
#[Url(as: 'saeule', except: '')]
public string $saeule = '';
#[Url]
public string $visibility = 'alle';
protected function fixedKind(): ?CompositionKind
{
return null;
}
protected function bestandFilterEigenschaften(): array
{
return ['query', 'kindFilter', 'pillar', 'saeule', 'visibility'];
}
public function resetFilters(): void
{
$this->query = '';
$this->kindFilter = 'alle';
$this->pillar = '';
$this->saeule = '';
$this->visibility = 'alle';
$this->bestandGrenze = Bestand::SCHRITT;
}
public function hasActiveFilters(): bool
{
return trim($this->query) !== '' || $this->kindFilter !== 'alle' || $this->pillar !== '' || $this->saeule !== '' || $this->visibility !== 'alle';
}
/**
* @return array<string, mixed>
*/
public function bestand(): array
{
$this->normalizeLegacyPillarFilter();
$fixed = $this->fixedKind();
$einheit = match ($fixed) {
CompositionKind::Kurs => ['Programm', 'Programme'],
CompositionKind::Uebung => ['Modul', 'Module'],
CompositionKind::Kombination => ['Fachliche Kombination', 'Fachliche Kombinationen'],
default => ['Programm oder Modul', 'Programme und Module'],
};
$gruppen = [];
foreach ($this->dimensionFilters() as $gruppe) {
$optionen = [];
foreach ($gruppe['values'] as $value) {
$optionen[(string) $value['id']] = $value['label'];
}
$gruppen[] = ['label' => $gruppe['label'], 'optionen' => $optionen];
}
return [
'suche' => ['eigenschaft' => 'query', 'label' => 'Suche', 'platzhalter' => 'Titel oder Kurzbeschreibung'],
'segmente' => $fixed === null ? [[
'eigenschaft' => 'kindFilter', 'label' => 'Art', 'vorgabe' => 'alle',
'optionen' => ['alle' => 'Alle', 'programm' => 'Programme', 'modul' => 'Module'],
]] : [],
'filter' => [[
'eigenschaft' => 'visibility', 'label' => 'Stand', 'vorgabe' => 'alle',
'optionen' => ['alle' => 'Jeder Stand', 'bibliothek' => 'In der Mitgliederbibliothek', 'verborgen' => 'Nicht in der Mitgliederbibliothek', 'entwurf' => 'Noch ohne Fassung'],
]],
'weitere' => [
...($this->pillarOptions() === [] ? [] : [[
'eigenschaft' => 'saeule', 'label' => 'Fachliche Säule', 'vorgabe' => '', 'alle' => 'Alle Säulen', 'optionen' => $this->pillarOptions(),
]]),
...($gruppen === [] ? [] : [[
'eigenschaft' => 'pillar', 'label' => 'Fachliche Einordnung', 'vorgabe' => '', 'alle' => 'Alle Einordnungen', 'gruppen' => $gruppen,
]]),
],
'zeilen' => $this->entries(),
'einheit' => $einheit,
'leer' => [
'titel' => $fixed === CompositionKind::Kombination ? 'Noch keine fachlichen Kombinationen' : 'Noch keine '.$einheit[1],
'text' => 'Mit „'.match ($fixed) {
CompositionKind::Kurs => 'Programm anlegen',
CompositionKind::Kombination => CompositionKind::Kombination->createLabel(),
default => 'Modul anlegen',
}.'“ oben entsteht der erste Eintrag. Der Name genügt.',
],
];
}
/**
* @return list<array{label: string, values: list<array{id: int, label: string}>}>
*/
public function dimensionFilters(): array
{
return app(EditorialVocabulary::class)->dimensionFilterGroups(ctype_digit($this->pillar) ? [(int) $this->pillar] : []);
}
/**
* Zeilen der gemeinsamen Bestandsliste.
*
* @return list<array<string, mixed>>
*/
public function entries(): array
{
$needle = mb_strtolower(trim($this->query));
$catalog = app(EditorialCatalog::class);
$offered = $catalog->offeredCompositionIds();
$this->normalizeLegacyPillarFilter();
$pillarIds = $this->pillar === '' ? null : $this->pillarSubjects((int) $this->pillar);
$saeuleIds = ctype_digit($this->saeule) && $this->saeule !== '' ? $this->saeuleSubjects((int) $this->saeule) : null;
$fixed = $this->fixedKind();
$kinds = $fixed !== null ? [$fixed] : match ($this->kindFilter) {
'programm' => [CompositionKind::Kurs],
'modul' => [CompositionKind::Uebung],
default => [CompositionKind::Kurs, CompositionKind::Uebung],
};
$canEdit = ContentElementResource::editor();
$rows = [];
$compositions = Composition::query()
->whereIn('kind', $kinds)
->with(['editorialContentKind', 'versions.members.contentElementVersion.element'])
->orderBy('designation')
->get();
foreach ($compositions as $composition) {
$kind = $composition->kind instanceof CompositionKind
? $composition->kind
: CompositionKind::from((string) $composition->kind);
if ($pillarIds !== null && ! in_array($composition->id, $pillarIds, true)) {
continue;
}
if ($saeuleIds !== null && ! in_array($composition->id, $saeuleIds, true)) {
continue;
}
$stand = $catalog->stand($composition);
if (! $this->matchesVisibility($stand)) {
continue;
}
$title = $catalog->compositionTitle($composition);
$summary = trim((string) ($composition->summary ?? ''));
$haystack = mb_strtolower($title.' '.$composition->designation.' '.$summary.' '.(string) $composition->description);
if ($needle !== '' && ! str_contains($haystack, $needle)) {
continue;
}
$cover = $catalog->cover($composition, $this->coverMembers($composition));
$url = EditorialCatalog::editUrl($composition);
$rows[] = [
'schluessel' => 'c'.$composition->id,
'titel' => $title,
'meta' => $kind->deskLabel().($title !== $composition->designation ? ' · '.$composition->designation : ''),
'text' => $summary,
'bild' => $cover['url'],
'bildFehlt' => 'Kein Titelbild',
'hinweise' => filled($cover['note']) ? ['Bild: '.$cover['note']] : [],
'symbol' => 'modul',
'url' => $url,
'chips' => array_map(fn (array $chip): array => [
'label' => $chip['label'],
'ton' => match ($chip['tone']) {
'pine' => 'gut',
'yellow' => 'offen',
'quiet' => 'leise',
default => 'neutral',
},
], $catalog->statusChips($composition, in_array($composition->id, $offered, true))),
'aktionen' => array_values(array_filter([
['label' => 'Aufbau', 'href' => $url, 'icon' => 'modul'],
$canEdit ? ['label' => 'Redaktion', 'href' => $url.'?redaktion=1', 'icon' => 'stift'] : null,
])),
];
}
return $rows;
}
/**
* Anlegedialog für Module und Programme; derselbe auf dem Arbeitsplatz und in den Listen.
*/
protected function createCompositionAction(CompositionKind $kind): Action
{
$label = $kind === CompositionKind::Kurs ? 'Programm' : 'Modul';
return Action::make($kind === CompositionKind::Kurs ? 'createProgram' : 'createModule')
->label($label.' anlegen')
->modalHeading('Neues '.$label)
->modalDescription('Der Name genügt. Danach öffnet sich der Aufbau; Texte, Bilder und Einordnung folgen dort und in der Redaktion.')
->modalSubmitActionLabel($label.' anlegen')
->modalWidth('lg')
->visible(fn (): bool => ContentElementResource::editor())
->schema([
TextInput::make('designation')->label('Name')->required()->maxLength(255),
])
->action(function (array $data) use ($kind): void {
abort_unless(ContentElementResource::editor(), 403);
$composition = app(EditorialCatalog::class)->create(trim((string) $data['designation']), $kind);
$this->redirect(EditorialCatalog::editUrl($composition), navigate: false);
});
}
/**
* @return list<array{target: string}>
*/
private function coverMembers(Composition $composition): array
{
$draft = $composition->working_draft;
if (is_array($draft) && is_array($draft['members'] ?? null) && $draft['members'] !== []) {
return $draft['members'];
}
$version = $composition->versions
->filter(fn ($item): bool => $item->status === ContentVersionStatus::Bereit)
->sortByDesc('number')
->first();
if ($version === null) {
return [];
}
$members = [];
foreach ($version->members as $member) {
$members[] = ['target' => $member->content_element_version_id
? 'element:'.$member->content_element_version_id
: 'composition:'.$member->nested_composition_version_id];
}
return $members;
}
/**
* Alte Adressen mit einem Säulenwert der Einordnung (`?pillar=…`) gelten für die zentrale Säule.
*/
private function normalizeLegacyPillarFilter(): void
{
if (ctype_digit($this->pillar) && $this->pillar !== '' && $this->saeule === '' && is_int($pillar = PillarFilterCompat::pillarOfLegacyValue((int) $this->pillar))) {
[$this->saeule, $this->pillar] = [(string) $pillar, ''];
}
}
/**
* @return array<string, string>
*/
private function pillarOptions(): array
{
return Pillar::query()->orderBy('sort_order')->orderBy('name')->pluck('name', 'id')
->mapWithKeys(fn (string $name, int|string $id): array => [(string) $id => $name])->all();
}
/**
* Direkt zugeordnete Module und Programme einer Säule. Die Säulen der enthaltenen Medien zählen hier nicht.
*
* @return list<int>
*/
private function saeuleSubjects(int $pillarId): array
{
return DB::table('composition_pillar')->where('pillar_id', $pillarId)->pluck('composition_id')
->map(fn (mixed $id): int => (int) $id)->all();
}
/**
* @return list<int>
*/
private function pillarSubjects(int $valueId): array
{
return IdentityDimensionValue::query()
->where('subject_type', (new Composition)->getMorphClass())
->where('editorial_dimension_value_id', $valueId)
->pluck('subject_id')
->map(fn (mixed $id): int => (int) $id)
->all();
}
/**
* @param array{text: string, number: int|null, released_number: int|null} $stand
*/
private function matchesVisibility(array $stand): bool
{
return match ($this->visibility) {
'bibliothek' => $stand['released_number'] !== null,
'verborgen' => $stand['number'] !== null && $stand['released_number'] === null,
'entwurf' => $stand['number'] === null,
default => true,
};
}
}