api.php 1.5 KB

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