96 lines
3.5 KiB
PHP
96 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\User;
|
|
use App\Notifications\AccountUpdate;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
class SupportDesk extends Component
|
|
{
|
|
public function boot(): void
|
|
{
|
|
abort_unless(auth()->check() && auth()->user()->hasVerifiedEmail(), 403);
|
|
if (auth()->user()->is_admin) {
|
|
abort_unless(auth()->user()->two_factor_confirmed_at, 403);
|
|
}
|
|
}
|
|
|
|
public string $subject = '';
|
|
|
|
public string $body = '';
|
|
|
|
public string $reply = '';
|
|
|
|
#[Locked]
|
|
public ?int $ticketId = null;
|
|
|
|
private function ticket()
|
|
{
|
|
$ticket = DB::table('tickets')->find($this->ticketId);
|
|
abort_unless($ticket && (auth()->user()->is_admin || $ticket->user_id === auth()->id()), 403);
|
|
|
|
return $ticket;
|
|
}
|
|
|
|
public function select(int $id): void
|
|
{
|
|
$this->ticketId = $id;
|
|
$this->ticket();
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$this->validate(['subject' => 'required|string|max:200', 'body' => 'required|string|min:10|max:10000']);
|
|
$this->ticketId = DB::transaction(function () {
|
|
$id = DB::table('tickets')->insertGetId(['user_id' => auth()->id(), 'subject' => $this->subject, 'status' => 'open', 'created_at' => now(), 'updated_at' => now()]);
|
|
DB::table('ticket_messages')->insert(['ticket_id' => $id, 'user_id' => auth()->id(), 'body' => $this->body, 'created_at' => now(), 'updated_at' => now()]);
|
|
|
|
return $id;
|
|
});
|
|
if (config('tarnax.support_email')) {
|
|
Notification::route('mail', config('tarnax.support_email'))->notify(new AccountUpdate('New Tarnax support ticket', $this->subject, url('/support'), 'support-update'));
|
|
}
|
|
$this->reset('subject', 'body');
|
|
}
|
|
|
|
public function send(): void
|
|
{
|
|
$ticket = $this->ticket();
|
|
$this->validate(['reply' => 'required|string|min:2|max:10000']);
|
|
DB::table('ticket_messages')->insert(['ticket_id' => $ticket->id, 'user_id' => auth()->id(), 'body' => $this->reply, 'created_at' => now(), 'updated_at' => now()]);
|
|
DB::table('tickets')->where('id', $ticket->id)->update(['status' => 'open', 'updated_at' => now()]);
|
|
$notification = new AccountUpdate('Your Tarnax support ticket has a reply', 'Sign in to read the reply.', url('/support'), 'support-update');
|
|
if (auth()->user()->is_admin) {
|
|
User::findOrFail($ticket->user_id)->notify($notification);
|
|
} elseif (config('tarnax.support_email')) {
|
|
Notification::route('mail', config('tarnax.support_email'))->notify($notification);
|
|
}
|
|
$this->reply = '';
|
|
}
|
|
|
|
public function close(): void
|
|
{
|
|
$ticket = $this->ticket();
|
|
DB::table('tickets')->where('id', $ticket->id)->update(['status' => 'closed', 'updated_at' => now()]);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$query = DB::table('tickets');
|
|
if (! auth()->user()->is_admin) {
|
|
$query->where('user_id', auth()->id());
|
|
}
|
|
$messages = collect();
|
|
if ($this->ticketId) {
|
|
$this->ticket();
|
|
$messages = DB::table('ticket_messages')->join('users', 'users.id', '=', 'ticket_messages.user_id')->where('ticket_id', $this->ticketId)->select('ticket_messages.*', 'users.name')->orderBy('ticket_messages.id')->get();
|
|
}
|
|
|
|
return view('livewire.support-desk', ['tickets' => $query->latest('updated_at')->limit(100)->get(), 'messages' => $messages]);
|
|
}
|
|
}
|