api.php 1.3 KB

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