ticket.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use App\Enums\TicketPriorities;
  4. use RuntimeException;
  5. /**
  6. * @var int $id
  7. */
  8. $core = Core::getInstance();
  9. $core->addBreadcrumb("/tickets", __("Tickets"));
  10. $user = $core->getUser();
  11. $ticket = Ticket::get($id);
  12. if(!$ticket) {
  13. throw new RuntimeException(__("Ticket not found."));
  14. }
  15. $core->addBreadcrumb("/tickets/$id", $ticket->subject);
  16. $request = Request::fromRequest();
  17. $ticket_submit = $request->int("ticket_submit");
  18. if($ticket_submit) {
  19. $assign_to_user_id = $request->int("assignee");
  20. $assign_to_user = null;
  21. if($assign_to_user_id) {
  22. $assign_to_user = User::requireStaff($assign_to_user_id);
  23. }
  24. $priority = $request->int("priority");
  25. if(!TicketPriorities::validate($priority)) {
  26. throw new RuntimeException(__("Invalid priority."));
  27. }
  28. if($assign_to_user) {
  29. if(!$assign_to_user->is($ticket->assigned_user_id)) {
  30. $ticket->assign($assign_to_user);
  31. }
  32. }
  33. else {
  34. $ticket->unassign();
  35. }
  36. $ticket->priority = $priority;
  37. $ticket->commit();
  38. Session::addAlert("success", __("Ticket updated successfully."));
  39. Http::redirect($ticket->getUrl());
  40. }
  41. $comment_submit = $request->int("comment_submit");
  42. if($comment_submit) {
  43. $content = $request->string("content");
  44. $internal = (int) $request->int("internal");
  45. $status = $request->int("status");
  46. $attachments = Request::getFileArray("attachments", true);
  47. $comment = new TicketComment();
  48. $comment->ticket_id = $ticket->id;
  49. $comment->user_id = $user->id;
  50. $comment->content = $content;
  51. $comment->internal = $internal;
  52. $comment->dts_created = Database::dts();
  53. $comment->commit();
  54. $ticket->status = $status;
  55. $ticket->commit();
  56. foreach($attachments as $attachment) {
  57. $comment->addAttachment($attachment);
  58. }
  59. Session::addAlert("success", __("Comment added successfully."));
  60. Http::redirect($ticket->getUrl());
  61. }
  62. View::include("header");
  63. View::include("ticket", compact("ticket"));
  64. View::include("footer");