Files
laraundfelix/app/Filament/Support/CatalogReading.php
LarsandCursor af85a94cd5
Deploy Test / test (push) Successful in 15s
Deploy Test / deploy (push) Successful in 22s
Zeige Medienfassungen in der Verwaltung und halte Beschreibungen davon getrennt.
Eine Fassung bleibt nummeriert und kann deaktiviert werden. Beschreibung und fachliche Säulen gehören zum Medium und legen keine neue Fassung an.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-15 11:25:08 +01:00

473 lines
15 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Support;
use App\Domain\Catalog\Bundle;
use App\Domain\Catalog\BundleService;
use App\Domain\Catalog\BundleStatus;
use App\Domain\Catalog\BundleVersion;
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\ContentResolver;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferStatus;
use App\Domain\Catalog\OfferVersion;
use App\Domain\Catalog\ReleaseContent;
use App\Domain\Catalog\ResolvedScope;
use App\Domain\Catalog\RightsProfile;
use App\Domain\Catalog\RightsProfileVersion;
use App\Domain\Catalog\StatusValue;
class CatalogReading
{
/**
* @return list<string>
*/
public function articleStand(LicenseArticle $article): array
{
$release = $article->currentRelease()->with(['contents.elementVersion.element'])->first();
if ($release === null) {
return ['Noch kein Stand veröffentlicht. Ein Lizenzartikel ohne Stand vergibt keine Inhalte.'];
}
$lines = [
'Stand '.$release->number.' · '.$this->status($release->status).' · '.$release->classification_note,
];
foreach ($release->contents as $content) {
$title = $content->elementVersion?->element?->working_title ?? 'Inhalt fehlt';
$lines[] = $content->path.' — '.$title;
}
if ($release->contents->isEmpty()) {
$lines[] = 'Dieser Stand hat keine festgeschriebenen Inhalte.';
}
return $lines;
}
/**
* @return list<string>
*/
public function articleContext(LicenseArticle $article): array
{
$lines = [];
foreach (BundleVersion::query()->where('status', BundleStatus::Veroeffentlicht)->with('bundle')->orderBy('id')->get() as $version) {
try {
$ids = app(BundleService::class)->articleIds($version);
} catch (DomainRuleException $exception) {
continue;
}
if (in_array($article->id, $ids, true)) {
$lines[] = 'Paket '.$version->bundle->name.' · Fassung '.$version->number;
}
}
$scopes = ResolvedScope::query()
->where('license_article_id', $article->id)
->with(['offerVersion.offer', 'rights.profile'])
->orderBy('id')
->get();
foreach ($scopes as $scope) {
$version = $scope->offerVersion;
$lines[] = 'Angebot '.$version->offer->name
.' · Fassung '.$version->number
.' · '.$this->status($version->status)
.' · '.$this->grant($version)
.' · '.$scope->rights->profile->name
.' ('.$this->capabilities($scope->rights).')';
}
return $lines === []
? ['Dieser Artikel ist noch in keinem veröffentlichten Paket und in keiner Angebotsfassung aufgelöst.']
: $lines;
}
/**
* @return list<string>
*/
public function bundleParts(Bundle $bundle): array
{
$version = $this->publishedBundle($bundle);
if ($version === null) {
return ['Noch keine veröffentlichte Fassung. Bestandteile werden mit der Fassung festgeschrieben.'];
}
$lines = ['Fassung '.$version->number];
foreach ($version->items as $item) {
if ($item->article !== null) {
$lines[] = 'Lizenzartikel '.$item->article->code;
continue;
}
$lines[] = 'Paket '.($item->nestedBundle->name ?? 'fehlt');
}
$older = $bundle->versions()->where('status', BundleStatus::Veroeffentlicht)->count() - 1;
if ($older > 0) {
$lines[] = $older.' ältere Fassung'.($older === 1 ? '' : 'en').' bleibt unverändert.';
}
return $lines;
}
/**
* @return list<string>
*/
public function bundleArticles(Bundle $bundle): array
{
$version = $this->publishedBundle($bundle);
if ($version === null) {
return ['Ohne Fassung gibt es nichts aufzulösen.'];
}
try {
$ids = app(BundleService::class)->articleIds($version);
} catch (DomainRuleException $exception) {
return [$exception->getMessage()];
}
$codes = LicenseArticle::query()->whereIn('id', $ids)->orderBy('code')->pluck('code')->all();
return $codes === []
? ['Die Fassung löst keinen Lizenzartikel auf.']
: $codes;
}
/**
* @return list<string>
*/
public function offerPositions(Offer $offer): array
{
$version = $this->activeOffer($offer);
if ($version === null) {
return ['Noch keine aktive Fassung. Ein Angebot ohne Fassung kann niemand erwerben.'];
}
$lines = [
'Fassung '.$version->number.' · '.$this->status($version->status).' · '.$this->grant($version),
];
foreach ($version->lines as $line) {
$target = $line->article !== null
? 'Lizenzartikel '.$line->article->code
: 'Paket '.($line->bundleVersion->bundle->name ?? 'fehlt').' · Fassung '.($line->bundleVersion->number ?? '–');
$lines[] = $target.' · Rechte '.$line->rights->profile->name;
}
return $lines;
}
/**
* @return list<string>
*/
public function offerGrants(Offer $offer): array
{
$version = $this->activeOffer($offer);
if ($version === null) {
return ['Der Leistungsumfang wird erst beim Aktivieren aufgelöst.'];
}
$lines = [];
foreach ($version->resolvedScopes as $scope) {
$lines[] = $scope->article->code.' · '.$scope->rights->profile->name.' · '.$this->capabilities($scope->rights);
}
return $lines === []
? ['Diese Fassung hat keinen aufgelösten Leistungsumfang.']
: $lines;
}
/**
* @return list<string>
*/
public function rights(RightsProfile $profile): array
{
$version = $profile->versions()->orderByDesc('number')->first();
if ($version === null) {
return ['Noch keine Fassung.'];
}
return [
'Fassung '.$version->number.' · '.$this->purpose($version->purpose),
$this->capabilities($version),
];
}
/**
* @return list<string>
*/
public function elementVersions(ContentElement $element): array
{
$versions = $element->versions()->orderByDesc('number')->get();
if ($versions->isEmpty()) {
return ['Noch keine Fassung.'];
}
return $versions->flatMap(function ($version): array {
$note = filled($version->change_note) ? ' · '.$version->change_note : '';
$lines = ['Fassung '.$version->number.' · '.$this->status($version->status).$note];
$metadata = is_array($version->metadata) ? $version->metadata : [];
if (filled($version->body)) {
$lines[] = $version->body;
}
if (is_string($metadata['file'] ?? null) && $metadata['file'] !== '') {
$lines[] = 'Datei: '.basename($metadata['file']);
} elseif (is_string($metadata['incoming'] ?? null) || is_array($metadata['incoming'] ?? null)) {
$lines[] = 'Die Datei wird noch übernommen.';
}
if (is_numeric($metadata['decision_seconds'] ?? null)) {
$lines[] = 'Entscheidungsmarke bei '.(int) $metadata['decision_seconds'].' Sekunden';
}
if (is_string($metadata['error'] ?? null) && $metadata['error'] !== '') {
$lines[] = $metadata['error'];
}
return $lines;
})->all();
}
/**
* @return list<string>
*/
public function elementUsage(ContentElement $element): array
{
$versionIds = $element->versions()->pluck('id');
$lines = [];
$members = CompositionMember::query()
->whereIn('content_element_version_id', $versionIds)
->with(['compositionVersion.composition', 'contentElementVersion'])
->orderBy('id')
->get();
foreach ($members as $member) {
$lines[] = self::compositionLabel($member->compositionVersion->composition)
.' · Fassung '.$member->compositionVersion->number
.' · Rolle '.$member->role;
}
$contents = ReleaseContent::query()
->whereIn('content_element_version_id', $versionIds)
->with(['release.article', 'elementVersion'])
->orderBy('id')
->get();
foreach ($contents as $content) {
$lines[] = 'Lizenzartikel '.$content->release->article->code
.' · Stand '.$content->release->number
.' · '.$content->path;
}
return $lines === []
? ['Noch in keiner Komposition und in keinem Artikel-Stand verwendet.']
: $lines;
}
/**
* @return list<string>
*/
public function compositionMembers(Composition $composition): array
{
$version = $composition->versions()
->where('status', ContentVersionStatus::Bereit)
->orderByDesc('number')
->with(['members.contentElementVersion.element', 'members.nestedCompositionVersion.composition'])
->first();
if ($version === null) {
return ['Noch keine bestätigte Fassung.'];
}
$lines = ['Fassung '.$version->number];
foreach ($version->members as $member) {
if ($member->contentElementVersion !== null) {
$lines[] = $member->role.' — '.$member->contentElementVersion->element->working_title;
continue;
}
$nested = $member->nestedCompositionVersion;
$lines[] = $member->role.' — '.($nested === null ? 'fehlt' : self::compositionLabel($nested->composition));
}
return $lines;
}
/**
* @return list<string>
*/
public function memberPillars(Composition $composition): array
{
$version = $composition->versions()
->where('status', ContentVersionStatus::Bereit)
->orderByDesc('number')
->first();
if ($version === null) {
return ['Noch keine Fassung, deshalb keine Säulen.'];
}
$rows = app(ContentResolver::class)->expand($version);
$versions = ContentElementVersion::query()
->whereIn('id', array_column($rows, 'version_id'))
->with('element.pillars')
->get()
->keyBy('id');
$lines = [];
foreach ($rows as $row) {
$element = $versions[$row['version_id']]->element;
$names = $element->pillars->pluck('name');
$lines[] = $element->working_title.' — '.($names->isEmpty() ? 'keiner Säule zugeordnet' : $names->join(', '));
}
return $lines === [] ? ['Diese Fassung enthält keine Medien.'] : $lines;
}
/**
* @return list<string>
*/
public function compositionUsage(Composition $composition): array
{
$versionIds = $composition->versions()->pluck('id');
$lines = [];
$parents = CompositionMember::query()
->whereIn('nested_composition_version_id', $versionIds)
->with('compositionVersion.composition')
->orderBy('id')
->get();
foreach ($parents as $member) {
$lines[] = self::compositionLabel($member->compositionVersion->composition)
.' · Fassung '.$member->compositionVersion->number
.' · Rolle '.$member->role;
}
$articles = LicenseArticle::query()
->whereHas('releases', function ($query) use ($versionIds): void {
$query->whereIn('root_composition_version_id', $versionIds);
})
->orderBy('code')
->pluck('code');
foreach ($articles as $code) {
$lines[] = 'Wurzel des Lizenzartikels '.$code;
}
return $lines === []
? ['Noch nicht in einer anderen Komposition und nicht als Wurzel eines Artikels verwendet.']
: $lines;
}
private function publishedBundle(Bundle $bundle): ?BundleVersion
{
return $bundle->versions()
->where('status', BundleStatus::Veroeffentlicht)
->orderByDesc('number')
->with(['items.article', 'items.nestedBundle'])
->first();
}
private function activeOffer(Offer $offer): ?OfferVersion
{
return $offer->versions()
->where('status', OfferStatus::Aktiv)
->orderByDesc('number')
->with([
'lines.article',
'lines.bundleVersion.bundle',
'lines.rights.profile',
'resolvedScopes.article',
'resolvedScopes.rights.profile',
])
->first();
}
private function grant(OfferVersion $version): string
{
$kind = match ($version->grant_kind) {
'kauf' => 'Kauf',
'auf_abruf' => 'Auf Abruf',
'bei_registrierung' => 'Bei Registrierung',
default => (string) $version->grant_kind,
};
if ($version->price_cents === null) {
return $kind;
}
return $kind.' · '.number_format($version->price_cents / 100, 2, ',', '.').' €';
}
private function capabilities(RightsProfileVersion $version): string
{
$parts = [];
foreach ($version->capabilities ?? [] as $class => $actions) {
$actions = is_array($actions) ? implode(', ', $actions) : (string) $actions;
$parts[] = $class.': '.$actions;
}
return $parts === [] ? 'keine Fähigkeiten' : implode('; ', $parts);
}
private function purpose(string $purpose): string
{
return match ($purpose) {
'ehrenamtlich' => 'Ehrenamtlich',
'kommerziell' => 'Kommerziell',
'institutionell' => 'Institutionell',
default => $purpose,
};
}
private static function compositionLabel(Composition $composition): string
{
$kind = $composition->kind;
$label = $kind instanceof CompositionKind ? $kind->label() : 'Zusammenstellung';
return $label.': '.$composition->designation;
}
private function status(mixed $status): string
{
return match (StatusValue::of($status)) {
'veroeffentlicht' => 'veröffentlicht',
'abgeloest_verfuegbar' => 'abgelöst, weiterhin verfügbar',
'gesperrt' => 'gesperrt',
'bereit' => 'bereit',
'entwurf' => 'Entwurf',
'aktiv' => 'aktiv',
'beendet' => 'beendet',
default => StatusValue::of($status) ?? 'unbekannt',
};
}
}