authorize('jobs.view-job', $jobId); $this->jobId = $jobId; $this->taskId = $taskId; $this->taskTitle = $taskTitle; } /** * @return list */ #[Computed] public function images(): array { return app(TaskImageQueries::class)->imagesOf($this->jobId, $this->taskId); } public function add(AddTaskImage $addImage): void { $this->authorize('jobs.update-task-status', $this->jobId); $this->resetErrorBag(); $this->validate( ['photo' => ['required', 'file', 'image', 'mimes:jpg,jpeg,png,webp,gif', 'max:'.self::MAX_KILOBYTES]], [ 'photo.required' => __('jobs.tasks.images.errors.photo_required'), 'photo.file' => __('jobs.tasks.images.errors.not_image'), 'photo.image' => __('jobs.tasks.images.errors.not_image'), 'photo.mimes' => __('jobs.tasks.images.errors.not_image'), 'photo.max' => __('jobs.tasks.images.errors.too_large', ['size' => self::MAX_KILOBYTES / 1024]), ], ); /** @var TemporaryUploadedFile $photo */ $photo = $this->photo; try { $addImage->execute($this->jobId, $this->taskId, (int) auth()->id(), (string) $photo->getRealPath(), $photo->getClientOriginalName(), $this->caption); } catch (InvalidTaskImageCaption) { $this->addError('caption', __('jobs.tasks.images.errors.invalid_caption')); return; } catch (InvalidTaskImage) { $this->addError('photo', __('jobs.tasks.images.errors.not_image')); return; } catch (TaskNotFound) { $this->notFound(); return; } $photo->delete(); $this->reset('photo', 'caption'); $this->changed(__('jobs.tasks.images.added')); } public function startEdit(int $imageId): void { $this->authorize('jobs.update-task-status', $this->jobId); $image = $this->image($imageId); if ($image === null) { return; } if (! $this->isOwn($image)) { $this->message = __('jobs.tasks.images.errors.not_uploader'); return; } $this->editingImageId = $image->id; $this->editingCaption = $image->caption ?? ''; $this->resetErrorBag(); $this->js("document.getElementById('image-caption-edit-{$image->id}')?.focus()"); } public function saveEdit(ChangeTaskImageCaption $changeCaption): void { $this->authorize('jobs.update-task-status', $this->jobId); $this->resetErrorBag(); if ($this->editingImageId === null) { return; } try { $changeCaption->execute($this->jobId, $this->taskId, $this->editingImageId, (int) auth()->id(), $this->editingCaption); } catch (InvalidTaskImageCaption) { $this->addError('editingCaption', __('jobs.tasks.images.errors.invalid_caption')); return; } catch (NotTaskImageUploader) { $this->cancelEdit(); $this->message = __('jobs.tasks.images.errors.not_uploader'); return; } catch (TaskNotFound|TaskImageNotFound) { $this->reset('editingImageId', 'editingCaption'); $this->notFound(); return; } $this->cancelEdit(); $this->changed(__('jobs.tasks.images.caption_saved')); } public function cancelEdit(): void { $imageId = $this->editingImageId; $this->reset('editingImageId', 'editingCaption'); $this->resetErrorBag(); if ($imageId !== null) { $this->js("document.getElementById('image-edit-button-{$imageId}')?.focus()"); } } public function confirmRemoval(int $imageId): void { $this->authorize('jobs.update-task-status', $this->jobId); $image = $this->image($imageId); if ($image === null) { return; } if (! $this->mayRemove($image)) { $this->message = __('jobs.tasks.images.errors.not_uploader'); return; } $this->removalImageId = $image->id; $this->js("document.getElementById('remove-image-dialog-{$this->taskId}').showModal()"); } public function remove(RemoveTaskImage $removeImage): void { $this->authorize('jobs.update-task-status', $this->jobId); if ($this->removalImageId === null) { return; } $imageId = $this->removalImageId; $this->reset('removalImageId'); $this->js("document.getElementById('remove-image-dialog-{$this->taskId}').close()"); try { $removeImage->execute($this->jobId, $this->taskId, $imageId, (int) auth()->id(), Gate::allows('jobs.remove-any-task-image')); } catch (NotTaskImageUploader) { $this->message = __('jobs.tasks.images.errors.not_uploader'); return; } catch (TaskNotFound|TaskImageNotFound) { $this->notFound(); return; } if ($this->editingImageId === $imageId) { $this->reset('editingImageId', 'editingCaption'); } $this->changed(__('jobs.tasks.images.removed')); // The removed image's buttons are gone: move focus to the field for a new image. $this->js("document.getElementById('image-new-{$this->taskId}')?.focus()"); } public function isOwn(TaskImageSummary $image): bool { return $image->uploaderId !== null && $image->uploaderId === (int) auth()->id(); } public function mayRemove(TaskImageSummary $image): bool { return $this->isOwn($image) || Gate::allows('jobs.remove-any-task-image'); } /** * "25.11.2026 klo 14.05" from "2026-11-25 14:05". */ public function formatTime(string $moment): string { $time = CarbonImmutable::createFromFormat('Y-m-d H:i', $moment); return __('jobs.tasks.comments.at', ['date' => $time->format('j.n.Y'), 'time' => $time->format('H.i')]); } public function uploaderName(TaskImageSummary $image): string { return $image->uploaderName === '' ? __('jobs.tasks.comments.unknown_author') : $image->uploaderName; } /** * The caption, or which image it is, who sent it and when. */ public function altText(TaskImageSummary $image, int $number): string { return $image->caption ?? __('jobs.tasks.images.alt', [ 'number' => $number, 'uploader' => $this->uploaderName($image), 'at' => $this->formatTime($image->uploadedAt), ]); } /** * @return array{0: int, 1: int} */ public function thumbnailSize(TaskImageSummary $image): array { $ratio = min(1, 400 / max($image->width, $image->height, 1)); return [max(1, (int) round($image->width * $ratio)), max(1, (int) round($image->height * $ratio))]; } private function image(int $imageId): ?TaskImageSummary { foreach ($this->images as $image) { if ($image->id === $imageId) { return $image; } } $this->notFound(); return null; } /** * Refreshes the list and tells the job page, whose task cards show the * number of images. */ private function changed(string $message): void { unset($this->images); $this->message = $message; $this->dispatch('task-images-changed'); } private function notFound(): void { $this->changed(__('jobs.tasks.images.not_found')); } }; ?> {{-- Named by the task, so the images of a job's tasks are distinguishable landmarks. --}}
@php($canUpload = Gate::allows('jobs.update-task-status', $jobId))

{{ __('jobs.tasks.images.title') }} @if ($this->images !== []) ({{ count($this->images) }}) @endif

{{ $message }} {{ __('jobs.tasks.images.uploading') }} {{ __('jobs.tasks.images.saving') }}

@if ($this->images === [])

{{ __('jobs.tasks.images.empty') }}

@else @endif @if ($canUpload)

{{ __('jobs.tasks.images.hint', ['size' => $this::MAX_KILOBYTES / 1024]) }}

@error('photo')

{{ $message }}

@enderror

{{ __('jobs.tasks.images.remove_question') }}

@endif