Files
laraundfelix/app/Http/Controllers/EditorialPreviewController.php
T
LarsandCursor 9195cdcd15
Deploy Test / test (push) Successful in 23s
Deploy Test / deploy (push) Successful in 22s
Zeige Katalogkarten, Titelbilder und klare Freigabestatus im Arbeitsplatz.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-16 17:54:12 +01:00

190 lines
6.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionStructureReader;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\StructureNode;
use App\Domain\Identity\StaffRole;
use App\Filament\Support\EditorialCatalog;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\StreamedResponse;
class EditorialPreviewController extends Controller
{
public function show(Composition $composition, ?CompositionVersion $version = null): View
{
$this->authorizeStaff();
if ($version === null) {
$version = $composition->versions()
->where('status', ContentVersionStatus::Bereit)
->orderByDesc('number')
->first();
}
abort_unless($version instanceof CompositionVersion, 404);
abort_unless((int) $version->composition_id === (int) $composition->id, 404);
abort_unless($version->status === ContentVersionStatus::Bereit, 404);
return $this->render(
$composition,
$version->learning_goal,
$version->duration_minutes,
$version->material_note,
$version->number,
false,
array_map(
fn (StructureNode $node): array => $this->present($node, app(EditorialCatalog::class)),
app(CompositionStructureReader::class)->tree($version),
),
);
}
public function draft(Composition $composition): View
{
$this->authorizeStaff();
$draft = $composition->working_draft;
abort_unless(is_array($draft) && ($draft['members'] ?? null) !== null, 404);
$catalog = app(EditorialCatalog::class);
$nodes = [];
foreach ($draft['members'] as $member) {
if (! is_array($member)) {
continue;
}
$row = $catalog->rowForTarget((string) ($member['target'] ?? ''), (int) $composition->id, allowSelf: true);
if ($row === null) {
continue;
}
$children = [];
if (preg_match('/^composition:(\d+)$/', (string) $row['target'], $match) === 1) {
$nested = CompositionVersion::query()->find($match[1]);
if ($nested instanceof CompositionVersion) {
$children = array_map(
fn (StructureNode $node): array => $this->present($node, $catalog),
app(CompositionStructureReader::class)->tree($nested),
);
}
}
$nodes[] = [
'role' => (string) ($member['role'] ?? $row['kind']),
'title' => $row['title'],
'kind' => $row['kind'],
'version' => $row['version_label'],
'open_url' => $row['open_url'],
'children' => $children,
];
}
return $this->render(
$composition,
$draft['learning_goal'] ?? null,
$draft['duration_minutes'] ?? null,
$draft['material_note'] ?? null,
null,
true,
$nodes,
);
}
public function card(string $kind, int $subject): StreamedResponse
{
$this->authorizeStaff();
abort_unless(in_array($kind, ['element', 'komposition'], true), 404);
$model = $kind === 'komposition'
? Composition::query()->findOrFail($subject)
: ContentElement::query()->findOrFail($subject);
$path = $model->thumbnail_path;
abort_unless(is_string($path) && str_starts_with($path, 'vorschau/') && ! str_contains($path, '..'), 404);
$disk = Storage::disk('media_derived');
abort_unless($disk->exists($path), 404);
return $disk->response($path);
}
/**
* @param list<array<string, mixed>> $nodes
*/
private function render(
Composition $composition,
mixed $learningGoal,
mixed $durationMinutes,
mixed $materialNote,
mixed $number,
bool $draft,
array $nodes,
): View {
$catalog = app(EditorialCatalog::class);
return view('filament.editorial.preview', [
'title' => $catalog->compositionTitle($composition),
'summary' => $composition->summary,
'description' => $composition->description,
'kind' => $composition->editorialContentKind?->label
?? ($composition->kind?->deskLabel()),
'learningGoal' => $learningGoal,
'durationMinutes' => $durationMinutes,
'materialNote' => $materialNote,
'number' => $number,
'draft' => $draft,
'backUrl' => $catalog->editUrl($composition),
'nodes' => $nodes,
]);
}
/**
* @return array<string, mixed>
*/
private function present(StructureNode $node, EditorialCatalog $catalog): array
{
$versionLabel = null;
$openUrl = null;
if ($node->contentElementVersionId !== null) {
$version = ContentElementVersion::query()->find($node->contentElementVersionId);
$versionLabel = $version instanceof ContentElementVersion ? 'Fassung '.$version->number : null;
$openUrl = $version instanceof ContentElementVersion ? route('content.preview', $version) : null;
}
if ($node->compositionVersionId !== null) {
$version = CompositionVersion::query()->find($node->compositionVersionId);
$versionLabel = $version instanceof CompositionVersion ? 'Fassung '.$version->number : $versionLabel;
}
return [
'role' => $node->role,
'title' => $node->title,
'kind' => $node->editorialKindLabel ?: ($node->outputClass !== null ? $catalog->outputLabel($node->outputClass) : $node->technicalKindLabel),
'version' => $versionLabel,
'open_url' => $openUrl,
'children' => array_map(
fn (StructureNode $child): array => $this->present($child, $catalog),
$node->children,
),
];
}
private function authorizeStaff(): void
{
$user = auth('staff')->user();
abort_unless(
$user instanceof User && ($user->hasStaffRole(StaffRole::Redakteur) || $user->hasStaffRole(StaffRole::Katalogverwalter)),
403,
);
}
}