TicketsController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Session;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Str;
  8. use Illuminate\Support\Facades\Notification;
  9. use App\Models\Ticket;
  10. use App\Models\Server;
  11. use App\Models\TicketComment;
  12. use App\Models\TicketCategory;
  13. use App\Models\TicketBlacklist;
  14. use App\Notifications\Ticket\User\CreateNotification;
  15. use App\Notifications\Ticket\Admin\AdminCreateNotification;
  16. use App\Notifications\Ticket\Admin\AdminReplyNotification;
  17. class TicketsController extends Controller
  18. {
  19. public function index()
  20. {
  21. $tickets = Ticket::where("user_id", Auth::user()->id)->paginate(10);
  22. $ticketcategories = TicketCategory::all();
  23. return view("ticket.index", compact("tickets", "ticketcategories"));
  24. }
  25. public function create() {
  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. $this->validate($request, array(
  37. "title" => "required",
  38. "ticketcategory" => "required",
  39. "priority" => "required",
  40. "message" => "required")
  41. );
  42. $ticket = new Ticket(array(
  43. "title" => $request->input("title"),
  44. "user_id" => Auth::user()->id,
  45. "ticket_id" => strtoupper(Str::random(5)),
  46. "ticketcategory_id" => $request->input("ticketcategory"),
  47. "priority" => $request->input("priority"),
  48. "message" => $request->input("message"),
  49. "status" => "Open",
  50. "server" => $request->input("server"))
  51. );
  52. $ticket->save();
  53. $user = Auth::user();
  54. $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
  55. $user->notify(new CreateNotification($ticket));
  56. Notification::send($admin, new AdminCreateNotification($ticket, $user));
  57. return redirect()->route('ticket.index')->with('success', __('A ticket has been opened, ID: #') . $ticket->ticket_id);
  58. }
  59. public function show($ticket_id) {
  60. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  61. $ticketcomments = $ticket->ticketcomments;
  62. $ticketcategory = $ticket->ticketcategory;
  63. $server = Server::where('id', $ticket->server)->first();
  64. return view("ticket.show", compact("ticket", "ticketcategory", "ticketcomments", "server"));
  65. }
  66. public function reply(Request $request) {
  67. #check in blacklist
  68. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  69. if($check && $check->status == "True"){
  70. 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"));
  71. }
  72. $this->validate($request, array("ticketcomment" => "required"));
  73. $ticket = Ticket::where('id', $request->input("ticket_id"))->firstOrFail();
  74. $ticket->status = "Client Reply";
  75. $ticket->update();
  76. $ticketcomment = TicketComment::create(array(
  77. "ticket_id" => $request->input("ticket_id"),
  78. "user_id" => Auth::user()->id,
  79. "ticketcomment" => $request->input("ticketcomment"),
  80. "message" => $request->input("message")
  81. ));
  82. $user = Auth::user();
  83. $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
  84. $newmessage = $request->input("ticketcomment");
  85. Notification::send($admin, new AdminReplyNotification($ticket, $user, $newmessage));
  86. return redirect()->back()->with('success', __('Your comment has been submitted'));
  87. }
  88. public function dataTable()
  89. {
  90. $query = Ticket::where("user_id", Auth::user()->id)->get();
  91. return datatables($query)
  92. ->addColumn('category', function (Ticket $tickets) {
  93. return $tickets->ticketcategory->name;
  94. })
  95. ->editColumn('title', function (Ticket $tickets) {
  96. return '<a class="text-info" href="' . route('ticket.show', ['ticket_id' => $tickets->ticket_id]) . '">' . "#" . $tickets->ticket_id . " - " . $tickets->title . '</a>';
  97. })
  98. ->editColumn('status', function (Ticket $tickets) {
  99. switch ($tickets->status) {
  100. case 'Open':
  101. $badgeColor = 'badge-success';
  102. break;
  103. case 'Closed':
  104. $badgeColor = 'badge-danger';
  105. break;
  106. case 'Answered':
  107. $badgeColor = 'badge-info';
  108. break;
  109. default:
  110. $badgeColor = 'badge-warning';
  111. break;
  112. }
  113. return '<span class="badge ' . $badgeColor . '">' . $tickets->status . '</span>';
  114. })
  115. ->editColumn('updated_at', function (Ticket $tickets) {
  116. return $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '';
  117. })
  118. ->rawColumns(['category', 'title', 'status', 'updated_at'])
  119. ->make(true);
  120. }
  121. }