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. }
  70. catch (Exception $e){
  71. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  72. }
  73. $ticket->status = 'Answered';
  74. $ticket->update();
  75. TicketComment::create([
  76. 'ticket_id' => $request->input('ticket_id'),
  77. 'user_id' => Auth::user()->id,
  78. 'ticketcomment' => $request->input('ticketcomment'),
  79. ]);
  80. try {
  81. $user = User::where('id', $ticket->user_id)->firstOrFail();
  82. } catch(Exception $e)
  83. {
  84. return redirect()->back()->with('warning', __('User not found on the server. Check on the admin database or try again later.'));
  85. }
  86. $newmessage = $request->input('ticketcomment');
  87. $user->notify(new ReplyNotification($ticket, $user, $newmessage));
  88. return redirect()->back()->with('success', __('Your comment has been submitted'));
  89. }
  90. public function dataTable()
  91. {
  92. $query = Ticket::query();
  93. return datatables($query)
  94. ->addColumn('category', function (Ticket $tickets) {
  95. return $tickets->ticketcategory->name;
  96. })
  97. ->editColumn('title', function (Ticket $tickets) {
  98. return '<a class="text-info" href="'.route('moderator.ticket.show', ['ticket_id' => $tickets->ticket_id]).'">'.'#'.$tickets->ticket_id.' - '.htmlspecialchars($tickets->title).'</a>';
  99. })
  100. ->editColumn('user_id', function (Ticket $tickets) {
  101. return '<a href="'.route('admin.users.show', $tickets->user->id).'">'.$tickets->user->name.'</a>';
  102. })
  103. ->addColumn('actions', function (Ticket $tickets) {
  104. $statusButtonColor = ($tickets->status == "Closed") ? 'btn-success' : 'btn-warning';
  105. $statusButtonIcon = ($tickets->status == "Closed") ? 'fa-redo' : 'fa-times';
  106. $statusButtonText = ($tickets->status == "Closed") ? __('Reopen') : __('Close');
  107. return '
  108. <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>
  109. <form class="d-inline" method="post" action="'.route('moderator.ticket.changeStatus', ['ticket_id' => $tickets->ticket_id]).'">
  110. '.csrf_field().'
  111. '.method_field('POST').'
  112. <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>
  113. </form>
  114. <form class="d-inline" method="post" action="'.route('moderator.ticket.delete', ['ticket_id' => $tickets->ticket_id]).'">
  115. '.csrf_field().'
  116. '.method_field('POST').'
  117. <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>
  118. </form>
  119. ';
  120. })
  121. ->editColumn('status', function (Ticket $tickets) {
  122. switch ($tickets->status) {
  123. case 'Reopened':
  124. case 'Open':
  125. $badgeColor = 'badge-success';
  126. break;
  127. case 'Closed':
  128. $badgeColor = 'badge-danger';
  129. break;
  130. case 'Answered':
  131. $badgeColor = 'badge-info';
  132. break;
  133. default:
  134. $badgeColor = 'badge-warning';
  135. break;
  136. }
  137. return '<span class="badge '.$badgeColor.'">'.$tickets->status.'</span>';
  138. })
  139. ->editColumn('priority', function (Ticket $tickets) {
  140. return __($tickets->priority);
  141. })
  142. ->editColumn('updated_at', function (Ticket $tickets) {
  143. return ['display' => $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '',
  144. 'raw' => $tickets->updated_at ? strtotime($tickets->updated_at) : ''];
  145. })
  146. ->rawColumns(['category', 'title', 'user_id', 'status', 'priority', 'updated_at', 'actions'])
  147. ->make(true);
  148. }
  149. public function blacklist()
  150. {
  151. return view('moderator.ticket.blacklist');
  152. }
  153. public function blacklistAdd(Request $request)
  154. {
  155. try {
  156. $user = User::where('id', $request->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. $check = TicketBlacklist::where('user_id', $user->id)->first();
  162. if ($check) {
  163. $check->reason = $request->reason;
  164. $check->status = 'True';
  165. $check->save();
  166. return redirect()->back()->with('info', __('Target User already in blacklist. Reason updated'));
  167. }
  168. TicketBlacklist::create([
  169. 'user_id' => $user->id,
  170. 'status' => 'True',
  171. 'reason' => $request->reason,
  172. ]);
  173. return redirect()->back()->with('success', __('Successfully add User to blacklist, User name: '.$user->name));
  174. }
  175. public function blacklistDelete($id)
  176. {
  177. $blacklist = TicketBlacklist::where('id', $id)->first();
  178. $blacklist->delete();
  179. return redirect()->back()->with('success', __('Successfully remove User from blacklist, User name: '.$blacklist->user->name));
  180. }
  181. public function blacklistChange($id)
  182. {
  183. $blacklist = TicketBlacklist::where('id', $id)->first();
  184. if ($blacklist->status == 'True') {
  185. $blacklist->status = 'False';
  186. } else {
  187. $blacklist->status = 'True';
  188. }
  189. $blacklist->update();
  190. return redirect()->back()->with('success', __('Successfully change status blacklist from, User name: '.$blacklist->user->name));
  191. }
  192. public function dataTableBlacklist()
  193. {
  194. $query = TicketBlacklist::with(['user']);
  195. $query->select('ticket_blacklists.*');
  196. return datatables($query)
  197. ->editColumn('user', function (TicketBlacklist $blacklist) {
  198. return '<a href="'.route('admin.users.show', $blacklist->user->id).'">'.$blacklist->user->name.'</a>';
  199. })
  200. ->editColumn('status', function (TicketBlacklist $blacklist) {
  201. switch ($blacklist->status) {
  202. case 'True':
  203. $text = 'Blocked';
  204. $badgeColor = 'badge-danger';
  205. break;
  206. default:
  207. $text = 'Unblocked';
  208. $badgeColor = 'badge-success';
  209. break;
  210. }
  211. return '<span class="badge '.$badgeColor.'">'.$text.'</span>';
  212. })
  213. ->editColumn('reason', function (TicketBlacklist $blacklist) {
  214. return $blacklist->reason;
  215. })
  216. ->addColumn('actions', function (TicketBlacklist $blacklist) {
  217. return '
  218. <form class="d-inline" method="post" action="'.route('moderator.ticket.blacklist.change', ['id' => $blacklist->id]).'">
  219. '.csrf_field().'
  220. '.method_field('POST').'
  221. <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>
  222. </form>
  223. <form class="d-inline" method="post" action="'.route('moderator.ticket.blacklist.delete', ['id' => $blacklist->id]).'">
  224. '.csrf_field().'
  225. '.method_field('POST').'
  226. <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>
  227. </form>
  228. ';
  229. })
  230. ->editColumn('created_at', function (TicketBlacklist $blacklist) {
  231. return $blacklist->created_at ? $blacklist->created_at->diffForHumans() : '';
  232. })
  233. ->rawColumns(['user', 'status', 'reason', 'created_at', 'actions'])
  234. ->make(true);
  235. }
  236. }