Der Medieneditor trennt Beschreibung, Inhalt, Fassungen und Verwendung. Der vorhandene Picker filtert nach Zusammenhang und lässt Entwürfe für die gemeinsame Freigabe sichtbar. Co-authored-by: Cursor <cursoragent@cursor.com>
571 lines
20 KiB
PHP
571 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Support;
|
|
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\CompositionKind;
|
|
use App\Domain\Catalog\CompositionMember;
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use App\Domain\Catalog\EditorialDimension;
|
|
use App\Domain\Catalog\IdentityDimensionValue;
|
|
use App\Domain\Catalog\Pillar;
|
|
use App\Domain\Catalog\ReleaseContent;
|
|
use App\Domain\Media\ThumbnailRef;
|
|
use App\Filament\Resources\ContentElements\ContentElementResource;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Str;
|
|
|
|
class MediaLibrary
|
|
{
|
|
public function __construct(private readonly EditorialCatalog $catalog) {}
|
|
|
|
/**
|
|
* @return list<array{value: string, label: string}>
|
|
*/
|
|
public function types(): array
|
|
{
|
|
return [
|
|
['value' => 'film', 'label' => 'Film'],
|
|
['value' => 'text', 'label' => 'Text'],
|
|
['value' => 'document', 'label' => 'Dokument'],
|
|
['value' => 'bild', 'label' => 'Bild'],
|
|
['value' => 'audio', 'label' => 'Hörstück'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return list<array{id: int, name: string}>
|
|
*/
|
|
public function pillars(): array
|
|
{
|
|
return Pillar::query()
|
|
->orderBy('name')
|
|
->get(['id', 'name'])
|
|
->map(fn (Pillar $pillar): array => ['id' => $pillar->id, 'name' => $pillar->name])
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @return list<array{id: int, label: string}>
|
|
*/
|
|
public function classifications(): array
|
|
{
|
|
$options = [];
|
|
|
|
foreach (EditorialDimension::query()->with('values')->orderBy('label')->get() as $dimension) {
|
|
foreach ($dimension->values->sortBy('label') as $value) {
|
|
$options[] = [
|
|
'id' => $value->id,
|
|
'label' => $dimension->label.': '.$value->label,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $filters
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function cardFor(ContentElement $element, bool $canEdit): array
|
|
{
|
|
$element->loadMissing(['versions', 'pillars']);
|
|
$versionIds = $element->versions->pluck('id')->all();
|
|
|
|
return $this->card(
|
|
$element,
|
|
$this->portrait($element),
|
|
$this->placements($versionIds)[$element->id] ?? [],
|
|
$this->licenses($versionIds)[$element->id] ?? [],
|
|
$canEdit,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $filters
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function cards(array $filters, bool $canEdit): array
|
|
{
|
|
$filters = $this->normalize($filters);
|
|
$elements = ContentElement::query()
|
|
->with(['versions', 'pillars'])
|
|
->orderBy('working_title')
|
|
->get();
|
|
$versionIds = $elements->flatMap(fn (ContentElement $element) => $element->versions->pluck('id'))->all();
|
|
$placements = $this->placements($versionIds);
|
|
$licenses = $this->licenses($versionIds);
|
|
$classified = $this->classifiedIds();
|
|
$needle = mb_strtolower($filters['suche']);
|
|
$cards = [];
|
|
|
|
foreach ($elements as $element) {
|
|
$portrait = $this->portrait($element);
|
|
$uses = $placements[$element->id] ?? [];
|
|
$codes = $licenses[$element->id] ?? [];
|
|
|
|
if (! $this->matches($element, $portrait, $uses, $filters, $needle, $classified)) {
|
|
continue;
|
|
}
|
|
|
|
$cards[] = $this->card($element, $portrait, $uses, $codes, $canEdit);
|
|
}
|
|
|
|
usort($cards, fn (array $left, array $right): int => strcasecmp(
|
|
mb_strtolower($left['title']),
|
|
mb_strtolower($right['title']),
|
|
));
|
|
|
|
return $cards;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $filters
|
|
* @return array<string, string>
|
|
*/
|
|
private function normalize(array $filters): array
|
|
{
|
|
return [
|
|
'suche' => trim($filters['suche'] ?? ''),
|
|
'art' => $this->choice($filters['art'] ?? '', ['film', 'text', 'document', 'bild', 'audio']),
|
|
'freigabe' => $this->choice($filters['freigabe'] ?? '', ['freigegeben', 'entwurf', 'ohne']),
|
|
'entwurf' => $this->choice($filters['entwurf'] ?? '', ['offen', 'keiner']),
|
|
'fassung' => $this->choice($filters['fassung'] ?? '', ['mit', 'nur']),
|
|
'nutzung' => $this->choice($filters['nutzung'] ?? '', ['verwendet', 'unbenutzt']),
|
|
'saeule' => ctype_digit($filters['saeule'] ?? '') ? $filters['saeule'] : '',
|
|
'einordnung' => ctype_digit($filters['einordnung'] ?? '') ? $filters['einordnung'] : '',
|
|
'verarbeitung' => $this->choice($filters['verarbeitung'] ?? '', ['fehler', 'uebernahme']),
|
|
'archiv' => $this->choice($filters['archiv'] ?? '', ['aktiv', 'archiviert', 'alle'], 'aktiv'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $allowed
|
|
*/
|
|
private function choice(string $value, array $allowed, string $fallback = ''): string
|
|
{
|
|
return in_array($value, $allowed, true) ? $value : $fallback;
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $versionIds
|
|
* @return array<int, list<array<string, mixed>>>
|
|
*/
|
|
private function placements(array $versionIds): array
|
|
{
|
|
if ($versionIds === []) {
|
|
return [];
|
|
}
|
|
|
|
$grouped = [];
|
|
$members = CompositionMember::query()
|
|
->whereIn('content_element_version_id', $versionIds)
|
|
->with(['compositionVersion.composition', 'contentElementVersion'])
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
foreach ($members as $member) {
|
|
$version = $member->contentElementVersion;
|
|
$compositionVersion = $member->compositionVersion;
|
|
$composition = $compositionVersion?->composition;
|
|
|
|
if (! $version instanceof ContentElementVersion || $compositionVersion === null || ! $composition instanceof Composition) {
|
|
continue;
|
|
}
|
|
|
|
$elementId = $version->content_element_id;
|
|
$key = $composition->id.'-'.$compositionVersion->id.'-'.$version->id;
|
|
$role = trim((string) $member->role);
|
|
|
|
if (! isset($grouped[$elementId][$key])) {
|
|
$grouped[$elementId][$key] = [
|
|
'compositionId' => $composition->id,
|
|
'name' => $this->catalog->compositionTitle($composition),
|
|
'type' => $this->kindLabel($composition),
|
|
'mediumVersion' => $this->versionLabel($version),
|
|
'hostVersion' => 'Fassung '.$compositionVersion->number,
|
|
'url' => EditorialCatalog::editUrl($composition),
|
|
'roles' => [],
|
|
];
|
|
}
|
|
|
|
if ($role !== '' && ! in_array($role, $grouped[$elementId][$key]['roles'], true)) {
|
|
$grouped[$elementId][$key]['roles'][] = $role;
|
|
}
|
|
}
|
|
|
|
$placements = [];
|
|
|
|
foreach ($grouped as $elementId => $rows) {
|
|
$list = array_values($rows);
|
|
usort($list, fn (array $left, array $right): int => [$left['type'], $left['name'], $left['hostVersion']]
|
|
<=> [$right['type'], $right['name'], $right['hostVersion']]);
|
|
$placements[$elementId] = $list;
|
|
}
|
|
|
|
return $placements;
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $versionIds
|
|
* @return array<int, list<string>>
|
|
*/
|
|
private function licenses(array $versionIds): array
|
|
{
|
|
if ($versionIds === []) {
|
|
return [];
|
|
}
|
|
|
|
$codes = [];
|
|
$contents = ReleaseContent::query()
|
|
->whereIn('content_element_version_id', $versionIds)
|
|
->with(['release.article', 'elementVersion'])
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
foreach ($contents as $content) {
|
|
$version = $content->elementVersion;
|
|
$code = $content->release?->article?->code;
|
|
|
|
if (! $version instanceof ContentElementVersion || ! is_string($code) || $code === '') {
|
|
continue;
|
|
}
|
|
|
|
$codes[$version->content_element_id][$code] = $code;
|
|
}
|
|
|
|
return array_map(fn (array $row): array => array_values($row), $codes);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, list<int>>
|
|
*/
|
|
private function classifiedIds(): array
|
|
{
|
|
$map = [];
|
|
$rows = IdentityDimensionValue::query()
|
|
->where('subject_type', (new ContentElement)->getMorphClass())
|
|
->where('origin', 'direkt')
|
|
->get(['subject_id', 'editorial_dimension_value_id']);
|
|
|
|
foreach ($rows as $row) {
|
|
$map[(int) $row->subject_id][] = (int) $row->editorial_dimension_value_id;
|
|
}
|
|
|
|
return $map;
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* ready: ?ContentElementVersion,
|
|
* draft: ?ContentElementVersion,
|
|
* blocked: ?ContentElementVersion,
|
|
* versionCount: int,
|
|
* archived: bool,
|
|
* processing: ?string,
|
|
* stand: string,
|
|
* standTone: string,
|
|
* draftNote: ?string,
|
|
* blockedNote: ?string
|
|
* }
|
|
*/
|
|
private function portrait(ContentElement $element): array
|
|
{
|
|
/** @var Collection<int, ContentElementVersion> $versions */
|
|
$versions = $element->versions->sortByDesc('number')->values();
|
|
$ready = $versions->first(fn (ContentElementVersion $version): bool => $version->status === ContentVersionStatus::Bereit);
|
|
$draft = $versions->first(fn (ContentElementVersion $version): bool => $version->status === ContentVersionStatus::Entwurf);
|
|
$blocked = $versions->first(fn (ContentElementVersion $version): bool => $version->status === ContentVersionStatus::Gesperrt);
|
|
$processing = $this->processing($versions);
|
|
|
|
if ($ready instanceof ContentElementVersion) {
|
|
$stand = 'Fassung '.$ready->number.' freigegeben';
|
|
$tone = 'ready';
|
|
} elseif ($draft instanceof ContentElementVersion) {
|
|
$stand = 'Entwurf, noch nicht freigegeben';
|
|
$tone = 'draft';
|
|
} elseif ($blocked instanceof ContentElementVersion) {
|
|
$stand = 'Fassung '.$blocked->number.' deaktiviert';
|
|
$tone = 'blocked';
|
|
} else {
|
|
$stand = 'Noch keine Fassung';
|
|
$tone = 'quiet';
|
|
}
|
|
|
|
return [
|
|
'ready' => $ready instanceof ContentElementVersion ? $ready : null,
|
|
'draft' => $draft instanceof ContentElementVersion ? $draft : null,
|
|
'blocked' => $blocked instanceof ContentElementVersion ? $blocked : null,
|
|
'versionCount' => $versions->count(),
|
|
'archived' => $element->archived_at !== null,
|
|
'processing' => $processing,
|
|
'stand' => $stand,
|
|
'standTone' => $tone,
|
|
'draftNote' => $ready instanceof ContentElementVersion && $draft instanceof ContentElementVersion
|
|
? 'Entwurf offen'
|
|
: null,
|
|
'blockedNote' => $blocked instanceof ContentElementVersion && ($ready instanceof ContentElementVersion || $draft instanceof ContentElementVersion)
|
|
? 'Fassung '.$blocked->number.' deaktiviert'
|
|
: null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, ContentElementVersion> $versions
|
|
*/
|
|
private function processing(Collection $versions): ?string
|
|
{
|
|
foreach ($versions as $version) {
|
|
$meta = is_array($version->metadata) ? $version->metadata : [];
|
|
|
|
if (is_string($meta['error'] ?? null) && trim($meta['error']) !== '') {
|
|
return 'Verarbeitungsfehler';
|
|
}
|
|
}
|
|
|
|
foreach ($versions as $version) {
|
|
$meta = is_array($version->metadata) ? $version->metadata : [];
|
|
$incoming = $meta['incoming'] ?? null;
|
|
$hasIncoming = (is_string($incoming) && $incoming !== '') || (is_array($incoming) && $incoming !== []);
|
|
|
|
if ($hasIncoming && ! is_string($meta['file'] ?? null)) {
|
|
return 'Datei wird übernommen';
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $portrait
|
|
* @param list<array<string, mixed>> $uses
|
|
* @param array<string, string> $filters
|
|
* @param array<int, list<int>> $classified
|
|
*/
|
|
private function matches(ContentElement $element, array $portrait, array $uses, array $filters, string $needle, array $classified): bool
|
|
{
|
|
if ($needle !== '' && ! str_contains($this->haystack($element), $needle)) {
|
|
return false;
|
|
}
|
|
|
|
if (! $this->matchesArt($element, $filters['art'])) {
|
|
return false;
|
|
}
|
|
|
|
$onlyBlocked = $portrait['ready'] === null && $portrait['draft'] === null && $portrait['blocked'] !== null;
|
|
|
|
if ($onlyBlocked && $filters['fassung'] === '' && $filters['archiv'] === 'aktiv') {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['archiv'] === 'aktiv' && $portrait['archived']) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['archiv'] === 'archiviert' && ! $portrait['archived']) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['freigabe'] === 'freigegeben' && $portrait['ready'] === null) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['freigabe'] === 'entwurf' && ($portrait['draft'] === null || $portrait['ready'] !== null)) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['freigabe'] === 'ohne' && $portrait['ready'] !== null) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['entwurf'] === 'offen' && $portrait['draft'] === null) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['entwurf'] === 'keiner' && $portrait['draft'] !== null) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['fassung'] === 'mit' && $portrait['blocked'] === null) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['fassung'] === 'nur' && ($portrait['blocked'] === null || $portrait['ready'] !== null || $portrait['draft'] !== null)) {
|
|
return false;
|
|
}
|
|
|
|
$used = $this->usageCount($uses) > 0;
|
|
|
|
if ($filters['nutzung'] === 'verwendet' && ! $used) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['nutzung'] === 'unbenutzt' && $used) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['saeule'] !== '' && ! $element->pillars->contains('id', (int) $filters['saeule'])) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['einordnung'] !== '' && ! in_array((int) $filters['einordnung'], $classified[$element->id] ?? [], true)) {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['verarbeitung'] === 'fehler' && $portrait['processing'] !== 'Verarbeitungsfehler') {
|
|
return false;
|
|
}
|
|
|
|
if ($filters['verarbeitung'] === 'uebernahme' && $portrait['processing'] !== 'Datei wird übernommen') {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private function matchesArt(ContentElement $element, string $art): bool
|
|
{
|
|
if ($art === '') {
|
|
return true;
|
|
}
|
|
|
|
$class = (string) $element->output_class;
|
|
|
|
if ($art === 'document') {
|
|
return in_array($class, ['document', 'pdf'], true);
|
|
}
|
|
|
|
return $class === $art;
|
|
}
|
|
|
|
private function haystack(ContentElement $element): string
|
|
{
|
|
return mb_strtolower(implode("\n", array_filter([
|
|
$element->working_title,
|
|
$element->display_title,
|
|
$element->description,
|
|
$element->summary,
|
|
$element->usage_note,
|
|
], fn (mixed $value): bool => is_string($value) && trim($value) !== '')));
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $portrait
|
|
* @param list<array<string, mixed>> $uses
|
|
* @param list<string> $codes
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function card(ContentElement $element, array $portrait, array $uses, array $codes, bool $canEdit): array
|
|
{
|
|
$description = trim((string) ($element->description ?: $element->summary ?: ''));
|
|
$usageNote = trim((string) $element->usage_note);
|
|
$licenseNote = trim((string) $element->license_note);
|
|
$typeKey = $this->typeKey((string) $element->output_class);
|
|
$count = $this->usageCount($uses);
|
|
|
|
return [
|
|
'id' => $element->id,
|
|
'title' => $element->working_title,
|
|
'type' => $this->catalog->outputLabel((string) $element->output_class),
|
|
'typeKey' => $typeKey,
|
|
'description' => $description === '' ? null : $description,
|
|
'usageNote' => $usageNote === '' ? null : $usageNote,
|
|
'licenseNote' => $licenseNote === '' ? null : $licenseNote,
|
|
'image' => ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path),
|
|
'imageAlt' => trim((string) ($element->thumbnail_alt ?: $element->working_title)),
|
|
'imageNote' => $this->imageNote($element),
|
|
'stand' => $portrait['stand'],
|
|
'standTone' => $portrait['standTone'],
|
|
'draftNote' => $portrait['draftNote'],
|
|
'blockedNote' => $portrait['blockedNote'],
|
|
'processing' => $portrait['processing'],
|
|
'archived' => $portrait['archived'],
|
|
'versionCount' => $portrait['versionCount'],
|
|
'versionLabel' => $this->versionCountLabel($portrait['versionCount']),
|
|
'usageCount' => $count,
|
|
'usageLabel' => $count === 0
|
|
? 'In keinem Modul oder Programm verwendet'
|
|
: ($count === 1 ? '1 Verwendung' : $count.' Verwendungen'),
|
|
'usages' => $uses,
|
|
'licenseText' => $this->licenseText($codes),
|
|
'url' => ContentElementResource::getUrl('view', ['record' => $element]),
|
|
'editUrl' => $canEdit ? ContentElementResource::getUrl('edit', ['record' => $element]) : null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<array<string, mixed>> $uses
|
|
*/
|
|
private function usageCount(array $uses): int
|
|
{
|
|
return count(array_unique(array_column($uses, 'compositionId')));
|
|
}
|
|
|
|
private function versionCountLabel(int $count): ?string
|
|
{
|
|
return match ($count) {
|
|
0 => null,
|
|
1 => '1 Fassung',
|
|
default => $count.' Fassungen',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $codes
|
|
*/
|
|
private function licenseText(array $codes): ?string
|
|
{
|
|
if ($codes === []) {
|
|
return null;
|
|
}
|
|
|
|
$named = count($codes) === 1
|
|
? 'Im Lizenzartikel '.$codes[0].' genannt.'
|
|
: 'In den Lizenzartikeln '.implode(', ', $codes).' genannt.';
|
|
|
|
return $named.' Das belegt keinen Zugriff in der Bibliothek.';
|
|
}
|
|
|
|
private function imageNote(ContentElement $element): ?string
|
|
{
|
|
if (is_string($element->thumbnail_error) && trim($element->thumbnail_error) !== '') {
|
|
return Str::limit(trim($element->thumbnail_error), 180);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function typeKey(string $outputClass): string
|
|
{
|
|
return match ($outputClass) {
|
|
'film' => 'film',
|
|
'audio' => 'audio',
|
|
'bild' => 'bild',
|
|
'document', 'pdf' => 'document',
|
|
default => 'text',
|
|
};
|
|
}
|
|
|
|
private function kindLabel(Composition $composition): string
|
|
{
|
|
$kind = $composition->kind;
|
|
|
|
return ($kind instanceof CompositionKind ? $kind : CompositionKind::from((string) $kind))->deskLabel();
|
|
}
|
|
|
|
private function versionLabel(ContentElementVersion $version): string
|
|
{
|
|
return match ($version->status) {
|
|
ContentVersionStatus::Entwurf => 'Entwurf',
|
|
ContentVersionStatus::Gesperrt => 'Fassung '.$version->number.', deaktiviert',
|
|
default => 'Fassung '.$version->number,
|
|
};
|
|
}
|
|
}
|