Add images to tasks
Everyone who may comment on a task sends images to it, e.g. photos taken with a phone. The server stores an upright, at most 2560 px JPEG without EXIF data (no location) and a 400 px thumbnail on the private disk, served only to those who see the job. Uploaders change captions (also the alt text) and remove their own images; admin and management may remove anyone's (jobs.remove-any-task-image). Removing a task also removes its image files. Livewire's temporary upload limit is raised to PHP's 40 MB so that the page's own 12 MB limit gives a translated message. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,464 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskImageQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskImageSummary;
|
||||
use App\Modules\Jobs\Application\UseCases\AddTaskImage;
|
||||
use App\Modules\Jobs\Application\UseCases\ChangeTaskImageCaption;
|
||||
use App\Modules\Jobs\Application\UseCases\RemoveTaskImage;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImage;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImageCaption;
|
||||
use App\Modules\Jobs\Domain\Exceptions\NotTaskImageUploader;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskImageNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
/**
|
||||
* A task's images on the job page (spec 8.2, K26), e.g. photos taken with
|
||||
* a phone: everyone who may comment on the task sends images; uploaders
|
||||
* change their captions and remove them, admin and management remove
|
||||
* anyone's. The server stores a downscaled copy without metadata.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
/**
|
||||
* The largest accepted upload in kilobytes.
|
||||
*/
|
||||
public const int MAX_KILOBYTES = 12288;
|
||||
|
||||
#[Locked]
|
||||
public int $jobId;
|
||||
|
||||
#[Locked]
|
||||
public int $taskId;
|
||||
|
||||
#[Locked]
|
||||
public string $taskTitle = '';
|
||||
|
||||
/**
|
||||
* @var TemporaryUploadedFile|null
|
||||
*/
|
||||
public $photo = null;
|
||||
|
||||
public string $caption = '';
|
||||
|
||||
/**
|
||||
* The image whose caption is being changed, if any.
|
||||
*/
|
||||
#[Locked]
|
||||
public ?int $editingImageId = null;
|
||||
|
||||
public string $editingCaption = '';
|
||||
|
||||
/**
|
||||
* The image the remove dialog asks about.
|
||||
*/
|
||||
#[Locked]
|
||||
public ?int $removalImageId = null;
|
||||
|
||||
public string $message = '';
|
||||
|
||||
public function mount(int $jobId, int $taskId, string $taskTitle): void
|
||||
{
|
||||
$this->authorize('jobs.view-job', $jobId);
|
||||
|
||||
$this->jobId = $jobId;
|
||||
$this->taskId = $taskId;
|
||||
$this->taskTitle = $taskTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<TaskImageSummary>
|
||||
*/
|
||||
#[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. --}}
|
||||
<section aria-label="{{ __('jobs.tasks.images.list_of', ['title' => $taskTitle]) }}" class="space-y-2">
|
||||
@php($canUpload = Gate::allows('jobs.update-task-status', $jobId))
|
||||
|
||||
<h4 class="font-medium">
|
||||
{{ __('jobs.tasks.images.title') }}
|
||||
@if ($this->images !== [])
|
||||
<span class="font-normal">({{ count($this->images) }})</span>
|
||||
@endif
|
||||
</h4>
|
||||
|
||||
<p role="status" aria-live="polite" class="text-sm">
|
||||
<span wire:loading.remove wire:target="photo, add">{{ $message }}</span>
|
||||
<span wire:loading wire:target="photo">{{ __('jobs.tasks.images.uploading') }}</span>
|
||||
<span wire:loading wire:target="add">{{ __('jobs.tasks.images.saving') }}</span>
|
||||
</p>
|
||||
|
||||
@if ($this->images === [])
|
||||
<p class="text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.images.empty') }}</p>
|
||||
@else
|
||||
<ul class="grid grid-cols-1 gap-4 min-[360px]:grid-cols-2 lg:grid-cols-3">
|
||||
@foreach ($this->images as $index => $image)
|
||||
@php($alt = $this->altText($image, $index + 1))
|
||||
@php([$thumbWidth, $thumbHeight] = $this->thumbnailSize($image))
|
||||
@php($fullUrl = route('jobs.tasks.images.show', [$jobId, $taskId, $image->id, 'full']))
|
||||
<li wire:key="task-image-{{ $image->id }}" class="min-w-0 space-y-2">
|
||||
<figure class="space-y-1">
|
||||
<a
|
||||
href="{{ $fullUrl }}"
|
||||
target="_blank"
|
||||
rel="noopener"
|
||||
class="block rounded-md focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:focus-visible:outline-blue-400"
|
||||
>
|
||||
<img
|
||||
src="{{ route('jobs.tasks.images.show', [$jobId, $taskId, $image->id, 'thumbnail']) }}"
|
||||
alt="{{ $alt }}"
|
||||
width="{{ $thumbWidth }}"
|
||||
height="{{ $thumbHeight }}"
|
||||
loading="lazy"
|
||||
class="h-auto w-full rounded-md border border-zinc-200 bg-zinc-100 dark:border-zinc-700 dark:bg-zinc-800"
|
||||
/>
|
||||
<span class="sr-only">{{ __('jobs.tasks.images.opens_in_new_tab') }}</span>
|
||||
</a>
|
||||
<figcaption class="text-sm break-words">
|
||||
@if ($image->caption !== null)
|
||||
<span class="block">{{ $image->caption }}</span>
|
||||
@endif
|
||||
<span class="block text-zinc-600 dark:text-zinc-300">
|
||||
{{ $this->uploaderName($image) }}, <time datetime="{{ str_replace(' ', 'T', $image->uploadedAt) }}">{{ $this->formatTime($image->uploadedAt) }}</time>
|
||||
</span>
|
||||
</figcaption>
|
||||
</figure>
|
||||
|
||||
@if ($editingImageId === $image->id)
|
||||
<form wire:submit="saveEdit" class="space-y-2">
|
||||
<x-input id="image-caption-edit-{{ $image->id }}" wire:model="editingCaption" :label="__('jobs.tasks.images.caption')" maxlength="255" />
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<x-button submit sm icon="check" :text="__('jobs.tasks.images.save')" />
|
||||
<x-button wire:click="cancelEdit" subtle color="black" sm :text="__('jobs.tasks.images.cancel')" />
|
||||
</div>
|
||||
</form>
|
||||
@else
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<a
|
||||
href="{{ $fullUrl }}?download=1"
|
||||
aria-label="{{ __('jobs.tasks.images.download_named', ['image' => $alt]) }}"
|
||||
class="inline-flex min-h-6 items-center rounded-md px-2 py-1 text-sm font-medium text-zinc-900 underline hover:no-underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:text-zinc-100 dark:focus-visible:outline-blue-400"
|
||||
>{{ __('jobs.tasks.images.download') }}</a>
|
||||
|
||||
@if ($canUpload && $this->isOwn($image))
|
||||
<x-button
|
||||
id="image-edit-button-{{ $image->id }}"
|
||||
wire:click="startEdit({{ $image->id }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="pencil"
|
||||
:text="__('jobs.tasks.images.edit_caption')"
|
||||
:aria-label="__('jobs.tasks.images.edit_caption_named', ['image' => $alt])"
|
||||
/>
|
||||
@endif
|
||||
@if ($canUpload && $this->mayRemove($image))
|
||||
<x-button
|
||||
wire:click="confirmRemoval({{ $image->id }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="trash"
|
||||
:text="__('jobs.tasks.images.remove')"
|
||||
:aria-label="__('jobs.tasks.images.remove_named', ['image' => $alt])"
|
||||
/>
|
||||
@endif
|
||||
</div>
|
||||
@endif
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@endif
|
||||
|
||||
@if ($canUpload)
|
||||
<form wire:submit="add" class="space-y-3">
|
||||
<div class="space-y-1">
|
||||
<label for="image-new-{{ $taskId }}" class="block text-sm font-semibold text-zinc-700 dark:text-zinc-300">{{ __('jobs.tasks.images.new') }}</label>
|
||||
<p id="image-new-{{ $taskId }}-hint" class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.images.hint', ['size' => $this::MAX_KILOBYTES / 1024]) }}</p>
|
||||
<input
|
||||
type="file"
|
||||
id="image-new-{{ $taskId }}"
|
||||
wire:model="photo"
|
||||
accept="image/jpeg,image/png,image/webp,image/gif"
|
||||
aria-describedby="image-new-{{ $taskId }}-hint @error('photo') image-new-{{ $taskId }}-error @enderror"
|
||||
@error('photo') aria-invalid="true" @enderror
|
||||
class="block w-full max-w-md rounded-md text-sm text-zinc-900 file:me-3 file:min-h-9 file:cursor-pointer file:rounded-md file:border-0 file:bg-zinc-200 file:px-3 file:py-2 file:font-medium file:text-zinc-900 hover:file:bg-zinc-300 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:text-zinc-100 dark:file:bg-zinc-700 dark:file:text-zinc-100 dark:hover:file:bg-zinc-600 dark:focus-visible:outline-blue-400"
|
||||
/>
|
||||
@error('photo')
|
||||
<p id="image-new-{{ $taskId }}-error" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="max-w-md">
|
||||
<x-input id="image-caption-new-{{ $taskId }}" wire:model="caption" :label="__('jobs.tasks.images.caption_optional')" maxlength="255" />
|
||||
</div>
|
||||
|
||||
<x-button
|
||||
submit
|
||||
sm
|
||||
icon="photo"
|
||||
:text="__('jobs.tasks.images.add')"
|
||||
:aria-label="__('jobs.tasks.images.add_named', ['title' => $taskTitle])"
|
||||
wire:loading.attr="disabled"
|
||||
wire:target="photo, add"
|
||||
/>
|
||||
</form>
|
||||
|
||||
<x-confirm-dialog id="remove-image-dialog-{{ $taskId }}" :title="__('jobs.tasks.images.remove_title')">
|
||||
<p>{{ __('jobs.tasks.images.remove_question') }}</p>
|
||||
|
||||
<x-slot:actions>
|
||||
<x-button id="remove-image-confirm-{{ $taskId }}" wire:click="remove" color="red" icon="trash" :text="__('jobs.tasks.images.remove')" />
|
||||
</x-slot:actions>
|
||||
</x-confirm-dialog>
|
||||
@endif
|
||||
</section>
|
||||
Reference in New Issue
Block a user