Files
laraundfelix/app/Filament/Support/ListsCompositions.php
T
LarsandClaude Sonnet 5.5 9140e731f9
Deploy Test / test (push) Successful in 2m43s
Deploy Test / deploy (push) Successful in 26s
Behandle Übungen aus Shinkan wie jede Übung, ordne die Navigation neu
Importierte Übungen werden im selben Editor bearbeitet und als neue Fassung gespeichert. Lokal bearbeitete Texte und
Felder bleiben bei der nächsten Übernahme erhalten, bei Änderung auf beiden Seiten entscheidet die Redaktion. Ein
Fehler im Editor setzte die Herkunft der Varianten auf lokal und ist behoben. Übungen haben einen Einstieg unter
Inhalte, Fachliche Säulen und Redaktionelle Stammdaten stehen in der Gruppe Stammdaten, die Übernahme ist ein Weg im
Übungsbestand.

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

360 lines
14 KiB
PHP

<?php
namespace App\Filament\Support;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\CompositionMember;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\EditorialVocabulary;
use App\Domain\Catalog\ExerciseSource;
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::Exercise => ['Übung', 'Übungen'],
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 = [];
$shinkan = $kinds === [CompositionKind::Exercise] || in_array(CompositionKind::Exercise, $kinds, true)
? ExerciseSource::query()->whereNotNull('composition_id')->pluck('composition_id')->all()
: [];
$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 : '').($kind === CompositionKind::Exercise ? ' · Herkunft: '.$this->exerciseOrigin($composition, $shinkan) : ''),
'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;
}
/**
* Herkunft einer Übung: lokal angelegt, aus Shinkan übernommen oder aus Shinkan übernommen und lokal ergänzt.
*
* @param list<int> $shinkan Übungen mit Quellbindung
*/
private function exerciseOrigin(Composition $composition, array $shinkan): string
{
if (! in_array($composition->id, $shinkan, true)) {
return 'lokal';
}
$latest = $composition->versions->sortByDesc('number')->first();
$local = $latest !== null && CompositionMember::query()->where('composition_version_id', $latest->id)->where('origin', 'lokal')->exists();
return $local ? 'Shinkan, lokal bearbeitet' : 'Shinkan';
}
/**
* Anlegedialog für Module und Programme; derselbe auf dem Arbeitsplatz und in den Listen.
*/
protected function createCompositionAction(CompositionKind $kind): Action
{
$label = match ($kind) {
CompositionKind::Kurs => 'Programm',
CompositionKind::Exercise => 'Übung',
default => 'Modul',
};
return Action::make(match ($kind) {
CompositionKind::Kurs => 'createProgram',
CompositionKind::Exercise => 'createExercise',
default => 'createModule',
})
->label($label.' anlegen')
->modalHeading(($kind === CompositionKind::Exercise ? 'Neue ' : '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,
};
}
}