TicketsController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $staffNotify = User::permission('admin.tickets.get_notification')->get();
  58. foreach($staffNotify as $staff){
  59. Notification::send($staff, new AdminCreateNotification($ticket, $user));
  60. }
  61. $user->notify(new CreateNotification($ticket));
  62. return redirect()->route('ticket.index')->with('success', __('A ticket has been opened, ID: #') . $ticket->ticket_id);
  63. }
  64. public function show($ticket_id, PterodactylSettings $ptero_settings)
  65. {
  66. $this->checkPermission(self::READ_PERMISSION);
  67. try {
  68. $ticket = Ticket::where('ticket_id', $ticket_id)->firstOrFail();
  69. } catch (Exception $e) {
  70. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  71. }
  72. $ticketcomments = $ticket->ticketcomments;
  73. $ticketcategory = $ticket->ticketcategory;
  74. $server = Server::where('id', $ticket->server)->first();
  75. $pterodactyl_url = $ptero_settings->panel_url;
  76. return view('ticket.show', compact('ticket', 'ticketcategory', 'ticketcomments', 'server', 'pterodactyl_url'));
  77. }
  78. public function reply(Request $request)
  79. {
  80. //check in blacklist
  81. $check = TicketBlacklist::where('user_id', Auth::user()->id)->first();
  82. if ($check && $check->status == 'True') {
  83. 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"));
  84. }
  85. $this->validate($request, ['ticketcomment' => 'required']);
  86. try {
  87. $ticket = Ticket::where('id', $request->input('ticket_id'))->firstOrFail();
  88. } catch (Exception $e) {
  89. return redirect()->back()->with('warning', __('Ticket not found on the server. It potentially got deleted earlier'));
  90. }
  91. $ticket->status = 'Client Reply';
  92. $ticket->update();
  93. $ticketcomment = TicketComment::create([
  94. 'ticket_id' => $request->input('ticket_id'),
  95. 'user_id' => Auth::user()->id,
  96. 'ticketcomment' => $request->input('ticketcomment'),
  97. 'message' => $request->input('message'),
  98. ]);
  99. $user = Auth::user();
  100. $newmessage = $request->input('ticketcomment');
  101. $staffNotify = User::permission('admin.tickets.get_notification')->get();
  102. foreach($staffNotify as $staff){
  103. Notification::send($staff, new AdminReplyNotification($ticket, $user, $newmessage));
  104. }
  105. return redirect()->back()->with('success', __('Your comment has been submitted'));
  106. }
  107. public function create()
  108. {
  109. $this->checkPermission(self::WRITE_PERMISSION);
  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. }