columns(1)->components([ TextInput::make('designation')->label('Bezeichnung')->required(), TextInput::make('display_title')->label('Anzeigetitel') ->helperText('Erscheint in der Bibliothek. Leer lässt die Bezeichnung stehen.'), Textarea::make('summary')->label('Kurzbeschreibung')->rows(3), Select::make('kind')->label('Art')->options(CompositionKind::options())->required() ->helperText('Übung, Kurs und fachliche Kombination sind verschiedene Zusammenstellungen. Die Art lässt sich hier ändern.'), Select::make('editorial_content_kind_id') ->label('Inhaltsart') ->options(fn (): array => EditorialContentKind::selectOptions()) ->nullable() ->helperText('Optional für Bibliothek und Website. Ändert weder die technische Art noch eine Fassung.'), Placeholder::make('usage')->label('Verwendung')->content(function (): HtmlString { $version = $this->currentReadyVersion(); if ($version === null) { return new HtmlString('Noch keine Fassung.'); } $lines = CompositionMember::query() ->where('nested_composition_version_id', $version->id) ->with('compositionVersion.composition') ->get() ->map(fn (CompositionMember $member): string => ($member->compositionVersion->composition->designation ?? 'Programm').' · '.$member->role) ->all(); return new HtmlString($lines === [] ? 'In keiner anderen Fassung enthalten.' : e(implode(' · ', $lines))); }), Placeholder::make('history')->label('Aktuelle Fassung')->content(function (): HtmlString { /** @var Composition $composition */ $composition = $this->getRecord(); $lines = app(CatalogReading::class)->compositionMembers($composition); return new HtmlString(collect($lines)->map(fn (string $line): string => e($line))->implode('
')); }), Placeholder::make('pillars')->label('Säulen der Medien')->content(function (): HtmlString { /** @var Composition $composition */ $composition = $this->getRecord(); $lines = app(CatalogReading::class)->memberPillars($composition); return new HtmlString(collect($lines)->map(fn (string $line): string => e($line))->implode('
')); }), Repeater::make('members') ->label('Bausteine der neuen Fassung') ->schema([ TextInput::make('role')->label('Rolle')->placeholder('Film, Merkblatt, Übung')->required(), Select::make('target')->label('Fassung')->required()->searchable() ->options(fn (): array => CatalogOptions::assignmentTargets($this->getRecord()->getKey())), ]) ->minItems(1) ->reorderable() ->addActionLabel('Baustein hinzufügen') ->helperText('Eine Zeile entfernen nimmt den Baustein nur aus der neuen Fassung. Eine neuere Medienfassung gilt erst, wenn du sie hier auswählst.'), TextInput::make('note')->label('Notiz zur neuen Fassung'), ]); } protected function getHeaderActions(): array { return [ Action::make('displayRelease') ->label('Zur Anzeige freigeben') ->requiresConfirmation() ->visible(function (): bool { $version = $this->currentReadyVersion(); return $version !== null && ! app(CompositionDisplayRelease::class)->isReleased($version); }) ->action(function (): void { $version = $this->currentReadyVersion(); $actor = Filament::auth()->user(); abort_unless($version !== null && $actor instanceof User, 403); app(CompositionDisplayRelease::class)->release($version, $actor); Notification::make()->title('Die aktuelle Fassung ist zur Anzeige freigegeben.')->success()->send(); }), Action::make('displayRevoke') ->label('Anzeigefreigabe zurücknehmen') ->color('danger') ->requiresConfirmation() ->visible(function (): bool { $version = $this->currentReadyVersion(); return $version !== null && app(CompositionDisplayRelease::class)->isReleased($version); }) ->action(function (): void { $version = $this->currentReadyVersion(); $actor = Filament::auth()->user(); abort_unless($version !== null && $actor instanceof User, 403); app(CompositionDisplayRelease::class)->revoke($version, $actor); Notification::make()->title('Die Anzeigefreigabe dieser Fassung ist zurückgenommen.')->success()->send(); }), ]; } protected function getSaveFormAction(): Action { return parent::getSaveFormAction()->label('Neue Fassung veröffentlichen'); } protected function getSavedNotificationTitle(): ?string { return $this->savedTitle ?? 'Die neue Fassung ist veröffentlicht.'; } /** * @param array $data * @return array */ protected function mutateFormDataBeforeFill(array $data): array { /** @var Composition $composition */ $composition = $this->getRecord(); $version = $composition->versions() ->where('status', ContentVersionStatus::Bereit) ->orderByDesc('number') ->with('members') ->first(); $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[] = ['role' => $member->role, 'target' => $target]; } $kind = $composition->kind; return [ 'designation' => $composition->designation, 'display_title' => $composition->display_title, 'summary' => $composition->summary, 'kind' => $kind instanceof CompositionKind ? $kind->value : $kind, 'editorial_content_kind_id' => $composition->editorial_content_kind_id, 'members' => $members, 'note' => null, ]; } /** * @param array $data */ protected function handleRecordUpdate(Model $record, array $data): Model { /** @var Composition $record */ try { $kind = CompositionKind::from($data['kind']); $record->designation = trim((string) $data['designation']); app(IdentityProfile::class)->fill($record, [ 'display_title' => $data['display_title'] ?? null, 'summary' => $data['summary'] ?? null, ], auth()->user()); $record->kind = $kind; $record->editorial_content_kind_id = filled($data['editorial_content_kind_id'] ?? null) ? (int) $data['editorial_content_kind_id'] : null; $record->save(); $members = []; foreach ($data['members'] as $member) { if (! is_string($member['target'] ?? null) || ! str_contains($member['target'], ':')) { throw new DomainRuleException('Jeder Baustein braucht eine Fassung.'); } [$type, $id] = explode(':', $member['target'], 2); $target = $type === 'composition' ? CompositionVersion::query()->findOrFail($id) : ContentElementVersion::query()->findOrFail($id); $members[] = ['role' => $member['role'], 'target' => $target]; } $note = is_string($data['note'] ?? null) ? trim($data['note']) : ''; if ($note !== '' || ! $this->sameMembers($record, $members)) { $actor = Filament::auth()->user(); app(ContentService::class)->publishComposition( $record, $members, $actor instanceof User ? $actor : null, $note !== '' ? $note : null, ); $this->savedTitle = 'Die neue Fassung ist veröffentlicht. Die bisherige Fassung bleibt erhalten.'; } else { $this->savedTitle = 'Bezeichnung und Art sind gespeichert. Die Bausteine sind unverändert, deshalb gibt es keine neue Fassung.'; } } catch (DomainRuleException $exception) { Notification::make()->title($exception->getMessage())->danger()->send(); throw new Halt; } return $record; } protected function afterSave(): void { $this->getRecord()->unsetRelations(); $this->getRecord()->refresh(); $this->fillForm(); } protected function getRedirectUrl(): ?string { /** @var Composition $record */ $record = $this->getRecord(); $kind = $record->kind instanceof CompositionKind ? $record->kind : CompositionKind::from((string) $record->kind); $resource = match ($kind) { CompositionKind::Kurs => CourseResource::class, CompositionKind::Kombination => CombinationResource::class, CompositionKind::Uebung => ExerciseResource::class, }; if ($resource !== static::getResource()) { return $resource::getUrl('edit', ['record' => $record]); } return null; } /** * @param list $members */ private function currentReadyVersion(): ?CompositionVersion { /** @var Composition $composition */ $composition = $this->getRecord(); return $composition->versions() ->where('status', ContentVersionStatus::Bereit) ->orderByDesc('number') ->first(); } private function sameMembers(Composition $composition, array $members): bool { $version = $composition->versions() ->where('status', ContentVersionStatus::Bereit) ->orderByDesc('number') ->with('members') ->first(); if ($version === null) { return false; } $current = $version->members->map(function ($member): string { $target = $member->content_element_version_id ? 'element:'.$member->content_element_version_id : 'composition:'.$member->nested_composition_version_id; return $member->role.'|'.$target; })->all(); $next = array_values(array_map( fn (array $member): string => $member['role'].'|'.($member['target'] instanceof ContentElementVersion ? 'element:' : 'composition:').$member['target']->id, $members, )); return $current === $next; } }