Files
laraundfelix/app/Filament/Support/ManagesEditorialSurface.php
T
Lars 41658d98dd
Deploy Test / test (push) Successful in 1m3s
Deploy Test / deploy (push) Successful in 23s
Richte Redaktionskarten und Redaktionskopf am Zeilenlayout aus.
2026-11-19 21:31:39 +01:00

435 lines
13 KiB
PHP

<?php
namespace App\Filament\Support;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\EditorialProfile;
use App\Domain\Catalog\EditorialProfileReader;
use App\Domain\Catalog\EditorialTextVariant;
use App\Domain\Catalog\PresentationAssignment;
use App\Domain\Catalog\PresentationMedia;
use App\Domain\Catalog\PresentationRole;
use App\Domain\Media\WebsiteRelease;
use App\Filament\Resources\ContentElements\ContentElementResource;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
/**
* Gemeinsame Bedienung für Präsentationsmedien und abweichende Formulierungen.
* Die Texte selbst bleiben an den vorhandenen Feldern der jeweiligen Seite.
*/
trait ManagesEditorialSurface
{
public string $presentationRole = 'titelbild';
public string $presentationQuery = '';
public string $presentationPick = '';
public string $presentationInspect = '';
public bool $bindToVersion = false;
public bool $redaktionOpen = false;
public function getBreadcrumbs(): array
{
if ($this->redaktionOpen) {
return [];
}
return parent::getBreadcrumbs();
}
public function getCachedHeaderActions(): array
{
if ($this->redaktionOpen) {
return [];
}
if ($this->cachedHeaderActions === []) {
$this->cacheInteractsWithHeaderActions();
}
return $this->cachedHeaderActions;
}
public string $redaktionTab = 'texte';
public ?int $replacingAssignment = null;
public string $deskFeedback = '';
public string $variantAudience = 'kachel';
public string $variantField = 'teaser';
public string $variantBody = '';
public function editorialSubject(): Model
{
return $this->getRecord();
}
public function editorialProfile(?string $audience = null): EditorialProfile
{
return app(EditorialProfileReader::class)->read($this->editorialSubject(), $audience);
}
/**
* @return list<string>
*/
public function editorialGaps(): array
{
$profile = $this->editorialProfile();
$gaps = [];
foreach ([
'display_title' => 'Anzeigename',
'teaser' => 'Teaser',
'summary' => 'Kurzbeschreibung',
'description' => 'Ausführliche Beschreibung',
] as $field => $label) {
if ($profile->value($field) === null) {
$gaps[] = $label;
}
}
return $gaps;
}
/**
* @return list<array<string, mixed>>
*/
public function presentationRows(): array
{
return app(PresentationMedia::class)->rows($this->editorialSubject());
}
/**
* @return array<string, string>
*/
public function presentationRoleOptions(): array
{
return PresentationRole::selectOptions();
}
/**
* @return array{items: list<array<string, mixed>>, more: bool}
*/
public function presentationChoices(): array
{
return app(EditorialCatalog::class)->presentationBrowse($this->presentationQuery);
}
public function assignPickedPresentation(): void
{
$this->guardEditorial();
$row = app(EditorialCatalog::class)->rowForTarget($this->presentationPick, 0, allowDraft: true);
if ($row === null || ! isset($row['element_id'])) {
$this->deskFeedback = 'Bitte ein Medium aus dem Bestand wählen.';
return;
}
$this->rememberAssignment((int) $row['element_id'], isset($row['target']) ? (string) $row['target'] : null);
$this->presentationPick = '';
$this->presentationInspect = '';
}
public function openRedaktion(): void
{
$this->guardEditorial();
$this->redaktionOpen = true;
$this->redaktionTab = 'texte';
}
public function closeRedaktion(): void
{
$this->redaktionOpen = false;
$this->presentationInspect = '';
$this->replacingAssignment = null;
}
public function cancelPresentationPick(): void
{
$this->presentationPick = '';
$this->presentationInspect = '';
$this->replacingAssignment = null;
$this->deskFeedback = 'Die vorhandene Zuordnung bleibt unverändert.';
}
public function beginExchange(int $assignmentId): void
{
$this->guardEditorial();
foreach ($this->presentationRows() as $row) {
if ((int) $row['id'] !== $assignmentId) {
continue;
}
$this->presentationRole = (string) $row['role'];
$this->replacingAssignment = $assignmentId;
$this->presentationPick = '';
$this->presentationInspect = '';
$this->redaktionTab = 'medien';
$this->deskFeedback = 'Wähle das neue Medium und übernimm es. Abbrechen lässt die bisherige Zuordnung stehen.';
if (method_exists($this, 'openPresentationPicker')) {
$this->openPresentationPicker();
}
return;
}
$this->deskFeedback = 'Diese Zuordnung gibt es nicht mehr.';
}
public function placePresentation(int $assignmentId, int $beforeId): void
{
$this->guardEditorial();
$rows = $this->presentationRows();
$moving = null;
foreach ($rows as $row) {
if ((int) $row['id'] === $assignmentId) {
$moving = $row;
}
}
if ($moving === null || ! $moving['ordered']) {
$this->deskFeedback = 'Diese Rolle hat eine feste einzelne Zuordnung und wird nicht umsortiert.';
return;
}
$ids = [];
foreach ($rows as $row) {
if ($row['role'] === $moving['role']) {
$ids[] = (int) $row['id'];
}
}
$from = array_search($assignmentId, $ids, true);
$to = array_search($beforeId, $ids, true);
if ($from === false || $to === false || $from === $to) {
return;
}
$direction = $to < $from ? -1 : 1;
for ($step = 0; $step < abs($to - $from); $step++) {
app(PresentationMedia::class)->move($this->editorialSubject(), $assignmentId, $direction, $this->deskActor());
}
$this->deskFeedback = 'Die Reihenfolge gilt nur in dieser Rolle.';
}
public function detachPresentation(int $assignmentId): void
{
$this->guardEditorial();
$this->deskFeedback = $this->reportPresentation(
fn (): string => app(PresentationMedia::class)->detach($this->editorialSubject(), $assignmentId, $this->deskActor()),
);
}
public function movePresentation(int $assignmentId, int $direction): void
{
$this->guardEditorial();
$this->deskFeedback = $this->reportPresentation(function () use ($assignmentId, $direction): string {
app(PresentationMedia::class)->move($this->editorialSubject(), $assignmentId, $direction, $this->deskActor());
return 'Die Reihenfolge ist gespeichert.';
});
}
public function releaseAssigned(int $elementId): void
{
abort_unless(ContentElementResource::editor(), 403);
$element = ContentElement::query()->find($elementId);
if (! $element instanceof ContentElement) {
$this->deskFeedback = 'Das Medium fehlt.';
return;
}
$this->deskFeedback = app(WebsiteRelease::class)->release($element, $this->deskActor());
}
public function withdrawAssigned(int $elementId): void
{
abort_unless(ContentElementResource::editor(), 403);
$element = ContentElement::query()->find($elementId);
if (! $element instanceof ContentElement) {
$this->deskFeedback = 'Das Medium fehlt.';
return;
}
$this->deskFeedback = app(WebsiteRelease::class)->withdraw($element, $this->deskActor());
}
/**
* @return list<EditorialTextVariant>
*/
public function variantRows(): array
{
$subject = $this->editorialSubject();
return EditorialTextVariant::query()
->where('subject_type', $subject->getMorphClass())
->where('subject_id', $subject->getKey())
->orderBy('audience')
->orderBy('field')
->get()
->all();
}
public function saveVariant(): void
{
$this->guardEditorial();
$this->deskFeedback = $this->reportPresentation(
fn (): string => app(EditorialProfileReader::class)->putVariant(
$this->editorialSubject(),
$this->variantField,
$this->variantAudience,
$this->variantBody,
$this->deskActor(),
),
);
$this->variantBody = '';
}
public function forgetVariant(int $variantId): void
{
$this->guardEditorial();
$variant = EditorialTextVariant::query()->find($variantId);
if (! $variant instanceof EditorialTextVariant) {
$this->deskFeedback = 'Diese Abweichung gibt es nicht mehr.';
return;
}
$this->reportPresentation(function () use ($variant): string {
app(EditorialProfileReader::class)->forgetVariant($this->editorialSubject(), $variant, $this->deskActor());
return 'Die abweichende Formulierung ist entfernt.';
});
$this->deskFeedback = 'Die abweichende Formulierung ist entfernt. Es gilt wieder die zentrale Angabe.';
}
public function variantFieldLabel(string $field): string
{
return match ($field) {
'display_title' => 'Anzeigename',
'teaser' => 'Teaser',
'summary' => 'Kurzbeschreibung',
'description' => 'Ausführliche Beschreibung',
default => $field,
};
}
public function canReleasePresentation(): bool
{
return ContentElementResource::editor();
}
public function canEditEditorial(): bool
{
return ContentElementResource::editor();
}
protected function rememberAssignment(int $elementId, ?string $target): void
{
$sourceId = null;
if ($this->bindToVersion && in_array($this->presentationRole, PresentationRole::BOUND_ROLES, true)) {
$subject = $this->editorialSubject();
$sourceId = $subject instanceof ContentElement
? $subject->versions()->where('status', ContentVersionStatus::Bereit)->orderByDesc('number')->value('id')
: $this->versionIdFromTarget($target);
}
$this->deskFeedback = $this->reportPresentation(
fn (): string => app(PresentationMedia::class)->assign(
$this->editorialSubject(),
$this->presentationRole,
$elementId,
$sourceId !== null ? (int) $sourceId : null,
$this->deskActor(),
),
);
$this->finishExchange();
}
protected function finishExchange(): void
{
$previous = $this->replacingAssignment;
if ($previous === null || ! str_contains($this->deskFeedback, 'zugeordnet')) {
return;
}
$this->replacingAssignment = null;
$still = PresentationAssignment::query()->find($previous);
if (! $still instanceof PresentationAssignment) {
$this->deskFeedback = 'Die Zuordnung ist ausgetauscht. '.$this->deskFeedback;
return;
}
if (str_contains($this->deskFeedback, 'bereits')) {
return;
}
$removed = $this->reportPresentation(
fn (): string => app(PresentationMedia::class)->detach($this->editorialSubject(), $previous, $this->deskActor()),
);
$this->deskFeedback = str_contains($removed, 'entfernt')
? 'Die bisherige Zuordnung ist ersetzt. '.$this->deskFeedback
: $removed;
}
protected function versionIdFromTarget(?string $target): ?int
{
if (is_string($target) && preg_match('/^element:(\d+)$/', $target, $match) === 1) {
return (int) $match[1];
}
return null;
}
protected function guardEditorial(): void
{
abort_unless($this->canEditEditorial(), 403);
}
protected function deskActor(): ?User
{
$user = auth()->user();
return $user instanceof User ? $user : null;
}
/**
* @param callable(): string $action
*/
protected function reportPresentation(callable $action): string
{
try {
return $action();
} catch (DomainRuleException $exception) {
return $exception->getMessage();
}
}
}