| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App;
- use App\Constraint\HasTextConstraint;
- 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::require($customer_id);
- $markers = [];
- $nvp = [];
- $location_id = $request->int("location_id");
- if($location_id) {
- $nvp["id"] = $location_id;
- }
- $name = $request->string("name");
- if($name) {
- $nvp["name"] = new HasTextConstraint($name);
- }
- $locations = $customer->getLocations($nvp);
- 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()
- ]);
- }
|