123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Server;
- use App\Models\Ticket;
- use App\Models\TicketBlacklist;
- use App\Models\TicketCategory;
- use App\Models\TicketComment;
- use App\Models\User;
- use App\Notifications\Ticket\Admin\AdminCreateNotification;
- use App\Notifications\Ticket\Admin\AdminReplyNotification;
- use App\Notifications\Ticket\User\CreateNotification;
- use App\Settings\LocaleSettings;
- use App\Settings\PterodactylSettings;
- use App\Settings\TicketSettings;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Notification;
- use Illuminate\Support\Str;
- class TicketsController extends Controller
- {
- public function index(LocaleSettings $locale_settings)
- {
- return view('ticket.index', [
- 'tickets' => Ticket::where('user_id', Auth::user()->id)->paginate(10),
- 'ticketcategories' => TicketCategory::all(),
- 'locale_datatables' => $locale_settings->datatables
- ]);
- }
- public function store(Request $request, TicketSettings $ticket_settings)
- {
- $this->validate(
- $request,
- [
- 'title' => 'required',
- 'ticketcategory' => 'required',
- 'priority' => 'required',
- 'message' => 'required',
- ]
- );
- $ticket = new Ticket(
- [
- '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',
- 'server' => $request->input('server'),
- ]
- );
- $ticket->save();
- $user = Auth::user();
- switch ($ticket_settings->notify) {
- case 'all':
- $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
- Notification::send($admin, new AdminCreateNotification($ticket, $user));
- case 'admin':
- $admin = User::where('role', 'admin')->get();
- Notification::send($admin, new AdminCreateNotification($ticket, $user));
- case 'moderator':
- $admin = User::where('role', 'mod')->get();
- Notification::send($admin, new AdminCreateNotification($ticket, $user));
- }
- $user->notify(new CreateNotification($ticket));
- return redirect()->route('ticket.index')->with('success', __('A ticket has been opened, ID: #') . $ticket->ticket_id);
- }
- public function show($ticket_id, PterodactylSettings $ptero_settings)
- {
- 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'));
- }
- $ticketcomments = $ticket->ticketcomments;
- $ticketcategory = $ticket->ticketcategory;
- $server = Server::where('id', $ticket->server)->first();
- $pterodactyl_url = $ptero_settings->panel_url;
- return view('ticket.show', compact('ticket', 'ticketcategory', 'ticketcomments', 'server', 'pterodactyl_url'));
- }
- public function reply(Request $request)
- {
- //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 reply a ticket because you're on the blacklist for a reason: '" . $check->reason . "', please contact the administrator"));
- }
- $this->validate($request, ['ticketcomment' => 'required']);
- try {
- $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
- } catch (Exception $e) {
- return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
- }
- $ticket->status = 'Client Reply';
- $ticket->update();
- $ticketcomment = TicketComment::create([
- 'ticket_id' => $request->input('ticket_id'),
- 'user_id' => Auth::user()->id,
- 'ticketcomment' => $request->input('ticketcomment'),
- 'message' => $request->input('message'),
- ]);
- $user = Auth::user();
- $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
- $newmessage = $request->input('ticketcomment');
- Notification::send($admin, new AdminReplyNotification($ticket, $user, $newmessage));
- return redirect()->back()->with('success', __('Your comment has been submitted'));
- }
- public function create()
- {
- //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'));
- }
- public function changeStatus($ticket_id)
- {
- try {
- $ticket = Ticket::where('user_id', Auth::user()->id)->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'));
- }
- if ($ticket->status == "Closed") {
- $ticket->status = "Reopened";
- $ticket->save();
- return redirect()->back()->with('success', __('A ticket has been reopened, ID: #') . $ticket->ticket_id);
- }
- $ticket->status = "Closed";
- $ticket->save();
- return redirect()->back()->with('success', __('A ticket has been closed, ID: #') . $ticket->ticket_id);
- }
- public function dataTable()
- {
- $query = Ticket::where('user_id', Auth::user()->id)->get();
- return datatables($query)
- ->addColumn('category', function (Ticket $tickets) {
- return $tickets->ticketcategory->name;
- })
- ->editColumn('title', function (Ticket $tickets) {
- return '<a class="text-info" href="' . route('ticket.show', ['ticket_id' => $tickets->ticket_id]) . '">' . '#' . $tickets->ticket_id . ' - ' . htmlspecialchars($tickets->title) . '</a>';
- })
- ->editColumn('status', function (Ticket $tickets) {
- switch ($tickets->status) {
- case 'Reopened':
- case 'Open':
- $badgeColor = 'badge-success';
- break;
- case 'Closed':
- $badgeColor = 'badge-danger';
- break;
- case 'Answered':
- $badgeColor = 'badge-info';
- break;
- default:
- $badgeColor = 'badge-warning';
- break;
- }
- return '<span class="badge ' . $badgeColor . '">' . $tickets->status . '</span>';
- })
- ->editColumn('priority', function (Ticket $tickets) {
- return __($tickets->priority);
- })
- ->editColumn('updated_at', function (Ticket $tickets) {
- return [
- 'display' => $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '',
- 'raw' => $tickets->updated_at ? strtotime($tickets->updated_at) : ''
- ];
- })
- ->addColumn('actions', function (Ticket $tickets) {
- $statusButtonColor = ($tickets->status == "Closed") ? 'btn-success' : 'btn-warning';
- $statusButtonIcon = ($tickets->status == "Closed") ? 'fa-redo' : 'fa-times';
- $statusButtonText = ($tickets->status == "Closed") ? __('Reopen') : __('Close');
- return '
- <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>
- </form>
- </form>
- ';
- })
- ->rawColumns(['category', 'title', 'status', 'updated_at', "actions"])
- ->make(true);
- }
- }
|