pressReleaseId = $pressReleaseId; } public function upload(PressReleaseAttachmentStorage $storage): void { $pressRelease = $this->getPressRelease(); $this->authorize('update', $pressRelease); if (! $this->canChangeAttachments($pressRelease)) { $this->addError('newFile', __('Anhänge können nur bei Entwürfen oder abgelehnten PMs geändert werden.')); return; } $maxKb = (int) (PressReleaseAttachmentStorage::MAX_BYTES / 1024); $allowedExtensions = implode(',', PressReleaseAttachmentStorage::ALLOWED_EXTENSIONS); $this->validate([ 'newFile' => ['required', 'file', 'mimes:'.$allowedExtensions, 'max:'.$maxKb], 'newTitle' => ['nullable', 'string', 'max:120'], 'newDescription' => ['nullable', 'string', 'max:500'], ]); $stored = $storage->store($this->newFile, $pressRelease->id); $pressRelease->attachments()->create([ 'disk' => $stored['disk'], 'path' => $stored['path'], 'original_name' => $stored['original_name'], 'mime' => $stored['mime'], 'size' => $stored['size'], 'title' => $this->newTitle ?: null, 'description' => $this->newDescription ?: null, 'sort_order' => ((int) $pressRelease->attachments()->max('sort_order')) + 1, ]); $this->reset(['newFile', 'newTitle', 'newDescription']); Flux::toast(text: __('Anhang hochgeladen.'), variant: 'success'); } public function removeNewFile(): void { $this->reset('newFile'); $this->resetErrorBag('newFile'); } public function startEdit(int $attachmentId): void { $attachment = $this->getAttachment($attachmentId); if (! $attachment) { return; } $this->editingId = $attachment->id; $this->editTitle = $attachment->title ?? ''; $this->editDescription = $attachment->description ?? ''; } public function cancelEdit(): void { $this->reset(['editingId', 'editTitle', 'editDescription']); } public function updateAttachment(): void { $pressRelease = $this->getPressRelease(); $this->authorize('update', $pressRelease); if (! $this->canChangeAttachments($pressRelease) || $this->editingId === null) { return; } $this->validate([ 'editTitle' => ['nullable', 'string', 'max:120'], 'editDescription' => ['nullable', 'string', 'max:500'], ]); $attachment = $this->getAttachment($this->editingId); if (! $attachment) { return; } $attachment->update([ 'title' => trim($this->editTitle) ?: null, 'description' => trim($this->editDescription) ?: null, ]); $this->cancelEdit(); Flux::toast(text: __('Anhang aktualisiert.'), variant: 'success'); } public function moveUp(int $attachmentId): void { $this->swapSortOrder($attachmentId, -1); } public function moveDown(int $attachmentId): void { $this->swapSortOrder($attachmentId, 1); } public function remove(int $attachmentId, PressReleaseAttachmentStorage $storage): void { $pressRelease = $this->getPressRelease(); $this->authorize('update', $pressRelease); if (! $this->canChangeAttachments($pressRelease)) { return; } $attachment = $this->getAttachment($attachmentId); if (! $attachment) { return; } $storage->delete($attachment->disk, $attachment->path); $attachment->delete(); Flux::toast(text: __('Anhang entfernt.'), variant: 'success'); } public function with(): array { $pressRelease = $this->getPressRelease(); return [ 'attachments' => $pressRelease->attachments() ->orderBy('sort_order') ->orderBy('id') ->get(), 'canEdit' => auth()->user()?->can('update', $pressRelease) === true && $this->canChangeAttachments($pressRelease), 'maxMb' => round(PressReleaseAttachmentStorage::MAX_BYTES / 1024 / 1024), 'allowedExtensions' => PressReleaseAttachmentStorage::ALLOWED_EXTENSIONS, ]; } private function swapSortOrder(int $attachmentId, int $direction): void { $pressRelease = $this->getPressRelease(); $this->authorize('update', $pressRelease); if (! $this->canChangeAttachments($pressRelease)) { return; } $attachments = $pressRelease->attachments()->orderBy('sort_order')->orderBy('id')->get(); $currentIndex = $attachments->search(fn (PressReleaseAttachment $att) => $att->id === $attachmentId); if ($currentIndex === false) { return; } $targetIndex = $currentIndex + $direction; if ($targetIndex < 0 || $targetIndex >= $attachments->count()) { return; } $current = $attachments[$currentIndex]; $target = $attachments[$targetIndex]; $currentSort = $current->sort_order; $current->update(['sort_order' => $target->sort_order]); $target->update(['sort_order' => $currentSort]); } private function getPressRelease(): PressRelease { return PressRelease::withoutGlobalScopes() ->findOrFail($this->pressReleaseId); } private function getAttachment(int $attachmentId): ?PressReleaseAttachment { return PressReleaseAttachment::query() ->where('press_release_id', $this->pressReleaseId) ->whereKey($attachmentId) ->first(); } private function canChangeAttachments(PressRelease $pressRelease): bool { if (auth()->user()?->canAccessAdmin()) { return ! in_array($pressRelease->status, [PressReleaseStatus::Archived], true); } return in_array( $pressRelease->status, [PressReleaseStatus::Draft, PressReleaseStatus::Rejected], true, ); } }; ?>
{{ __('Anhänge / Downloads') }} — {{ count($attachments) }}/10
{{ strtoupper(implode(' · ', $allowedExtensions)) }} · max. {{ $maxMb }} MB
@if ($canEdit)
@if ($newFile) @endif
{{ __('Hochladen') }} {{ __('Lädt…') }}
{{ __('Titel (optional)') }} {{ __('Beschreibung (optional)') }}
@endif @if ($attachments->isEmpty())

{{ __('Noch keine Anhänge — Pressemappen, Factsheets oder Bildmaterial-Pakete passen hier rein.') }}

@else
@foreach ($attachments as $attachment)
@if ($editingId === $attachment->id && $canEdit) {{-- Inline-Edit-Form --}}
{{ __('Titel') }} {{ __('Beschreibung') }}
{{ __('Abbrechen') }} {{ __('Speichern') }}
@else
@php $ext = strtolower(pathinfo($attachment->original_name ?? '', PATHINFO_EXTENSION)); $iconName = match (true) { $ext === 'pdf' => 'document-text', in_array($ext, ['doc', 'docx'], true) => 'document-text', in_array($ext, ['xls', 'xlsx'], true) => 'table-cells', in_array($ext, ['ppt', 'pptx'], true) => 'presentation-chart-bar', $ext === 'zip' => 'archive-box', default => 'document', }; @endphp

{{ $attachment->title ?? $attachment->original_name }}

@if ($attachment->title)

{{ $attachment->original_name }}

@endif @if ($attachment->description)

{{ $attachment->description }}

@endif

{{ strtoupper($ext ?: '?') }} · @php $bytes = (int) $attachment->size; $sizeLabel = $bytes >= 1048576 ? number_format($bytes / 1048576, 1, ',', '.').' MB' : number_format(max(1, (int) round($bytes / 1024)), 0, ',', '.').' KB'; @endphp {{ $sizeLabel }}

@if ($attachment->url()) {{ __('Download') }} @endif @if ($canEdit) @endif
@endif
@endforeach
@endif