253 lines
8.5 KiB
PHP
253 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Support;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\EditorialVocabulary;
|
|
use App\Domain\Cms\Catalog\PublicMaterialReader;
|
|
use App\Domain\Cms\Widgets\Support\WebsiteImage;
|
|
use App\Http\Controllers\ContentPreviewController;
|
|
|
|
/**
|
|
* Gemeinsamer visueller Picker (Dokument 14 §9): Zustand, Suche, Treffer, Betrachten, Markieren und Detail. Die
|
|
* Ansicht ist filament.verwaltung.picker. Die Seite bestimmt Zweck, Titel, Typen, Mehrfachauswahl und was
|
|
* „Übernehmen“ bewirkt (pickerAllowsMany, pickerKinds, pickerTitle, pickerConfirmLabel, pickerKontext, confirmPicker,
|
|
* closePicker). Genutzt von der Redaktion (ManagesEditorialSurface) und vom CMS (ChoosesWebsiteImages).
|
|
*/
|
|
trait ChoosesMedia
|
|
{
|
|
public bool $pickerOpen = false;
|
|
|
|
public string $pickerPurpose = 'praesentation';
|
|
|
|
public string $pickerQuery = '';
|
|
|
|
public string $pickerKind = 'alle';
|
|
|
|
public string $pickerDimension = '';
|
|
|
|
/** Filter „Fachliche Materialart“ (Kennung), gleiche Regel wie Medieneditor und Medienfilter. */
|
|
public string $pickerMaterialart = '';
|
|
|
|
public string $pickerInspected = '';
|
|
|
|
public string $pickerSelected = '';
|
|
|
|
/** @var array<string, string> Ziel => Titel; bleibt bei Suche und Filter erhalten */
|
|
public array $pickerMarked = [];
|
|
|
|
public string $presentationRole = 'titelbild';
|
|
|
|
public bool $bindToVersion = false;
|
|
|
|
public ?string $replacingKey = null;
|
|
|
|
abstract public function pickerAllowsMany(): bool;
|
|
|
|
abstract public function closePicker(): void;
|
|
|
|
abstract public function confirmPicker(): void;
|
|
|
|
public function inspectPicker(string $target, bool $mark = false): void
|
|
{
|
|
$this->pickerInspected = $target;
|
|
|
|
if ($mark) {
|
|
$this->markPicker($target, true);
|
|
}
|
|
}
|
|
|
|
public function togglePick(string $target): void
|
|
{
|
|
$this->markPicker($target, ! isset($this->pickerMarked[$target]));
|
|
}
|
|
|
|
public function markPicker(string $target, bool $on): void
|
|
{
|
|
if (! $on) {
|
|
unset($this->pickerMarked[$target]);
|
|
|
|
if ($this->pickerSelected === $target) {
|
|
$this->pickerSelected = '';
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$row = app(EditorialCatalog::class)->rowForTarget($target, $this->pickerExceptId(), allowDraft: true);
|
|
|
|
if ($row === null) {
|
|
return;
|
|
}
|
|
|
|
if (! $this->pickerAllowsMany()) {
|
|
$this->pickerMarked = [];
|
|
$this->pickerSelected = $target;
|
|
}
|
|
|
|
$this->pickerMarked[$target] = (string) $row['title'];
|
|
|
|
if ($this->pickerInspected === '') {
|
|
$this->pickerInspected = $target;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array{items: list<array<string, mixed>>, more: bool}
|
|
*/
|
|
public function pickerResult(): array
|
|
{
|
|
if (! $this->pickerOpen) {
|
|
return ['items' => [], 'more' => false];
|
|
}
|
|
|
|
if ($this->pickerPurpose === 'aufbau') {
|
|
return $this->filterByMaterialart(app(EditorialCatalog::class)->browse(
|
|
$this->pickerQuery,
|
|
$this->pickerKind,
|
|
$this->pickerDimension,
|
|
$this->pickerExceptId(),
|
|
));
|
|
}
|
|
|
|
$result = app(EditorialCatalog::class)->presentationBrowse($this->pickerQuery, $this->pickerKind);
|
|
$result = $this->filterByMaterialart($result);
|
|
|
|
// CMS: nur Bilder, die als Website-Bild wählbar sind (keine vertraulichen Arten, nicht archiviert).
|
|
if ($this->pickerPurpose === 'website-bild') {
|
|
$ids = array_values(array_filter(array_map(fn (array $item): int => (int) ($item['element_id'] ?? 0), $result['items'])));
|
|
$elements = ContentElement::query()->with('editorialContentKind')->whereKey($ids)->get()->keyBy('id');
|
|
$result['items'] = array_values(array_filter($result['items'], function (array $item) use ($elements): bool {
|
|
$element = $elements->get((int) ($item['element_id'] ?? 0));
|
|
|
|
return $element !== null && WebsiteImage::isSelectable($element);
|
|
}));
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function pickerDetail(): ?array
|
|
{
|
|
$target = $this->pickerInspected !== '' ? $this->pickerInspected : $this->pickerSelected;
|
|
|
|
if ($target === '') {
|
|
return null;
|
|
}
|
|
|
|
$row = app(EditorialCatalog::class)->rowForTarget($target, $this->pickerExceptId(), allowDraft: true);
|
|
|
|
if ($row === null) {
|
|
return null;
|
|
}
|
|
|
|
$row['playback'] = null;
|
|
$row['file_url'] = null;
|
|
$elementId = isset($row['element_id']) ? (int) $row['element_id'] : 0;
|
|
|
|
if (preg_match('/^element:(\d+)$/', (string) $row['target'], $match) === 1) {
|
|
$version = ContentElementVersion::query()->with('element.editorialContentKind')->find($match[1]);
|
|
|
|
if ($version instanceof ContentElementVersion) {
|
|
$row['playback'] = app(ContentPreviewController::class)->playback($version);
|
|
}
|
|
}
|
|
|
|
$element = $elementId > 0 ? ContentElement::query()->with(['editorialContentKind', 'pillars'])->find($elementId) : null;
|
|
|
|
if ($element instanceof ContentElement) {
|
|
$row['visibility'] = app(PublicMaterialReader::class)->visibilityNote($element);
|
|
$row['released'] = $element->website_released_at !== null;
|
|
$row['output_label'] = PublicMaterialReader::outputLabel((string) $element->output_class);
|
|
$row['pillars'] = $element->pillars->pluck('name')->all();
|
|
$row['material'] = $element->editorialContentKind?->label;
|
|
$row['usage'] = app(CatalogReading::class)->elementUsage($element);
|
|
}
|
|
|
|
return $row;
|
|
}
|
|
|
|
/**
|
|
* @return list<array{target: string, label: string, current: bool}>
|
|
*/
|
|
public function pickerVersions(): array
|
|
{
|
|
$detail = $this->pickerDetail();
|
|
|
|
if ($detail === null) {
|
|
return [];
|
|
}
|
|
|
|
$options = app(EditorialCatalog::class)->alternatives((string) ($detail['identity'] ?? ''), $this->pickerExceptId());
|
|
|
|
if ($options === []) {
|
|
return [];
|
|
}
|
|
|
|
$current = $options[array_key_last($options)]['target'];
|
|
|
|
return array_map(fn (array $option): array => $option + ['current' => $option['target'] === $current], $options);
|
|
}
|
|
|
|
protected function pickerExceptId(): int
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
protected function resetPicker(string $purpose): void
|
|
{
|
|
$this->pickerPurpose = $purpose;
|
|
$this->pickerOpen = true;
|
|
$this->pickerQuery = '';
|
|
$this->pickerDimension = '';
|
|
$this->pickerMaterialart = '';
|
|
$this->pickerInspected = '';
|
|
$this->pickerSelected = '';
|
|
$this->pickerMarked = [];
|
|
$this->replacingKey = null;
|
|
$this->bindToVersion = false;
|
|
}
|
|
|
|
/**
|
|
* Fachliche Materialarten, nach denen der Picker Medien eingrenzen kann (Kontext Medium).
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function pickerMaterialarten(): array
|
|
{
|
|
return app(EditorialVocabulary::class)->kindFilterOptions('medium', $this->pickerMaterialart);
|
|
}
|
|
|
|
/**
|
|
* @param array{items: list<array<string, mixed>>, more: bool} $result
|
|
* @return array{items: list<array<string, mixed>>, more: bool}
|
|
*/
|
|
private function filterByMaterialart(array $result): array
|
|
{
|
|
if (! ctype_digit($this->pickerMaterialart)) {
|
|
return $result;
|
|
}
|
|
|
|
$ids = array_values(array_filter(array_map(fn (array $item): int => (int) ($item['element_id'] ?? 0), $result['items'])));
|
|
$passend = ContentElement::query()->whereKey($ids)->where('editorial_content_kind_id', (int) $this->pickerMaterialart)->pluck('id')->map(fn ($id): int => (int) $id)->all();
|
|
$result['items'] = array_values(array_filter($result['items'], fn (array $item): bool => in_array((int) ($item['element_id'] ?? 0), $passend, true)));
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Einordnungsfilter des Pickers: dieselbe Filterregel wie Medien- und Kompositionslisten (verwendete inaktive Werte
|
|
* bleiben auffindbar, ein gewählter Filter bleibt sichtbar).
|
|
*
|
|
* @return list<array{label: string, values: list<array{id: int, label: string}>}>
|
|
*/
|
|
public function pickerDimensionGroups(): array
|
|
{
|
|
return app(EditorialVocabulary::class)->dimensionFilterGroups(ctype_digit($this->pickerDimension) ? [(int) $this->pickerDimension] : []);
|
|
}
|
|
}
|