Files
Virtnet.bond/app/Livewire/Tickets/Show.php

95 lines
2.4 KiB
PHP

<?php
namespace App\Livewire\Tickets;
use App\Attributes\DisabledIf;
use App\Livewire\Component;
use App\Models\Ticket;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\WithFileUploads;
#[DisabledIf('tickets_disabled')]
class Show extends Component
{
use WithFileUploads;
#[Locked]
public Ticket $ticket;
#[Rule('required', 'string')]
public string $message = '';
#[Rule(['attachments.*' => 'image|max:5120'])] // 5MB max per image
public array $attachments = [];
public function updatedAttachments()
{
$this->validateOnly('attachments.*');
}
public function removeAttachment($index)
{
unset($this->attachments[$index]);
$this->attachments = array_values($this->attachments);
}
public function uploadImage()
{
$this->validate([
'attachments.*' => 'image|max:5120',
]);
$uploadedPaths = [];
foreach ($this->attachments as $attachment) {
$path = $attachment->store('ticket-attachments', 'public');
$uploadedPaths[] = [
'path' => $path,
'name' => $attachment->getClientOriginalName(),
'url' => Storage::url($path),
];
}
return $uploadedPaths;
}
public function save()
{
$this->validate();
// Handle attachments
$attachmentData = [];
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
$path = $attachment->store('ticket-attachments', 'public');
$attachmentData[] = [
'path' => $path,
'name' => $attachment->getClientOriginalName(),
'url' => Storage::url($path),
];
}
}
$this->ticket->messages()->create([
'user_id' => Auth::id(),
'message' => $this->message,
'attachments' => !empty($attachmentData) ? $attachmentData : null,
]);
$this->notify('Message sent successfully');
$this->message = '';
$this->attachments = [];
$this->dispatch('saved');
}
public function render()
{
return view('tickets.show')->layoutData([
'sidebar' => true,
]);
}
}