TicketsController.php 9.4 KB

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