Mache Medien, Übungen und Kurse in der Verwaltung bearbeitbar.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4f2e5a5f3b
commit
3ee9f529ca
33 files changed
+1105
-284
No files matched your search
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Compositions;
|
||||
|
||||
use App\Domain\Catalog\Composition;
|
||||
use App\Domain\Catalog\CompositionKind;
|
||||
use App\Filament\Resources\ContentElements\ContentElementResource;
|
||||
use Filament\Resources\Resource;
|
||||
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;
|
||||
|
||||
abstract class CompositionKindResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Composition::class;
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Inhalte';
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'designation';
|
||||
|
||||
abstract public static function kind(): CompositionKind;
|
||||
|
||||
public static function canViewAny(): bool
|
||||
{
|
||||
return ContentElementResource::editor();
|
||||
}
|
||||
|
||||
public static function getEditAuthorizationResponse(Model $record): Response
|
||||
{
|
||||
return ContentElementResource::editor() ? Response::allow() : Response::deny();
|
||||
}
|
||||
|
||||
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 getEloquentQuery(): Builder
|
||||
{
|
||||
return parent::getEloquentQuery()->where('kind', static::kind());
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Compositions;
|
||||
|
||||
use App\Domain\Catalog\Composition;
|
||||
use App\Filament\Resources\Compositions\Pages\ListCompositions;
|
||||
use App\Filament\Resources\Compositions\Pages\ViewComposition;
|
||||
use App\Filament\Resources\ContentElements\ContentElementResource;
|
||||
use App\Filament\Support\CatalogReading;
|
||||
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\Model;
|
||||
|
||||
class CompositionResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Composition::class;
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Inhalte';
|
||||
|
||||
protected static ?string $navigationLabel = 'Kurse und Übungen';
|
||||
|
||||
protected static ?string $modelLabel = 'Kurs oder Übung';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Kurse und Übungen';
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'designation';
|
||||
|
||||
protected static ?int $navigationSort = 2;
|
||||
|
||||
public static function canViewAny(): bool
|
||||
{
|
||||
return ContentElementResource::editor();
|
||||
}
|
||||
|
||||
public static function getViewAuthorizationResponse(Model $record): Response
|
||||
{
|
||||
return ContentElementResource::editor() ? Response::allow() : Response::deny();
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return $schema->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}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Compositions\Pages;
|
||||
|
||||
use App\Domain\Catalog\Composition;
|
||||
use App\Domain\Catalog\CompositionKind;
|
||||
use App\Domain\Catalog\CompositionVersion;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Domain\Catalog\ContentVersionStatus;
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Filament\Resources\Combinations\CombinationResource;
|
||||
use App\Filament\Resources\Courses\CourseResource;
|
||||
use App\Filament\Resources\Exercises\ExerciseResource;
|
||||
use App\Filament\Support\CatalogOptions;
|
||||
use App\Filament\Support\CatalogReading;
|
||||
use App\Models\User;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\Components\Placeholder;
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Exceptions\Halt;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\HtmlString;
|
||||
|
||||
abstract class EditComposition extends EditRecord
|
||||
{
|
||||
protected ?string $savedTitle = null;
|
||||
|
||||
public function getSubheading(): string|Htmlable|null
|
||||
{
|
||||
return 'Bausteine hinzufügen, gegen eine neuere Medienfassung tauschen oder aus der Liste nehmen. Speichern legt dafür eine neue Fassung an. Die bisherige Fassung bleibt und wird nicht nachträglich verändert.';
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->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('<br>'));
|
||||
}),
|
||||
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<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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<string, mixed> $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<array{role: string, target: ContentElementVersion|CompositionVersion}> $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;
|
||||
}
|
||||
}
|
||||
+13
-8
@@ -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]));
|
||||
}
|
||||
}),
|
||||
];
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Compositions\Pages;
|
||||
|
||||
use App\Domain\Catalog\Composition;
|
||||
use App\Domain\Catalog\CompositionVersion;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Filament\Resources\Compositions\CompositionResource;
|
||||
use App\Filament\Support\CatalogOptions;
|
||||
use App\Filament\Support\ReportsDomainRules;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewComposition extends ViewRecord
|
||||
{
|
||||
use ReportsDomainRules;
|
||||
|
||||
protected static string $resource = CompositionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('publish')
|
||||
->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();
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user