action = $action; $core = Core::getInstance(); $this->user = $core->getUser(); } public function run(): void { switch($this->action) { case "create": $this->create(); break; default: throw new RuntimeException(__("Invalid action.")); } } private function create(): void { $attachments = Request::getFileArray("attachments", true); $request = Request::fromRequest(); $location_id = $request->int("location_id"); $customer_id = $request->string("customer_id"); $location_name = $request->string("location_name"); $postal_code = $request->string("postal_code"); $location = null; if($location_id) { $location = Location::get($location_id); if(!$location) { throw new RuntimeException(__("Location not found.")); } } else if($customer_id) { $customer = Customer::get($customer_id); if(!$customer) { throw new RuntimeException(__("Customer not found.")); } if(!$location_name) { throw new RuntimeException(__("Location name is required.")); } if(!$postal_code) { throw new RuntimeException(__("Postcode is required.")); } $address = new Address(); $address->postal_code = $postal_code; try { $results = Geolocation::geocode($postal_code); $result = $results[0]; $address->latitude = $result->geometry->location->lat; $address->longitude = $result->geometry->location->lng; } catch(RuntimeException $e) { } $address->commit(); $location = new Location(); $location->customer_id = $customer->id; $location->address_id = $address->id; $location->name = $location_name; $location->dts_created = Database::dts(); $location->commit(); } $ticket = new Ticket(); $ticket->location_id = $location->id; $ticket->creator_user_id = $this->user->id; $ticket->assigned_user_id = $this->user->id; $ticket->subject = $request->string("subject"); $ticket->contact_name = $request->string("contact_name"); $ticket->dts_created = Database::dts(); $ticket->commit(); $comment = new TicketComment(); $comment->ticket_id = $ticket->id; $comment->user_id = $this->user->id; $comment->content = $request->string("content"); $comment->dts_created = Database::dts(); $comment->commit(); foreach($attachments as $attachment) { $comment->addAttachment($attachment); } Session::addAlert("success", __("Ticket created successfully.")); Json::success([ "id" => $ticket->id ]); } }; $controller->run();