TicketsController.php 9.2 KB

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