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
@@ -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}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ContentElements\Pages;
|
||||
|
||||
use App\Domain\Catalog\ContentElement;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Domain\Catalog\ContentVersionStatus;
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Filament\Resources\ContentElements\ContentElementResource;
|
||||
use App\Filament\Support\CatalogReading;
|
||||
use App\Filament\Support\IncomingFile;
|
||||
use App\Filament\Support\ReportsDomainRules;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Forms\Components\Placeholder;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Exceptions\Halt;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\HtmlString;
|
||||
|
||||
class EditContentElement extends EditRecord
|
||||
{
|
||||
use ReportsDomainRules;
|
||||
|
||||
protected static string $resource = ContentElementResource::class;
|
||||
|
||||
protected ?string $savedTitle = null;
|
||||
|
||||
public function getSubheading(): string|Htmlable|null
|
||||
{
|
||||
return 'Speichern legt eine neue Fassung an. Die bisherige Fassung bleibt erhalten und kann weiter angesehen werden. Ohne neue Datei bleibt die vorhandene Datei der neuen Fassung zugeordnet.';
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->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('<br>');
|
||||
$links = $element->versions()->orderByDesc('number')->get()->map(function (ContentElementVersion $version): string {
|
||||
return '<a href="'.e(route('content.preview', $version)).'" target="_blank" rel="noopener">Fassung '.$version->number.' ansehen</a>';
|
||||
})->implode(' · ');
|
||||
|
||||
return new HtmlString($html.($links !== '' ? '<br>'.$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('<br>'));
|
||||
}),
|
||||
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<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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<string, mixed> $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();
|
||||
}
|
||||
}
|
||||
@@ -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]));
|
||||
}
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ContentElements\Pages;
|
||||
|
||||
use App\Domain\Catalog\ContentElement;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\ContentService;
|
||||
use App\Filament\Resources\ContentElements\ContentElementResource;
|
||||
use App\Filament\Support\ReportsDomainRules;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
|
||||
class ViewContentElement extends ViewRecord
|
||||
{
|
||||
use ReportsDomainRules;
|
||||
|
||||
protected static string $resource = ContentElementResource::class;
|
||||
|
||||
public function getSubheading(): string|Htmlable|null
|
||||
{
|
||||
return 'Text und Datei gehören zur Fassung. Eine Filmfassung kann eine Entscheidungsmarke in Sekunden tragen. Zum Kurs wird das Element in einer Komposition, zum Paket über den Lizenzartikel.';
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('preview')
|
||||
->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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user