TicketsController.php 10 KB

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