tickets.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use App\Constraint\NotEqualsConstraint;
  4. use App\Constraint\NotInConstraint;
  5. use App\Enums\TicketStatuses;
  6. use RuntimeException;
  7. $core = Core::getInstance();
  8. $core->addBreadcrumb("/tickets", __("Tickets"));
  9. $core = Core::getInstance();
  10. $core->addScript("/js/ticket-modal.js");
  11. $request = Request::fromRequest();
  12. $batch_submit = $request->int("batch_submit");
  13. if($batch_submit) {
  14. $ticket_ids = $request->int("ticket_ids");
  15. $tickets = [];
  16. foreach($ticket_ids as $ticket_id) {
  17. $ticket = Ticket::get($ticket_id);
  18. if(!$ticket) {
  19. throw new RuntimeException(__("Ticket not found."));
  20. }
  21. $tickets[] = $ticket;
  22. }
  23. $assign_to_user = null;
  24. $assign_to_user_id = $request->int("assign_to");
  25. if($assign_to_user_id) {
  26. $assign_to_user = User::requireStaff($assign_to_user_id);
  27. }
  28. $status = $request->int("status");
  29. if($status !== null && !TicketStatuses::validate($status)) {
  30. throw new RuntimeException(__("Invalid status."));
  31. }
  32. foreach($tickets as $ticket) {
  33. if($assign_to_user_id) {
  34. $ticket->assign($assign_to_user);
  35. }
  36. if($status !== null) {
  37. $ticket->status = $status;
  38. $ticket->commit();
  39. }
  40. }
  41. Session::addAlert("success", __("Batch action applied successfully."));
  42. Http::redirect("/tickets");
  43. }
  44. $nvp = [
  45. "dts_deleted" => null
  46. ];
  47. $customer_id = $request->int("customer_id");
  48. if($customer_id) {
  49. $customer = Customer::get($customer_id);
  50. if($customer) {
  51. $nvp["location_id"] = array_column($customer->getLocations(), "id");
  52. }
  53. else {
  54. $nvp["location_id"] = 0;
  55. }
  56. }
  57. $location_id = $request->int("location_id");
  58. if($location_id) {
  59. $nvp["location_id"] = $location_id;
  60. }
  61. $status = $request->int("status");
  62. if($status !== null) {
  63. $nvp["status"] = $status;
  64. }
  65. $number = $request->int("number");
  66. if($number !== null) {
  67. $nvp["id"] = $number;
  68. }
  69. $assigned_user_id = $request->int("assigned_user_id");
  70. if($assigned_user_id !== null) {
  71. $nvp["assigned_user_id"] = $assigned_user_id;
  72. }
  73. $custom_filter = $request->string("custom_filter");
  74. switch($custom_filter) {
  75. case "open":
  76. $nvp["status"] = new NotEqualsConstraint(TicketStatuses::CLOSED);
  77. break;
  78. case "unsolved":
  79. $nvp["status"] = new NotInConstraint(TicketStatuses::CLOSED, TicketStatuses::SOLVED);
  80. break;
  81. case "unassigned":
  82. $nvp["assigned_user_id"] = null;
  83. break;
  84. }
  85. $count = Ticket::count($nvp);
  86. $pagination = new Pagination($count);
  87. $pagination->setTerms(__("%s ticket"), __("%s tickets"));
  88. $tickets = Ticket::query($nvp, [
  89. "sort" => "dts_updated",
  90. "order" => "desc",
  91. "offset" => $pagination->getOffset(),
  92. "limit" => $pagination->getPageSize()
  93. ]);
  94. View::include("header");
  95. View::include("tickets", compact("tickets", "pagination", "customer_id", "location_id", "status", "assigned_user_id", "custom_filter"));
  96. View::include("modals/ticket");
  97. View::include("footer");