2022-08-01 16:52:16 +00:00
< ? php
namespace App\Http\Controllers ;
use App\Models\Server ;
2023-01-05 17:01:42 +00:00
use App\Models\Ticket ;
2022-08-12 09:01:51 +00:00
use App\Models\TicketBlacklist ;
2023-01-05 17:01:42 +00:00
use App\Models\TicketCategory ;
use App\Models\TicketComment ;
use App\Models\User ;
2022-08-01 16:52:16 +00:00
use App\Notifications\Ticket\Admin\AdminCreateNotification ;
use App\Notifications\Ticket\Admin\AdminReplyNotification ;
2023-01-05 17:01:42 +00:00
use App\Notifications\Ticket\User\CreateNotification ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Notification ;
use Illuminate\Support\Str ;
2022-08-01 16:52:16 +00:00
class TicketsController extends Controller
{
public function index ()
{
2023-01-05 17:01:42 +00:00
$tickets = Ticket :: where ( 'user_id' , Auth :: user () -> id ) -> paginate ( 10 );
2022-08-01 16:52:16 +00:00
$ticketcategories = TicketCategory :: all ();
2023-01-05 17:01:42 +00:00
return view ( 'ticket.index' , compact ( 'tickets' , 'ticketcategories' ));
2022-08-01 16:52:16 +00:00
}
2023-01-05 17:01:42 +00:00
public function create ()
{
//check in blacklist
2022-08-12 09:01:51 +00:00
$check = TicketBlacklist :: where ( 'user_id' , Auth :: user () -> id ) -> first ();
2023-01-05 17:01:42 +00:00
if ( $check && $check -> status == 'True' ) {
return redirect () -> route ( 'ticket.index' ) -> with ( 'error' , __ ( " You can't make a ticket because you're on the blacklist for a reason: ' " . $check -> reason . " ', please contact the administrator " ));
2022-08-12 09:01:51 +00:00
}
2022-08-01 16:52:16 +00:00
$ticketcategories = TicketCategory :: all ();
$servers = Auth :: user () -> servers ;
2023-01-05 17:01:42 +00:00
return view ( 'ticket.create' , compact ( 'ticketcategories' , 'servers' ));
2022-08-01 16:52:16 +00:00
}
2023-01-05 17:01:42 +00:00
public function store ( Request $request )
{
$this -> validate ( $request , [
'title' => 'required' ,
'ticketcategory' => 'required' ,
'priority' => 'required' ,
'message' => 'required' , ]
);
$ticket = new Ticket ([
'title' => $request -> input ( 'title' ),
'user_id' => Auth :: user () -> id ,
2023-01-31 09:16:45 +00:00
'ticket_id' => strtoupper ( Str :: random ( 8 )),
2023-01-05 17:01:42 +00:00
'ticketcategory_id' => $request -> input ( 'ticketcategory' ),
'priority' => $request -> input ( 'priority' ),
'message' => $request -> input ( 'message' ),
'status' => 'Open' ,
'server' => $request -> input ( 'server' ), ]
);
2022-08-01 16:52:16 +00:00
$ticket -> save ();
$user = Auth :: user ();
2023-01-13 18:32:32 +00:00
if ( config ( 'SETTINGS::TICKET:NOTIFY' ) == " all " ){ $admin = User :: where ( 'role' , 'admin' ) -> orWhere ( 'role' , 'mod' ) -> get ();}
if ( config ( 'SETTINGS::TICKET:NOTIFY' ) == " admin " ){ $admin = User :: where ( 'role' , 'admin' ) -> get ();}
if ( config ( 'SETTINGS::TICKET:NOTIFY' ) == " moderator " ){ $admin = User :: where ( 'role' , 'mod' ) -> get ();}
2022-08-01 16:52:16 +00:00
$user -> notify ( new CreateNotification ( $ticket ));
2023-01-13 18:32:32 +00:00
if ( config ( 'SETTINGS::TICKET:NOTIFY' ) != " none " ){
Notification :: send ( $admin , new AdminCreateNotification ( $ticket , $user ));
}
2023-01-05 17:01:42 +00:00
return redirect () -> route ( 'ticket.index' ) -> with ( 'success' , __ ( 'A ticket has been opened, ID: #' ) . $ticket -> ticket_id );
2022-08-01 16:52:16 +00:00
}
2023-01-05 17:01:42 +00:00
public function show ( $ticket_id )
{
$ticket = Ticket :: where ( 'ticket_id' , $ticket_id ) -> firstOrFail ();
2022-08-01 16:52:16 +00:00
$ticketcomments = $ticket -> ticketcomments ;
$ticketcategory = $ticket -> ticketcategory ;
$server = Server :: where ( 'id' , $ticket -> server ) -> first ();
2023-01-05 17:01:42 +00:00
return view ( 'ticket.show' , compact ( 'ticket' , 'ticketcategory' , 'ticketcomments' , 'server' ));
2022-08-01 16:52:16 +00:00
}
2023-01-05 17:01:42 +00:00
public function reply ( Request $request )
{
//check in blacklist
2022-08-12 09:01:51 +00:00
$check = TicketBlacklist :: where ( 'user_id' , Auth :: user () -> id ) -> first ();
2023-01-05 17:01:42 +00:00
if ( $check && $check -> status == 'True' ) {
return redirect () -> route ( 'ticket.index' ) -> with ( 'error' , __ ( " You can't reply a ticket because you're on the blacklist for a reason: ' " . $check -> reason . " ', please contact the administrator " ));
2022-08-12 09:01:51 +00:00
}
2023-01-05 17:01:42 +00:00
$this -> validate ( $request , [ 'ticketcomment' => 'required' ]);
$ticket = Ticket :: where ( 'id' , $request -> input ( 'ticket_id' )) -> firstOrFail ();
$ticket -> status = 'Client Reply' ;
2022-08-01 16:52:16 +00:00
$ticket -> update ();
2023-01-05 17:01:42 +00:00
$ticketcomment = TicketComment :: create ([
'ticket_id' => $request -> input ( 'ticket_id' ),
'user_id' => Auth :: user () -> id ,
'ticketcomment' => $request -> input ( 'ticketcomment' ),
'message' => $request -> input ( 'message' ),
]);
2022-08-01 16:52:16 +00:00
$user = Auth :: user ();
$admin = User :: where ( 'role' , 'admin' ) -> orWhere ( 'role' , 'mod' ) -> get ();
2023-01-05 17:01:42 +00:00
$newmessage = $request -> input ( 'ticketcomment' );
2022-08-01 16:52:16 +00:00
Notification :: send ( $admin , new AdminReplyNotification ( $ticket , $user , $newmessage ));
2023-01-05 17:01:42 +00:00
2022-08-01 16:52:16 +00:00
return redirect () -> back () -> with ( 'success' , __ ( 'Your comment has been submitted' ));
}
2023-01-05 23:01:23 +00:00
public function close ( $ticket_id )
{
$ticket = Ticket :: where ( 'user_id' , Auth :: user () -> id ) -> where ( " ticket_id " , $ticket_id ) -> firstOrFail ();
$ticket -> status = " Closed " ;
$ticket -> save ();
return redirect () -> back () -> with ( 'success' , __ ( 'A ticket has been closed, ID: #' ) . $ticket -> ticket_id );
}
2022-08-10 16:53:13 +00:00
public function dataTable ()
{
2023-01-05 17:01:42 +00:00
$query = Ticket :: where ( 'user_id' , Auth :: user () -> id ) -> get ();
2022-08-10 16:53:13 +00:00
return datatables ( $query )
-> addColumn ( 'category' , function ( Ticket $tickets ) {
return $tickets -> ticketcategory -> name ;
})
-> editColumn ( 'title' , function ( Ticket $tickets ) {
2023-01-13 18:18:50 +00:00
return '<a class="text-info" href="' . route ( 'ticket.show' , [ 'ticket_id' => $tickets -> ticket_id ]) . '">' . '#' . $tickets -> ticket_id . ' - ' . htmlspecialchars ( $tickets -> title ) . '</a>' ;
2022-08-10 16:53:13 +00:00
})
-> editColumn ( 'status' , function ( Ticket $tickets ) {
switch ( $tickets -> status ) {
case 'Open' :
$badgeColor = 'badge-success' ;
2023-01-05 17:01:42 +00:00
break ;
2022-08-10 16:53:13 +00:00
case 'Closed' :
$badgeColor = 'badge-danger' ;
break ;
case 'Answered' :
$badgeColor = 'badge-info' ;
break ;
default :
$badgeColor = 'badge-warning' ;
break ;
}
2023-01-05 17:01:42 +00:00
return '<span class="badge ' . $badgeColor . '">' . $tickets -> status . '</span>' ;
2022-08-10 16:53:13 +00:00
})
2023-01-05 23:01:23 +00:00
-> editColumn ( 'priority' , function ( Ticket $tickets ) {
return __ ( $tickets -> priority );
})
2022-08-10 16:53:13 +00:00
-> editColumn ( 'updated_at' , function ( Ticket $tickets ) {
2023-02-01 09:06:15 +00:00
return [ 'display' => $tickets -> updated_at ? $tickets -> updated_at -> diffForHumans () : '' ,
'raw' => $tickets -> updated_at ? strtotime ( $tickets -> updated_at ) : '' ];
2022-08-10 16:53:13 +00:00
})
2023-01-12 09:31:56 +00:00
-> addColumn ( 'actions' , function ( Ticket $tickets ) {
return '
2023-01-12 09:35:57 +00:00
< a data - content = " '.__('View').' " data - toggle = " popover " data - trigger = " hover " data - placement = " top " href = " '.route('ticket.show', ['ticket_id' => $tickets->ticket_id ]).' " class = " btn btn-sm text-white btn-info mr-1 " >< i class = " fas fa-eye " ></ i ></ a >
2023-01-12 09:31:56 +00:00
< form class = " d-inline " method = " post " action = " '.route('ticket.close', ['ticket_id' => $tickets->ticket_id ]).' " >
'.csrf_field().'
'.method_field(' POST ').'
< button data - content = " '.__('Close').' " data - toggle = " popover " data - trigger = " hover " data - placement = " top " class = " btn btn-sm text-white btn-warning mr-1 " >< i class = " fas fa-times " ></ i ></ button >
</ form >
</ form >
' ;
})
-> rawColumns ([ 'category' , 'title' , 'status' , 'updated_at' , " actions " ])
2022-08-10 16:53:13 +00:00
-> make ( true );
}
2022-08-01 16:52:16 +00:00
}