Files
laraundfelix/app/Filament/Support/ManagesEditorialSurface.php
T
LarsandCursor f29ea58fd5
CI Feature / test (push) Successful in 54s
Deploy Test / test (push) Successful in 47s
Deploy Test / deploy (push) Successful in 24s
Ergänze die gemeinsame redaktionelle Basis an den Fachobjekten.
Texte, Materialart und Präsentationsmedien bleiben am Medium, Modul, Programm und Lizenzartikel und werden von Verwaltung und Karten aus derselben Quelle gelesen, ohne Fassung oder Lizenz zu verändern.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 07:54:36 +01:00

283 lines
8.3 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\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 bool $bindToVersion = true;
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)->browse($this->presentationQuery, 'alle', '', 0, elementsOnly: true);
}
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 = '';
}
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(),
),
);
}
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();
}
}
}