Files
laraundfelix/app/Filament/Support/EditorialCatalog.php
T
LarsandClaude Sonnet 5.5 bbee288e69
CI Feature / test (push) Successful in 2m34s
Pflege Übungen im Aufbauarbeitsplatz und weise Verwendung und Sperrfolgen nach
Eigener Eintrag Übungen unter Inhalte (Bestand, Anlegen, gemeinsamer Aufbau mit Varianten, Verwendung und
Variantenauswahl im Modul und Programm), Verwendungsnachweis über verschachtelte und ältere Stände,
Auswirkungsbericht einer Mediensperre, gesperrte Bestandteile verhindern die Angebotsaktivierung, Vorschaubilder
gesperrter Medien werden nicht mehr ausgeliefert, Rückfalltest gegen die Daten des neuen Stands.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 17:03:32 +01:00

1129 lines
42 KiB
PHP

<?php
namespace App\Filament\Support;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\CompositionMember;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\IdentityDimensionValue;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\OfferStatus;
use App\Domain\Cms\Catalog\PublicMaterialReader;
use App\Filament\Resources\Combinations\CombinationResource;
use App\Filament\Resources\ContentElements\ContentElementResource;
use App\Filament\Resources\Courses\CourseResource;
use App\Filament\Resources\Exercises\ExerciseResource;
use App\Filament\Resources\Practices\PracticeResource;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class EditorialCatalog
{
/**
* @return list<array<string, string|null>>
*/
public function rows(CompositionVersion $version, int $exceptCompositionId): array
{
$version->loadMissing([
'members.variant',
'members.contentElementVersion.element.editorialContentKind',
'members.nestedCompositionVersion.composition.editorialContentKind',
]);
$rows = [];
foreach ($version->members as $member) {
$target = $member->content_element_version_id
? 'element:'.$member->content_element_version_id
: 'composition:'.$member->nested_composition_version_id;
$row = $this->rowForTarget($target, $exceptCompositionId, allowSelf: true, allowDraft: true, allowBlocked: true);
if ($row === null) {
continue;
}
$row['role'] = $member->role;
$row['note'] = trim((string) ($member->placement_note ?? ''));
$row['variant'] = $member->variant?->key;
$row['requirement'] = $member->requirement ?: 'erforderlich';
$row['selection'] = is_array($member->variant_selection) ? $member->variant_selection : null;
$rows[] = $row;
}
return $rows;
}
/**
* @return array<string, string|null>|null
*/
public function rowForTarget(string $target, int $exceptCompositionId, bool $allowSelf = false, bool $allowDraft = false, bool $allowBlocked = false): ?array
{
if (preg_match('/^composition-draft:(\d+)$/', $target, $draftMatch) === 1) {
return $this->draftCompositionRow((int) $draftMatch[1], $exceptCompositionId, $allowSelf);
}
if (preg_match('/^medium:(\d+)$/', $target, $mediumMatch) === 1) {
return $this->rowForPresentationMedium((int) $mediumMatch[1]);
}
if (preg_match('/^(element|composition):(\d+)$/', $target, $match) !== 1) {
return null;
}
if ($match[1] === 'element') {
$version = ContentElementVersion::query()
->with('element.editorialContentKind')
->find($match[2]);
$acceptable = [ContentVersionStatus::Bereit];
if ($allowDraft) {
$acceptable[] = ContentVersionStatus::Entwurf;
}
if ($allowBlocked) {
$acceptable[] = ContentVersionStatus::Gesperrt;
}
if (! $version instanceof ContentElementVersion || ! in_array($version->status, $acceptable, true)) {
return null;
}
$element = $version->element;
$kind = $element->editorialContentKind?->label ?: $this->outputLabel((string) $element->output_class);
$title = filled($element->display_title) ? $element->display_title : $element->working_title;
return [
'target' => 'element:'.$version->id,
'element_id' => (string) $element->id,
'title' => $title,
'kind' => $kind,
'output' => (string) $element->output_class,
'body' => (string) ($version->body ?? ''),
'summary' => (string) ($element->summary ?: $element->description ?: ''),
'medium_description' => (string) ($element->description ?? ''),
'usage_note' => (string) ($element->usage_note ?? ''),
'license_note' => (string) ($element->license_note ?? ''),
'tone' => $this->tone((string) $element->output_class, $kind),
'image' => $this->cardUrl('element', $element->id, $element->thumbnail_path),
'version_label' => match ($version->status) {
ContentVersionStatus::Entwurf => 'Entwurf',
ContentVersionStatus::Gesperrt => 'deaktiviert',
default => 'Fassung '.$version->number,
},
'committed' => app(ContentService::class)->isCommitted($version),
'identity' => 'medium:'.$element->id,
'open_url' => route('content.preview', $version),
'open_label' => 'Medium ansehen',
'edit_url' => ContentElementResource::getUrl('view', ['record' => $element]),
];
}
$version = CompositionVersion::query()
->with('composition.editorialContentKind')
->find($match[2]);
$acceptable = [ContentVersionStatus::Bereit];
if ($allowBlocked) {
$acceptable[] = ContentVersionStatus::Gesperrt;
}
if (! $version instanceof CompositionVersion || ! in_array($version->status, $acceptable, true)) {
return null;
}
if (! $allowSelf && (int) $version->composition_id === $exceptCompositionId) {
return null;
}
$composition = $version->composition;
// Art, die dieser Programmstand nicht kennt: nicht anbieten und nicht laden (Rückfallschutz).
if ($composition === null) {
return null;
}
$kind = $composition->editorialContentKind?->label
?: ($composition->kind instanceof CompositionKind ? $composition->kind->deskLabel() : 'Zusammenstellung');
return [
'target' => 'composition:'.$version->id,
'title' => $this->compositionTitle($composition),
'kind' => $kind,
'summary' => (string) ($composition->summary ?? ''),
'tone' => $this->tone('', $kind),
'image' => $this->cardUrl('komposition', $composition->id, $composition->thumbnail_path),
'version_label' => $version->status === ContentVersionStatus::Gesperrt
? 'deaktiviert'
: 'Fassung '.$version->number,
'identity' => 'zusammenstellung:'.$composition->id,
'open_url' => route('editorial.preview', ['composition' => $composition, 'version' => $version]),
'open_label' => 'Aufbau ansehen',
'edit_url' => self::editUrl($composition),
];
}
/**
* @return array<string, string|null>|null
*/
private function draftCompositionRow(int $compositionId, int $exceptCompositionId, bool $allowSelf): ?array
{
if (! $allowSelf && $compositionId === $exceptCompositionId) {
return null;
}
$composition = Composition::query()->with('editorialContentKind')->find($compositionId);
if (! $composition instanceof Composition) {
return null;
}
$kind = $composition->editorialContentKind?->label
?: ($composition->kind instanceof CompositionKind ? $composition->kind->deskLabel() : 'Zusammenstellung');
return [
'target' => 'composition-draft:'.$composition->id,
'title' => $this->compositionTitle($composition),
'kind' => $kind,
'summary' => (string) ($composition->summary ?? ''),
'tone' => $this->tone('', $kind),
'image' => $this->cardUrl('komposition', $composition->id, $composition->thumbnail_path),
'version_label' => 'Entwurf',
'identity' => 'zusammenstellung:'.$composition->id,
'open_url' => route('editorial.draft-preview', ['composition' => $composition]),
'open_label' => 'Entwurf ansehen',
'edit_url' => self::editUrl($composition),
];
}
/**
* @return list<array<string, string|null>>
*/
public function search(string $query, string $scope, int $exceptCompositionId): array
{
$query = trim($query);
$like = '%'.str_replace(['%', '_'], ['', ''], $query).'%';
$limit = $scope === 'alles' ? 20 : 40;
$rows = [];
if ($scope !== 'module') {
$elements = ContentElementVersion::query()
->where('status', ContentVersionStatus::Bereit)
->with('element.editorialContentKind')
->whereHas('element', function ($builder) use ($query, $like): void {
if ($query === '') {
return;
}
$builder->where('working_title', 'like', $like)
->orWhere('display_title', 'like', $like)
->orWhere('teaser', 'like', $like)
->orWhere('search_terms', 'like', $like)
->orWhere('summary', 'like', $like);
})
->orderByDesc('id')
->limit($limit)
->get();
foreach ($elements as $version) {
$row = $this->rowForTarget('element:'.$version->id, $exceptCompositionId);
if ($row !== null) {
$rows[] = $row;
}
}
}
if ($scope !== 'medien') {
$versions = CompositionVersion::query()
->where('status', ContentVersionStatus::Bereit)
->where('composition_id', '!=', $exceptCompositionId)
->with('composition.editorialContentKind')
->whereHas('composition', function ($builder) use ($scope, $query, $like): void {
if ($scope === 'module') {
$builder->whereIn('kind', [CompositionKind::Uebung, CompositionKind::Exercise]);
}
if ($scope === 'uebungen') {
$builder->where('kind', CompositionKind::Exercise);
}
if ($query === '') {
return;
}
$builder->where(function ($inner) use ($like): void {
$inner->where('designation', 'like', $like)
->orWhere('display_title', 'like', $like)
->orWhere('teaser', 'like', $like)
->orWhere('search_terms', 'like', $like)
->orWhere('summary', 'like', $like);
});
})
->orderByDesc('id')
->limit($limit)
->get();
foreach ($versions as $version) {
$row = $this->rowForTarget('composition:'.$version->id, $exceptCompositionId);
if ($row !== null) {
$rows[] = $row;
}
}
}
return array_slice($rows, 0, 40);
}
/**
* @return array{items: list<array<string, string|null>>, more: bool}
*/
public function browse(string $query, string $kind, string $dimension, int $exceptCompositionId, bool $elementsOnly = false, int $pillarId = 0): array
{
$query = trim($query);
$like = '%'.str_replace(['%', '_'], ['', ''], $query).'%';
$limit = 48;
$rows = [];
$dimensionId = ctype_digit($dimension) ? (int) $dimension : 0;
if ($kind !== 'modul') {
$latestElements = ContentElementVersion::query()
->selectRaw('MAX(id)')
->whereIn('status', [ContentVersionStatus::Bereit, ContentVersionStatus::Entwurf])
->whereHas('element', function ($builder) use ($query, $like, $kind, $dimensionId, $pillarId): void {
$this->constrainElement($builder, $query, $like, $kind, $dimensionId, $pillarId);
})
->groupBy('content_element_id');
$elements = ContentElementVersion::query()
->with('element.editorialContentKind')
->whereIn('id', $latestElements)
->orderByDesc('id')
->limit($limit + 1)
->get();
foreach ($elements as $version) {
$row = $this->rowForTarget('element:'.$version->id, $exceptCompositionId, allowDraft: true);
if ($row !== null) {
$rows[] = $row;
}
}
}
if (! $elementsOnly && in_array($kind, ['alle', 'modul'], true)) {
$latestCompositions = CompositionVersion::query()
->selectRaw('MAX(id)')
->where('status', ContentVersionStatus::Bereit)
->where('composition_id', '!=', $exceptCompositionId)
->whereHas('composition', function ($builder) use ($query, $like, $kind, $dimensionId, $pillarId): void {
if ($kind === 'modul') {
$builder->whereIn('kind', [CompositionKind::Uebung, CompositionKind::Exercise]);
}
if ($dimensionId > 0) {
$builder->whereIn('id', $this->dimensionSubjectIds(Composition::class, $dimensionId));
}
if ($pillarId > 0) {
$builder->whereIn('id', $this->pillarSubjectIds(Composition::class, $pillarId));
}
if ($query === '') {
return;
}
$builder->where(function ($inner) use ($like): void {
$inner->where('designation', 'like', $like)
->orWhere('display_title', 'like', $like)
->orWhere('teaser', 'like', $like)
->orWhere('search_terms', 'like', $like)
->orWhere('summary', 'like', $like);
});
})
->groupBy('composition_id');
$versions = CompositionVersion::query()
->with('composition.editorialContentKind')
->whereIn('id', $latestCompositions)
->orderByDesc('id')
->limit($limit + 1)
->get();
foreach ($versions as $version) {
$row = $this->rowForTarget('composition:'.$version->id, $exceptCompositionId);
if ($row !== null) {
$rows[] = $row;
}
}
$unreleased = Composition::query()
->where('id', '!=', $exceptCompositionId)
->whereNotNull('working_draft')
->whereDoesntHave('versions', function ($builder): void {
$builder->where('status', ContentVersionStatus::Bereit);
})
->when($kind === 'modul', function ($builder): void {
$builder->whereIn('kind', [CompositionKind::Uebung, CompositionKind::Exercise]);
})
->orderByDesc('id')
->get();
foreach ($unreleased as $composition) {
$draft = is_array($composition->working_draft) ? $composition->working_draft : [];
$draftMembers = is_array($draft['members'] ?? null) ? $draft['members'] : [];
if ($draftMembers === []) {
continue;
}
if ($dimensionId > 0 && ! in_array($composition->id, $this->dimensionSubjectIds(Composition::class, $dimensionId), true)) {
continue;
}
if ($pillarId > 0 && ! in_array($composition->id, $this->pillarSubjectIds(Composition::class, $pillarId), true)) {
continue;
}
$title = $this->compositionTitle($composition);
if ($query !== '' && ! str_contains(mb_strtolower($title.' '.(string) ($composition->summary ?? '')), mb_strtolower($query))) {
continue;
}
$row = $this->rowForTarget('composition-draft:'.$composition->id, $exceptCompositionId);
if ($row !== null) {
$rows[] = $row;
}
}
}
$rows = $this->withVisibility($rows);
return [
'items' => array_slice($rows, 0, $limit),
'more' => count($rows) > $limit,
];
}
/**
* Bestandssuche für Präsentationsmedien.
* Fassungen bleiben im didaktischen Aufbau. Bilder, Filme und Hörstücke ohne Inhaltsfassung kommen dazu.
*
* @return array{items: list<array<string, string|null>>, more: bool}
*/
public function presentationBrowse(string $query, string $kind = 'alle'): array
{
$catalog = $this->browse($query, $kind, '', 0, elementsOnly: true);
$outputs = match ($kind) {
'bild' => ['bild'],
'film' => ['film'],
'audio' => ['audio'],
'dokument' => [],
default => ['bild', 'film', 'audio'],
};
// Reine Texte sind Lehrinhalt oder Beschreibung, keine Präsentationsmedien.
$catalog['items'] = array_values(array_filter(
$catalog['items'],
fn (array $item): bool => ($item['output'] ?? '') !== 'text',
));
$seen = [];
foreach ($catalog['items'] as $item) {
if (isset($item['element_id'])) {
$seen[(int) $item['element_id']] = true;
}
}
$query = trim($query);
$like = '%'.str_replace(['%', '_'], ['', ''], $query).'%';
$elements = ContentElement::query()
->with('editorialContentKind')
->whereIn('output_class', $outputs)
->whereNull('archived_at')
->whereDoesntHave('versions')
->where(function ($builder): void {
$builder->whereNull('editorial_content_kind_id')
->orWhereDoesntHave('editorialContentKind', function ($kind): void {
$kind->where('slug', 'traineranleitung');
});
})
->when($query !== '', function ($builder) use ($like): void {
$builder->where(function ($inner) use ($like): void {
$inner->where('working_title', 'like', $like)
->orWhere('display_title', 'like', $like)
->orWhere('teaser', 'like', $like)
->orWhere('search_terms', 'like', $like)
->orWhere('summary', 'like', $like);
});
})
->orderBy('working_title')
->limit(48)
->get();
foreach ($elements as $element) {
if (isset($seen[$element->id])) {
continue;
}
$row = $this->rowForPresentationMedium($element->id);
if ($row !== null) {
$catalog['items'][] = $row;
}
}
return $catalog;
}
/**
* @return array<string, string|null>|null
*/
private function rowForPresentationMedium(int $id): ?array
{
$element = ContentElement::query()->with('editorialContentKind')->find($id);
if (! $element instanceof ContentElement
|| $element->archived_at !== null
|| ! in_array($element->output_class, ['bild', 'film', 'audio'], true)
|| $element->editorialContentKind?->slug === 'traineranleitung') {
return null;
}
$kind = $element->editorialContentKind?->label ?: $this->outputLabel((string) $element->output_class);
$title = filled($element->display_title) ? $element->display_title : $element->working_title;
return [
'target' => 'medium:'.$element->id,
'element_id' => (string) $element->id,
'title' => $title,
'kind' => $kind,
'output' => (string) $element->output_class,
'body' => '',
'summary' => (string) ($element->summary ?: $element->description ?: ''),
'medium_description' => (string) ($element->description ?? ''),
'usage_note' => (string) ($element->usage_note ?? ''),
'license_note' => (string) ($element->license_note ?? ''),
'tone' => $this->tone((string) $element->output_class, $kind),
'image' => $element->output_class === 'bild'
? $this->cardUrl('element', $element->id, $element->thumbnail_path)
: null,
'version_label' => 'Ohne Inhaltsfassung',
'committed' => false,
'identity' => 'medium:'.$element->id,
'open_url' => ContentElementResource::getUrl('view', ['record' => $element]),
'open_label' => 'Medium ansehen',
'edit_url' => ContentElementResource::getUrl('view', ['record' => $element]),
];
}
public function tone(string $outputClass, string $kindLabel): string
{
$value = mb_strtolower($outputClass.' '.$kindLabel);
return match (true) {
str_contains($value, 'film') => 'film',
str_contains($value, 'hör'), str_contains($value, 'audio') => 'audio',
str_contains($value, 'dokument'), str_contains($value, 'pdf') => 'dokument',
str_contains($value, 'modul'), str_contains($value, 'programm') => 'modul',
default => 'text',
};
}
public function cardUrl(string $kind, int $id, ?string $path): ?string
{
if (! is_string($path) || ! str_starts_with($path, 'vorschau/') || str_contains($path, '..')) {
return null;
}
return route('editorial.card', ['kind' => $kind, 'subject' => $id]);
}
/**
* @param list<array<string, mixed>> $rows
* @return list<array<string, mixed>>
*/
private function withVisibility(array $rows): array
{
$ids = [];
foreach ($rows as $row) {
if (isset($row['element_id'])) {
$ids[] = (int) $row['element_id'];
}
}
if ($ids === []) {
return $rows;
}
$reader = app(PublicMaterialReader::class);
$elements = ContentElement::query()
->with('editorialContentKind')
->whereIn('id', array_values(array_unique($ids)))
->get()
->keyBy('id');
foreach ($rows as $index => $row) {
$element = $elements->get((int) ($row['element_id'] ?? 0));
if ($element instanceof ContentElement) {
$rows[$index]['visibility'] = $reader->visibilityNote($element);
}
}
return $rows;
}
/**
* @param Builder<ContentElement> $builder
*/
private function constrainElement($builder, string $query, string $like, string $kind, int $dimensionId, int $pillarId = 0): void
{
$outputs = match ($kind) {
'film' => ['film'],
'text' => ['text'],
'bild' => ['bild'],
'audio' => ['audio'],
'dokument' => ['pdf', 'document'],
default => null,
};
if ($outputs !== null) {
$builder->whereIn('output_class', $outputs);
}
if ($dimensionId > 0) {
$builder->whereIn('id', $this->dimensionSubjectIds(ContentElement::class, $dimensionId));
}
if ($pillarId > 0) {
$builder->whereIn('id', $this->pillarSubjectIds(ContentElement::class, $pillarId));
}
if ($query === '') {
return;
}
$builder->where(function ($inner) use ($like): void {
$inner->where('working_title', 'like', $like)
->orWhere('display_title', 'like', $like)
->orWhere('teaser', 'like', $like)
->orWhere('search_terms', 'like', $like)
->orWhere('summary', 'like', $like)
->orWhere('description', 'like', $like);
});
}
/**
* Direkt einer Säule zugeordnete Medien oder Module und Programme (zentrale Säulenverwaltung).
*
* @param class-string<Model> $class
* @return list<int>
*/
private function pillarSubjectIds(string $class, int $pillarId): array
{
[$table, $column] = $class === Composition::class
? ['composition_pillar', 'composition_id']
: ['content_element_pillar', 'content_element_id'];
return DB::table($table)->where('pillar_id', $pillarId)->pluck($column)
->map(fn (mixed $id): int => (int) $id)->all();
}
/**
* @param class-string<Model> $class
* @return list<int>
*/
private function dimensionSubjectIds(string $class, int $dimensionId): array
{
return IdentityDimensionValue::query()
->where('subject_type', (new $class)->getMorphClass())
->where('editorial_dimension_value_id', $dimensionId)
->where('origin', 'direkt')
->pluck('subject_id')
->map(fn (mixed $id): int => (int) $id)
->all();
}
/**
* @return list<array{target: string, label: string}>
*/
public function alternatives(string $identity, int $exceptCompositionId): array
{
if (preg_match('/^(medium|zusammenstellung):(\d+)$/', $identity, $match) !== 1) {
return [];
}
$options = [];
if ($match[1] === 'medium') {
$versions = ContentElementVersion::query()
->where('content_element_id', $match[2])
->whereIn('status', [ContentVersionStatus::Bereit, ContentVersionStatus::Entwurf])
->orderBy('number')
->get();
foreach ($versions as $version) {
$options[] = [
'target' => 'element:'.$version->id,
'label' => $version->status === ContentVersionStatus::Entwurf ? 'Entwurf' : 'Fassung '.$version->number,
];
}
return $options;
}
if ((int) $match[2] === $exceptCompositionId) {
return [];
}
$versions = CompositionVersion::query()
->where('composition_id', $match[2])
->where('status', ContentVersionStatus::Bereit)
->orderBy('number')
->get();
foreach ($versions as $version) {
$options[] = [
'target' => 'composition:'.$version->id,
'label' => 'Fassung '.$version->number,
];
}
return $options;
}
public function defaultRole(string $kind): string
{
$kind = mb_strtolower($kind);
return match (true) {
str_contains($kind, 'film') => 'Film',
str_contains($kind, 'hör') || str_contains($kind, 'audio') => 'Hörstück',
str_contains($kind, 'modul') => 'Modul',
str_contains($kind, 'programm') => 'Programm',
str_contains($kind, 'text'), str_contains($kind, 'dokument'), str_contains($kind, 'merkblatt'), str_contains($kind, 'unterlage') => 'Unterlage',
default => 'Bestandteil',
};
}
/**
* @return list<string>
*/
public function usage(CompositionVersion $version): array
{
$lines = [];
$parents = CompositionMember::query()
->where('nested_composition_version_id', $version->id)
->with('compositionVersion.composition')
->get();
foreach ($parents as $member) {
$parent = $member->compositionVersion?->composition;
$name = $parent instanceof Composition ? $this->compositionTitle($parent) : 'Zusammenstellung';
$lines[] = $name.', Rolle '.$member->role;
}
$articles = ArticleRelease::query()
->where('root_composition_version_id', $version->id)
->with('article')
->get();
foreach ($articles as $release) {
$lines[] = 'Lizenzartikel '.$release->article->code.', Stand '.$release->number;
}
return $lines;
}
/**
* @return array{text: string, number: int|null, released_number: int|null}
*/
public function stand(Composition $composition): array
{
$ready = $composition->versions
->filter(fn (CompositionVersion $version): bool => $version->status === ContentVersionStatus::Bereit)
->sortByDesc('number')
->values();
$latest = $ready->first();
$release = app(CompositionDisplayRelease::class);
$released = $ready->first(fn (CompositionVersion $version): bool => $release->isReleased($version));
$draft = is_array($composition->working_draft) && $composition->working_draft !== [];
if (! $latest instanceof CompositionVersion) {
return [
'text' => $draft ? 'Entwurf gespeichert' : 'Noch ohne Fassung',
'number' => null,
'released_number' => $released instanceof CompositionVersion ? $released->number : null,
];
}
$visible = $released instanceof CompositionVersion;
$text = 'Fassung '.$latest->number.($visible ? ', in der Mitgliederbibliothek' : ', nicht in der Mitgliederbibliothek');
$text = ($draft ? 'Entwurf gespeichert. ' : '').$text;
if ($visible && $released->number !== $latest->number) {
$text .= '. Angezeigt wird Fassung '.$released->number;
}
return [
'text' => $text,
'number' => $latest->number,
'released_number' => $visible ? $released->number : null,
];
}
/**
* @return list<array{label: string, tone: string}>
*/
public function statusChips(Composition $composition, bool $offered): array
{
$stand = $this->stand($composition);
$chips = [];
if ($stand['number'] === null) {
$chips[] = [
'label' => str_contains($stand['text'], 'Entwurf') ? 'Entwurf gespeichert' : 'Noch ohne Fassung',
'tone' => 'yellow',
];
} else {
$chips[] = ['label' => 'Fassung '.$stand['number'], 'tone' => 'ink'];
if (str_starts_with($stand['text'], 'Entwurf gespeichert')) {
$chips[] = ['label' => 'Entwurf offen', 'tone' => 'yellow'];
}
// Zeigt die Bibliothek eine ältere Fassung, nennt der Chip sie; „In der Mitgliederbibliothek“ neben
// „Fassung 2“ läse sich sonst, als wäre Fassung 2 sichtbar.
$chips[] = match (true) {
$stand['released_number'] === null => ['label' => 'Nicht in der Mitgliederbibliothek', 'tone' => 'quiet'],
$stand['released_number'] === $stand['number'] => ['label' => 'In der Mitgliederbibliothek', 'tone' => 'pine'],
default => ['label' => 'Bibliothek zeigt Fassung '.$stand['released_number'], 'tone' => 'pine'],
};
}
$chips[] = ['label' => 'Nicht auf der Website', 'tone' => 'quiet'];
$chips[] = $offered
? ['label' => 'Angebot aktiv', 'tone' => 'pine']
: ['label' => 'Kein aktives Angebot', 'tone' => 'quiet'];
return $chips;
}
/**
* @return list<int>
*/
public function offeredCompositionIds(): array
{
$articleIds = DB::table('resolved_scopes')
->join('offer_versions', 'offer_versions.id', '=', 'resolved_scopes.offer_version_id')
->where('offer_versions.status', OfferStatus::Aktiv->value)
->pluck('resolved_scopes.license_article_id');
if ($articleIds->isEmpty()) {
return [];
}
$releaseIds = LicenseArticle::query()
->whereIn('id', $articleIds)
->whereNotNull('current_release_id')
->pluck('current_release_id');
$versionIds = ArticleRelease::query()
->whereIn('id', $releaseIds)
->whereNotNull('root_composition_version_id')
->pluck('root_composition_version_id');
return CompositionVersion::query()
->whereIn('id', $versionIds)
->pluck('composition_id')
->map(fn (mixed $id): int => (int) $id)
->unique()
->values()
->all();
}
/**
* @param list<array<string, mixed>> $members
* @return list<array{id: int, title: string, image: string}>
*/
public function filmPictures(array $members): array
{
$versionIds = [];
foreach ($members as $member) {
$target = (string) ($member['target'] ?? '');
if (preg_match('/^element:(\d+)$/', $target, $match) === 1) {
$versionIds[] = (int) $match[1];
}
}
if ($versionIds === []) {
return [];
}
$pictures = [];
foreach (ContentElementVersion::query()->with('element')->whereIn('id', $versionIds)->get() as $version) {
$element = $version->element;
if (! $element instanceof ContentElement || $element->output_class !== 'film') {
continue;
}
$image = $this->cardUrl('element', $element->id, $element->thumbnail_path);
if ($image === null || isset($pictures[$element->id])) {
continue;
}
$pictures[$element->id] = [
'id' => $element->id,
'title' => filled($element->display_title) ? $element->display_title : $element->working_title,
'image' => $image,
];
}
return array_values($pictures);
}
/**
* @return list<array{id: int, title: string, image: string, kind: string}>
*/
public function mediaPictures(string $query): array
{
$query = trim($query);
if (mb_strlen($query) < 2) {
return [];
}
$like = '%'.str_replace(['%', '_'], ['', ''], $query).'%';
return ContentElement::query()
->where('thumbnail_path', 'like', 'vorschau/%')
->where(function ($builder) use ($like): void {
$builder->where('working_title', 'like', $like)
->orWhere('display_title', 'like', $like)
->orWhere('teaser', 'like', $like)
->orWhere('search_terms', 'like', $like)
->orWhere('summary', 'like', $like);
})
->orderBy('working_title')
->limit(12)
->get()
->map(function (ContentElement $element): ?array {
$image = $this->cardUrl('element', $element->id, $element->thumbnail_path);
if ($image === null) {
return null;
}
return [
'id' => $element->id,
'title' => filled($element->display_title) ? $element->display_title : $element->working_title,
'image' => $image,
'kind' => $element->editorialContentKind?->label ?: $this->outputLabel((string) $element->output_class),
];
})
->filter()
->values()
->all();
}
/**
* @param list<array<string, mixed>> $members
* @return array{url: ?string, note: ?string, own: bool}
*/
public function cover(Composition $composition, array $members): array
{
$own = $this->cardUrl('komposition', $composition->id, $composition->thumbnail_path);
if ($own !== null) {
return ['url' => $own, 'note' => null, 'own' => true];
}
$picture = $this->suggestPicture($members);
if ($picture === null) {
return ['url' => null, 'note' => null, 'own' => false];
}
return [
'url' => $this->cardUrl($picture['kind'], $picture['subject'], 'vorschau/gesetzt'),
'note' => $picture['note'],
'own' => false,
];
}
/**
* Das eigene Titelbild, sonst das erste Vorschaubild aus dem freigegebenen Aufbau.
*
* @return array{kind: string, subject: int, alt: string, note: string}|null
*/
public function visiblePicture(Composition $composition, ?CompositionVersion $version): ?array
{
if ($this->cardUrl('komposition', $composition->id, $composition->thumbnail_path) !== null) {
return [
'kind' => 'komposition',
'subject' => $composition->id,
'alt' => filled($composition->thumbnail_alt) ? (string) $composition->thumbnail_alt : $this->compositionTitle($composition),
'note' => '',
];
}
if ($version === null) {
return null;
}
$version->loadMissing('members');
$members = [];
foreach ($version->members as $member) {
$members[] = [
'target' => $member->content_element_version_id
? 'element:'.$member->content_element_version_id
: 'composition:'.$member->nested_composition_version_id,
];
}
return $this->suggestPicture($members);
}
/**
* @param list<array<string, mixed>> $members
* @param list<int> $seen
* @return array{kind: string, subject: int, alt: string, note: string}|null
*/
private function suggestPicture(array $members, array $seen = []): ?array
{
foreach ($members as $member) {
$target = (string) ($member['target'] ?? '');
if (preg_match('/^element:(\d+)$/', $target, $match) === 1) {
$version = ContentElementVersion::query()->with('element')->find((int) $match[1]);
$element = $version?->element;
if (! $element instanceof ContentElement || $this->cardUrl('element', $element->id, $element->thumbnail_path) === null) {
continue;
}
$title = filled($element->display_title) ? $element->display_title : $element->working_title;
return [
'kind' => 'element',
'subject' => $element->id,
'alt' => filled($element->thumbnail_alt) ? (string) $element->thumbnail_alt : $title,
'note' => $element->output_class === 'film'
? 'Vorschlag aus dem Film '.$title
: 'Vorschlag aus '.$title,
];
}
if (preg_match('/^composition:(\d+)$/', $target, $match) !== 1 || in_array((int) $match[1], $seen, true)) {
continue;
}
$version = CompositionVersion::query()->with('composition', 'members')->find((int) $match[1]);
if (! $version instanceof CompositionVersion) {
continue;
}
$nested = $version->composition;
if ($nested === null) {
continue;
}
if ($this->cardUrl('komposition', $nested->id, $nested->thumbnail_path) !== null) {
return [
'kind' => 'komposition',
'subject' => $nested->id,
'alt' => filled($nested->thumbnail_alt) ? (string) $nested->thumbnail_alt : $this->compositionTitle($nested),
'note' => 'Vorschlag aus '.$this->compositionTitle($nested),
];
}
$children = [];
foreach ($version->members as $child) {
$children[] = [
'target' => $child->content_element_version_id
? 'element:'.$child->content_element_version_id
: 'composition:'.$child->nested_composition_version_id,
];
}
$picture = $this->suggestPicture($children, [...$seen, $version->id]);
if ($picture !== null) {
return $picture;
}
}
return null;
}
public function create(string $designation, CompositionKind $kind): Composition
{
$composition = app(ContentService::class)->createComposition($designation, $kind);
$slug = match ($kind) {
CompositionKind::Kurs => 'programm',
CompositionKind::Uebung => 'modul',
CompositionKind::Exercise => 'uebung',
default => null,
};
$editorial = $slug === null ? null : EditorialContentKind::query()->where('slug', $slug)->first();
if ($editorial !== null) {
$composition->editorial_content_kind_id = $editorial->id;
$composition->save();
}
return $composition;
}
public static function editUrl(Composition $composition): string
{
$kind = $composition->kind instanceof CompositionKind
? $composition->kind
: CompositionKind::from((string) $composition->kind);
$resource = match ($kind) {
CompositionKind::Kurs => CourseResource::class,
CompositionKind::Kombination => CombinationResource::class,
CompositionKind::Uebung => ExerciseResource::class,
CompositionKind::Exercise => PracticeResource::class,
};
return $resource::getUrl('edit', ['record' => $composition]);
}
public function compositionTitle(Composition $composition): string
{
return filled($composition->display_title) ? $composition->display_title : $composition->designation;
}
public function outputLabel(string $outputClass): string
{
return match ($outputClass) {
'film' => 'Film',
'audio' => 'Hörstück',
'bild' => 'Bild',
'document', 'pdf' => 'Dokument',
default => 'Text',
};
}
}