TicketsController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Server;
  4. use App\Models\Ticket;
  5. use App\Models\TicketBlacklist;
  6. use App\Models\TicketCategory;
  7. use App\Models\TicketComment;
  8. use App\Models\User;
  9. use App\Notifications\Ticket\Admin\AdminCreateNotification;
  10. use App\Notifications\Ticket\Admin\AdminReplyNotification;
  11. use App\Notifications\Ticket\User\CreateNotification;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\Notification;
  15. use Illuminate\Support\Str;
  16. class TicketsController extends Controller
  17. {
  18. public function index()
  19. {
  20. $tickets = Ticket::where('user_id', Auth::user()->id)->paginate(10);
  21. $ticketcategories = TicketCategory::all();
  22. return view('ticket.index', compact('tickets', 'ticketcategories'));
  23. }
  24. public function create()
  25. {
  26. //check in blacklist
  27. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  28. if ($check && $check->status == 'True') {
  29. 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"));
  30. }
  31. $ticketcategories = TicketCategory::all();
  32. $servers = Auth::user()->servers;
  33. return view('ticket.create', compact('ticketcategories', 'servers'));
  34. }
  35. public function store(Request $request)
  36. {
  37. $this->validate($request, [
  38. 'title' => 'required',
  39. 'ticketcategory' => 'required',
  40. 'priority' => 'required',
  41. 'message' => 'required', ]
  42. );
  43. $ticket = new Ticket([
  44. 'title' => $request->input('title'),
  45. 'user_id' => Auth::user()->id,
  46. 'ticket_id' => strtoupper(Str::random(8)),
  47. 'ticketcategory_id' => $request->input('ticketcategory'),
  48. 'priority' => $request->input('priority'),
  49. 'message' => $request->input('message'),
  50. 'status' => 'Open',
  51. 'server' => $request->input('server'), ]
  52. );
  53. $ticket->save();
  54. $user = Auth::user();
  55. if(config('SETTINGS::TICKET:NOTIFY') == "all"){ $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();}
  56. if(config('SETTINGS::TICKET:NOTIFY') == "admin"){ $admin = User::where('role', 'admin')->get();}
  57. if(config('SETTINGS::TICKET:NOTIFY') == "moderator"){ $admin = User::where('role', 'mod')->get();}
  58. $user->notify(new CreateNotification($ticket));
  59. if(config('SETTINGS::TICKET:NOTIFY') != "none"){
  60. Notification::send($admin, new AdminCreateNotification($ticket, $user));
  61. }
  62. return redirect()->route('ticket.index')->with('success', __('A ticket has been opened, ID: #').$ticket->ticket_id);
  63. }
  64. public function show($ticket_id)
  65. {
  66. $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
  67. $ticketcomments = $ticket->ticketcomments;
  68. $ticketcategory = $ticket->ticketcategory;
  69. $server = Server::where('id', $ticket->server)->first();
  70. return view('ticket.show', compact('ticket', 'ticketcategory', 'ticketcomments', 'server'));
  71. }
  72. public function reply(Request $request)
  73. {
  74. //check in blacklist
  75. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  76. if ($check && $check->status == 'True') {
  77. 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"));
  78. }
  79. $this->validate($request, ['ticketcomment' => 'required']);
  80. $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
  81. $ticket->status = 'Client Reply';
  82. $ticket->update();
  83. $ticketcomment = TicketComment::create([
  84. 'ticket_id' => $request->input('ticket_id'),
  85. 'user_id' => Auth::user()->id,
  86. 'ticketcomment' => $request->input('ticketcomment'),
  87. 'message' => $request->input('message'),
  88. ]);
  89. $user = Auth::user();
  90. $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
  91. $newmessage = $request->input('ticketcomment');
  92. Notification::send($admin, new AdminReplyNotification($ticket, $user, $newmessage));
  93. return redirect()->back()->with('success', __('Your comment has been submitted'));
  94. }
  95. public function close($ticket_id)
  96. {
  97. $ticket = Ticket::where('user_id', Auth::user()->id)->where("ticket_id", $ticket_id)->firstOrFail();
  98. $ticket->status = "Closed";
  99. $ticket->save();
  100. return redirect()->back()->with('success', __('A ticket has been closed, ID: #') . $ticket->ticket_id);
  101. }
  102. public function dataTable()
  103. {
  104. $query = Ticket::where('user_id', Auth::user()->id)->get();
  105. return datatables($query)
  106. ->addColumn('category', function (Ticket $tickets) {
  107. return $tickets->ticketcategory->name;
  108. })
  109. ->editColumn('title', function (Ticket $tickets) {
  110. return '<a class="text-info" href="'.route('ticket.show', ['ticket_id' => $tickets->ticket_id]).'">'.'#'.$tickets->ticket_id.' - '.htmlspecialchars($tickets->title).'</a>';
  111. })
  112. ->editColumn('status', function (Ticket $tickets) {
  113. switch ($tickets->status) {
  114. case 'Open':
  115. $badgeColor = 'badge-success';
  116. break;
  117. case 'Closed':
  118. $badgeColor = 'badge-danger';
  119. break;
  120. case 'Answered':
  121. $badgeColor = 'badge-info';
  122. break;
  123. default:
  124. $badgeColor = 'badge-warning';
  125. break;
  126. }
  127. return '<span class="badge '.$badgeColor.'">'.$tickets->status.'</span>';
  128. })
  129. ->editColumn('priority', function (Ticket $tickets) {
  130. return __($tickets->priority);
  131. })
  132. ->editColumn('updated_at', function (Ticket $tickets) {
  133. return ['display' => $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '',
  134. 'raw' => $tickets->updated_at ? strtotime($tickets->updated_at) : ''];
  135. })
  136. ->addColumn('actions', function (Ticket $tickets) {
  137. return '
  138. <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>
  139. <form class="d-inline" method="post" action="'.route('ticket.close', ['ticket_id' => $tickets->ticket_id]).'">
  140. '.csrf_field().'
  141. '.method_field('POST').'
  142. <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>
  143. </form>
  144. </form>
  145. ';
  146. })
  147. ->rawColumns(['category', 'title', 'status', 'updated_at', "actions"])
  148. ->make(true);
  149. }
  150. }