tickets_ajax.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App;
  3. use RuntimeException;
  4. /**
  5. * @var string $action
  6. */
  7. $controller = new class($action) {
  8. /** @var string */
  9. private $action;
  10. /** @var User */
  11. private $user;
  12. public function __construct(string $action) {
  13. $this->action = $action;
  14. $core = Core::getInstance();
  15. $this->user = $core->getUser();
  16. }
  17. public function run(): void {
  18. switch($this->action) {
  19. case "create":
  20. $this->create();
  21. break;
  22. default:
  23. throw new RuntimeException(__("Invalid action."));
  24. }
  25. }
  26. private function create(): void {
  27. $attachments = Request::getFileArray("attachments", true);
  28. $request = Request::fromRequest();
  29. $ticket = new Ticket();
  30. $ticket->location_id = $request->int("location_id");
  31. $ticket->creator_user_id = $this->user->id;
  32. $ticket->subject = $request->string("subject");
  33. $ticket->contact_name = $request->string("contact_name");
  34. $ticket->dts_created = Database::dts();
  35. $ticket->commit();
  36. $comment = new TicketComment();
  37. $comment->ticket_id = $ticket->id;
  38. $comment->user_id = $this->user->id;
  39. $comment->content = $request->string("content");
  40. $comment->dts_created = Database::dts();
  41. $comment->commit();
  42. foreach($attachments as $attachment) {
  43. $comment->addAttachment($attachment);
  44. }
  45. Session::addAlert("success", __("Ticket created successfully."));
  46. Json::success([
  47. "id" => $ticket->id
  48. ]);
  49. }
  50. };
  51. $controller->run();