From 208d46ff28c52672281d0c7debf20f1b2d47c95d Mon Sep 17 00:00:00 2001 From: isUnknown Date: Fri, 6 Feb 2026 11:58:54 +0100 Subject: [PATCH] refactor: one PHP file per route with subdirectories Properly organize routes following design-to-pack pattern: - routes/markers/get.php - routes/markers/create.php - routes/markers/update.php - routes/markers/delete.php - routes/position/update.php - routes/image/capture.php - routes/image/check-flag.php - routes/image/clear-flag.php Each route in its own file for better maintainability. Co-Authored-By: Claude Sonnet 4.5 --- public/site/plugins/map-editor/index.php | 11 +- .../site/plugins/map-editor/routes/image.php | 185 ---------- .../map-editor/routes/image/capture.php | 109 ++++++ .../map-editor/routes/image/check-flag.php | 39 ++ .../map-editor/routes/image/clear-flag.php | 41 +++ .../plugins/map-editor/routes/markers.php | 346 ------------------ .../map-editor/routes/markers/create.php | 114 ++++++ .../map-editor/routes/markers/delete.php | 57 +++ .../plugins/map-editor/routes/markers/get.php | 84 +++++ .../map-editor/routes/markers/update.php | 99 +++++ .../{position.php => position/update.php} | 3 +- 11 files changed, 552 insertions(+), 536 deletions(-) delete mode 100644 public/site/plugins/map-editor/routes/image.php create mode 100644 public/site/plugins/map-editor/routes/image/capture.php create mode 100644 public/site/plugins/map-editor/routes/image/check-flag.php create mode 100644 public/site/plugins/map-editor/routes/image/clear-flag.php delete mode 100644 public/site/plugins/map-editor/routes/markers.php create mode 100644 public/site/plugins/map-editor/routes/markers/create.php create mode 100644 public/site/plugins/map-editor/routes/markers/delete.php create mode 100644 public/site/plugins/map-editor/routes/markers/get.php create mode 100644 public/site/plugins/map-editor/routes/markers/update.php rename public/site/plugins/map-editor/routes/{position.php => position/update.php} (96%) diff --git a/public/site/plugins/map-editor/index.php b/public/site/plugins/map-editor/index.php index e9c4643..0cf7ee4 100644 --- a/public/site/plugins/map-editor/index.php +++ b/public/site/plugins/map-editor/index.php @@ -51,9 +51,14 @@ Kirby::plugin('geoproject/map-editor', [ ], 'api' => [ 'routes' => [ - require __DIR__ . '/routes/markers.php', - require __DIR__ . '/routes/position.php', - require __DIR__ . '/routes/image.php', + require __DIR__ . '/routes/markers/get.php', + require __DIR__ . '/routes/markers/create.php', + require __DIR__ . '/routes/markers/update.php', + require __DIR__ . '/routes/markers/delete.php', + require __DIR__ . '/routes/position/update.php', + require __DIR__ . '/routes/image/capture.php', + require __DIR__ . '/routes/image/check-flag.php', + require __DIR__ . '/routes/image/clear-flag.php', ] ], 'hooks' => [ diff --git a/public/site/plugins/map-editor/routes/image.php b/public/site/plugins/map-editor/routes/image.php deleted file mode 100644 index 73aa889..0000000 --- a/public/site/plugins/map-editor/routes/image.php +++ /dev/null @@ -1,185 +0,0 @@ - 'map-editor/pages/(:all)/capture-image', - 'method' => 'POST', - 'auth' => false, - 'action' => function (string $pageId) { - try { - $user = kirby()->user(); - - if (!$user && !kirby()->option('debug', false)) { - return [ - 'status' => 'error', - 'message' => 'Unauthorized', - 'code' => 401 - ]; - } - - $mapPage = kirby()->page($pageId); - if (!$mapPage) { - return [ - 'status' => 'error', - 'message' => 'Map page not found', - 'code' => 404 - ]; - } - - if (!$mapPage->permissions()->can('update')) { - return [ - 'status' => 'error', - 'message' => 'Forbidden', - 'code' => 403 - ]; - } - - $data = kirby()->request()->data(); - - if (!isset($data['image'])) { - return [ - 'status' => 'error', - 'message' => 'Image data is required', - 'code' => 400 - ]; - } - - $imageData = $data['image']; - if (preg_match('/^data:image\/(png|jpeg|jpg);base64,(.+)$/', $imageData, $matches)) { - $imageData = $matches[2]; - $extension = $matches[1] === 'jpeg' ? 'jpg' : $matches[1]; - } else { - return [ - 'status' => 'error', - 'message' => 'Invalid image format', - 'code' => 400 - ]; - } - - $decodedImage = base64_decode($imageData); - if ($decodedImage === false) { - return [ - 'status' => 'error', - 'message' => 'Failed to decode image', - 'code' => 400 - ]; - } - - $filename = 'map-static.' . $extension; - $tempPath = sys_get_temp_dir() . '/' . uniqid() . '.' . $extension; - file_put_contents($tempPath, $decodedImage); - - $existingFile = $mapPage->files()->filterBy('name', 'map-static')->first(); - if ($existingFile) { - $existingFile->delete(); - } - - try { - $file = $mapPage->createFile([ - 'source' => $tempPath, - 'filename' => $filename - ]); - - @unlink($tempPath); - } catch (Exception $e) { - @unlink($tempPath); - throw $e; - } - - return [ - 'status' => 'success', - 'data' => [ - 'message' => 'Image saved successfully', - 'filename' => $filename, - 'path' => $file->root() - ] - ]; - - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ], - - // GET check if regeneration flag exists - [ - 'pattern' => 'map-editor/pages/(:all)/check-regenerate-flag', - 'method' => 'GET', - 'auth' => false, - 'action' => function (string $pageId) { - try { - $page = kirby()->page($pageId); - if (!$page) { - return [ - 'status' => 'error', - 'message' => 'Page not found', - 'code' => 404 - ]; - } - - $markerFile = $page->root() . '/.regenerate-map-image'; - $needsRegeneration = file_exists($markerFile); - - return [ - 'status' => 'success', - 'data' => [ - 'needsRegeneration' => $needsRegeneration - ] - ]; - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ], - - // DELETE clear regeneration flag - [ - 'pattern' => 'map-editor/pages/(:all)/clear-regenerate-flag', - 'method' => 'DELETE', - 'auth' => false, - 'action' => function (string $pageId) { - try { - $page = kirby()->page($pageId); - if (!$page) { - return [ - 'status' => 'error', - 'message' => 'Page not found', - 'code' => 404 - ]; - } - - $markerFile = $page->root() . '/.regenerate-map-image'; - if (file_exists($markerFile)) { - unlink($markerFile); - } - - return [ - 'status' => 'success', - 'data' => [ - 'message' => 'Flag cleared' - ] - ]; - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ] -]; diff --git a/public/site/plugins/map-editor/routes/image/capture.php b/public/site/plugins/map-editor/routes/image/capture.php new file mode 100644 index 0000000..2afd67d --- /dev/null +++ b/public/site/plugins/map-editor/routes/image/capture.php @@ -0,0 +1,109 @@ + 'map-editor/pages/(:all)/capture-image', + 'method' => 'POST', + 'auth' => false, + 'action' => function (string $pageId) { + try { + $user = kirby()->user(); + + if (!$user && !kirby()->option('debug', false)) { + return [ + 'status' => 'error', + 'message' => 'Unauthorized', + 'code' => 401 + ]; + } + + $mapPage = kirby()->page($pageId); + if (!$mapPage) { + return [ + 'status' => 'error', + 'message' => 'Map page not found', + 'code' => 404 + ]; + } + + if (!$mapPage->permissions()->can('update')) { + return [ + 'status' => 'error', + 'message' => 'Forbidden', + 'code' => 403 + ]; + } + + $data = kirby()->request()->data(); + + if (!isset($data['image'])) { + return [ + 'status' => 'error', + 'message' => 'Image data is required', + 'code' => 400 + ]; + } + + $imageData = $data['image']; + if (preg_match('/^data:image\/(png|jpeg|jpg);base64,(.+)$/', $imageData, $matches)) { + $imageData = $matches[2]; + $extension = $matches[1] === 'jpeg' ? 'jpg' : $matches[1]; + } else { + return [ + 'status' => 'error', + 'message' => 'Invalid image format', + 'code' => 400 + ]; + } + + $decodedImage = base64_decode($imageData); + if ($decodedImage === false) { + return [ + 'status' => 'error', + 'message' => 'Failed to decode image', + 'code' => 400 + ]; + } + + $filename = 'map-static.' . $extension; + $tempPath = sys_get_temp_dir() . '/' . uniqid() . '.' . $extension; + file_put_contents($tempPath, $decodedImage); + + $existingFile = $mapPage->files()->filterBy('name', 'map-static')->first(); + if ($existingFile) { + $existingFile->delete(); + } + + try { + $file = $mapPage->createFile([ + 'source' => $tempPath, + 'filename' => $filename + ]); + + @unlink($tempPath); + } catch (Exception $e) { + @unlink($tempPath); + throw $e; + } + + return [ + 'status' => 'success', + 'data' => [ + 'message' => 'Image saved successfully', + 'filename' => $filename, + 'path' => $file->root() + ] + ]; + + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/image/check-flag.php b/public/site/plugins/map-editor/routes/image/check-flag.php new file mode 100644 index 0000000..ce509fd --- /dev/null +++ b/public/site/plugins/map-editor/routes/image/check-flag.php @@ -0,0 +1,39 @@ + 'map-editor/pages/(:all)/check-regenerate-flag', + 'method' => 'GET', + 'auth' => false, + 'action' => function (string $pageId) { + try { + $page = kirby()->page($pageId); + if (!$page) { + return [ + 'status' => 'error', + 'message' => 'Page not found', + 'code' => 404 + ]; + } + + $markerFile = $page->root() . '/.regenerate-map-image'; + $needsRegeneration = file_exists($markerFile); + + return [ + 'status' => 'success', + 'data' => [ + 'needsRegeneration' => $needsRegeneration + ] + ]; + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/image/clear-flag.php b/public/site/plugins/map-editor/routes/image/clear-flag.php new file mode 100644 index 0000000..423516e --- /dev/null +++ b/public/site/plugins/map-editor/routes/image/clear-flag.php @@ -0,0 +1,41 @@ + 'map-editor/pages/(:all)/clear-regenerate-flag', + 'method' => 'DELETE', + 'auth' => false, + 'action' => function (string $pageId) { + try { + $page = kirby()->page($pageId); + if (!$page) { + return [ + 'status' => 'error', + 'message' => 'Page not found', + 'code' => 404 + ]; + } + + $markerFile = $page->root() . '/.regenerate-map-image'; + if (file_exists($markerFile)) { + unlink($markerFile); + } + + return [ + 'status' => 'success', + 'data' => [ + 'message' => 'Flag cleared' + ] + ]; + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/markers.php b/public/site/plugins/map-editor/routes/markers.php deleted file mode 100644 index c6c94d6..0000000 --- a/public/site/plugins/map-editor/routes/markers.php +++ /dev/null @@ -1,346 +0,0 @@ - 'map-editor/pages/(:all)/markers', - 'method' => 'GET', - 'auth' => false, - 'action' => function (string $pageId) { - try { - $user = kirby()->user(); - - if (!$user && !kirby()->option('debug', false)) { - return [ - 'status' => 'error', - 'message' => 'Unauthorized', - 'code' => 401 - ]; - } - - $mapPage = kirby()->page($pageId); - if (!$mapPage) { - return [ - 'status' => 'error', - 'message' => 'Map page not found', - 'code' => 404 - ]; - } - - if (!$mapPage->isReadable()) { - return [ - 'status' => 'error', - 'message' => 'Forbidden', - 'code' => 403 - ]; - } - - $markerPages = $mapPage - ->children() - ->listed() - ->filterBy('intendedTemplate', 'marker') - ->sortBy('num', 'asc'); - - $markers = []; - foreach ($markerPages as $marker) { - $iconFile = $marker->markerIcon()->toFile(); - $iconUrl = $iconFile ? $iconFile->url() : null; - $iconSize = $marker->markerIconSize()->isNotEmpty() - ? (int) $marker->markerIconSize()->value() - : 40; - - $markers[] = [ - 'id' => $marker->id(), - 'slug' => $marker->slug(), - 'title' => $marker->title()->value(), - 'position' => [ - 'lat' => (float) $marker->latitude()->value(), - 'lon' => (float) $marker->longitude()->value() - ], - 'num' => $marker->num(), - 'panelUrl' => (string) $marker->panel()->url(), - 'iconUrl' => $iconUrl, - 'iconSize' => $iconSize - ]; - } - - return [ - 'status' => 'success', - 'data' => [ - 'markers' => $markers - ] - ]; - - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ], - - // POST create new marker - [ - 'pattern' => 'map-editor/pages/(:all)/markers', - 'method' => 'POST', - 'auth' => false, - 'action' => function (string $pageId) { - try { - $user = kirby()->user(); - - if (!$user && !kirby()->option('debug', false)) { - return [ - 'status' => 'error', - 'message' => 'Unauthorized', - 'code' => 401 - ]; - } - - $mapPage = kirby()->page($pageId); - if (!$mapPage) { - return [ - 'status' => 'error', - 'message' => 'Map page not found', - 'code' => 404 - ]; - } - - if (!$mapPage->permissions()->can('create')) { - return [ - 'status' => 'error', - 'message' => 'Forbidden', - 'code' => 403 - ]; - } - - $data = kirby()->request()->data(); - - if (!isset($data['position']['lat']) || !isset($data['position']['lon'])) { - return [ - 'status' => 'error', - 'message' => 'Position (lat, lon) is required', - 'code' => 400 - ]; - } - - $lat = (float) $data['position']['lat']; - $lon = (float) $data['position']['lon']; - - if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) { - return [ - 'status' => 'error', - 'message' => 'Invalid coordinates', - 'code' => 400 - ]; - } - - $existingMarkers = $mapPage - ->children() - ->filterBy('intendedTemplate', 'marker'); - $nextNum = $existingMarkers->count() + 1; - - $title = 'Marqueur ' . $nextNum; - $slug = Str::slug($title); - - $newMarker = $mapPage->createChild([ - 'slug' => $slug, - 'template' => 'marker', - 'content' => [ - 'title' => $title, - 'latitude' => $lat, - 'longitude' => $lon - ] - ]); - - $newMarker->changeStatus('listed', $nextNum); - - $iconFile = $newMarker->markerIcon()->toFile(); - $iconUrl = $iconFile ? $iconFile->url() : null; - $iconSize = $newMarker->markerIconSize()->isNotEmpty() - ? (int) $newMarker->markerIconSize()->value() - : 40; - - return [ - 'status' => 'success', - 'data' => [ - 'marker' => [ - 'id' => $newMarker->id(), - 'slug' => $newMarker->slug(), - 'title' => $title, - 'position' => [ - 'lat' => $lat, - 'lon' => $lon - ], - 'num' => $nextNum, - 'panelUrl' => (string) $newMarker->panel()->url(), - 'iconUrl' => $iconUrl, - 'iconSize' => $iconSize - ] - ] - ]; - - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ], - - // PATCH update marker position - [ - 'pattern' => 'map-editor/pages/(:all)/markers/(:all)', - 'method' => 'PATCH', - 'auth' => false, - 'action' => function (string $pageId, string $markerId) { - try { - $user = kirby()->user(); - - if (!$user && !kirby()->option('debug', false)) { - return [ - 'status' => 'error', - 'message' => 'Unauthorized', - 'code' => 401 - ]; - } - - $marker = kirby()->page($markerId); - if (!$marker) { - return [ - 'status' => 'error', - 'message' => 'Marker not found', - 'code' => 404 - ]; - } - - if (!$marker->permissions()->can('update')) { - return [ - 'status' => 'error', - 'message' => 'Forbidden', - 'code' => 403 - ]; - } - - $data = kirby()->request()->data(); - - if (!isset($data['position']['lat']) || !isset($data['position']['lon'])) { - return [ - 'status' => 'error', - 'message' => 'Position (lat, lon) is required', - 'code' => 400 - ]; - } - - $lat = (float) $data['position']['lat']; - $lon = (float) $data['position']['lon']; - - if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) { - return [ - 'status' => 'error', - 'message' => 'Invalid coordinates', - 'code' => 400 - ]; - } - - $marker->update([ - 'latitude' => $lat, - 'longitude' => $lon - ]); - - $iconFile = $marker->markerIcon()->toFile(); - $iconUrl = $iconFile ? $iconFile->url() : null; - $iconSize = $marker->markerIconSize()->isNotEmpty() - ? (int) $marker->markerIconSize()->value() - : 40; - - return [ - 'status' => 'success', - 'data' => [ - 'marker' => [ - 'id' => $marker->id(), - 'slug' => $marker->slug(), - 'title' => $marker->title()->value(), - 'position' => [ - 'lat' => $lat, - 'lon' => $lon - ], - 'num' => $marker->num(), - 'panelUrl' => (string) $marker->panel()->url(), - 'iconUrl' => $iconUrl, - 'iconSize' => $iconSize - ] - ] - ]; - - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ], - - // DELETE marker - [ - 'pattern' => 'map-editor/pages/(:all)/markers/(:all)', - 'method' => 'DELETE', - 'auth' => false, - 'action' => function (string $pageId, string $markerId) { - try { - $user = kirby()->user(); - - if (!$user && !kirby()->option('debug', false)) { - return [ - 'status' => 'error', - 'message' => 'Unauthorized', - 'code' => 401 - ]; - } - - $marker = kirby()->page($markerId); - if (!$marker) { - return [ - 'status' => 'error', - 'message' => 'Marker not found', - 'code' => 404 - ]; - } - - if (!$marker->permissions()->can('delete')) { - return [ - 'status' => 'error', - 'message' => 'Forbidden', - 'code' => 403 - ]; - } - - $marker->delete(true); - - return [ - 'status' => 'success', - 'data' => [ - 'message' => 'Marker deleted successfully' - ] - ]; - - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - 'code' => 500 - ]; - } - } - ] -]; diff --git a/public/site/plugins/map-editor/routes/markers/create.php b/public/site/plugins/map-editor/routes/markers/create.php new file mode 100644 index 0000000..20d9e80 --- /dev/null +++ b/public/site/plugins/map-editor/routes/markers/create.php @@ -0,0 +1,114 @@ + 'map-editor/pages/(:all)/markers', + 'method' => 'POST', + 'auth' => false, + 'action' => function (string $pageId) { + try { + $user = kirby()->user(); + + if (!$user && !kirby()->option('debug', false)) { + return [ + 'status' => 'error', + 'message' => 'Unauthorized', + 'code' => 401 + ]; + } + + $mapPage = kirby()->page($pageId); + if (!$mapPage) { + return [ + 'status' => 'error', + 'message' => 'Map page not found', + 'code' => 404 + ]; + } + + if (!$mapPage->permissions()->can('create')) { + return [ + 'status' => 'error', + 'message' => 'Forbidden', + 'code' => 403 + ]; + } + + $data = kirby()->request()->data(); + + if (!isset($data['position']['lat']) || !isset($data['position']['lon'])) { + return [ + 'status' => 'error', + 'message' => 'Position (lat, lon) is required', + 'code' => 400 + ]; + } + + $lat = (float) $data['position']['lat']; + $lon = (float) $data['position']['lon']; + + if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) { + return [ + 'status' => 'error', + 'message' => 'Invalid coordinates', + 'code' => 400 + ]; + } + + $existingMarkers = $mapPage + ->children() + ->filterBy('intendedTemplate', 'marker'); + $nextNum = $existingMarkers->count() + 1; + + $title = 'Marqueur ' . $nextNum; + $slug = Str::slug($title); + + $newMarker = $mapPage->createChild([ + 'slug' => $slug, + 'template' => 'marker', + 'content' => [ + 'title' => $title, + 'latitude' => $lat, + 'longitude' => $lon + ] + ]); + + $newMarker->changeStatus('listed', $nextNum); + + $iconFile = $newMarker->markerIcon()->toFile(); + $iconUrl = $iconFile ? $iconFile->url() : null; + $iconSize = $newMarker->markerIconSize()->isNotEmpty() + ? (int) $newMarker->markerIconSize()->value() + : 40; + + return [ + 'status' => 'success', + 'data' => [ + 'marker' => [ + 'id' => $newMarker->id(), + 'slug' => $newMarker->slug(), + 'title' => $title, + 'position' => [ + 'lat' => $lat, + 'lon' => $lon + ], + 'num' => $nextNum, + 'panelUrl' => (string) $newMarker->panel()->url(), + 'iconUrl' => $iconUrl, + 'iconSize' => $iconSize + ] + ] + ]; + + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/markers/delete.php b/public/site/plugins/map-editor/routes/markers/delete.php new file mode 100644 index 0000000..fcadc40 --- /dev/null +++ b/public/site/plugins/map-editor/routes/markers/delete.php @@ -0,0 +1,57 @@ + 'map-editor/pages/(:all)/markers/(:all)', + 'method' => 'DELETE', + 'auth' => false, + 'action' => function (string $pageId, string $markerId) { + try { + $user = kirby()->user(); + + if (!$user && !kirby()->option('debug', false)) { + return [ + 'status' => 'error', + 'message' => 'Unauthorized', + 'code' => 401 + ]; + } + + $marker = kirby()->page($markerId); + if (!$marker) { + return [ + 'status' => 'error', + 'message' => 'Marker not found', + 'code' => 404 + ]; + } + + if (!$marker->permissions()->can('delete')) { + return [ + 'status' => 'error', + 'message' => 'Forbidden', + 'code' => 403 + ]; + } + + $marker->delete(true); + + return [ + 'status' => 'success', + 'data' => [ + 'message' => 'Marker deleted successfully' + ] + ]; + + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/markers/get.php b/public/site/plugins/map-editor/routes/markers/get.php new file mode 100644 index 0000000..1aacb20 --- /dev/null +++ b/public/site/plugins/map-editor/routes/markers/get.php @@ -0,0 +1,84 @@ + 'map-editor/pages/(:all)/markers', + 'method' => 'GET', + 'auth' => false, + 'action' => function (string $pageId) { + try { + $user = kirby()->user(); + + if (!$user && !kirby()->option('debug', false)) { + return [ + 'status' => 'error', + 'message' => 'Unauthorized', + 'code' => 401 + ]; + } + + $mapPage = kirby()->page($pageId); + if (!$mapPage) { + return [ + 'status' => 'error', + 'message' => 'Map page not found', + 'code' => 404 + ]; + } + + if (!$mapPage->isReadable()) { + return [ + 'status' => 'error', + 'message' => 'Forbidden', + 'code' => 403 + ]; + } + + $markerPages = $mapPage + ->children() + ->listed() + ->filterBy('intendedTemplate', 'marker') + ->sortBy('num', 'asc'); + + $markers = []; + foreach ($markerPages as $marker) { + $iconFile = $marker->markerIcon()->toFile(); + $iconUrl = $iconFile ? $iconFile->url() : null; + $iconSize = $marker->markerIconSize()->isNotEmpty() + ? (int) $marker->markerIconSize()->value() + : 40; + + $markers[] = [ + 'id' => $marker->id(), + 'slug' => $marker->slug(), + 'title' => $marker->title()->value(), + 'position' => [ + 'lat' => (float) $marker->latitude()->value(), + 'lon' => (float) $marker->longitude()->value() + ], + 'num' => $marker->num(), + 'panelUrl' => (string) $marker->panel()->url(), + 'iconUrl' => $iconUrl, + 'iconSize' => $iconSize + ]; + } + + return [ + 'status' => 'success', + 'data' => [ + 'markers' => $markers + ] + ]; + + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/markers/update.php b/public/site/plugins/map-editor/routes/markers/update.php new file mode 100644 index 0000000..898beb2 --- /dev/null +++ b/public/site/plugins/map-editor/routes/markers/update.php @@ -0,0 +1,99 @@ + 'map-editor/pages/(:all)/markers/(:all)', + 'method' => 'PATCH', + 'auth' => false, + 'action' => function (string $pageId, string $markerId) { + try { + $user = kirby()->user(); + + if (!$user && !kirby()->option('debug', false)) { + return [ + 'status' => 'error', + 'message' => 'Unauthorized', + 'code' => 401 + ]; + } + + $marker = kirby()->page($markerId); + if (!$marker) { + return [ + 'status' => 'error', + 'message' => 'Marker not found', + 'code' => 404 + ]; + } + + if (!$marker->permissions()->can('update')) { + return [ + 'status' => 'error', + 'message' => 'Forbidden', + 'code' => 403 + ]; + } + + $data = kirby()->request()->data(); + + if (!isset($data['position']['lat']) || !isset($data['position']['lon'])) { + return [ + 'status' => 'error', + 'message' => 'Position (lat, lon) is required', + 'code' => 400 + ]; + } + + $lat = (float) $data['position']['lat']; + $lon = (float) $data['position']['lon']; + + if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) { + return [ + 'status' => 'error', + 'message' => 'Invalid coordinates', + 'code' => 400 + ]; + } + + $marker->update([ + 'latitude' => $lat, + 'longitude' => $lon + ]); + + $iconFile = $marker->markerIcon()->toFile(); + $iconUrl = $iconFile ? $iconFile->url() : null; + $iconSize = $marker->markerIconSize()->isNotEmpty() + ? (int) $marker->markerIconSize()->value() + : 40; + + return [ + 'status' => 'success', + 'data' => [ + 'marker' => [ + 'id' => $marker->id(), + 'slug' => $marker->slug(), + 'title' => $marker->title()->value(), + 'position' => [ + 'lat' => $lat, + 'lon' => $lon + ], + 'num' => $marker->num(), + 'panelUrl' => (string) $marker->panel()->url(), + 'iconUrl' => $iconUrl, + 'iconSize' => $iconSize + ] + ] + ]; + + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + 'code' => 500 + ]; + } + } +]; diff --git a/public/site/plugins/map-editor/routes/position.php b/public/site/plugins/map-editor/routes/position/update.php similarity index 96% rename from public/site/plugins/map-editor/routes/position.php rename to public/site/plugins/map-editor/routes/position/update.php index 6ab5e36..a884eb7 100644 --- a/public/site/plugins/map-editor/routes/position.php +++ b/public/site/plugins/map-editor/routes/position/update.php @@ -1,8 +1,7 @@