| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace App;
- use RuntimeException;
- /**
- * @var string $action
- */
- $request = Request::fromRequest();
- header("Content-Type: application/json");
- try {
- switch($action) {
- case "get-markers":
- $customer_id = $request->int("customer_id");
- $customer = Customer::get($customer_id);
- if(!$customer) {
- throw new RuntimeException(__("Customer not found."));
- }
- $markers = [];
- $locations = $customer->getLocations();
- Address::load(array_column($locations, "address_id"));
- foreach($locations as $location) {
- $address = $location->getAddress();
- if($address->hasGeolocation()) {
- $markers[] = [
- "id" => (int) $location->id,
- "name" => $location->name,
- "latLng" => [
- "lat" => (float) $address->latitude,
- "lng" => (float) $address->longitude,
- ],
- "url" => $location->getUrl(),
- ];
- }
- }
- echo json_encode([
- "success" => true,
- "markers" => $markers
- ]);
- break;
- default:
- throw new RuntimeException(__("Invalid action."));
- }
- }
- catch(\Exception $e) {
- http_response_code(400);
- echo json_encode([
- "success" => false,
- "message" => $e->getMessage()
- ]);
- }
|