Stelle alle CMS-Bilder auf die zentrale Medienverwaltung um.
- Einstieg, Bild und Text, redaktionelle Karten, Premium-Karten und Abschnittshintergrund speichern die Kennung des Mediums (Bildschlüssel image) und lesen über WebsiteMedia; öffentlich nur mit Website-Freigabe, in der Redaktionsvorschau sonst das Vorschaubild mit Hinweis. - Gemeinsame Felder: Medienpicker mit Vorschaubild und Status, Upload direkt über WebsiteIntake, Anzeige des gewählten Mediums mit Verweis auf den Medienmanager. Die CMS-eigene Bildauswahl entfällt. - Frühere CMS-Bilder übernimmt ein Warteschlangen-Job (dedupliziert, nichts gelöscht); website_media_aliases ordnet frühere Pfade zu, wirksam erst nach der ersten Website-Freigabe; /medien/cms/ folgt danach der Freigabe. - Medienansicht nennt die Website-Seiten, die ein Bild verwenden. - Bewusst geändert: Der Alternativtext gehört zum Medium und ist im Widget nur noch eine optionale Abweichung; die Tests zur Alt-Text-Pflicht prüfen jetzt Übernahme und andere Registry-Fehler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7c63424ea6
commit
053a8d4648
35 files changed
+1170
-164
No files matched your search
@@ -0,0 +1,349 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Widgets\Support;
|
||||
|
||||
use App\Domain\Catalog\ContentElement;
|
||||
use App\Domain\Catalog\EditorialContentKind;
|
||||
use App\Domain\Cms\Catalog\PublicMaterialReader;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Media\ThumbnailRef;
|
||||
use App\Domain\Media\WebsiteIntake;
|
||||
use App\Domain\Media\WebsiteMedia;
|
||||
use App\Domain\Media\WebsiteMediaAlias;
|
||||
use App\Domain\Media\WebsitePresentation;
|
||||
use App\Filament\Resources\ContentElements\ContentElementResource;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Bilder in CMS-Widgets und Abschnittshintergründen aus dem zentralen Medienbestand
|
||||
* (docs/architektur/medienzugriff-fuer-komponenten.md).
|
||||
*
|
||||
* Gespeichert wird im jeweiligen Bildschlüssel (refs.image, style.image, data.items[].image) die Kennung des
|
||||
* Inhaltselements (int). Frühere Werte sind Pfade unter cms/ (string):
|
||||
* - mit wirksamer Zuordnung (WebsiteMediaAlias) gelten sie als das zugeordnete Medium,
|
||||
* - ohne Zuordnung gelten sie weiter als öffentliches CMS-Bild (PublicMedia), bis das Widget umgestellt ist.
|
||||
*
|
||||
* Öffentlich erscheint ein Medium nur über WebsiteMedia (Website-Freigabe, Route website.media). In der
|
||||
* Redaktionsvorschau erscheint ein nicht freigegebenes Bild als Vorschaubild der Bibliothek (Route editorial.card)
|
||||
* mit Hinweis. Eine CMS-Verwendung gibt nichts frei.
|
||||
*
|
||||
* Je Anfrage gebunden (scoped): Ergebnisse werden innerhalb einer Anfrage zwischengespeichert.
|
||||
*/
|
||||
class WebsiteImage
|
||||
{
|
||||
/** @var array<string, int|string|null> */
|
||||
private array $resolved = [];
|
||||
|
||||
/** @var array<int, WebsitePresentation|false> */
|
||||
private array $presentations = [];
|
||||
|
||||
public function __construct(private readonly WebsiteMedia $media) {}
|
||||
|
||||
/**
|
||||
* Gespeicherter oder eingegebener Wert → Kennung (int), früherer Pfad (string) oder null.
|
||||
*/
|
||||
public static function normalize(mixed $value): int|string|null
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$value = array_values($value)[0] ?? null;
|
||||
}
|
||||
|
||||
if (is_int($value) && $value > 0) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (is_string($value) && preg_match('/^[1-9][0-9]*$/', $value) === 1) {
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
return PublicMedia::isPublicPath($value) ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Kennung des Mediums: gespeicherte Kennung oder wirksam zugeordneter früherer Pfad; sonst null.
|
||||
*/
|
||||
public function elementId(mixed $value): ?int
|
||||
{
|
||||
$value = self::normalize($value);
|
||||
|
||||
if (is_int($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! array_key_exists($value, $this->resolved)) {
|
||||
$this->resolved[$value] = WebsiteMediaAlias::effectiveFor($value)?->content_element_id;
|
||||
}
|
||||
|
||||
$id = $this->resolved[$value];
|
||||
|
||||
return is_int($id) ? $id : null;
|
||||
}
|
||||
|
||||
public function presentation(mixed $value): ?WebsitePresentation
|
||||
{
|
||||
$id = $this->elementId($value);
|
||||
|
||||
if ($id === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! array_key_exists($id, $this->presentations)) {
|
||||
$this->presentations[$id] = $this->media->present($id) ?? false;
|
||||
}
|
||||
|
||||
return $this->presentations[$id] ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bildadresse für die Darstellung. Öffentlich nur freigegebene Medien (oder frühere CMS-Bilder ohne Zuordnung);
|
||||
* in der Redaktionsvorschau zusätzlich das Vorschaubild eines nicht freigegebenen Mediums.
|
||||
*/
|
||||
public function url(mixed $value, bool $preview = false): ?string
|
||||
{
|
||||
$presentation = $this->presentation($value);
|
||||
|
||||
if ($presentation !== null) {
|
||||
return $presentation->kind === 'bild' ? $presentation->url : null;
|
||||
}
|
||||
|
||||
$id = $this->elementId($value);
|
||||
|
||||
if ($id !== null) {
|
||||
if (! $preview) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$element = ContentElement::query()->find($id);
|
||||
|
||||
return $element === null ? null : ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path);
|
||||
}
|
||||
|
||||
$legacy = self::normalize($value);
|
||||
|
||||
return is_string($legacy) ? PublicMedia::url($legacy) : null;
|
||||
}
|
||||
|
||||
/** Alternativtext: Angabe im Widget, sonst der Alternativtext des Mediums. */
|
||||
public function alt(mixed $value, mixed $override = null): string
|
||||
{
|
||||
if (is_string($override) && trim($override) !== '') {
|
||||
return trim($override);
|
||||
}
|
||||
|
||||
return $this->presentation($value)?->alt ?? '';
|
||||
}
|
||||
|
||||
/** Öffentliche Quellenzeile des Mediums (nur solange es freigegeben ist). */
|
||||
public function credit(mixed $value): ?string
|
||||
{
|
||||
return $this->presentation($value)?->credit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hinweis für die Redaktionsvorschau, wenn das gewählte Bild öffentlich nicht erscheint; sonst null.
|
||||
*/
|
||||
public function previewNotice(mixed $value, string $label = 'Das gewählte Bild'): ?string
|
||||
{
|
||||
$status = $this->status($value);
|
||||
|
||||
if ($status === null || $status['public']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $label.' „'.$status['title'].'“ erscheint öffentlich nicht: '.$status['note'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Zustand für Editor und Vorschau.
|
||||
*
|
||||
* @return array{public: bool, state: string, title: string, note: string, thumb: ?string, manageUrl: ?string}|null
|
||||
*/
|
||||
public function status(mixed $value): ?array
|
||||
{
|
||||
$value = self::normalize($value);
|
||||
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = $this->elementId($value);
|
||||
|
||||
if ($id === null) {
|
||||
return [
|
||||
'public' => true,
|
||||
'state' => 'legacy',
|
||||
'title' => basename((string) $value),
|
||||
'note' => 'Früheres CMS-Bild, noch nicht in den Medienmanager übernommen.',
|
||||
'thumb' => PublicMedia::url((string) $value),
|
||||
'manageUrl' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$element = ContentElement::query()->with('editorialContentKind')->find($id);
|
||||
|
||||
if ($element === null) {
|
||||
return ['public' => false, 'state' => 'missing', 'title' => 'Medium '.$id, 'note' => 'Das Medium gibt es nicht mehr.', 'thumb' => null, 'manageUrl' => null];
|
||||
}
|
||||
|
||||
$title = (string) ($element->display_title ?: $element->working_title);
|
||||
$thumb = ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path);
|
||||
$manageUrl = ContentElementResource::getUrl('view', ['record' => $element]);
|
||||
$public = $this->presentation($id) !== null;
|
||||
|
||||
$note = match (true) {
|
||||
$public => 'Für die Website freigegeben.',
|
||||
$element->archived_at !== null => 'Das Medium ist archiviert.',
|
||||
filled($element->thumbnail_error) => 'Die Datei ließ sich nicht verarbeiten: '.Str::limit((string) $element->thumbnail_error, 120),
|
||||
$element->thumbnail_path === null => 'Wird gerade verarbeitet. Danach wird es automatisch für die Website freigegeben.',
|
||||
default => 'Nicht für die Website freigegeben. Die Freigabe steht im Medienmanager beim Medium unter „Beschreibung“ (Website-Freigabe).',
|
||||
};
|
||||
|
||||
return [
|
||||
'public' => $public,
|
||||
'state' => $public ? 'released' : ($element->thumbnail_path === null && ! filled($element->thumbnail_error) ? 'pending' : 'unreleased'),
|
||||
'title' => $title,
|
||||
'note' => $note,
|
||||
'thumb' => $thumb,
|
||||
'manageUrl' => $manageUrl,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft einen Wert beim Speichern. Ein früherer Pfad mit wirksamer Zuordnung wird zur Kennung des Mediums.
|
||||
*/
|
||||
public function clean(mixed $value, WidgetErrors $errors, string $errorKey): int|string|null
|
||||
{
|
||||
$raw = is_array($value) ? (array_values($value)[0] ?? null) : $value;
|
||||
$value = self::normalize($raw);
|
||||
|
||||
if ($value === null) {
|
||||
if ($raw !== null && $raw !== '') {
|
||||
$errors->add($errorKey, 'Bitte ein Bild aus dem Medienmanager wählen.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
return $this->elementId($value) ?? $value;
|
||||
}
|
||||
|
||||
$element = ContentElement::query()->find($value);
|
||||
|
||||
if ($element === null || ! self::isSelectable($element)) {
|
||||
$errors->add($errorKey, 'Dieses Medium ist kein Bild für die Website (nur Bilder aus dem Medienmanager, keine Traineranleitungen).');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $element->id;
|
||||
}
|
||||
|
||||
public static function isSelectable(ContentElement $element): bool
|
||||
{
|
||||
if ($element->output_class !== 'bild' || $element->archived_at !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$slug = $element->editorialContentKind?->slug
|
||||
?? ($element->editorial_content_kind_id !== null ? EditorialContentKind::query()->whereKey($element->editorial_content_kind_id)->value('slug') : null);
|
||||
|
||||
return ! in_array($slug, PublicMaterialReader::CONFIDENTIAL_KINDS, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Auswahl für den Medienpicker: Bilder aus dem Medienmanager mit Vorschaubild, Titel und Freigabestatus (HTML).
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function options(?string $search = null, int $limit = 60): array
|
||||
{
|
||||
$search = trim((string) $search);
|
||||
|
||||
$elements = ContentElement::query()
|
||||
->with('editorialContentKind')
|
||||
->where('output_class', 'bild')
|
||||
->whereNull('archived_at')
|
||||
->when($search !== '', function ($query) use ($search): void {
|
||||
$like = '%'.mb_strtolower($search).'%';
|
||||
$query->where(fn ($inner) => $inner
|
||||
->whereRaw('LOWER(working_title) LIKE ?', [$like])
|
||||
->orWhereRaw('LOWER(COALESCE(display_title, \'\')) LIKE ?', [$like]));
|
||||
})
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$options = [];
|
||||
foreach ($elements as $element) {
|
||||
if (self::isSelectable($element)) {
|
||||
$options[$element->id] = $this->optionHtml($element->id);
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/** HTML-Bezeichnung einer Option oder des gewählten Werts (auch für frühere Pfade). */
|
||||
public function optionHtml(mixed $value): ?string
|
||||
{
|
||||
$status = $this->status($value);
|
||||
|
||||
if ($status === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$badge = match ($status['state']) {
|
||||
'released' => ['ok', 'Website: freigegeben'],
|
||||
'pending' => ['wait', 'Wird verarbeitet'],
|
||||
'legacy' => ['quiet', 'Früheres CMS-Bild'],
|
||||
default => ['warn', 'Website: nicht freigegeben'],
|
||||
};
|
||||
$thumb = $status['thumb'] !== null
|
||||
? '<img src="'.e($status['thumb']).'" alt="" loading="lazy">'
|
||||
: '<span class="luf-media-option-empty" aria-hidden="true"></span>';
|
||||
|
||||
return '<span class="luf-media-option">'.$thumb
|
||||
.'<span class="luf-media-option-text"><strong>'.e($status['title']).'</strong>'
|
||||
.'<span class="luf-media-badge luf-media-badge-'.$badge[0].'">'.e($badge[1]).'</span></span></span>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Neues Bild direkt aus dem CMS: Datei in den Eingang legen und über WebsiteIntake als Medium anlegen.
|
||||
* Die Warteschlange leitet das Bild ab und gibt die Ableitung für die Website frei.
|
||||
*/
|
||||
public function accept(TemporaryUploadedFile $file, ?string $alt = null): ?ContentElement
|
||||
{
|
||||
$base = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) ?: 'bild';
|
||||
$name = $base.'-'.Str::lower(Str::random(6)).'.'.($file->guessExtension() ?: 'jpg');
|
||||
|
||||
try {
|
||||
$path = $file->storeAs('cms', $name, 'cms_incoming');
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_string($path) || $path === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$actor = auth('staff')->user();
|
||||
|
||||
return app(WebsiteIntake::class)->acceptImage(
|
||||
$path,
|
||||
Str::title(str_replace(['-', '_'], ' ', $base)),
|
||||
null,
|
||||
filled($alt) ? trim((string) $alt) : null,
|
||||
$actor instanceof User ? $actor : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user