Files
laraundfelix/app/Filament/Support/ListsCompositions.php
T
LarsandClaude Opus 5.5 71e71b4ef2
CI Feature / test (push) Successful in 59s
Deploy Test / test (push) Successful in 56s
Deploy Test / deploy (push) Successful in 23s
Übernimm verdeckte Angebotsbedingungen und halte die Listenlänge beim Anlegen.
- „Geänderte Fassung aktivieren“ übernimmt Inhaberart, Laufzeitregel,
  Steuerklasse und „dynamisch“ aus der aktiven Fassung. Bisher wurden sie still
  auf Person/dauerhaft zurückgesetzt, obwohl die Maske „vorbelegt“ sagte. Ohne
  aktive Fassung gilt wie bisher die Vorgabe.
- Nur Such- und Filteränderungen lassen die Liste wieder oben beginnen; Eingaben
  in einem geöffneten Anlegedialog klappen „Weitere anzeigen“ nicht mehr zu.
- Test: Kombinationen öffnen Aufbau und Redaktion aus der Liste.

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

292 lines
11 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\EditorialDimension;
use App\Domain\Catalog\IdentityDimensionValue;
use App\Filament\Resources\ContentElements\ContentElementResource;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
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 = '';
#[Url]
public string $visibility = 'alle';
protected function fixedKind(): ?CompositionKind
{
return null;
}
protected function bestandFilterEigenschaften(): array
{
return ['query', 'kindFilter', 'pillar', 'visibility'];
}
public function resetFilters(): void
{
$this->query = '';
$this->kindFilter = 'alle';
$this->pillar = '';
$this->visibility = 'alle';
$this->bestandGrenze = Bestand::SCHRITT;
}
public function hasActiveFilters(): bool
{
return trim($this->query) !== '' || $this->kindFilter !== 'alle' || $this->pillar !== '' || $this->visibility !== 'alle';
}
/**
* @return array<string, mixed>
*/
public function bestand(): array
{
$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' => $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
{
$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;
}
/**
* 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();
$pillarIds = $this->pillar === '' ? null : $this->pillarSubjects((int) $this->pillar);
$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;
}
$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;
}
/**
* @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,
};
}
}