TicketsController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Http\Controllers\Moderation;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Server;
  5. use App\Models\Ticket;
  6. use App\Models\TicketBlacklist;
  7. use App\Models\TicketCategory;
  8. use App\Models\TicketComment;
  9. use App\Models\User;
  10. use App\Notifications\Ticket\User\ReplyNotification;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. class TicketsController extends Controller
  14. {
  15. public function index()
  16. {
  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. {
  23. try {
  24. $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
  25. } catch (Exception $e)
  26. {
  27. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  28. }
  29. $ticketcomments = $ticket->ticketcomments;
  30. $ticketcategory = $ticket->ticketcategory;
  31. $server = Server::where('id', $ticket->server)->first();
  32. return view('moderator.ticket.show', compact('ticket', 'ticketcategory', 'ticketcomments', 'server'));
  33. }
  34. public function changeStatus($ticket_id)
  35. {
  36. try {
  37. $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
  38. } catch(Exception $e)
  39. {
  40. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  41. }
  42. if($ticket->status == "Closed"){
  43. $ticket->status = "Reopened";
  44. $ticket->save();
  45. return redirect()->back()->with('success', __('A ticket has been reopened, ID: #') . $ticket->ticket_id);
  46. }
  47. $ticket->status = 'Closed';
  48. $ticket->save();
  49. $ticketOwner = $ticket->user;
  50. return redirect()->back()->with('success', __('A ticket has been closed, ID: #').$ticket->ticket_id);
  51. }
  52. public function delete($ticket_id)
  53. {
  54. try {
  55. $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
  56. } catch (Exception $e)
  57. {
  58. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  59. }
  60. TicketComment::where('ticket_id', $ticket->id)->delete();
  61. $ticket->delete();
  62. return redirect()->back()->with('success', __('A ticket has been deleted, ID: #').$ticket_id);
  63. }
  64. public function reply(Request $request)
  65. {
  66. $this->validate($request, ['ticketcomment' => 'required']);
  67. try {
  68. $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
  69. } catch (Exception $e){
  70. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  71. }
  72. $ticket->status = 'Answered';
  73. $ticket->update();
  74. TicketComment::create([
  75. 'ticket_id' => $request->input('ticket_id'),
  76. 'user_id' => Auth::user()->id,
  77. 'ticketcomment' => $request->input('ticketcomment'),
  78. ]);
  79. try {
  80. $user = User::where('id', $ticket->user_id)->firstOrFail();
  81. } catch(Exception $e)
  82. {
  83. return redirect()->back()->with('warning', __('User not found on the server. Check on the admin database or try again later.'));
  84. }
  85. $newmessage = $request->input('ticketcomment');
  86. $user->notify(new ReplyNotification($ticket, $user, $newmessage));
  87. return redirect()->back()->with('success', __('Your comment has been submitted'));
  88. }
  89. public function dataTable()
  90. {
  91. $query = Ticket::query();
  92. return datatables($query)
  93. ->addColumn('category', function (Ticket $tickets) {
  94. return $tickets->ticketcategory->name;
  95. })
  96. ->editColumn('title', function (Ticket $tickets) {
  97. return '<a class="text-info" href="'.route('moderator.ticket.show', ['ticket_id' => $tickets->ticket_id]).'">'.'#'.$tickets->ticket_id.' - '.htmlspecialchars($tickets->title).'</a>';
  98. })
  99. ->editColumn('user_id', function (Ticket $tickets) {
  100. return '<a href="'.route('admin.users.show', $tickets->user->id).'">'.$tickets->user->name.'</a>';
  101. })
  102. ->addColumn('actions', function (Ticket $tickets) {
  103. $statusButtonColor = ($tickets->status == "Closed") ? 'btn-success' : 'btn-warning';
  104. $statusButtonIcon = ($tickets->status == "Closed") ? 'fa-redo' : 'fa-times';
  105. $statusButtonText = ($tickets->status == "Closed") ? __('Reopen') : __('Close');
  106. return '
  107. <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>
  108. <form class="d-inline" method="post" action="'.route('moderator.ticket.changeStatus', ['ticket_id' => $tickets->ticket_id]).'">
  109. '.csrf_field().'
  110. '.method_field('POST').'
  111. <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>
  112. </form>
  113. <form class="d-inline" method="post" action="'.route('moderator.ticket.delete', ['ticket_id' => $tickets->ticket_id]).'">
  114. '.csrf_field().'
  115. '.method_field('POST').'
  116. <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>
  117. </form>
  118. ';
  119. })
  120. ->editColumn('status', function (Ticket $tickets) {
  121. switch ($tickets->status) {
  122. case 'Reopened':
  123. case 'Open':
  124. $badgeColor = 'badge-success';
  125. break;
  126. case 'Closed':
  127. $badgeColor = 'badge-danger';
  128. break;
  129. case 'Answered':
  130. $badgeColor = 'badge-info';
  131. break;
  132. default:
  133. $badgeColor = 'badge-warning';
  134. break;
  135. }
  136. return '<span class="badge '.$badgeColor.'">'.$tickets->status.'</span>';
  137. })
  138. ->editColumn('priority', function (Ticket $tickets) {
  139. return __($tickets->priority);
  140. })
  141. ->editColumn('updated_at', function (Ticket $tickets) {
  142. return ['display' => $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '',
  143. 'raw' => $tickets->updated_at ? strtotime($tickets->updated_at) : ''];
  144. })
  145. ->rawColumns(['category', 'title', 'user_id', 'status', 'priority', 'updated_at', 'actions'])
  146. ->make(true);
  147. }
  148. public function blacklist()
  149. {
  150. return view('moderator.ticket.blacklist');
  151. }
  152. public function blacklistAdd(Request $request)
  153. {
  154. try {
  155. $user = User::where('id', $request->user_id)->firstOrFail();
  156. $check = TicketBlacklist::where('user_id', $user->id)->first();
  157. }
  158. catch (Exception $e){
  159. return redirect()->back()->with('warning', __('User not found on the server. Check the admin database or try again later.'));
  160. }
  161. if ($check) {
  162. $check->reason = $request->reason;
  163. $check->status = 'True';
  164. $check->save();
  165. return redirect()->back()->with('info', __('Target User already in blacklist. Reason updated'));
  166. }
  167. TicketBlacklist::create([
  168. 'user_id' => $user->id,
  169. 'status' => 'True',
  170. 'reason' => $request->reason,
  171. ]);
  172. return redirect()->back()->with('success', __('Successfully add User to blacklist, User name: '.$user->name));
  173. }
  174. public function blacklistDelete($id)
  175. {
  176. $blacklist = TicketBlacklist::where('id', $id)->first();
  177. $blacklist->delete();
  178. return redirect()->back()->with('success', __('Successfully remove User from blacklist, User name: '.$blacklist->user->name));
  179. }
  180. public function blacklistChange($id)
  181. {
  182. try {
  183. $blacklist = TicketBlacklist::where('id', $id)->first();
  184. }
  185. catch (Exception $e){
  186. return redirect()->back()->with('warning', __('User not found on the server. Check the admin database or try again later.'));
  187. }
  188. if ($blacklist->status == 'True') {
  189. $blacklist->status = 'False';
  190. } else {
  191. $blacklist->status = 'True';
  192. }
  193. $blacklist->update();
  194. return redirect()->back()->with('success', __('Successfully change status blacklist from, User name: '.$blacklist->user->name));
  195. }
  196. public function dataTableBlacklist()
  197. {
  198. $query = TicketBlacklist::with(['user']);
  199. $query->select('ticket_blacklists.*');
  200. return datatables($query)
  201. ->editColumn('user', function (TicketBlacklist $blacklist) {
  202. return '<a href="'.route('admin.users.show', $blacklist->user->id).'">'.$blacklist->user->name.'</a>';
  203. })
  204. ->editColumn('status', function (TicketBlacklist $blacklist) {
  205. switch ($blacklist->status) {
  206. case 'True':
  207. $text = 'Blocked';
  208. $badgeColor = 'badge-danger';
  209. break;
  210. default:
  211. $text = 'Unblocked';
  212. $badgeColor = 'badge-success';
  213. break;
  214. }
  215. return '<span class="badge '.$badgeColor.'">'.$text.'</span>';
  216. })
  217. ->editColumn('reason', function (TicketBlacklist $blacklist) {
  218. return $blacklist->reason;
  219. })
  220. ->addColumn('actions', function (TicketBlacklist $blacklist) {
  221. return '
  222. <form class="d-inline" method="post" action="'.route('moderator.ticket.blacklist.change', ['id' => $blacklist->id]).'">
  223. '.csrf_field().'
  224. '.method_field('POST').'
  225. <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>
  226. </form>
  227. <form class="d-inline" method="post" action="'.route('moderator.ticket.blacklist.delete', ['id' => $blacklist->id]).'">
  228. '.csrf_field().'
  229. '.method_field('POST').'
  230. <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>
  231. </form>
  232. ';
  233. })
  234. ->editColumn('created_at', function (TicketBlacklist $blacklist) {
  235. return $blacklist->created_at ? $blacklist->created_at->diffForHumans() : '';
  236. })
  237. ->rawColumns(['user', 'status', 'reason', 'created_at', 'actions'])
  238. ->make(true);
  239. }
  240. }