> */ public array $members = []; /** @var list */ public array $dimensionIds = []; public function mount(int|string $record): void { parent::mount($record); $this->loadEditor(); $this->returnUrl = $this->safeReturn(request()->query('zurueck')); $adopt = request()->query('uebernehmen'); if (is_string($adopt)) { $this->addPart($adopt); } $flashed = session('redaktion'); if (is_string($flashed) && $flashed !== '' && $this->feedback === '') { $this->feedback = $flashed; } } public function getHeading(): string|Htmlable|null { $title = trim($this->displayTitle) !== '' ? trim($this->displayTitle) : trim($this->designation); return $title !== '' ? $title : parent::getHeading(); } public function getSubheading(): string|Htmlable|null { return $this->hostKind()->deskExplanation().' '.$this->statusSentence(); } public function getMaxContentWidth(): Width|string|null { return Width::Full; } protected function mutateFormDataBeforeFill(array $data): array { return []; } protected function getHeaderActions(): array { return [ Action::make('preview') ->label('Vorschau für Trainer') ->url(fn (): ?string => $this->previewUrl()) ->openUrlInNewTab() ->visible(fn (): bool => $this->previewUrl() !== null), Action::make('displayRelease') ->label('Zur Anzeige freigeben') ->requiresConfirmation() ->modalHeading('Diese Fassung zur Anzeige freigeben') ->modalDescription('Freigegeben wird die zuletzt übernommene Fassung. Eine frühere Fassung bleibt unverändert. Ungespeicherte Änderungen am Aufbau sind nicht Teil dieser Freigabe.') ->visible(function (): bool { $version = $this->currentReadyVersion(); return $version !== null && ! app(CompositionDisplayRelease::class)->isReleased($version); }) ->action(function (): void { $this->releaseDisplay(); }), Action::make('displayRevoke') ->label('Anzeige zurücknehmen') ->color('danger') ->requiresConfirmation() ->modalDescription('Nur die Anzeige dieser Fassung wird zurückgenommen. Der Inhalt der Fassung bleibt gespeichert.') ->visible(function (): bool { $version = $this->currentReadyVersion(); return $version !== null && app(CompositionDisplayRelease::class)->isReleased($version); }) ->action(function (): void { $version = $this->currentReadyVersion(); $actor = $this->actor(); abort_unless($version !== null && $actor instanceof User, 403); app(CompositionDisplayRelease::class)->revoke($version, $actor); $this->feedback = 'Die Anzeige dieser Fassung ist zurückgenommen. Der Inhalt bleibt gespeichert.'; }), ]; } public function hostKind(): CompositionKind { /** @var Composition $composition */ $composition = $this->getRecord(); $kind = $composition->kind; return $kind instanceof CompositionKind ? $kind : CompositionKind::from((string) $kind); } public function hostLabel(): string { return $this->hostKind()->deskLabel(); } public function isProgram(): bool { return $this->hostKind() === CompositionKind::Kurs; } public function selfUrl(): string { return $this->safeReturn(static::getResource()::getUrl('edit', ['record' => $this->getRecord()])); } public function previewUrl(): ?string { $version = $this->currentReadyVersion(); if ($version === null) { return null; } return route('editorial.preview', ['composition' => $this->getRecord(), 'version' => $version]); } public function previewUrlFor(int $versionId): ?string { $version = $this->ownedVersion($versionId); if ($version === null || $version->status !== ContentVersionStatus::Bereit) { return null; } return route('editorial.preview', ['composition' => $this->getRecord(), 'version' => $version]); } /** * @return list> */ public function hits(): array { if (trim($this->search) === '' && ! $this->listCatalog) { return []; } return app(EditorialCatalog::class)->search( $this->search, $this->catalogScope, (int) $this->getRecord()->getKey(), ); } /** * @return list */ public function versionChoices(): array { /** @var Composition $composition */ $composition = $this->getRecord(); $release = app(CompositionDisplayRelease::class); $latest = $this->currentReadyVersion(); $rows = []; foreach ($composition->versions()->orderByDesc('number')->get() as $version) { $released = $version->status === ContentVersionStatus::Bereit && $release->isReleased($version); $rows[] = [ 'id' => $version->id, 'number' => $version->number, 'text' => 'Fassung '.$version->number .($version->status === ContentVersionStatus::Bereit ? '' : ', nicht bereit') .($released ? ', zur Anzeige freigegeben' : ''), 'current' => $latest !== null && $latest->id === $version->id, ]; } return $rows; } /** * @return list */ public function usageLines(): array { $version = $this->currentReadyVersion(); if ($version === null) { return []; } return app(EditorialCatalog::class)->usage($version); } /** * @return list}> */ public function dimensionGroups(): 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; } /** * @return list */ public function selectedAlternatives(): array { $member = $this->selectedMember(); if ($member === null) { return []; } return app(EditorialCatalog::class)->alternatives( (string) ($member['identity'] ?? ''), (int) $this->getRecord()->getKey(), ); } public function childrenOf(string $target): string { if (preg_match('/^composition:(\d+)$/', $target, $match) !== 1) { return ''; } $version = CompositionVersion::query()->find($match[1]); if (! $version instanceof CompositionVersion) { return ''; } $lines = []; $this->childLines(app(CompositionStructureReader::class)->tree($version), $lines); return implode(' · ', $lines); } /** * @param list $nodes * @param list $lines */ protected function childLines(array $nodes, array &$lines): void { foreach ($nodes as $node) { $lines[] = $node->role.': '.$node->title; $this->childLines($node->children, $lines); } } public function publishHint(): string { if ($this->members === []) { return 'Eine Fassung braucht mindestens einen Bestandteil. Anzeige und Kurzbeschreibung kannst du schon speichern. Ein Film ist dafür nicht nötig.'; } if (! $this->needsRevision()) { return 'Lernziel, Zeit, Material und Aufbau sind unverändert. Es entsteht keine neue Fassung.'; } return 'Übernehmen legt eine neue Fassung an. Die bisherige Fassung bleibt und wird nicht überschrieben.'; } public function selectPart(int $index): void { if (! isset($this->members[$index])) { return; } $this->commitRoleDraft(); $this->selected = $index; $this->panel = 'bestandteil'; $this->syncRoleDraft(); } public function addPart(string $target): void { if (preg_match('/^(element|composition):\d+$/', $target) !== 1) { $this->feedback = 'Dieser Bestandteil ist nicht verfügbar.'; return; } foreach ($this->members as $index => $member) { if (($member['target'] ?? '') === $target) { $this->selected = $index; $this->panel = 'bestandteil'; $this->feedback = 'Diese Fassung ist schon enthalten.'; return; } } $row = app(EditorialCatalog::class)->rowForTarget($target, (int) $this->getRecord()->getKey()); if ($row === null) { $this->feedback = 'Diese Fassung ist nicht bereit oder gehört zu diesem Eintrag.'; return; } $row['role'] = app(EditorialCatalog::class)->defaultRole((string) $row['kind']); $this->members[] = $row; $this->members = array_values($this->members); $this->selected = array_key_last($this->members); $this->panel = 'bestandteil'; $this->syncRoleDraft(); $this->search = ''; $this->feedback = $row['title'].' ist in den Aufbau übernommen. Die neue Fassung entsteht erst mit „Neue Fassung übernehmen“.'; } public function removePart(int $index): void { if (! isset($this->members[$index])) { return; } unset($this->members[$index]); $this->members = array_values($this->members); $this->selected = null; $this->roleDraft = ''; $this->panel = 'angaben'; $this->feedback = 'Der Bestandteil ist aus dem Arbeitsstand genommen. Gespeichert ist das erst mit einer neuen Fassung.'; } public function movePart(int $index, int $direction): void { $this->commitRoleDraft(); $target = $index + $direction; if (! isset($this->members[$index], $this->members[$target])) { return; } $current = $this->members[$index]; $this->members[$index] = $this->members[$target]; $this->members[$target] = $current; $this->members = array_values($this->members); $this->selected = $target; $this->syncRoleDraft(); } public function useVersion(int $index, string $target): void { if (! isset($this->members[$index])) { return; } $row = app(EditorialCatalog::class)->rowForTarget($target, (int) $this->getRecord()->getKey(), allowSelf: true); if ($row === null || ($row['identity'] ?? '') !== ($this->members[$index]['identity'] ?? '')) { $this->feedback = 'Diese Fassung passt nicht zu dem ausgewählten Bestandteil.'; return; } $this->commitRoleDraft(); $row['role'] = $this->members[$index]['role'] ?? $row['kind']; $this->members[$index] = $row; $this->syncRoleDraft(); $this->feedback = $row['version_label'].' ist für diese Stelle ausgewählt. Übernehmen legt dafür eine neue Fassung an.'; } public function loadTemplate(int $versionId): void { $version = $this->ownedVersion($versionId); if ($version === null || $version->status !== ContentVersionStatus::Bereit) { $this->feedback = 'Diese Fassung kann nicht als Vorlage geladen werden.'; return; } $this->applyVersion($version); $this->templateNumber = $version->number; $this->feedback = 'Fassung '.$version->number.' ist als Arbeitsstand geladen. Übernehmen legt eine neue Fassung an und lässt die geladene Fassung unverändert.'; } public function addText(): void { $title = trim($this->newTextTitle); $body = trim($this->newTextBody); if ($title === '' || $body === '') { $this->feedback = 'Der Text braucht einen Titel und einen Inhalt.'; return; } try { $service = app(ContentService::class); $element = $service->createElement('text', 'text', $title); $version = $service->publishElementVersion($element, $body); } catch (DomainRuleException $exception) { $this->feedback = $exception->getMessage(); return; } $this->newTextTitle = ''; $this->newTextBody = ''; $this->addPart('element:'.$version->id); } public function startModule(): void { $name = trim($this->newModuleName); if ($name === '') { $this->feedback = 'Das neue Modul braucht einen Namen.'; return; } $composition = app(EditorialCatalog::class)->create($name, CompositionKind::Uebung); $this->newModuleName = ''; $url = EditorialCatalog::editUrl($composition).'?zurueck='.urlencode($this->selfUrl()); $this->redirect($url, navigate: false); } public function saveDisplay(): void { /** @var Composition $record */ $record = $this->getRecord(); $name = trim($this->designation); if ($name === '') { $this->feedback = 'Der Name fehlt.'; return; } $record->designation = $name; app(IdentityProfile::class)->fill($record, [ 'display_title' => $this->displayTitle, 'summary' => $this->summary, ], $this->actor()); $this->syncDimensions($record, array_map(intval(...), $this->dimensionIds)); $record->refresh(); $this->getRecord()->setRawAttributes($record->getAttributes()); $this->feedback = 'Die Anzeige ist gespeichert. Die Fassung bleibt unverändert.'; } public function savePreview(): void { if (! $this->previewUpload instanceof TemporaryUploadedFile) { $this->feedback = 'Wähle zuerst eine Bilddatei.'; return; } $extension = strtolower($this->previewUpload->getClientOriginalExtension()); if (! in_array($extension, ['png', 'jpg', 'jpeg', 'webp'], true)) { $this->feedback = 'Das Vorschaubild muss eine Bilddatei sein.'; return; } $path = $this->previewUpload->storeAs( 'vorschau', 'karte-'.bin2hex(random_bytes(4)).'.'.$extension, 'cms_incoming', ); $this->previewUpload = null; if (! is_string($path)) { $this->feedback = 'Das Vorschaubild wurde nicht übernommen.'; return; } $this->storePreview($path); } public function savePreviewImage(string $storedPath): void { $this->storePreview($storedPath); } public function publishRevision(): bool { $this->commitRoleDraft(); if ($this->members === []) { $this->feedback = 'Eine Fassung braucht mindestens einen Bestandteil. Anzeige und Kurzbeschreibung kannst du schon speichern.'; return false; } if (! $this->needsRevision()) { $this->feedback = 'Die Fassung ist unverändert.'; return false; } $minutes = $this->minutes($this->durationMinutes); if ($minutes === false) { $this->feedback = 'Der Zeitbedarf ist eine Zahl von Minuten oder bleibt leer.'; return false; } $payload = []; foreach (array_values($this->members) as $member) { $role = trim((string) ($member['role'] ?? '')); $target = (string) ($member['target'] ?? ''); if ($role === '' || preg_match('/^(element|composition):(\d+)$/', $target, $match) !== 1) { $this->feedback = 'Jeder Bestandteil braucht eine Rolle und eine vorhandene Fassung.'; return false; } $model = $match[1] === 'composition' ? CompositionVersion::query()->find($match[2]) : ContentElementVersion::query()->find($match[2]); if ($model === null) { $this->feedback = 'Ein Bestandteil ist nicht mehr vorhanden.'; return false; } $payload[] = ['role' => $role, 'target' => $model]; } /** @var Composition $record */ $record = $this->getRecord(); $note = trim($this->revisionNote); try { app(ContentService::class)->publishComposition( $record, $payload, $this->actor(), $note !== '' ? $note : null, [ 'learning_goal' => $this->blank($this->learningGoal), 'duration_minutes' => $minutes, 'material_note' => $this->blank($this->materialNote), ], ); } catch (DomainRuleException $exception) { $this->feedback = $exception->getMessage(); return false; } $this->revisionNote = ''; $this->templateNumber = null; $this->loadEditor(); $this->feedback = 'Die neue Fassung ist übernommen. Die bisherige Fassung bleibt erhalten.'; return true; } public function returnAndInclude(): void { if ($this->returnUrl === '') { return; } if ($this->needsRevision()) { if (! $this->publishRevision()) { return; } } $version = $this->currentReadyVersion(); $url = $this->returnUrl; if ($version === null) { session()->flash('redaktion', 'Es gibt noch keine Fassung, deshalb wurde nichts eingebunden.'); $this->redirect($url, navigate: false); return; } $join = str_contains($url, '?') ? '&' : '?'; $this->redirect($url.$join.'uebernehmen=composition:'.$version->id, navigate: false); } /** * @return array|null */ public function selectedMember(): ?array { if ($this->selected === null) { return null; } return $this->members[$this->selected] ?? null; } protected function loadEditor(): void { /** @var Composition $composition */ $composition = $this->getRecord(); $composition->refresh(); $version = $this->currentReadyVersion(); $this->designation = $composition->designation; $this->displayTitle = (string) ($composition->display_title ?? ''); $this->summary = (string) ($composition->summary ?? ''); $this->thumbnailPath = $composition->thumbnail_path; $this->thumbnailError = $composition->thumbnail_error; $this->dimensionIds = IdentityDimensionValue::query() ->where('subject_type', $composition->getMorphClass()) ->where('subject_id', $composition->id) ->where('origin', 'direkt') ->pluck('editorial_dimension_value_id') ->map(fn (mixed $id): string => (string) (int) $id) ->all(); $this->catalogScope = match ($this->hostKind()) { CompositionKind::Kurs => 'module', CompositionKind::Uebung => 'medien', default => 'alles', }; if ($version === null) { $this->members = []; $this->learningGoal = ''; $this->durationMinutes = ''; $this->materialNote = ''; return; } $this->applyVersion($version); } protected function applyVersion(CompositionVersion $version): void { $this->members = app(EditorialCatalog::class)->rows($version, (int) $this->getRecord()->getKey()); $this->learningGoal = (string) ($version->learning_goal ?? ''); $this->durationMinutes = $version->duration_minutes === null ? '' : (string) $version->duration_minutes; $this->materialNote = (string) ($version->material_note ?? ''); $this->selected = null; $this->roleDraft = ''; $this->panel = 'angaben'; } public function updatedRoleDraft(string $value): void { $this->commitRoleDraft($value); } protected function commitRoleDraft(?string $value = null): void { if ($this->selected === null || ! isset($this->members[$this->selected])) { return; } $this->members[$this->selected]['role'] = trim($value ?? $this->roleDraft); } protected function syncRoleDraft(): void { $this->roleDraft = $this->selected !== null && isset($this->members[$this->selected]) ? (string) ($this->members[$this->selected]['role'] ?? '') : ''; } public function releaseDisplay(): void { $this->releaseCurrent(); } protected function releaseCurrent(): void { $version = $this->currentReadyVersion(); $actor = $this->actor(); abort_unless($version !== null && $actor instanceof User, 403); if ($this->needsRevision()) { $this->feedback = 'Übernimm zuerst die Änderungen. Freigegeben wird nur eine bereits übernommene Fassung.'; return; } app(CompositionDisplayRelease::class)->release($version, $actor); $this->feedback = 'Fassung '.$version->number.' ist zur Anzeige freigegeben. Frühere Fassungen bleiben erhalten.'; } protected function currentReadyVersion(): ?CompositionVersion { /** @var Composition $composition */ $composition = $this->getRecord(); return $composition->versions() ->where('status', ContentVersionStatus::Bereit) ->orderByDesc('number') ->first(); } protected function ownedVersion(int $versionId): ?CompositionVersion { /** @var Composition $composition */ $composition = $this->getRecord(); return $composition->versions()->whereKey($versionId)->first(); } protected function needsRevision(): bool { $current = $this->currentReadyVersion(); if ($current === null) { return $this->members !== []; } return ! $this->sameMembers($current) || $this->didacticChanged($current); } protected function sameMembers(CompositionVersion $version): bool { $version->loadMissing('members'); $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 => trim((string) ($member['role'] ?? '')).'|'.($member['target'] ?? ''), $this->members, )); return $current === $next; } protected function didacticChanged(CompositionVersion $version): bool { $minutes = $this->minutes($this->durationMinutes); if ($minutes === false) { return true; } $storedMinutes = $version->duration_minutes === null ? null : (int) $version->duration_minutes; return $version->learning_goal !== $this->blank($this->learningGoal) || $version->material_note !== $this->blank($this->materialNote) || $storedMinutes !== $minutes; } protected function minutes(string $value): int|false|null { $value = trim($value); if ($value === '') { return null; } if (preg_match('/^\d+$/', $value) !== 1) { return false; } $minutes = (int) $value; return $minutes > 0 ? $minutes : false; } protected function blank(string $value): ?string { $value = trim($value); return $value === '' ? null : $value; } /** * @param list $ids */ protected function syncDimensions(Composition $composition, array $ids): void { IdentityDimensionValue::query() ->where('subject_type', $composition->getMorphClass()) ->where('subject_id', $composition->id) ->where('origin', 'direkt') ->whereNotIn('editorial_dimension_value_id', $ids === [] ? [0] : $ids) ->delete(); foreach (EditorialDimensionValue::query()->whereIn('id', $ids)->get() as $value) { app(IdentityDimension::class)->assign($composition, $value, 'direkt'); } } protected function storePreview(string $path): void { if (! str_starts_with($path, 'vorschau/') || str_contains($path, '..')) { $this->feedback = 'Das Vorschaubild wurde nicht übernommen.'; return; } /** @var Composition $record */ $record = $this->getRecord(); $before = $record->versions()->count(); DeriveThumbnail::dispatch('komposition', $record->id, $path); $record->refresh(); $this->thumbnailPath = $record->thumbnail_path; $this->thumbnailError = $record->thumbnail_error; $this->feedback = $record->versions()->count() === $before && filled($record->thumbnail_path) ? 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.' : ($record->thumbnail_error ?: 'Das Vorschaubild wurde nicht übernommen.'); } protected function statusSentence(): string { $version = $this->currentReadyVersion(); if ($version === null) { return 'Noch keine Fassung.'; } $released = app(CompositionDisplayRelease::class)->isReleased($version); return 'Zuletzt übernommen: Fassung '.$version->number.'. ' .($released ? 'Sie ist zur Anzeige freigegeben.' : 'Sie ist noch nicht zur Anzeige freigegeben.'); } protected function actor(): ?User { $actor = Filament::auth()->user(); return $actor instanceof User ? $actor : null; } protected function safeReturn(mixed $url): string { if (! is_string($url) || $url === '') { return ''; } $parts = parse_url($url); $path = is_array($parts) ? ($parts['path'] ?? '') : ''; if (! is_string($path) || ! str_starts_with($path, '/verwaltung/') || str_contains($path, '..')) { return ''; } $query = is_array($parts) ? ($parts['query'] ?? '') : ''; return $path.(is_string($query) && $query !== '' ? '?'.$query : ''); } /** * @param array $data */ protected function handleRecordUpdate(Model $record, array $data): Model { return $record; } }