TicketsController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers\Moderation;
  3. use App\Models\User;
  4. use App\Models\Ticket;
  5. use App\Models\Server;
  6. use App\Models\TicketCategory;
  7. use App\Models\TicketComment;
  8. use App\Http\Controllers\Controller;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use App\Notifications\Ticket\User\ReplyNotification;
  13. class TicketsController extends Controller
  14. {
  15. public function index() {
  16. $tickets = Ticket::orderBy('id','desc')->paginate(10);
  17. $ticketcategories = TicketCategory::all();
  18. return view("moderator.ticket.index", compact("tickets", "ticketcategories"));
  19. }
  20. public function show($ticket_id) {
  21. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  22. $ticketcomments = $ticket->ticketcomments;
  23. $ticketcategory = $ticket->ticketcategory;
  24. $server = Server::where('id', $ticket->server)->first();
  25. return view("moderator.ticket.show", compact("ticket", "ticketcategory", "ticketcomments", "server"));
  26. }
  27. public function close($ticket_id) {
  28. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  29. $ticket->status = "Closed";
  30. $ticket->save();
  31. $ticketOwner = $ticket->user;
  32. return redirect()->back()->with('success', __('A ticket has been closed, ID: #') . $ticket->ticket_id);
  33. }
  34. public function delete($ticket_id){
  35. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  36. TicketComment::where("ticket_id", $ticket->id)->delete();
  37. $ticket->delete();
  38. return redirect()->back()->with('success', __('A ticket has been deleted, ID: #') . $ticket_id);
  39. }
  40. public function reply(Request $request) {
  41. $this->validate($request, array("ticketcomment" => "required"));
  42. $ticket = Ticket::where('id', $request->input("ticket_id"))->firstOrFail();
  43. $ticket->status = "Answered";
  44. $ticket->update();
  45. $ticketcomment = TicketComment::create(array(
  46. "ticket_id" => $request->input("ticket_id"),
  47. "user_id" => Auth::user()->id,
  48. "ticketcomment" => $request->input("ticketcomment"),
  49. ));
  50. $user = User::where('id', $ticket->user_id)->firstOrFail();
  51. $newmessage = $request->input("ticketcomment");
  52. $user->notify(new ReplyNotification($ticket, $user, $newmessage));
  53. return redirect()->back()->with('success', __('Your comment has been submitted'));
  54. }
  55. public function dataTable()
  56. {
  57. $query = Ticket::query();
  58. return datatables($query)
  59. ->addColumn('category', function (Ticket $tickets) {
  60. return $tickets->ticketcategory->name;
  61. })
  62. ->editColumn('title', function (Ticket $tickets) {
  63. return '<a class="text-info" href="' . route('moderator.ticket.show', ['ticket_id' => $tickets->ticket_id]) . '">' . "#" . $tickets->ticket_id . " - " . $tickets->title . '</a>';
  64. })
  65. ->editColumn('user_id', function (Ticket $tickets) {
  66. return '<a href="' . route('admin.users.show', $tickets->user->id) . '">' . $tickets->user->name . '</a>';
  67. })
  68. ->addColumn('actions', function (Ticket $tickets) {
  69. return '
  70. <a data-content="'.__("View").'" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('moderator.ticket.show', ['ticket_id' => $tickets->ticket_id]) . '" class="btn btn-sm text-white btn-info mr-1"><i class="fas fa-eye"></i></a>
  71. <form class="d-inline" method="post" action="' . route('moderator.ticket.close', ['ticket_id' => $tickets->ticket_id ]) . '">
  72. ' . csrf_field() . '
  73. ' . method_field("POST") . '
  74. <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>
  75. </form>
  76. <form class="d-inline" method="post" action="' . route('moderator.ticket.delete', ['ticket_id' => $tickets->ticket_id ]) . '">
  77. ' . csrf_field() . '
  78. ' . method_field("POST") . '
  79. <button data-content="'.__("Delete").'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm text-white btn-danger mr-1"><i class="fas fa-trash"></i></button>
  80. </form>
  81. ';
  82. })
  83. ->editColumn('status', function (Ticket $tickets) {
  84. switch ($tickets->status) {
  85. case 'Open':
  86. $badgeColor = 'badge-success';
  87. break;
  88. case 'Closed':
  89. $badgeColor = 'badge-danger';
  90. break;
  91. case 'Answered':
  92. $badgeColor = 'badge-info';
  93. break;
  94. default:
  95. $badgeColor = 'badge-warning';
  96. break;
  97. }
  98. return '<span class="badge ' . $badgeColor . '">' . $tickets->status . '</span>';
  99. })
  100. ->editColumn('updated_at', function (Ticket $tickets) {
  101. return $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '';
  102. })
  103. ->rawColumns(['category', 'title', 'user_id', 'status', 'updated_at', 'actions'])
  104. ->make(true);
  105. }
  106. }