tickets.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $tickets[] = Ticket::require($ticket_id);
  18. }
  19. $assign_to_user = null;
  20. $assign_to_user_id = $request->int("assign_to");
  21. if($assign_to_user_id) {
  22. $assign_to_user = User::requireStaff($assign_to_user_id);
  23. }
  24. $status = $request->int("status");
  25. if($status !== null && !TicketStatuses::validate($status)) {
  26. throw new RuntimeException(__("Invalid status."));
  27. }
  28. foreach($tickets as $ticket) {
  29. if($assign_to_user_id) {
  30. $ticket->assign($assign_to_user);
  31. }
  32. if($status !== null) {
  33. $ticket->status = $status;
  34. $ticket->commit();
  35. }
  36. }
  37. Session::addAlert("success", __("Batch action applied successfully."));
  38. Http::redirect("/tickets");
  39. }
  40. $nvp = [
  41. "dts_deleted" => null
  42. ];
  43. $customer_id = $request->int("customer_id");
  44. if($customer_id) {
  45. $customer = Customer::get($customer_id);
  46. if($customer) {
  47. $nvp["location_id"] = array_column($customer->getLocations(), "id");
  48. }
  49. else {
  50. $nvp["location_id"] = 0;
  51. }
  52. }
  53. $location_id = $request->int("location_id");
  54. if($location_id) {
  55. $nvp["location_id"] = $location_id;
  56. }
  57. $status = $request->int("status");
  58. if($status !== null) {
  59. $nvp["status"] = $status;
  60. }
  61. $number = $request->int("number");
  62. if($number !== null) {
  63. $nvp["id"] = $number;
  64. }
  65. $assigned_user_id = $request->int("assigned_user_id");
  66. if($assigned_user_id !== null) {
  67. $nvp["assigned_user_id"] = $assigned_user_id;
  68. }
  69. $custom_filter = $request->string("custom_filter");
  70. switch($custom_filter) {
  71. case "open":
  72. $nvp["status"] = new NotEqualsConstraint(TicketStatuses::CLOSED);
  73. break;
  74. case "unsolved":
  75. $nvp["status"] = new NotInConstraint(TicketStatuses::CLOSED, TicketStatuses::SOLVED);
  76. break;
  77. case "unassigned":
  78. $nvp["assigned_user_id"] = null;
  79. break;
  80. }
  81. $count = Ticket::count($nvp);
  82. $pagination = new Pagination($count);
  83. $pagination->setTerms(__("%s ticket"), __("%s tickets"));
  84. $tickets = Ticket::query($nvp, [
  85. "sort" => "dts_updated",
  86. "order" => "desc",
  87. "offset" => $pagination->getOffset(),
  88. "limit" => $pagination->getPageSize()
  89. ]);
  90. View::include("header");
  91. View::include("tickets", compact("tickets", "pagination", "customer_id", "location_id", "status", "assigned_user_id", "custom_filter"));
  92. View::include("modals/ticket");
  93. View::include("footer");