172 lines
7.6 KiB
PHP
172 lines
7.6 KiB
PHP
<?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\IdentityProfile;
|
|
use App\Domain\Catalog\Pillar;
|
|
use App\Domain\Catalog\PillarService;
|
|
use App\Filament\Resources\ContentElements\ContentElementResource;
|
|
use App\Filament\Support\CatalogReading;
|
|
use App\Filament\Support\ElementRevision;
|
|
use App\Filament\Support\ReportsDomainRules;
|
|
use App\Http\Controllers\ContentPreviewController;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\CheckboxList;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Resources\Pages\ViewRecord;
|
|
use Filament\Schemas\Components\View;
|
|
use Filament\Schemas\Schema;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class ViewContentElement extends ViewRecord
|
|
{
|
|
use ReportsDomainRules;
|
|
|
|
protected static string $resource = ContentElementResource::class;
|
|
|
|
public ?int $fassung = null;
|
|
|
|
public function mount(int|string $record): void
|
|
{
|
|
parent::mount($record);
|
|
|
|
$requested = request()->integer('fassung');
|
|
|
|
if ($requested > 0) {
|
|
$this->fassung = $requested;
|
|
}
|
|
}
|
|
|
|
public function getSubheading(): string|Htmlable|null
|
|
{
|
|
return 'Jede Fassung hat eine eigene Nummer und bleibt einsehbar. Beschreibung, Arbeitstitel und Säulen gehören zum Medium und ändern keine Fassung. Eine deaktivierte Fassung wird nicht mehr ausgeliefert und lässt sich nicht neu einbauen.';
|
|
}
|
|
|
|
public function infolist(Schema $schema): Schema
|
|
{
|
|
return $schema->components([
|
|
View::make('filament.media.viewer')->viewData(fn (): array => $this->viewerData()),
|
|
]);
|
|
}
|
|
|
|
public function showVersion(int $version): void
|
|
{
|
|
abort_unless($this->ownedVersion($version) instanceof ContentElementVersion, 404);
|
|
$this->fassung = $version;
|
|
}
|
|
|
|
public function deactivateAction(): Action
|
|
{
|
|
return Action::make('deactivate')
|
|
->label('Deaktivieren')
|
|
->modalHeading('Fassung deaktivieren')
|
|
->modalDescription('Die Fassung bleibt hier einsehbar. Sie wird nicht mehr ausgeliefert und kann nicht in eine neue Übung oder einen neuen Kurs übernommen werden.')
|
|
->modalSubmitActionLabel('Deaktivieren')
|
|
->visible(fn (): bool => ContentElementResource::editor())
|
|
->schema([
|
|
Textarea::make('reason')->label('Begründung')->required(),
|
|
])
|
|
->action(function (array $data, array $arguments): void {
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
$this->report(function () use ($data, $arguments): void {
|
|
$version = $this->ownedVersion((int) ($arguments['version'] ?? 0));
|
|
abort_unless($version instanceof ContentElementVersion, 404);
|
|
app(ContentService::class)->block($version, (string) $data['reason']);
|
|
}, 'Die Fassung ist deaktiviert.');
|
|
$this->getRecord()->unsetRelation('versions');
|
|
});
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('describe')
|
|
->label('Beschreibung ändern')
|
|
->visible(fn (): bool => ContentElementResource::editor())
|
|
->fillForm(fn (): array => [
|
|
'working_title' => $this->getRecord()->working_title,
|
|
'description' => $this->getRecord()->description,
|
|
'usage_note' => $this->getRecord()->usage_note,
|
|
'license_note' => $this->getRecord()->license_note,
|
|
'pillars' => $this->getRecord()->pillars()->pluck('pillars.id')->all(),
|
|
])
|
|
->schema([
|
|
TextInput::make('working_title')->label('Arbeitstitel')->required(),
|
|
Textarea::make('description')->label('Beschreibung')->rows(4)
|
|
->helperText('Gilt für das Medium. Speichern legt keine neue Fassung an.'),
|
|
Textarea::make('usage_note')->label('Hinweise für den Einsatz')->rows(4),
|
|
Textarea::make('license_note')->label('Interner Lizenzvermerk')->rows(3)
|
|
->helperText('Nur in der Verwaltung. Nicht in der Bibliothek und nicht auf der Website.'),
|
|
CheckboxList::make('pillars')
|
|
->label('Fachliche Säulen')
|
|
->options(fn (): array => Pillar::query()->orderBy('name')->pluck('name', 'id')->all()),
|
|
])
|
|
->action(function (array $data): void {
|
|
$this->report(function () use ($data): void {
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
app(ContentService::class)->describe($element, (string) $data['working_title'], $data['description'] ?? null);
|
|
app(IdentityProfile::class)->fill($element, [
|
|
'usage_note' => $data['usage_note'] ?? null,
|
|
'license_note' => $data['license_note'] ?? null,
|
|
], auth()->user());
|
|
app(PillarService::class)->assign($element, array_map(intval(...), $data['pillars'] ?? []));
|
|
}, 'Die Beschreibung ist gespeichert.');
|
|
$this->getRecord()->refresh();
|
|
}),
|
|
Action::make('revise')
|
|
->label('Neue Fassung anlegen')
|
|
->visible(fn (): bool => ContentElementResource::editor())
|
|
->schema(fn (): array => ElementRevision::fields($this->getRecord()))
|
|
->action(function (array $data): void {
|
|
$this->report(function () use ($data): string {
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
|
|
return ElementRevision::publish($element, $data);
|
|
}, 'Die neue Fassung ist angelegt.');
|
|
$this->getRecord()->unsetRelation('versions');
|
|
}),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array{element: ContentElement, versions: Collection<int, ContentElementVersion>, shown: ?ContentElementVersion, fileUrl: ?string, pending: bool, pillars: list<string>, usage: list<string>, canRevise: bool}
|
|
*/
|
|
private function viewerData(): array
|
|
{
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
$versions = $element->versions()->orderByDesc('number')->get();
|
|
$shown = $versions->firstWhere('id', $this->fassung)
|
|
?? $versions->first(fn (ContentElementVersion $version): bool => $version->status === ContentVersionStatus::Bereit)
|
|
?? $versions->first();
|
|
$preview = app(ContentPreviewController::class);
|
|
|
|
return [
|
|
'element' => $element,
|
|
'versions' => $versions,
|
|
'shown' => $shown,
|
|
'fileUrl' => $shown === null ? null : $preview->fileUrl($shown),
|
|
'pending' => $shown !== null && $preview->isPending($shown),
|
|
'pillars' => $element->pillars->pluck('name')->all(),
|
|
'usage' => app(CatalogReading::class)->elementUsage($element),
|
|
'canRevise' => ContentElementResource::editor(),
|
|
];
|
|
}
|
|
|
|
private function ownedVersion(int $id): ?ContentElementVersion
|
|
{
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
|
|
return $element->versions()->whereKey($id)->first();
|
|
}
|
|
}
|