TicketsController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Server;
  4. use App\Models\Ticket;
  5. use App\Models\TicketBlacklist;
  6. use App\Models\TicketCategory;
  7. use App\Models\TicketComment;
  8. use App\Models\User;
  9. use App\Notifications\Ticket\Admin\AdminCreateNotification;
  10. use App\Notifications\Ticket\Admin\AdminReplyNotification;
  11. use App\Notifications\Ticket\User\CreateNotification;
  12. use App\Settings\LocaleSettings;
  13. use App\Settings\PterodactylSettings;
  14. use App\Settings\TicketSettings;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Notification;
  18. use Illuminate\Support\Str;
  19. class TicketsController extends Controller
  20. {
  21. public function index(LocaleSettings $locale_settings)
  22. {
  23. return view('ticket.index', [
  24. 'tickets' => Ticket::where('user_id', Auth::user()->id)->paginate(10),
  25. 'ticketcategories' => TicketCategory::all(),
  26. 'locale_datatables' => $locale_settings->datatables
  27. ]);
  28. }
  29. public function create()
  30. {
  31. //check in blacklist
  32. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  33. if ($check && $check->status == 'True') {
  34. 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"));
  35. }
  36. $ticketcategories = TicketCategory::all();
  37. $servers = Auth::user()->servers;
  38. return view('ticket.create', compact('ticketcategories', 'servers'));
  39. }
  40. public function store(Request $request, TicketSettings $ticket_settings)
  41. {
  42. $this->validate($request, [
  43. 'title' => 'required',
  44. 'ticketcategory' => 'required',
  45. 'priority' => 'required',
  46. 'message' => 'required',]
  47. );
  48. $ticket = new Ticket([
  49. 'title' => $request->input('title'),
  50. 'user_id' => Auth::user()->id,
  51. 'ticket_id' => strtoupper(Str::random(8)),
  52. 'ticketcategory_id' => $request->input('ticketcategory'),
  53. 'priority' => $request->input('priority'),
  54. 'message' => $request->input('message'),
  55. 'status' => 'Open',
  56. 'server' => $request->input('server'),]
  57. );
  58. $ticket->save();
  59. $user = Auth::user();
  60. switch ($ticket_settings->notify) {
  61. case 'all':
  62. $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
  63. Notification::send($admin, new AdminCreateNotification($ticket, $user));
  64. case 'admin':
  65. $admin = User::where('role', 'admin')->get();
  66. Notification::send($admin, new AdminCreateNotification($ticket, $user));
  67. case 'moderator':
  68. $admin = User::where('role', 'mod')->get();
  69. Notification::send($admin, new AdminCreateNotification($ticket, $user));
  70. }
  71. $user->notify(new CreateNotification($ticket));
  72. return redirect()->route('ticket.index')->with('success', __('A ticket has been opened, ID: #') . $ticket->ticket_id);
  73. }
  74. public function show($ticket_id, PterodactylSettings $ptero_settings)
  75. {
  76. try {
  77. $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
  78. } catch (Exception $e) {
  79. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  80. }
  81. $ticketcomments = $ticket->ticketcomments;
  82. $ticketcategory = $ticket->ticketcategory;
  83. $server = Server::where('id', $ticket->server)->first();
  84. $pterodactyl_url = $ptero_settings->panel_url;
  85. return view('ticket.show', compact('ticket', 'ticketcategory', 'ticketcomments', 'server', 'pterodactyl_url'));
  86. }
  87. public function reply(Request $request)
  88. {
  89. //check in blacklist
  90. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  91. if ($check && $check->status == 'True') {
  92. 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"));
  93. }
  94. $this->validate($request, ['ticketcomment' => 'required']);
  95. try {
  96. $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
  97. } catch (Exception $e) {
  98. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  99. }
  100. $ticket->status = 'Client Reply';
  101. $ticket->update();
  102. $ticketcomment = TicketComment::create([
  103. 'ticket_id' => $request->input('ticket_id'),
  104. 'user_id' => Auth::user()->id,
  105. 'ticketcomment' => $request->input('ticketcomment'),
  106. 'message' => $request->input('message'),
  107. ]);
  108. $user = Auth::user();
  109. $admin = User::where('role', 'admin')->orWhere('role', 'mod')->get();
  110. $newmessage = $request->input('ticketcomment');
  111. Notification::send($admin, new AdminReplyNotification($ticket, $user, $newmessage));
  112. return redirect()->back()->with('success', __('Your comment has been submitted'));
  113. }
  114. public function create()
  115. {
  116. //check in blacklist
  117. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  118. if ($check && $check->status == 'True') {
  119. 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"));
  120. }
  121. $ticketcategories = TicketCategory::all();
  122. $servers = Auth::user()->servers;
  123. return view('ticket.create', compact('ticketcategories', 'servers'));
  124. }
  125. public function changeStatus($ticket_id)
  126. {
  127. try {
  128. $ticket = Ticket::where('user_id', Auth::user()->id)->where("ticket_id", $ticket_id)->firstOrFail();
  129. } catch (Exception $e) {
  130. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  131. }
  132. if ($ticket->status == "Closed") {
  133. $ticket->status = "Reopened";
  134. $ticket->save();
  135. return redirect()->back()->with('success', __('A ticket has been reopened, ID: #') . $ticket->ticket_id);
  136. }
  137. $ticket->status = "Closed";
  138. $ticket->save();
  139. return redirect()->back()->with('success', __('A ticket has been closed, ID: #') . $ticket->ticket_id);
  140. }
  141. public function dataTable()
  142. {
  143. $query = Ticket::where('user_id', Auth::user()->id)->get();
  144. return datatables($query)
  145. ->addColumn('category', function (Ticket $tickets) {
  146. return $tickets->ticketcategory->name;
  147. })
  148. ->editColumn('title', function (Ticket $tickets) {
  149. return '<a class="text-info" href="' . route('ticket.show', ['ticket_id' => $tickets->ticket_id]) . '">' . '#' . $tickets->ticket_id . ' - ' . htmlspecialchars($tickets->title) . '</a>';
  150. })
  151. ->editColumn('status', function (Ticket $tickets) {
  152. switch ($tickets->status) {
  153. case 'Reopened':
  154. case 'Open':
  155. $badgeColor = 'badge-success';
  156. break;
  157. case 'Closed':
  158. $badgeColor = 'badge-danger';
  159. break;
  160. case 'Answered':
  161. $badgeColor = 'badge-info';
  162. break;
  163. default:
  164. $badgeColor = 'badge-warning';
  165. break;
  166. }
  167. return '<span class="badge ' . $badgeColor . '">' . $tickets->status . '</span>';
  168. })
  169. ->editColumn('priority', function (Ticket $tickets) {
  170. return __($tickets->priority);
  171. })
  172. ->editColumn('updated_at', function (Ticket $tickets) {
  173. return ['display' => $tickets->updated_at ? $tickets->updated_at->diffForHumans() : '',
  174. 'raw' => $tickets->updated_at ? strtotime($tickets->updated_at) : ''];
  175. })
  176. ->addColumn('actions', function (Ticket $tickets) {
  177. $statusButtonColor = ($tickets->status == "Closed") ? 'btn-success' : 'btn-warning';
  178. $statusButtonIcon = ($tickets->status == "Closed") ? 'fa-redo' : 'fa-times';
  179. $statusButtonText = ($tickets->status == "Closed") ? __('Reopen') : __('Close');
  180. return '
  181. <a data-content="' . __('View') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('ticket.show', ['ticket_id' => $tickets->ticket_id]) . '" class="btn btn-sm text-white btn-info mr-1"><i class="fas fa-eye"></i></a>
  182. <form class="d-inline" method="post" action="' . route('ticket.changeStatus', ['ticket_id' => $tickets->ticket_id]) . '">
  183. ' . csrf_field() . '
  184. ' . method_field('POST') . '
  185. <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>
  186. </form>
  187. </form>
  188. ';
  189. })
  190. ->rawColumns(['category', 'title', 'status', 'updated_at', "actions"])
  191. ->make(true);
  192. }
  193. }