tickets.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use App\Enums\TicketStatuses;
  4. use RuntimeException;
  5. $core = Core::getInstance();
  6. $core->addBreadcrumb("/tickets", __("Tickets"));
  7. $core = Core::getInstance();
  8. $core->addScript("/js/ticket-modal.js");
  9. $request = Request::fromRequest();
  10. $batch_submit = $request->int("batch_submit");
  11. if($batch_submit) {
  12. $ticket_ids = $request->int("ticket_ids");
  13. $tickets = [];
  14. foreach($ticket_ids as $ticket_id) {
  15. $ticket = Ticket::get($ticket_id);
  16. if(!$ticket) {
  17. throw new RuntimeException(__("Ticket not found."));
  18. }
  19. $tickets[] = $ticket;
  20. }
  21. $assign_to_user = null;
  22. $assign_to_user_id = $request->int("assign_to");
  23. if($assign_to_user_id) {
  24. $assign_to_user = User::get($assign_to_user_id);
  25. if(!$assign_to_user) {
  26. throw new RuntimeException(__("User not found."));
  27. }
  28. if(!$assign_to_user->isAdmin()) {
  29. throw new RuntimeException(__("%s is not staff.", $assign_to_user->getDisplayName()));
  30. }
  31. }
  32. $status = $request->int("status");
  33. if($status !== null && !TicketStatuses::validate($status)) {
  34. throw new RuntimeException(__("Invalid status."));
  35. }
  36. foreach($tickets as $ticket) {
  37. if($assign_to_user_id) {
  38. $ticket->assign($assign_to_user);
  39. }
  40. if($status !== null) {
  41. $ticket->status = $status;
  42. $ticket->commit();
  43. }
  44. }
  45. Session::addAlert("success", __("Batch action applied successfully."));
  46. Http::redirect("/tickets");
  47. }
  48. $nvp = [
  49. "dts_deleted" => null
  50. ];
  51. $tickets = Ticket::query($nvp, ["sort" => "dts_updated", "order" => "desc"]);
  52. View::include("header");
  53. View::include("tickets", compact("tickets"));
  54. View::include("modals/ticket");
  55. View::include("footer");