113 lines
3.2 KiB
PHP
113 lines
3.2 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\RateLimiter;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Livewire\WithFileUploads;
|
|
|
|
#[DisabledIf('tickets_disabled')]
|
|
class Create extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public string $message = '';
|
|
|
|
public string $subject = '';
|
|
|
|
public string $department = '';
|
|
|
|
public ?int $service = null;
|
|
|
|
public string $priority = '';
|
|
|
|
public array $attachments = [];
|
|
|
|
public function updatedAttachments()
|
|
{
|
|
$this->validate([
|
|
'attachments.*' => 'image|max:5120', // 5MB max per image
|
|
]);
|
|
}
|
|
|
|
public function removeAttachment($index)
|
|
{
|
|
unset($this->attachments[$index]);
|
|
$this->attachments = array_values($this->attachments);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
// Add rules for the department
|
|
$this->validate([
|
|
'department' => count((array) config('settings.ticket_departments')) > 0 ? 'required|in:' . implode(',', array_values((array) config('settings.ticket_departments'))) : '',
|
|
'service' => 'required|exists:services,id',
|
|
'subject' => 'required|string',
|
|
'message' => 'required|string',
|
|
'priority' => 'required|in:low,medium,high',
|
|
'attachments.*' => 'image|max:5120',
|
|
]);
|
|
|
|
if (RateLimiter::tooManyAttempts('create-ticket', 1)) {
|
|
$this->notify('Too many ticket creation attempts. Please try again in 60 seconds.', 'error');
|
|
|
|
return;
|
|
}
|
|
|
|
RateLimiter::increment('create-ticket', 30);
|
|
|
|
// 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),
|
|
];
|
|
}
|
|
}
|
|
|
|
$ticket = Ticket::create([
|
|
'user_id' => Auth::id(),
|
|
'department' => $this->department,
|
|
'service_id' => $this->service,
|
|
'subject' => $this->subject,
|
|
'priority' => $this->priority,
|
|
]);
|
|
|
|
$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');
|
|
|
|
$this->redirect(route('tickets.show', $ticket), true);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = Auth::user();
|
|
|
|
return view('tickets.create', [
|
|
'departments' => (array) config('settings.ticket_departments'),
|
|
'services' => $user->services()->orderBy('id', 'desc')->get(),
|
|
])->layoutData([
|
|
'title' => 'Create Ticket',
|
|
'sidebar' => true,
|
|
]);
|
|
}
|
|
}
|