TicketsController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\Models\TicketBlacklist;
  9. use App\Http\Controllers\Controller;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use App\Notifications\Ticket\User\ReplyNotification;
  14. class TicketsController extends Controller
  15. {
  16. public function index() {
  17. $tickets = Ticket::orderBy('id','desc')->paginate(10);
  18. $ticketcategories = TicketCategory::all();
  19. return view("moderator.ticket.index", compact("tickets", "ticketcategories"));
  20. }
  21. public function show($ticket_id) {
  22. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  23. $ticketcomments = $ticket->ticketcomments;
  24. $ticketcategory = $ticket->ticketcategory;
  25. $server = Server::where('id', $ticket->server)->first();
  26. return view("moderator.ticket.show", compact("ticket", "ticketcategory", "ticketcomments", "server"));
  27. }
  28. public function close($ticket_id) {
  29. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  30. $ticket->status = "Closed";
  31. $ticket->save();
  32. $ticketOwner = $ticket->user;
  33. return redirect()->back()->with('success', __('A ticket has been closed, ID: #') . $ticket->ticket_id);
  34. }
  35. public function delete($ticket_id){
  36. $ticket = Ticket::where("ticket_id", $ticket_id)->firstOrFail();
  37. TicketComment::where("ticket_id", $ticket->id)->delete();
  38. $ticket->delete();
  39. return redirect()->back()->with('success', __('A ticket has been deleted, ID: #') . $ticket_id);
  40. }
  41. public function reply(Request $request) {
  42. $this->validate($request, array("ticketcomment" => "required"));
  43. $ticket = Ticket::where('id', $request->input("ticket_id"))->firstOrFail();
  44. $ticket->status = "Answered";
  45. $ticket->update();
  46. TicketComment::create(array(
  47. "ticket_id" => $request->input("ticket_id"),
  48. "user_id" => Auth::user()->id,
  49. "ticketcomment" => $request->input("ticketcomment"),
  50. ));
  51. $user = User::where('id', $ticket->user_id)->firstOrFail();
  52. $newmessage = $request->input("ticketcomment");
  53. $user->notify(new ReplyNotification($ticket, $user, $newmessage));
  54. return redirect()->back()->with('success', __('Your comment has been submitted'));
  55. }
  56. public function dataTable()
  57. {
  58. $query = Ticket::query();
  59. return datatables($query)
  60. ->addColumn('category', function (Ticket $tickets) {
  61. return $tickets->ticketcategory->name;
  62. })
  63. ->editColumn('title', function (Ticket $tickets) {
  64. return '<a class="text-info" href="' . route('moderator.ticket.show', ['ticket_id' => $tickets->ticket_id]) . '">' . "#" . $tickets->ticket_id . " - " . $tickets->title . '</a>';
  65. })
  66. ->editColumn('user_id', function (Ticket $tickets) {
  67. return '<a href="' . route('admin.users.show', $tickets->user->id) . '">' . $tickets->user->name . '</a>';
  68. })
  69. ->addColumn('actions', function (Ticket $tickets) {
  70. return '
  71. <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>
  72. <form class="d-inline" method="post" action="' . route('moderator.ticket.close', ['ticket_id' => $tickets->ticket_id ]) . '">
  73. ' . csrf_field() . '
  74. ' . method_field("POST") . '
  75. <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>
  76. </form>
  77. <form class="d-inline" method="post" action="' . route('moderator.ticket.delete', ['ticket_id' => $tickets->ticket_id ]) . '">
  78. ' . csrf_field() . '
  79. ' . method_field("POST") . '
  80. <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>
  81. </form>
  82. ';
  83. })
  84. ->editColumn('status', function (Ticket $tickets) {
  85. switch ($tickets->status) {
  86. case 'Open':
  87. $badgeColor = 'badge-success';
  88. break;
  89. case 'Closed':
  90. $badgeColor = 'badge-danger';
  91. break;
  92. case 'Answered':
  93. $badgeColor = 'badge-info';
  94. break;
  95. default:
  96. $badgeColor = 'badge-warning';
  97. break;
  98. }
  99. return '<span class="badge ' . $badgeColor . '">' . $tickets->status . '</span>';
  100. })
  101. ->editColumn('updated_at', function (Ticket $tickets) {
  102. return $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '';
  103. })
  104. ->rawColumns(['category', 'title', 'user_id', 'status', 'updated_at', 'actions'])
  105. ->make(true);
  106. }
  107. public function blacklist() {
  108. $users = User::paginate();
  109. $ticketcategories = TicketCategory::all();
  110. return view("moderator.ticket.blacklist", compact("users", "ticketcategories"));
  111. }
  112. public function blacklistAdd(Request $request) {
  113. $user = User::where('id', $request->user_id)->first();
  114. $check = TicketBlacklist::where('user_id', $user->id)->first();
  115. if($check){
  116. $check->reason = $request->reason;
  117. $check->status = "True";
  118. $check->save();
  119. return redirect()->back()->with('info', __('Target User already in blacklist. Reason updated'));
  120. }
  121. TicketBlacklist::create(array(
  122. "user_id" => $user->id,
  123. "status" => "True",
  124. "reason" => $request->reason,
  125. ));
  126. return redirect()->back()->with('success', __('Successfully add User to blacklist, User name: ' . $user->name));
  127. }
  128. public function blacklistDelete($id) {
  129. $blacklist = TicketBlacklist::where('id', $id)->first();
  130. $blacklist->delete();
  131. return redirect()->back()->with('success', __('Successfully remove User from blacklist, User name: ' . $blacklist->user->name));
  132. }
  133. public function blacklistChange($id) {
  134. $blacklist = TicketBlacklist::where('id', $id)->first();
  135. if($blacklist->status == "True")
  136. {
  137. $blacklist->status = "False";
  138. } else {
  139. $blacklist->status = "True";
  140. }
  141. $blacklist->update();
  142. return redirect()->back()->with('success', __('Successfully change status blacklist from, User name: ' . $blacklist->user->name));
  143. }
  144. public function dataTableBlacklist()
  145. {
  146. $query = TicketBlacklist::with(['user']);
  147. return datatables($query)
  148. ->editColumn('user', function (TicketBlacklist $blacklist) {
  149. return '<a href="' . route('admin.users.show', $blacklist->user->id) . '">' . $blacklist->user->name . '</a>';
  150. })
  151. ->editColumn('status', function (TicketBlacklist $blacklist) {
  152. switch ($blacklist->status) {
  153. case 'True':
  154. $text = "Blocked";
  155. $badgeColor = 'badge-danger';
  156. break;
  157. default:
  158. $text = "Unblocked";
  159. $badgeColor = 'badge-success';
  160. break;
  161. }
  162. return '<span class="badge ' . $badgeColor . '">' . $text . '</span>';
  163. })
  164. ->editColumn('reason', function (TicketBlacklist $blacklist) {
  165. return $blacklist->reason;
  166. })
  167. ->addColumn('actions', function (TicketBlacklist $blacklist) {
  168. return '
  169. <form class="d-inline" method="post" action="' . route('moderator.ticket.blacklist.change', ['id' => $blacklist->id ]) . '">
  170. ' . csrf_field() . '
  171. ' . method_field("POST") . '
  172. <button data-content="'.__("Change Status").'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm text-white btn-warning mr-1"><i class="fas fa-sync-alt"></i></button>
  173. </form>
  174. <form class="d-inline" method="post" action="' . route('moderator.ticket.blacklist.delete', ['id' => $blacklist->id ]) . '">
  175. ' . csrf_field() . '
  176. ' . method_field("POST") . '
  177. <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>
  178. </form>
  179. ';
  180. })
  181. ->editColumn('created_at', function (TicketBlacklist $blacklist) {
  182. return $blacklist->created_at ? $blacklist->created_at->diffForHumans() : '';
  183. })
  184. ->rawColumns(['user', 'status', 'reason', 'created_at', 'actions'])
  185. ->make(true);
  186. }
  187. }