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 ;
2023-02-05 17:20:55 +00:00
use App\Settings\LocaleSettings ;
2023-02-06 20:16:54 +00:00
use App\Settings\PterodactylSettings ;
use App\Settings\TicketSettings ;
2023-01-05 17:01:42 +00:00
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
{
2023-05-03 07:53:41 +00:00
const READ_PERMISSION = 'user.ticket.read' ;
const WRITE_PERMISSION = 'user.ticket.write' ;
2023-02-05 17:20:55 +00:00
public function index ( LocaleSettings $locale_settings )
2022-08-01 16:52:16 +00:00
{
2023-02-05 17:20:55 +00:00
return view ( 'ticket.index' , [
'tickets' => Ticket :: where ( 'user_id' , Auth :: user () -> id ) -> paginate ( 10 ),
'ticketcategories' => TicketCategory :: all (),
'locale_datatables' => $locale_settings -> datatables
]);
2022-08-01 16:52:16 +00:00
}
2023-01-05 17:01:42 +00:00
2023-02-06 20:16:54 +00:00
public function store ( Request $request , TicketSettings $ticket_settings )
2023-01-05 17:01:42 +00:00
{
2023-02-20 12:23:53 +00:00
$this -> validate (
$request ,
[
2023-02-14 17:33:43 +00:00
'title' => 'required' ,
'ticketcategory' => 'required' ,
'priority' => 'required' ,
2023-02-20 12:23:53 +00:00
'message' => 'required' ,
2023-04-29 23:00:45 +00:00
'g-recaptcha-response' => [ 'required' , 'recaptcha' ],
2023-02-20 12:23:53 +00:00
]
2023-01-05 17:01:42 +00:00
);
2023-02-20 12:23:53 +00:00
$ticket = new Ticket (
[
2023-02-14 17:33:43 +00:00
'title' => $request -> input ( 'title' ),
'user_id' => Auth :: user () -> id ,
'ticket_id' => strtoupper ( Str :: random ( 8 )),
'ticketcategory_id' => $request -> input ( 'ticketcategory' ),
'priority' => $request -> input ( 'priority' ),
'message' => $request -> input ( 'message' ),
'status' => 'Open' ,
2023-02-20 12:23:53 +00:00
'server' => $request -> input ( 'server' ),
]
2023-01-05 17:01:42 +00:00
);
2022-08-01 16:52:16 +00:00
$ticket -> save ();
$user = Auth :: user ();
2023-05-05 10:29:28 +00:00
$staffNotify = User :: permission ( 'admin.tickets.get_notification' ) -> get ();
foreach ( $staffNotify as $staff ){
Notification :: send ( $staff , new AdminCreateNotification ( $ticket , $user ));
2023-02-14 17:33:43 +00:00
}
2023-05-05 10:29:28 +00:00
2022-08-01 16:52:16 +00:00
$user -> notify ( new CreateNotification ( $ticket ));
2023-01-05 17:01:42 +00:00
2023-02-14 17:33:43 +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
2023-02-06 20:16:54 +00:00
public function show ( $ticket_id , PterodactylSettings $ptero_settings )
2023-01-05 17:01:42 +00:00
{
2023-05-03 07:53:41 +00:00
$this -> checkPermission ( self :: READ_PERMISSION );
2023-02-14 17:33:43 +00:00
try {
$ticket = Ticket :: where ( 'ticket_id' , $ticket_id ) -> firstOrFail ();
} catch ( Exception $e ) {
return redirect () -> back () -> with ( 'warning' , __ ( 'Ticket not found on the server. It potentially got deleted earlier' ));
}
2022-08-01 16:52:16 +00:00
$ticketcomments = $ticket -> ticketcomments ;
$ticketcategory = $ticket -> ticketcategory ;
$server = Server :: where ( 'id' , $ticket -> server ) -> first ();
2023-02-06 20:16:54 +00:00
$pterodactyl_url = $ptero_settings -> panel_url ;
2023-01-05 17:01:42 +00:00
2023-02-06 20:16:54 +00:00
return view ( 'ticket.show' , compact ( 'ticket' , 'ticketcategory' , 'ticketcomments' , 'server' , 'pterodactyl_url' ));
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' ) {
2023-02-14 17:33:43 +00:00
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' ]);
2023-02-14 14:02:05 +00:00
try {
2023-02-14 17:33:43 +00:00
$ticket = Ticket :: where ( 'id' , $request -> input ( 'ticket_id' )) -> firstOrFail ();
} catch ( Exception $e ) {
2023-02-14 14:02:05 +00:00
return redirect () -> back () -> with ( 'warning' , __ ( 'Ticket not found on the server. It potentially got deleted earlier' ));
}
2023-01-05 17:01:42 +00:00
$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 ();
2023-01-05 17:01:42 +00:00
$newmessage = $request -> input ( 'ticketcomment' );
2023-05-05 10:29:28 +00:00
$staffNotify = User :: permission ( 'admin.tickets.get_notification' ) -> get ();
foreach ( $staffNotify as $staff ){
Notification :: send ( $staff , 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-02-14 17:33:43 +00:00
public function create ()
{
2023-05-03 07:53:41 +00:00
$this -> checkPermission ( self :: WRITE_PERMISSION );
2023-02-14 17:33:43 +00:00
//check in blacklist
$check = TicketBlacklist :: where ( 'user_id' , Auth :: user () -> id ) -> first ();
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 " ));
}
$ticketcategories = TicketCategory :: all ();
$servers = Auth :: user () -> servers ;
return view ( 'ticket.create' , compact ( 'ticketcategories' , 'servers' ));
}
2023-02-11 23:15:36 +00:00
public function changeStatus ( $ticket_id )
2023-02-14 17:33:43 +00:00
{
2023-02-14 14:02:05 +00:00
try {
2023-02-14 17:33:43 +00:00
$ticket = Ticket :: where ( 'user_id' , Auth :: user () -> id ) -> where ( " ticket_id " , $ticket_id ) -> firstOrFail ();
} catch ( Exception $e ) {
2023-02-14 14:02:05 +00:00
return redirect () -> back () -> with ( 'warning' , __ ( 'Ticket not found on the server. It potentially got deleted earlier' ));
}
2023-02-14 17:33:43 +00:00
if ( $ticket -> status == " Closed " ) {
2023-02-11 23:15:36 +00:00
$ticket -> status = " Reopened " ;
$ticket -> save ();
return redirect () -> back () -> with ( 'success' , __ ( 'A ticket has been reopened, ID: #' ) . $ticket -> ticket_id );
}
2023-01-05 23:01:23 +00:00
$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-02-14 17:33:43 +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 ) {
2023-02-11 23:15:36 +00:00
case 'Reopened' :
2022-08-10 16:53:13 +00:00
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-02-14 17:33:43 +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-20 12:23:53 +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 ) {
2023-02-11 23:15:36 +00:00
$statusButtonColor = ( $tickets -> status == " Closed " ) ? 'btn-success' : 'btn-warning' ;
$statusButtonIcon = ( $tickets -> status == " Closed " ) ? 'fa-redo' : 'fa-times' ;
$statusButtonText = ( $tickets -> status == " Closed " ) ? __ ( 'Reopen' ) : __ ( 'Close' );
2023-01-12 09:31:56 +00:00
return '
2023-02-14 17:33:43 +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 >
< form class = " d-inline " method = " post " action = " ' . route('ticket.changeStatus', ['ticket_id' => $tickets->ticket_id ]) . ' " >
' . csrf_field() . '
' . method_field(' POST ') . '
< button data - content = " ' . __( $statusButtonText ) . ' " data - toggle = " popover " data - trigger = " hover " data - placement = " top " class = " btn btn-sm text-white ' . $statusButtonColor . ' mr-1 " >< i class = " fas ' . $statusButtonIcon . ' " ></ i ></ button >
2023-01-12 09:31:56 +00:00
</ 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
}