399 lines
16 KiB
PHP
399 lines
16 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\Domain\Media\ThumbnailRef;
|
|
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 App\Jobs\DeriveThumbnail;
|
|
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;
|
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class ViewContentElement extends ViewRecord
|
|
{
|
|
use ReportsDomainRules;
|
|
use WithFileUploads;
|
|
|
|
protected static string $resource = ContentElementResource::class;
|
|
|
|
public ?int $fassung = null;
|
|
|
|
public string $frameSecond = '';
|
|
|
|
public string $bodyDraft = '';
|
|
|
|
public string $thumbnailAlt = '';
|
|
|
|
public string $pictureFeedback = '';
|
|
|
|
public mixed $previewUpload = null;
|
|
|
|
public function mount(int|string $record): void
|
|
{
|
|
parent::mount($record);
|
|
|
|
$requested = request()->integer('fassung');
|
|
|
|
if ($requested > 0) {
|
|
$this->fassung = $requested;
|
|
}
|
|
|
|
$this->thumbnailAlt = (string) ($this->getRecord()->thumbnail_alt ?? '');
|
|
$this->syncBodyDraft();
|
|
}
|
|
|
|
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;
|
|
$this->syncBodyDraft();
|
|
}
|
|
|
|
public function saveShownText(): void
|
|
{
|
|
$shown = $this->shownVersion();
|
|
|
|
if (! ContentElementResource::editor() || ! $shown instanceof ContentElementVersion || $shown->element?->output_class !== 'text') {
|
|
return;
|
|
}
|
|
|
|
$created = app(ContentService::class)->isCommitted($shown);
|
|
$this->report(function () use ($shown): void {
|
|
app(ContentService::class)->reviseElementContent($shown, $this->bodyDraft);
|
|
}, $created
|
|
? 'Die neue Fassung ist angelegt. Die bisherige bleibt erhalten.'
|
|
: 'Der Text ist in derselben Fassung gespeichert.');
|
|
$this->getRecord()->unsetRelation('versions');
|
|
$this->syncBodyDraft();
|
|
}
|
|
|
|
public function adoptFrame(): void
|
|
{
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
$shown = $this->shownVersion();
|
|
|
|
if (! $shown instanceof ContentElementVersion || $element->output_class !== 'film') {
|
|
$this->pictureFeedback = 'Für dieses Medium gibt es keine Filmstelle.';
|
|
|
|
return;
|
|
}
|
|
|
|
if (! is_numeric($this->frameSecond) || trim($this->frameSecond) === '') {
|
|
$this->pictureFeedback = 'Wähle zuerst eine Zeitposition.';
|
|
|
|
return;
|
|
}
|
|
|
|
$seconds = (float) $this->frameSecond;
|
|
|
|
if ($seconds < 0 || $seconds > 7200) {
|
|
$this->pictureFeedback = 'Die Zeitposition liegt außerhalb des Films.';
|
|
|
|
return;
|
|
}
|
|
|
|
$this->keepAlt($element);
|
|
$element->thumbnail_error = null;
|
|
$element->save();
|
|
DeriveThumbnail::dispatch('element', $element->id, null, null, $shown->id, $seconds);
|
|
$element->refresh();
|
|
$this->pictureFeedback = $element->thumbnail_error
|
|
?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.';
|
|
}
|
|
|
|
public function storePreview(): void
|
|
{
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
|
|
if (! $this->previewUpload instanceof TemporaryUploadedFile) {
|
|
$this->pictureFeedback = 'Wähle zuerst eine Bilddatei.';
|
|
|
|
return;
|
|
}
|
|
|
|
$extension = strtolower($this->previewUpload->getClientOriginalExtension());
|
|
|
|
if (! in_array($extension, ['png', 'jpg', 'jpeg', 'webp'], true)) {
|
|
$this->pictureFeedback = '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->pictureFeedback = 'Das Vorschaubild wurde nicht übernommen.';
|
|
|
|
return;
|
|
}
|
|
|
|
$this->savePreviewImage($path);
|
|
}
|
|
|
|
public function savePreviewImage(string $path): void
|
|
{
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
$this->keepAlt($element);
|
|
$element->thumbnail_error = null;
|
|
$element->save();
|
|
DeriveThumbnail::dispatch('element', $element->id, $path);
|
|
$element->refresh();
|
|
$this->pictureFeedback = $element->thumbnail_error
|
|
?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.';
|
|
}
|
|
|
|
public function useOwnImage(): void
|
|
{
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
|
|
if ($element->output_class !== 'bild') {
|
|
$this->pictureFeedback = 'Die Datei dieses Mediums ist kein Bild. Lade ein Vorschaubild hoch.';
|
|
|
|
return;
|
|
}
|
|
|
|
$this->keepAlt($element);
|
|
$element->thumbnail_error = null;
|
|
$element->save();
|
|
DeriveThumbnail::dispatch('element', $element->id);
|
|
$element->refresh();
|
|
$this->pictureFeedback = $element->thumbnail_error
|
|
?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.';
|
|
}
|
|
|
|
public function framePreviewUrl(): ?string
|
|
{
|
|
$shown = $this->shownVersion();
|
|
|
|
if (! $shown instanceof ContentElementVersion || ! is_numeric($this->frameSecond) || trim($this->frameSecond) === '') {
|
|
return null;
|
|
}
|
|
|
|
return route('editorial.frame', [
|
|
'version' => $shown->id,
|
|
'sekunde' => $this->frameSecond,
|
|
]);
|
|
}
|
|
|
|
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('releaseVersion')
|
|
->label('Fassung freigeben')
|
|
->modalHeading('Fassung freigeben')
|
|
->modalDescription('Diese Fassung wird freigegeben. Sie erscheint dadurch nicht in der Mitgliederbibliothek, nicht auf der Website und nicht in einem Angebot. Eine deaktivierte Fassung bleibt unverändert.')
|
|
->modalSubmitActionLabel('Freigeben')
|
|
->visible(fn (): bool => ContentElementResource::editor())
|
|
->action(function (array $arguments): void {
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
$this->report(function () use ($arguments): string {
|
|
$version = $this->ownedVersion((int) ($arguments['version'] ?? 0));
|
|
abort_unless($version instanceof ContentElementVersion, 404);
|
|
$released = app(ContentService::class)->releaseElementVersion($version);
|
|
|
|
return 'Fassung '.$released->number.' ist freigegeben.';
|
|
}, 'Die Fassung ist freigegeben.');
|
|
$this->getRecord()->unsetRelation('versions');
|
|
}),
|
|
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);
|
|
$committed = $shown instanceof ContentElementVersion && app(ContentService::class)->isCommitted($shown);
|
|
|
|
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(),
|
|
'canEditBody' => ContentElementResource::editor()
|
|
&& $shown instanceof ContentElementVersion
|
|
&& $element->output_class === 'text'
|
|
&& $shown->status !== ContentVersionStatus::Gesperrt,
|
|
'bodyCommitted' => $committed,
|
|
'pictureUrl' => ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path),
|
|
'pictureCaption' => $this->pictureCaption($element),
|
|
];
|
|
}
|
|
|
|
private function syncBodyDraft(): void
|
|
{
|
|
$this->bodyDraft = (string) ($this->shownVersion()?->body ?? '');
|
|
}
|
|
|
|
private function shownVersion(): ?ContentElementVersion
|
|
{
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
$versions = $element->versions()->orderByDesc('number')->get();
|
|
|
|
return $versions->firstWhere('id', $this->fassung)
|
|
?? $versions->first(fn (ContentElementVersion $version): bool => $version->status === ContentVersionStatus::Bereit)
|
|
?? $versions->first();
|
|
}
|
|
|
|
private function keepAlt(ContentElement $element): void
|
|
{
|
|
$alt = trim($this->thumbnailAlt);
|
|
|
|
if ($alt === '') {
|
|
return;
|
|
}
|
|
|
|
$element->thumbnail_alt = $alt;
|
|
}
|
|
|
|
private function pictureCaption(ContentElement $element): string
|
|
{
|
|
$source = $element->thumbnail_source;
|
|
|
|
if (! is_string($source) || $source === '') {
|
|
return '';
|
|
}
|
|
|
|
if (preg_match('/^frame:\d+@([\d.]+)$/', $source, $match) === 1) {
|
|
return 'Vorschaubild aus dem Film bei '.$match[1].' Sekunden.';
|
|
}
|
|
|
|
return match ($source) {
|
|
'eigen' => 'Eigenes Vorschaubild.',
|
|
'datei' => 'Vorschaubild aus der Datei des Mediums.',
|
|
default => '',
|
|
};
|
|
}
|
|
|
|
private function ownedVersion(int $id): ?ContentElementVersion
|
|
{
|
|
/** @var ContentElement $element */
|
|
$element = $this->getRecord();
|
|
|
|
return $element->versions()->whereKey($id)->first();
|
|
}
|
|
}
|