diff --git a/app/Domain/Catalog/Composition.php b/app/Domain/Catalog/Composition.php index 25ef2d5..8a6f282 100644 --- a/app/Domain/Catalog/Composition.php +++ b/app/Domain/Catalog/Composition.php @@ -9,12 +9,14 @@ class Composition extends Model { protected $fillable = [ 'designation', + 'kind', 'archived_at', ]; protected function casts(): array { return [ + 'kind' => CompositionKind::class, 'archived_at' => 'datetime', ]; } diff --git a/app/Domain/Catalog/CompositionKind.php b/app/Domain/Catalog/CompositionKind.php new file mode 100644 index 0000000..3c9819c --- /dev/null +++ b/app/Domain/Catalog/CompositionKind.php @@ -0,0 +1,60 @@ + 'Fachliche Kombination', + self::Uebung => 'Übung', + self::Kurs => 'Kurs', + }; + } + + public function plural(): string + { + return match ($this) { + self::Kombination => 'Fachliche Kombinationen', + self::Uebung => 'Übungen', + self::Kurs => 'Kurse', + }; + } + + public function createLabel(): string + { + return match ($this) { + self::Kombination => 'Kombination anlegen', + self::Uebung => 'Übung anlegen', + self::Kurs => 'Kurs anlegen', + }; + } + + public function explanation(): string + { + return match ($this) { + self::Kombination => 'Didaktische oder thematische Zusammenstellung, zum Beispiel ein Materialsatz. Sie wird in Übungen und Kursen wiederverwendet und ist selbst kein Kurs.', + self::Uebung => 'Eine konkrete Lernsituation, zum Beispiel aus Film und Merkblatt. Dieselben Medien können in mehreren Übungen stecken.', + self::Kurs => 'Ein Ablauf für den Kursbetrieb aus Übungen, Kombinationen und Medien. Ein Kurs ist keine Übung.', + }; + } + + /** + * @return array + */ + public static function options(): array + { + $options = []; + + foreach (self::cases() as $case) { + $options[$case->value] = $case->label(); + } + + return $options; + } +} diff --git a/app/Domain/Catalog/ContentService.php b/app/Domain/Catalog/ContentService.php index 69f4a20..299fa25 100644 --- a/app/Domain/Catalog/ContentService.php +++ b/app/Domain/Catalog/ContentService.php @@ -118,10 +118,11 @@ public function publishComposition( }); } - public function createComposition(string $designation): Composition + public function createComposition(string $designation, CompositionKind $kind = CompositionKind::Uebung): Composition { return Composition::query()->create([ - 'designation' => $this->required($designation, 'Die Bezeichnung der Komposition fehlt.'), + 'designation' => $this->required($designation, 'Die Bezeichnung fehlt.'), + 'kind' => $kind, ]); } diff --git a/app/Filament/Resources/Combinations/CombinationResource.php b/app/Filament/Resources/Combinations/CombinationResource.php new file mode 100644 index 0000000..3225278 --- /dev/null +++ b/app/Filament/Resources/Combinations/CombinationResource.php @@ -0,0 +1,34 @@ + ListCombinations::route('/'), + 'edit' => EditCombination::route('/{record}'), + ]; + } +} diff --git a/app/Filament/Resources/Combinations/Pages/EditCombination.php b/app/Filament/Resources/Combinations/Pages/EditCombination.php new file mode 100644 index 0000000..f726b6f --- /dev/null +++ b/app/Filament/Resources/Combinations/Pages/EditCombination.php @@ -0,0 +1,11 @@ +columns([ + TextColumn::make('designation')->label('Bezeichnung')->searchable(), + TextColumn::make('versions_count')->counts('versions')->label('Fassungen'), + ]); + } + + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery()->where('kind', static::kind()); + } +} diff --git a/app/Filament/Resources/Compositions/CompositionResource.php b/app/Filament/Resources/Compositions/CompositionResource.php deleted file mode 100644 index 47ccf06..0000000 --- a/app/Filament/Resources/Compositions/CompositionResource.php +++ /dev/null @@ -1,79 +0,0 @@ -components([ - Section::make('Mitglieder')->description('Geordnete Fassungen, keine Kopie der Dateien.')->schema([ - TextEntry::make('members')->hiddenLabel()->listWithLineBreaks() - ->state(fn (Composition $record): array => app(CatalogReading::class)->compositionMembers($record)), - ]), - Section::make('Verwendung')->schema([ - TextEntry::make('usage')->hiddenLabel()->listWithLineBreaks() - ->state(fn (Composition $record): array => app(CatalogReading::class)->compositionUsage($record)), - ]), - ]); - } - - public static function form(Schema $schema): Schema - { - return $schema; - } - - public static function table(Table $table): Table - { - return $table->columns([ - TextColumn::make('designation')->label('Bezeichnung')->searchable(), - TextColumn::make('versions_count')->counts('versions')->label('Fassungen'), - ]); - } - - public static function getPages(): array - { - return [ - 'index' => ListCompositions::route('/'), - 'view' => ViewComposition::route('/{record}'), - ]; - } -} diff --git a/app/Filament/Resources/Compositions/Pages/EditComposition.php b/app/Filament/Resources/Compositions/Pages/EditComposition.php new file mode 100644 index 0000000..a237fa5 --- /dev/null +++ b/app/Filament/Resources/Compositions/Pages/EditComposition.php @@ -0,0 +1,214 @@ +columns(1)->components([ + TextInput::make('designation')->label('Bezeichnung')->required(), + 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.'), + 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('
')); + }), + 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 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, + 'kind' => $kind instanceof CompositionKind ? $kind->value : $kind, + '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']); + $record->kind = $kind; + $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 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; + } +} diff --git a/app/Filament/Resources/Compositions/Pages/ListCompositions.php b/app/Filament/Resources/Compositions/Pages/ListCompositionKind.php similarity index 54% rename from app/Filament/Resources/Compositions/Pages/ListCompositions.php rename to app/Filament/Resources/Compositions/Pages/ListCompositionKind.php index 9de540a..afc7d17 100644 --- a/app/Filament/Resources/Compositions/Pages/ListCompositions.php +++ b/app/Filament/Resources/Compositions/Pages/ListCompositionKind.php @@ -2,8 +2,8 @@ namespace App\Filament\Resources\Compositions\Pages; +use App\Domain\Catalog\CompositionKind; use App\Domain\Catalog\ContentService; -use App\Filament\Resources\Compositions\CompositionResource; use App\Filament\Resources\ContentElements\ContentElementResource; use App\Filament\Support\ReportsDomainRules; use Filament\Actions\Action; @@ -11,30 +11,35 @@ use Filament\Resources\Pages\ListRecords; use Illuminate\Contracts\Support\Htmlable; -class ListCompositions extends ListRecords +abstract class ListCompositionKind extends ListRecords { use ReportsDomainRules; - protected static string $resource = CompositionResource::class; + abstract protected function kind(): CompositionKind; public function getSubheading(): string|Htmlable|null { - return 'Hier entsteht der Kurs: fertige Fassungen in einer Reihenfolge, zum Beispiel Film und Merkblatt. Dieselbe Fassung kann in weiteren Kursen stecken. Verkauf und Rechte entstehen am Lizenzartikel, am Paket und am Angebot.'; + return $this->kind()->explanation(); } protected function getHeaderActions(): array { return [ Action::make('create') - ->label('Kurs anlegen') + ->label($this->kind()->createLabel()) ->visible(fn (): bool => ContentElementResource::editor()) ->schema([ TextInput::make('designation')->label('Bezeichnung')->required(), ]) ->action(function (array $data): void { - $this->report(function () use ($data): void { - app(ContentService::class)->createComposition($data['designation']); - }, 'Der Kurs ist angelegt.'); + $composition = null; + $this->report(function () use ($data, &$composition): void { + $composition = app(ContentService::class)->createComposition($data['designation'], $this->kind()); + }, $this->kind()->label().' ist angelegt.'); + + if ($composition !== null) { + $this->redirect(static::getResource()::getUrl('edit', ['record' => $composition])); + } }), ]; } diff --git a/app/Filament/Resources/Compositions/Pages/ViewComposition.php b/app/Filament/Resources/Compositions/Pages/ViewComposition.php deleted file mode 100644 index af8eaf5..0000000 --- a/app/Filament/Resources/Compositions/Pages/ViewComposition.php +++ /dev/null @@ -1,62 +0,0 @@ -label('Fassung veröffentlichen') - ->schema([ - Repeater::make('members')->label('Bausteine')->schema([ - TextInput::make('role')->label('Rolle im Kurs')->placeholder('Film, Merkblatt, Übung')->required(), - Select::make('target')->label('Bereitgestellte Fassung')->options(fn (): array => CatalogOptions::readyTargets())->required(), - ])->minItems(1)->helperText('Nur bereitgestellte Fassungen. Eine Datei, die noch übernommen wird, erscheint hier noch nicht.'), - TextInput::make('note')->label('Bestätigungsnotiz'), - ]) - ->action(function (array $data): void { - $this->report(function () use ($data): void { - /** @var Composition $composition */ - $composition = $this->getRecord(); - $members = []; - - foreach ($data['members'] as $member) { - [$type, $id] = explode(':', $member['target'], 2); - $target = $type === 'composition' - ? CompositionVersion::query()->findOrFail($id) - : ContentElementVersion::query()->findOrFail($id); - $members[] = ['role' => $member['role'], 'target' => $target]; - } - - app(ContentService::class)->publishComposition( - $composition, - $members, - auth()->user(), - $data['note'] ?? null, - ); - }, 'Die Kompositionsfassung ist bestätigt und bereit.'); - $this->getRecord()->unsetRelations(); - $this->getRecord()->refresh(); - }), - ]; - } -} diff --git a/app/Filament/Resources/ContentElements/ContentElementResource.php b/app/Filament/Resources/ContentElements/ContentElementResource.php index ea20482..c2d0c29 100644 --- a/app/Filament/Resources/ContentElements/ContentElementResource.php +++ b/app/Filament/Resources/ContentElements/ContentElementResource.php @@ -4,18 +4,16 @@ use App\Domain\Catalog\ContentElement; use App\Domain\Identity\StaffRole; +use App\Filament\Resources\ContentElements\Pages\EditContentElement; use App\Filament\Resources\ContentElements\Pages\ListContentElements; -use App\Filament\Resources\ContentElements\Pages\ViewContentElement; -use App\Filament\Support\CatalogReading; use App\Models\User; use Filament\Facades\Filament; -use Filament\Infolists\Components\TextEntry; use Filament\Resources\Resource; -use Filament\Schemas\Components\Section; use Filament\Schemas\Schema; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; use Illuminate\Auth\Access\Response; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class ContentElementResource extends Resource @@ -24,11 +22,11 @@ class ContentElementResource extends Resource protected static string|\UnitEnum|null $navigationGroup = 'Inhalte'; - protected static ?string $navigationLabel = 'Inhaltselemente'; + protected static ?string $navigationLabel = 'Medien'; - protected static ?string $modelLabel = 'Inhaltselement'; + protected static ?string $modelLabel = 'Medium'; - protected static ?string $pluralModelLabel = 'Inhaltselemente'; + protected static ?string $pluralModelLabel = 'Medien'; protected static ?string $recordTitleAttribute = 'working_title'; @@ -46,25 +44,11 @@ public static function editor(): bool return $user instanceof User && $user->hasStaffRole(StaffRole::Redakteur); } - public static function getViewAuthorizationResponse(Model $record): Response + public static function getEditAuthorizationResponse(Model $record): Response { return self::editor() ? Response::allow() : Response::deny(); } - public static function infolist(Schema $schema): Schema - { - return $schema->components([ - Section::make('Fassungen')->schema([ - TextEntry::make('versions')->hiddenLabel()->listWithLineBreaks() - ->state(fn (ContentElement $record): array => app(CatalogReading::class)->elementVersions($record)), - ]), - Section::make('Verwendung')->description('Kompositionen und Lizenzartikel, die auf dieses Material zeigen.')->schema([ - TextEntry::make('usage')->hiddenLabel()->listWithLineBreaks() - ->state(fn (ContentElement $record): array => app(CatalogReading::class)->elementUsage($record)), - ]), - ]); - } - public static function form(Schema $schema): Schema { return $schema; @@ -75,16 +59,30 @@ public static function table(Table $table): Table return $table->columns([ TextColumn::make('working_title')->label('Arbeitstitel')->searchable(), TextColumn::make('kind')->label('Art'), - TextColumn::make('output_class')->label('Ausgabeklasse'), - TextColumn::make('versions_count')->counts('versions')->label('Fassungen'), + TextColumn::make('stand')->label('Aktuelle Fassung')->state(function (ContentElement $record): string { + $version = $record->versions->sortByDesc('number')->first(); + + if ($version === null) { + return 'noch keine Fassung'; + } + + $file = is_array($version->metadata) ? ($version->metadata['file'] ?? null) : null; + + return 'Fassung '.$version->number.(is_string($file) && $file !== '' ? ' · mit Datei' : ' · ohne Datei'); + }), ]); } + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery()->with('versions'); + } + public static function getPages(): array { return [ 'index' => ListContentElements::route('/'), - 'view' => ViewContentElement::route('/{record}'), + 'edit' => EditContentElement::route('/{record}'), ]; } } diff --git a/app/Filament/Resources/ContentElements/Pages/EditContentElement.php b/app/Filament/Resources/ContentElements/Pages/EditContentElement.php new file mode 100644 index 0000000..b26d954 --- /dev/null +++ b/app/Filament/Resources/ContentElements/Pages/EditContentElement.php @@ -0,0 +1,234 @@ +columns(1)->components([ + TextInput::make('working_title')->label('Arbeitstitel')->required(), + Placeholder::make('history')->label('Bisherige Fassungen')->content(function (): HtmlString { + /** @var ContentElement $element */ + $element = $this->getRecord(); + $lines = app(CatalogReading::class)->elementVersions($element); + $html = collect($lines)->map(fn (string $line): string => e($line))->implode('
'); + $links = $element->versions()->orderByDesc('number')->get()->map(function (ContentElementVersion $version): string { + return 'Fassung '.$version->number.' ansehen'; + })->implode(' · '); + + return new HtmlString($html.($links !== '' ? '
'.$links : '')); + }), + Placeholder::make('usage')->label('Verwendung')->content(function (): HtmlString { + /** @var ContentElement $element */ + $element = $this->getRecord(); + $lines = app(CatalogReading::class)->elementUsage($element); + + return new HtmlString(collect($lines)->map(fn (string $line): string => e($line))->implode('
')); + }), + Section::make('Neue Fassung')->schema([ + Textarea::make('body')->label('Text')->rows(8), + self::uploadField(), + Toggle::make('drop_file') + ->label('Datei in der neuen Fassung nicht mehr verwenden') + ->visible(fn (): bool => is_string($this->latest()?->metadata['file'] ?? null)), + TextInput::make('decision_seconds')->label('Entscheidungsmarke in Sekunden')->numeric()->minValue(0), + TextInput::make('change_note')->label('Änderungsnotiz'), + ]), + ]); + } + + public static function uploadField(): FileUpload + { + return FileUpload::make('incoming') + ->label('Datei') + ->disk('cms_incoming') + ->directory('inhalte') + ->visibility('private') + ->maxSize(64 * 1024) + ->helperText('Film, Audio, Bild oder PDF bis 64 MB.'); + } + + protected function getHeaderActions(): array + { + return [ + Action::make('preview') + ->label('Aktuelle Fassung ansehen') + ->url(fn (): ?string => ($version = $this->latest()) === null ? null : route('content.preview', $version)) + ->visible(fn (): bool => $this->latest() !== null) + ->openUrlInNewTab(), + Action::make('block') + ->label('Fassung sperren') + ->visible(fn (): bool => $this->getRecord()->versions()->where('status', ContentVersionStatus::Bereit)->exists()) + ->schema([ + Select::make('version')->label('Fassung')->options(function (): array { + return $this->getRecord()->versions() + ->where('status', ContentVersionStatus::Bereit) + ->orderByDesc('number') + ->pluck('number', 'id') + ->map(fn ($number): string => 'Fassung '.$number) + ->all(); + })->required(), + Textarea::make('reason')->label('Begründung')->required(), + ]) + ->action(function (array $data): void { + $this->report(function () use ($data): void { + $version = ContentElementVersion::query()->findOrFail($data['version']); + app(ContentService::class)->block($version, $data['reason']); + }, 'Die Fassung ist gesperrt.'); + $this->getRecord()->unsetRelation('versions'); + $this->fillForm(); + }), + Action::make('delete') + ->label('Löschen') + ->color('danger') + ->requiresConfirmation() + ->visible(fn (): bool => $this->getRecord()->versions()->doesntExist()) + ->action(function (): void { + $this->getRecord()->delete(); + $this->redirect(ContentElementResource::getUrl('index')); + }), + ]; + } + + protected function getSaveFormAction(): Action + { + return parent::getSaveFormAction()->label('Neue Fassung anlegen'); + } + + protected function getSavedNotificationTitle(): ?string + { + return $this->savedTitle ?? 'Die neue Fassung ist angelegt.'; + } + + /** + * @param array $data + * @return array + */ + protected function mutateFormDataBeforeFill(array $data): array + { + $latest = $this->latest(); + $metadata = is_array($latest?->metadata) ? $latest->metadata : []; + + return [ + 'working_title' => $this->getRecord()->working_title, + 'body' => $latest?->body, + 'decision_seconds' => $metadata['decision_seconds'] ?? null, + 'drop_file' => false, + 'change_note' => null, + ]; + } + + /** + * @param array $data + */ + protected function handleRecordUpdate(Model $record, array $data): Model + { + /** @var ContentElement $record */ + try { + $record->working_title = trim((string) $data['working_title']); + $record->save(); + + $latest = $record->versions()->orderByDesc('number')->first(); + $metadata = []; + $incoming = IncomingFile::store($data['incoming'] ?? null); + $dropFile = (bool) ($data['drop_file'] ?? false); + $previousFile = is_array($latest?->metadata) ? ($latest->metadata['file'] ?? null) : null; + + if (is_string($incoming) && $incoming !== '') { + $metadata['incoming'] = $incoming; + } elseif (! $dropFile && is_string($previousFile) && $previousFile !== '') { + $metadata['file'] = $previousFile; + } + + $decision = is_numeric($data['decision_seconds'] ?? null) ? (int) $data['decision_seconds'] : null; + + if ($decision !== null) { + $metadata['decision_seconds'] = $decision; + } + + $body = is_string($data['body'] ?? null) ? $data['body'] : null; + $note = is_string($data['change_note'] ?? null) ? $data['change_note'] : null; + $previousDecision = is_array($latest?->metadata) && is_numeric($latest->metadata['decision_seconds'] ?? null) + ? (int) $latest->metadata['decision_seconds'] + : null; + $changed = $incoming !== null + || $dropFile + || trim((string) $body) !== trim((string) $latest?->body) + || $decision !== $previousDecision + || (is_string($note) && trim($note) !== ''); + + if (! $changed) { + $this->savedTitle = 'Der Arbeitstitel ist geändert. Text und Datei sind gleich geblieben, deshalb gibt es keine neue Fassung.'; + + return $record; + } + + if (trim((string) $body) === '' && $metadata === []) { + throw new DomainRuleException('Eine neue Fassung braucht einen Text oder eine Datei.'); + } + + app(ContentService::class)->publishElementVersion($record, $body, $metadata, $note); + $this->savedTitle = $incoming !== null + ? 'Die neue Fassung ist angelegt. Die Datei wird übernommen; die bisherige Fassung bleibt.' + : 'Die neue Fassung ist angelegt. Die bisherige Fassung bleibt erhalten.'; + } catch (DomainRuleException $exception) { + Notification::make()->title($exception->getMessage())->danger()->send(); + + throw new Halt; + } + + return $record; + } + + protected function afterSave(): void + { + $this->getRecord()->unsetRelation('versions'); + $this->getRecord()->refresh(); + $this->fillForm(); + } + + private function latest(): ?ContentElementVersion + { + /** @var ContentElement $element */ + $element = $this->getRecord(); + + return $element->versions()->orderByDesc('number')->first(); + } +} diff --git a/app/Filament/Resources/ContentElements/Pages/ListContentElements.php b/app/Filament/Resources/ContentElements/Pages/ListContentElements.php index 2729a52..15b5f5f 100644 --- a/app/Filament/Resources/ContentElements/Pages/ListContentElements.php +++ b/app/Filament/Resources/ContentElements/Pages/ListContentElements.php @@ -4,9 +4,11 @@ use App\Domain\Catalog\ContentService; use App\Filament\Resources\ContentElements\ContentElementResource; +use App\Filament\Support\IncomingFile; use App\Filament\Support\ReportsDomainRules; use Filament\Actions\Action; use Filament\Forms\Components\Select; +use Filament\Forms\Components\Textarea; use Filament\Forms\Components\TextInput; use Filament\Resources\Pages\ListRecords; use Illuminate\Contracts\Support\Htmlable; @@ -19,14 +21,14 @@ class ListContentElements extends ListRecords public function getSubheading(): string|Htmlable|null { - return 'Das Material selbst: Film, Text, PDF. Unter Kurse und Übungen werden daraus Abläufe, unter Pakete verkaufbare Zusammenstellungen.'; + return 'Die Dateien und Texte. Eine Übung, ein Kurs oder eine fachliche Kombination verweist darauf; die Datei wird nicht für jede Verwendung neu hochgeladen.'; } protected function getHeaderActions(): array { return [ Action::make('create') - ->label('Element anlegen') + ->label('Medium anlegen') ->visible(fn (): bool => ContentElementResource::editor()) ->schema([ TextInput::make('working_title')->label('Arbeitstitel')->required(), @@ -45,11 +47,37 @@ protected function getHeaderActions(): array 'pdf' => 'PDF', 'text' => 'Text', ])->required(), + Textarea::make('body')->label('Text'), + EditContentElement::uploadField(), + TextInput::make('decision_seconds')->label('Entscheidungsmarke in Sekunden')->numeric()->minValue(0), + TextInput::make('change_note')->label('Änderungsnotiz'), ]) ->action(function (array $data): void { - $this->report(function () use ($data): void { - app(ContentService::class)->createElement($data['kind'], $data['output_class'], $data['working_title']); - }, 'Das Inhaltselement ist angelegt.'); + $element = null; + $this->report(function () use ($data, &$element): void { + $service = app(ContentService::class); + $element = $service->createElement($data['kind'], $data['output_class'], $data['working_title']); + $metadata = []; + $incoming = IncomingFile::store($data['incoming'] ?? null); + + if (is_string($incoming) && $incoming !== '') { + $metadata['incoming'] = $incoming; + } + + if (is_numeric($data['decision_seconds'] ?? null)) { + $metadata['decision_seconds'] = (int) $data['decision_seconds']; + } + + $body = $data['body'] ?? null; + + if ((is_string($body) && $body !== '') || $metadata !== []) { + $service->publishElementVersion($element, is_string($body) ? $body : null, $metadata, $data['change_note'] ?? null); + } + }, 'Das Medium ist angelegt.'); + + if ($element !== null) { + $this->redirect(ContentElementResource::getUrl('edit', ['record' => $element])); + } }), ]; } diff --git a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php deleted file mode 100644 index ce6e2cf..0000000 --- a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php +++ /dev/null @@ -1,95 +0,0 @@ -label('Ansehen') - ->url(function (): ?string { - $version = $this->latestVersion(); - - return $version === null ? null : route('content.preview', $version); - }) - ->visible(fn (): bool => $this->latestVersion() !== null) - ->openUrlInNewTab(), - Action::make('version') - ->label('Fassung veröffentlichen') - ->schema([ - Textarea::make('body')->label('Text'), - FileUpload::make('incoming') - ->label('Datei') - ->disk('cms_incoming') - ->directory('inhalte') - ->helperText('Film, Audio oder PDF. Die Datei wird übernommen und ist danach unter Ansehen sichtbar.'), - TextInput::make('decision_seconds') - ->label('Entscheidungsmarke in Sekunden') - ->numeric() - ->minValue(0) - ->helperText('Nur für Filme: der Kursplayer pausiert an dieser Stelle.'), - TextInput::make('change_note')->label('Änderungsnotiz'), - ]) - ->action(function (array $data): void { - $this->report(function () use ($data): void { - /** @var ContentElement $element */ - $element = $this->getRecord(); - $metadata = []; - $incoming = $data['incoming'] ?? null; - - if (is_array($incoming)) { - $incoming = array_values(array_filter($incoming, 'is_string'))[0] ?? null; - } - - if (is_string($incoming) && $incoming !== '') { - $metadata['incoming'] = $incoming; - } - - if (is_numeric($data['decision_seconds'] ?? null)) { - $metadata['decision_seconds'] = (int) $data['decision_seconds']; - } - - app(ContentService::class)->publishElementVersion( - $element, - $data['body'] ?? null, - $metadata, - $data['change_note'] ?? null, - ); - }, 'Die Fassung ist gespeichert.'); - $this->getRecord()->unsetRelations(); - $this->getRecord()->refresh(); - }), - ]; - } - - private function latestVersion(): ?ContentElementVersion - { - /** @var ContentElement $element */ - $element = $this->getRecord(); - - return $element->versions()->orderByDesc('number')->first(); - } -} diff --git a/app/Filament/Resources/Courses/CourseResource.php b/app/Filament/Resources/Courses/CourseResource.php new file mode 100644 index 0000000..1a852b9 --- /dev/null +++ b/app/Filament/Resources/Courses/CourseResource.php @@ -0,0 +1,34 @@ + ListCourses::route('/'), + 'edit' => EditCourse::route('/{record}'), + ]; + } +} diff --git a/app/Filament/Resources/Courses/Pages/EditCourse.php b/app/Filament/Resources/Courses/Pages/EditCourse.php new file mode 100644 index 0000000..51e2165 --- /dev/null +++ b/app/Filament/Resources/Courses/Pages/EditCourse.php @@ -0,0 +1,11 @@ + ListExercises::route('/'), + 'edit' => EditExercise::route('/{record}'), + ]; + } +} diff --git a/app/Filament/Resources/Exercises/Pages/EditExercise.php b/app/Filament/Resources/Exercises/Pages/EditExercise.php new file mode 100644 index 0000000..c8ac861 --- /dev/null +++ b/app/Filament/Resources/Exercises/Pages/EditExercise.php @@ -0,0 +1,11 @@ +orderBy('working_title')->pluck('working_title', 'id')->all(); } + /** + * @return array + */ + public static function assignmentTargets(?int $exceptCompositionId = null): array + { + $options = []; + + foreach (ContentElementVersion::query()->where('status', ContentVersionStatus::Bereit)->with('element')->orderBy('id')->get() as $version) { + $options['element:'.$version->id] = 'Medium: '.$version->element->working_title.' · Fassung '.$version->number; + } + + foreach (CompositionVersion::query()->where('status', ContentVersionStatus::Bereit)->with('composition')->orderBy('id')->get() as $version) { + if ($exceptCompositionId !== null && (int) $version->composition_id === $exceptCompositionId) { + continue; + } + + $kind = $version->composition->kind; + $label = $kind instanceof CompositionKind ? $kind->label() : 'Zusammenstellung'; + $options['composition:'.$version->id] = $label.': '.$version->composition->designation.' · Fassung '.$version->number; + } + + return $options; + } + /** * @return array */ diff --git a/app/Filament/Support/CatalogReading.php b/app/Filament/Support/CatalogReading.php index 8d6c91e..adbcc8d 100644 --- a/app/Filament/Support/CatalogReading.php +++ b/app/Filament/Support/CatalogReading.php @@ -7,6 +7,7 @@ use App\Domain\Catalog\BundleStatus; use App\Domain\Catalog\BundleVersion; use App\Domain\Catalog\Composition; +use App\Domain\Catalog\CompositionKind; use App\Domain\Catalog\CompositionMember; use App\Domain\Catalog\ContentElement; use App\Domain\Catalog\ContentVersionStatus; @@ -263,7 +264,7 @@ public function elementUsage(ContentElement $element): array ->get(); foreach ($members as $member) { - $lines[] = 'Komposition '.$member->compositionVersion->composition->designation + $lines[] = self::compositionLabel($member->compositionVersion->composition) .' · Fassung '.$member->compositionVersion->number .' · Rolle '.$member->role; } @@ -310,7 +311,7 @@ public function compositionMembers(Composition $composition): array } $nested = $member->nestedCompositionVersion; - $lines[] = $member->role.' — Komposition '.($nested->composition->designation ?? 'fehlt'); + $lines[] = $member->role.' — '.($nested === null ? 'fehlt' : self::compositionLabel($nested->composition)); } return $lines; @@ -331,7 +332,7 @@ public function compositionUsage(Composition $composition): array ->get(); foreach ($parents as $member) { - $lines[] = 'Komposition '.$member->compositionVersion->composition->designation + $lines[] = self::compositionLabel($member->compositionVersion->composition) .' · Fassung '.$member->compositionVersion->number .' · Rolle '.$member->role; } @@ -414,6 +415,14 @@ private function purpose(string $purpose): string }; } + private static function compositionLabel(Composition $composition): string + { + $kind = $composition->kind; + $label = $kind instanceof CompositionKind ? $kind->label() : 'Zusammenstellung'; + + return $label.': '.$composition->designation; + } + private function status(mixed $status): string { return match (StatusValue::of($status)) { diff --git a/app/Filament/Support/IncomingFile.php b/app/Filament/Support/IncomingFile.php new file mode 100644 index 0000000..514a27f --- /dev/null +++ b/app/Filament/Support/IncomingFile.php @@ -0,0 +1,46 @@ +getClientOriginalExtension()); + $stem = preg_replace('/[^A-Za-z0-9_-]+/', '-', pathinfo($value->getClientOriginalName(), PATHINFO_FILENAME)) ?? ''; + $stem = trim($stem, '-'); + $stem = ($stem !== '' ? $stem : 'datei').'-'.bin2hex(random_bytes(4)); + + if (preg_match('/^[a-z0-9]{1,8}$/', $extension) !== 1) { + return null; + } + + $path = $value->storeAs('inhalte', $stem.'.'.$extension, 'cms_incoming'); + + return is_string($path) && $path !== '' ? $path : null; + } + + if (! is_string($value) || $value === '' || str_contains($value, '..') || ! str_starts_with($value, 'inhalte/')) { + return null; + } + + return Storage::disk('cms_incoming')->exists($value) ? $value : null; + } +} diff --git a/app/Http/Controllers/Member/LibraryController.php b/app/Http/Controllers/Member/LibraryController.php index 4c4a0b1..07d5c54 100644 --- a/app/Http/Controllers/Member/LibraryController.php +++ b/app/Http/Controllers/Member/LibraryController.php @@ -84,6 +84,7 @@ public function show(Request $request, LicenseArticle $article): View 'release' => $release, 'entries' => $entries, 'title' => app(CourseOutline::class)->title($article), + 'kind' => app(CourseOutline::class)->kindLabel($article), ]); } diff --git a/app/Http/CourseOutline.php b/app/Http/CourseOutline.php index fe656ee..5c7a3c0 100644 --- a/app/Http/CourseOutline.php +++ b/app/Http/CourseOutline.php @@ -2,6 +2,7 @@ namespace App\Http; +use App\Domain\Catalog\CompositionKind; use App\Domain\Catalog\LicenseArticle; class CourseOutline @@ -24,4 +25,18 @@ public function title(LicenseArticle $article): string ?? $release->classification_note ?? $article->code; } + + public function kindLabel(LicenseArticle $article): ?string + { + $release = $article->currentRelease; + + if ($release === null) { + return null; + } + + $release->loadMissing('rootCompositionVersion.composition'); + $kind = $release->rootCompositionVersion?->composition?->kind; + + return $kind instanceof CompositionKind ? $kind->label() : null; + } } diff --git a/database/migrations/2026_11_15_140000_add_kind_to_compositions.php b/database/migrations/2026_11_15_140000_add_kind_to_compositions.php new file mode 100644 index 0000000..9e5a52e --- /dev/null +++ b/database/migrations/2026_11_15_140000_add_kind_to_compositions.php @@ -0,0 +1,25 @@ +string('kind')->default('uebung'); + }); + + DB::table('compositions')->where('designation', 'like', '%Materialsatz%')->update(['kind' => 'kombination']); + } + + public function down(): void + { + Schema::table('compositions', function (Blueprint $table) { + $table->dropColumn('kind'); + }); + } +}; diff --git a/database/seeders/DemoCatalogSeeder.php b/database/seeders/DemoCatalogSeeder.php index cd65c1c..6fd393a 100644 --- a/database/seeders/DemoCatalogSeeder.php +++ b/database/seeders/DemoCatalogSeeder.php @@ -4,6 +4,7 @@ use App\Domain\Catalog\ArticleService; use App\Domain\Catalog\BundleService; +use App\Domain\Catalog\CompositionKind; use App\Domain\Catalog\ContentService; use App\Domain\Catalog\LicenseArticle; use App\Domain\Catalog\OfferService; @@ -32,7 +33,7 @@ public function run(): void ['decision_seconds' => 42], ); $inner = $content->publishComposition( - $content->createComposition('Materialsatz Alltag'), + $content->createComposition('Materialsatz Alltag', CompositionKind::Kombination), [['role' => 'Merkblatt', 'target' => $sheet]], ); $exercise = $content->publishComposition( diff --git a/docs/betrieb/MVP-Stand.md b/docs/betrieb/MVP-Stand.md index 8318aa8..4ef5e81 100644 --- a/docs/betrieb/MVP-Stand.md +++ b/docs/betrieb/MVP-Stand.md @@ -9,6 +9,7 @@ ## Bedienbar - Lizenzartikel, verschachtelte Pakete, Rechteprofile und Angebote als Katalogverwalter. Die jeweilige Seite zeigt den Stand, die Bestandteile, die aufgelösten Artikel und die daraus entstehenden Berechtigungen. Ein neuer Stand zeigt Inhaber, Angebote und Inhaltszahl; ein Rückzug stellt den vorherigen Stand wieder her und nennt die Betroffenen. - Manuelle Vergabe als Kundenbetreuung, am Benutzer in der Verwaltung. - Registrierung, Anmeldung, kostenloser Abruf, Testkauf, Ablehnung und Widerruf im Mitgliederbereich. Die Verwaltung fragt den Authenticator-Code auf demselben Browser 30 Tage lang nicht erneut ab. In der Authenticator-App heißt der Eintrag „Lara&Felix“, außerhalb der Produktion „Lara&Felix (dev)“. Ein bestehender Eintrag übernimmt den Namen erst bei erneuter Einrichtung. +- Medien werden in der Verwaltung angelegt, mit Datei oder Text versehen und als neue Fassung fortgeschrieben. Die bisherige Fassung bleibt. Übungen, Kurse und fachliche Kombinationen sind getrennte Zusammenstellungen; ihre Bausteine lassen sich hinzufügen, tauschen und aus der neuen Fassung nehmen. - Kurse im Mitgliederbereich zeigen den Namen der Übung, die Bausteine in Reihenfolge und einen Player. Ein Film mit Entscheidungsmarke lässt sich an dieser Stelle pausieren und von vorn starten. Die Verwaltung zeigt den Text einer Fassung und, sobald die Datei übernommen ist, die Datei selbst. - Bibliothek entscheidet je Abruf erneut. Filme mit Datei werden im Player geladen, Texte und PDF über einen kurz signierten Abruf. diff --git a/resources/views/member/article.blade.php b/resources/views/member/article.blade.php index c85bc60..5e2b65e 100644 --- a/resources/views/member/article.blade.php +++ b/resources/views/member/article.blade.php @@ -5,6 +5,9 @@ @section('content') @include('member.partials.nav')

{{ $title }}

+ @if ($kind) +

{{ $kind }}

+ @endif @if ($release?->classification_note)

{{ $release->classification_note }}

@endif diff --git a/resources/views/member/library.blade.php b/resources/views/member/library.blade.php index 4bb493b..606e7af 100644 --- a/resources/views/member/library.blade.php +++ b/resources/views/member/library.blade.php @@ -16,7 +16,7 @@
    @foreach ($grants as $grant)
  • - {{ $courses->title($grant->article) }} + {{ $courses->kindLabel($grant->article) ? $courses->kindLabel($grant->article).': ' : '' }}{{ $courses->title($grant->article) }} @if ($grant->article->currentRelease?->classification_note) {{ $grant->article->currentRelease->classification_note }} @endif diff --git a/tests/Feature/CatalogAdministrationTest.php b/tests/Feature/CatalogAdministrationTest.php index 9aba3a4..ef65981 100644 --- a/tests/Feature/CatalogAdministrationTest.php +++ b/tests/Feature/CatalogAdministrationTest.php @@ -96,9 +96,10 @@ public function test_catalog_pages_show_contents_packages_and_resolved_rights(): $sheet->load('element'); $this->actingAs($editor, 'staff') - ->get(ContentElementResource::getUrl('view', ['record' => $sheet->element])) + ->get(ContentElementResource::getUrl('edit', ['record' => $sheet->element])) ->assertOk() - ->assertSee('Komposition Übung Alltag') - ->assertSee('Lizenzartikel demo-merkblatt'); + ->assertSee('Übung: Übung Alltag') + ->assertSee('Lizenzartikel demo-merkblatt') + ->assertSee('Neue Fassung anlegen'); } } diff --git a/tests/Feature/MediaAdministrationTest.php b/tests/Feature/MediaAdministrationTest.php new file mode 100644 index 0000000..6be7c51 --- /dev/null +++ b/tests/Feature/MediaAdministrationTest.php @@ -0,0 +1,149 @@ +createElement('text', 'text', 'Merkblatt'); + $content->publishElementVersion($element, 'Erste Fassung'); + $editor = User::factory()->redakteur()->create(); + $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + $this->actingAs($editor, 'staff'); + + Livewire::test(EditContentElement::class, ['record' => $element->getKey()]) + ->assertOk() + ->assertSee('Erste Fassung') + ->fillForm([ + 'working_title' => 'Merkblatt', + 'body' => 'Zweite Fassung', + 'change_note' => 'Korrektur', + ]) + ->call('save') + ->assertHasNoFormErrors(); + + $versions = $element->versions()->orderBy('number')->get(); + + $this->assertCount(2, $versions); + $this->assertSame('Erste Fassung', $versions[0]->body); + $this->assertSame('Zweite Fassung', $versions[1]->body); + $this->assertSame(ContentVersionStatus::Bereit, $versions[0]->status); + } + + public function test_an_uploaded_file_becomes_a_ready_version(): void + { + Storage::fake('cms_incoming'); + Storage::fake('media_derived'); + Storage::disk('cms_incoming')->put('inhalte/film.mp4', 'filmbytes'); + $content = app(ContentService::class); + $element = $content->createElement('film', 'film', 'Übungsfilm'); + $editor = User::factory()->redakteur()->create(); + $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + $this->actingAs($editor, 'staff'); + + Livewire::test(EditContentElement::class, ['record' => $element->getKey()]) + ->fillForm([ + 'working_title' => 'Übungsfilm', + 'body' => 'Anleitung zum Film', + 'incoming' => ['inhalte/film.mp4'], + 'decision_seconds' => 12, + ]) + ->call('save') + ->assertHasNoFormErrors(); + + $version = $element->versions()->first(); + + $this->assertNotNull($version); + $this->assertSame(ContentVersionStatus::Bereit, $version->status); + $this->assertSame('inhalte/'.$version->id.'/film.mp4', $version->metadata['file']); + $this->assertSame(12, $version->metadata['decision_seconds']); + Storage::disk('media_derived')->assertExists($version->metadata['file']); + } + + public function test_an_exercise_can_gain_and_drop_a_medium_without_losing_the_old_version(): void + { + $content = app(ContentService::class); + $sheet = $content->publishElementVersion( + $content->createElement('pdf', 'pdf', 'Merkblatt'), + 'Hinweis', + ); + $film = $content->publishElementVersion( + $content->createElement('film', 'film', 'Übungsfilm'), + 'Filmtext', + ); + $exercise = $content->publishComposition( + $content->createComposition('Alltag üben'), + [['role' => 'Merkblatt', 'target' => $sheet]], + ); + $editor = User::factory()->redakteur()->create(); + $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + $this->actingAs($editor, 'staff'); + + $this->get(ExerciseResource::getUrl('edit', ['record' => $exercise->composition])) + ->assertOk() + ->assertSee('Merkblatt') + ->assertSee('Baustein hinzufügen') + ->assertSee('Neue Fassung veröffentlichen'); + + $this->get(CourseResource::getUrl('index'))->assertOk()->assertSee('Ein Kurs ist keine Übung'); + $this->get(CombinationResource::getUrl('index'))->assertOk()->assertSee('Materialsatz'); + $this->get(ContentElementResource::getUrl('index'))->assertOk()->assertSee('Übungsfilm'); + + Livewire::test(EditExercise::class, ['record' => $exercise->composition_id]) + ->fillForm([ + 'designation' => 'Alltag üben', + 'kind' => 'uebung', + 'members' => [ + ['role' => 'Film', 'target' => 'element:'.$film->id], + ], + 'note' => 'Film statt Merkblatt', + ]) + ->call('save') + ->assertHasNoFormErrors(); + + $versions = $exercise->composition->versions()->with('members')->orderBy('number')->get(); + + $this->assertCount(2, $versions); + $this->assertSame($sheet->id, $versions[0]->members->first()->content_element_version_id); + $this->assertSame($film->id, $versions[1]->members->first()->content_element_version_id); + $this->assertSame('Film', $versions[1]->members->first()->role); + } + + public function test_a_catalog_keeper_cannot_edit_media(): void + { + $element = app(ContentService::class)->createElement('text', 'text', 'Hinweis'); + + $keeper = User::factory()->katalogverwalter()->create(); + $keeper->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + + $this->actingAs($keeper, 'staff') + ->get(ContentElementResource::getUrl('edit', ['record' => $element])) + ->assertForbidden(); + } +}