diff --git a/partials/landing/account/dashboard.php b/partials/landing/account/dashboard.php index 1509179..ba6bec1 100644 --- a/partials/landing/account/dashboard.php +++ b/partials/landing/account/dashboard.php @@ -12,6 +12,44 @@ $info = ''; $crypto = null; try { $crypto = new \App\Crypto($app->config()); } catch (\Throwable) {} +function geocode_address(?string $street, ?string $zip, ?string $city, ?string $region): array +{ + $parts = array_filter([ + $street ?: null, + $zip ?: null, + $city ?: null, + $region ?: null, + ]); + if (!$parts) { + return [null, null]; + } + + $query = implode(', ', $parts); + $url = 'https://nominatim.openstreetmap.org/search?' . http_build_query([ + 'format' => 'jsonv2', + 'limit' => 1, + 'q' => $query, + ]); + + $ctx = stream_context_create([ + 'http' => [ + 'method' => 'GET', + 'header' => "User-Agent: papa-kind-treff/1.0\r\nAccept-Language: de\r\n", + 'timeout' => 6, + ], + ]); + + $resp = @file_get_contents($url, false, $ctx); + if ($resp === false) { + return [null, null]; + } + $json = json_decode($resp, true); + if (!is_array($json) || empty($json[0]['lat']) || empty($json[0]['lon'])) { + return [null, null]; + } + return [round((float)$json[0]['lat'], 7), round((float)$json[0]['lon'], 7)]; +} + // POST Aktionen if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; @@ -50,16 +88,25 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { ]); $info = 'Kind hinzugefügt.'; } elseif ($action === 'event_add') { - $stmt = $pdo->prepare('INSERT INTO events (created_by, title, teaser_public, description, location_label, zip, city, region, lat, lng, starts_at, allow_kids, visibility, status, created_at, updated_at) VALUES (:uid, :title, :teaser, :descr, :loc, :zip, :city, :region, NULL, NULL, :start, :allow, :vis, :status, NOW(), NOW())'); + $street = trim((string)($_POST['street'] ?? '')); + $zip = trim((string)($_POST['zip'] ?? '')); + $city = trim((string)($_POST['city'] ?? '')); + $region = trim((string)($_POST['region'] ?? '')); + [$lat, $lng] = geocode_address($street, $zip, $city, $region); + + $stmt = $pdo->prepare('INSERT INTO events (created_by, title, teaser_public, description, location_label, street, zip, city, region, lat, lng, starts_at, allow_kids, visibility, status, created_at, updated_at) VALUES (:uid, :title, :teaser, :descr, :loc, :street, :zip, :city, :region, :lat, :lng, :start, :allow, :vis, :status, NOW(), NOW())'); $stmt->execute([ 'uid' => $userId, 'title' => trim((string)$_POST['title']), 'teaser' => trim((string)$_POST['teaser']), 'descr' => trim((string)$_POST['description']), 'loc' => trim((string)$_POST['location_label']), - 'zip' => trim((string)$_POST['zip']), - 'city' => trim((string)$_POST['city']), - 'region' => trim((string)$_POST['region']), + 'street' => $street ?: null, + 'zip' => $zip, + 'city' => $city, + 'region' => $region, + 'lat' => $lat, + 'lng' => $lng, 'start' => $_POST['starts_at'] ?? null, 'allow' => isset($_POST['allow_kids']) ? 1 : 0, 'vis' => $_POST['visibility'] ?? 'public', @@ -355,10 +402,17 @@ $events = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
Wird zur Karten-/Umkreissuche genutzt.
+