tickets_ajax.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. try {
  54. $results = Geolocation::geocode($postal_code);
  55. $result = $results[0];
  56. $address->latitude = $result->geometry->location->lat;
  57. $address->longitude = $result->geometry->location->lng;
  58. }
  59. catch(RuntimeException $e) {
  60. }
  61. $address->commit();
  62. $location = new Location();
  63. $location->customer_id = $customer->id;
  64. $location->address_id = $address->id;
  65. $location->name = $location_name;
  66. $location->dts_created = Database::dts();
  67. $location->commit();
  68. }
  69. $ticket = new Ticket();
  70. $ticket->location_id = $location->id;
  71. $ticket->creator_user_id = $this->user->id;
  72. $ticket->assigned_user_id = $this->user->id;
  73. $ticket->subject = $request->string("subject");
  74. $ticket->contact_name = $request->string("contact_name");
  75. $ticket->dts_created = Database::dts();
  76. $ticket->commit();
  77. $comment = new TicketComment();
  78. $comment->ticket_id = $ticket->id;
  79. $comment->user_id = $this->user->id;
  80. $comment->content = $request->string("content");
  81. $comment->dts_created = Database::dts();
  82. $comment->commit();
  83. foreach($attachments as $attachment) {
  84. $comment->addAttachment($attachment);
  85. }
  86. Session::addAlert("success", __("Ticket created successfully."));
  87. Json::success([
  88. "id" => $ticket->id
  89. ]);
  90. }
  91. };
  92. $controller->run();