1
0

api.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App;
  3. use RuntimeException;
  4. /**
  5. * @var string $action
  6. */
  7. $request = Request::fromRequest();
  8. header("Content-Type: application/json");
  9. try {
  10. switch($action) {
  11. case "get-markers":
  12. $customer_id = $request->int("customer_id");
  13. $customer = Customer::get($customer_id);
  14. if(!$customer) {
  15. throw new RuntimeException(__("Customer not found."));
  16. }
  17. $markers = [];
  18. $locations = $customer->getLocations();
  19. Address::load(array_column($locations, "address_id"));
  20. foreach($locations as $location) {
  21. $address = $location->getAddress();
  22. if($address->hasGeolocation()) {
  23. $markers[] = [
  24. "id" => (int) $location->id,
  25. "name" => $location->name,
  26. "latLng" => [
  27. "lat" => (float) $address->latitude,
  28. "lng" => (float) $address->longitude,
  29. ],
  30. "url" => $location->getUrl(),
  31. ];
  32. }
  33. }
  34. echo json_encode([
  35. "success" => true,
  36. "markers" => $markers
  37. ]);
  38. break;
  39. default:
  40. throw new RuntimeException(__("Invalid action."));
  41. }
  42. }
  43. catch(\Exception $e) {
  44. http_response_code(400);
  45. echo json_encode([
  46. "success" => false,
  47. "message" => $e->getMessage()
  48. ]);
  49. }