73 lines
2.0 KiB
PHP
73 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Lesesicht für Hero, Teaser und Slideshow.
|
|
* Liefert nur eine freigegebene öffentliche Ableitung, sonst nichts.
|
|
*/
|
|
class WebsiteMedia
|
|
{
|
|
public function present(ContentElement|int $element): ?WebsitePresentation
|
|
{
|
|
$element = $element instanceof ContentElement
|
|
? $element
|
|
: ContentElement::query()->find($element);
|
|
|
|
if (! $element instanceof ContentElement || $element->website_released_at === null) {
|
|
return null;
|
|
}
|
|
|
|
$path = $element->website_public_path;
|
|
$expected = '#^website/'.$element->id.'/datei\.[a-z0-9]+$#';
|
|
|
|
if (! is_string($path) || preg_match($expected, $path) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
if (! Storage::disk('media_public')->exists($path)) {
|
|
return null;
|
|
}
|
|
|
|
$title = trim((string) ($element->display_title ?: $element->working_title));
|
|
$alt = trim((string) ($element->website_alt ?? ''));
|
|
|
|
return new WebsitePresentation(
|
|
elementId: $element->id,
|
|
title: $title,
|
|
alt: $alt !== '' ? $alt : $title,
|
|
credit: filled($element->website_credit) ? trim((string) $element->website_credit) : null,
|
|
kind: $element->output_class === 'film' ? 'film' : 'bild',
|
|
url: route('website.media', $element),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Slideshow: freigegebene Medien in der angegebenen Reihenfolge. Nicht freigegebene Einträge entfallen.
|
|
*
|
|
* @param list<int|string> $ids
|
|
* @return list<WebsitePresentation>
|
|
*/
|
|
public function presentMany(array $ids): array
|
|
{
|
|
$presentations = [];
|
|
|
|
foreach ($ids as $id) {
|
|
if (! is_numeric($id)) {
|
|
continue;
|
|
}
|
|
|
|
$presentation = $this->present((int) $id);
|
|
|
|
if ($presentation !== null) {
|
|
$presentations[] = $presentation;
|
|
}
|
|
}
|
|
|
|
return $presentations;
|
|
}
|
|
}
|