Files
laraundfelix/app/Filament/Pages/EditorialDesk.php
T
LarsandCursor 9195cdcd15
Deploy Test / test (push) Successful in 23s
Deploy Test / deploy (push) Successful in 22s
Zeige Katalogkarten, Titelbilder und klare Freigabestatus im Arbeitsplatz.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-16 17:54:12 +01:00

251 lines
7.4 KiB
PHP

<?php
namespace App\Filament\Pages;
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 App\Filament\Support\EditorialCatalog;
use Filament\Pages\Page;
use Filament\Support\Enums\Width;
use Illuminate\Contracts\Support\Htmlable;
use Livewire\Attributes\Url;
class EditorialDesk extends Page
{
protected static string|\UnitEnum|null $navigationGroup = 'Inhalte';
protected static ?string $navigationLabel = 'Arbeitsplatz';
protected static ?int $navigationSort = 0;
protected static ?string $slug = 'arbeitsplatz';
protected static ?string $title = 'Arbeitsplatz';
protected string $view = 'filament.editorial.desk-page';
#[Url]
public string $query = '';
#[Url]
public string $kindFilter = 'alle';
#[Url]
public string $pillar = '';
#[Url]
public string $visibility = 'alle';
public string $programName = '';
public string $moduleName = '';
public string $feedback = '';
public function getHeading(): string|Htmlable|null
{
return 'Programme und Module';
}
public function getSubheading(): string|Htmlable|null
{
return 'Vorhandene Einträge öffnen oder einen neuen anlegen. Technische Kennungen sind dafür nicht nötig.';
}
public function getMaxContentWidth(): Width|string|null
{
return Width::Full;
}
public static function canAccess(): bool
{
return ContentElementResource::editor();
}
/**
* @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;
}
public function resetFilters(): void
{
$this->query = '';
$this->kindFilter = 'alle';
$this->pillar = '';
$this->visibility = 'alle';
}
public function hasActiveFilters(): bool
{
return trim($this->query) !== '' || $this->kindFilter !== 'alle' || $this->pillar !== '' || $this->visibility !== 'alle';
}
/**
* @return list<array<string, mixed>>
*/
public function entries(): array
{
$query = trim($this->query);
$needle = mb_strtolower($query);
$catalog = app(EditorialCatalog::class);
$offered = $catalog->offeredCompositionIds();
$pillarIds = $this->pillar === '' ? null : $this->pillarSubjects((int) $this->pillar);
$rows = [];
$compositions = Composition::query()
->whereIn('kind', [CompositionKind::Kurs, CompositionKind::Uebung])
->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 ($this->kindFilter === 'programm' && $kind !== CompositionKind::Kurs) {
continue;
}
if ($this->kindFilter === 'modul' && $kind !== CompositionKind::Uebung) {
continue;
}
if ($pillarIds !== null && ! in_array($composition->id, $pillarIds, true)) {
continue;
}
$stand = $catalog->stand($composition);
if (! $this->matchesVisibility($stand)) {
continue;
}
$title = $catalog->compositionTitle($composition);
$summary = (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));
$rows[] = [
'title' => $title,
'kind' => $kind->deskLabel(),
'tone' => $kind === CompositionKind::Kurs ? 'programm' : 'modul',
'summary' => $summary,
'url' => EditorialCatalog::editUrl($composition),
'image' => $cover['url'],
'image_note' => $cover['note'],
'chips' => $catalog->statusChips($composition, in_array($composition->id, $offered, true)),
];
}
return $rows;
}
/**
* @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) {
$target = $member->content_element_version_id
? 'element:'.$member->content_element_version_id
: 'composition:'.$member->nested_composition_version_id;
$members[] = ['target' => $target];
}
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,
};
}
public function createProgram(): void
{
$this->create(CompositionKind::Kurs, $this->programName);
}
public function createModule(): void
{
$this->create(CompositionKind::Uebung, $this->moduleName);
}
private function create(CompositionKind $kind, string $name): void
{
$name = trim($name);
if ($name === '') {
$this->feedback = 'Der Name fehlt.';
return;
}
$composition = app(EditorialCatalog::class)->create($name, $kind);
$this->redirect(EditorialCatalog::editUrl($composition), navigate: false);
}
}