tickets_ajax.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $location_id = $request->int("location_id");
  30. $customer_id = $request->string("customer_id");
  31. $location_name = $request->string("location_name");
  32. $postal_code = $request->string("postal_code");
  33. $location = null;
  34. if($location_id) {
  35. $location = Location::get($location_id);
  36. if(!$location) {
  37. throw new RuntimeException(__("Location not found."));
  38. }
  39. }
  40. else if($customer_id) {
  41. $customer = Customer::get($customer_id);
  42. if(!$customer) {
  43. throw new RuntimeException(__("Customer not found."));
  44. }
  45. if(!$location_name) {
  46. throw new RuntimeException(__("Location name is required."));
  47. }
  48. if(!$postal_code) {
  49. throw new RuntimeException(__("Postcode is required."));
  50. }
  51. $address = new Address();
  52. $address->postal_code = $postal_code;
  53. $address->geocode();
  54. $address->commit();
  55. $location = new Location();
  56. $location->customer_id = $customer->id;
  57. $location->address_id = $address->id;
  58. $location->name = $location_name;
  59. $location->dts_created = Database::dts();
  60. $location->commit();
  61. }
  62. $ticket = new Ticket();
  63. $ticket->location_id = $location->id;
  64. $ticket->creator_user_id = $this->user->id;
  65. $ticket->assigned_user_id = $this->user->id;
  66. $ticket->subject = $request->string("subject");
  67. $ticket->contact_name = $request->string("contact_name");
  68. $ticket->dts_created = Database::dts();
  69. $ticket->commit();
  70. $comment = new TicketComment();
  71. $comment->ticket_id = $ticket->id;
  72. $comment->user_id = $this->user->id;
  73. $comment->content = $request->string("content");
  74. $comment->dts_created = Database::dts();
  75. $comment->commit();
  76. foreach($attachments as $attachment) {
  77. $comment->addAttachment($attachment);
  78. }
  79. Session::addAlert("success", __("Ticket created successfully."));
  80. Json::success([
  81. "id" => $ticket->id
  82. ]);
  83. }
  84. };
  85. $controller->run();