diff --git a/public/api/contributions.php b/public/api/contributions.php index 4a833aa..f969d40 100644 --- a/public/api/contributions.php +++ b/public/api/contributions.php @@ -205,7 +205,7 @@ function handle_update($input) { } // Builds dynamic SQL Query to only update sent Fields - $updatable_fields = ['category', 'title', 'description', 'status']; + $updatable_fields = ['category', 'title', 'description', 'status', 'address']; $set_clauses = []; $params = [':id' => $contribution_id]; diff --git a/public/js/app.js b/public/js/app.js index 357119d..eb2278d 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -448,6 +448,14 @@ function submitCreate() { return; } + // Triggers Reverse Geocoding in Background + if (response.contribution_id && drawnGeometry) { + const coords = drawnGeomType === 'point' ? drawnGeometry.coordinates : + drawnGeomType === 'line' ? drawnGeometry.coordinates[0] : + drawnGeometry.coordinates[0][0]; + reverseGeocode(response.contribution_id, coords[1], coords[0]); + } + Swal.fire('Eingereicht!', 'Ihr Beitrag wurde erfolgreich eingereicht und wird nach Prüfung durch das Moderationsteam veröffentlicht.', 'success'); closeCreateModal(); loadContributions(); @@ -917,6 +925,31 @@ function categoryIcon(cat) { return ''; } +// Reverse Geocodes Coordinates and saves Address to Contribution via API +function reverseGeocode(contributionId, lat, lng) { + fetch('https://nominatim.openstreetmap.org/reverse?format=json&lat=' + lat + '&lon=' + lng + '&zoom=18&addressdetails=1', { + headers: { 'Accept-Language': 'de' } + }) + .then(function (r) { return r.json(); }) + .then(function (data) { + if (data.display_name) { + const addr = data.address || {}; + const parts = []; + if (addr.road) parts.push(addr.road + (addr.house_number ? ' ' + addr.house_number : '')); + if (addr.city || addr.town || addr.village) parts.push(addr.city || addr.town || addr.village); + const shortAddress = parts.length > 0 ? parts.join(', ') : data.display_name.split(',').slice(0, 2).join(','); + + // Saves Address to Database via API + apiCall({ + action: 'update', + contribution_id: contributionId, + address: shortAddress + }, function () {}); + } + }) + .catch(function () {}); +} + // ===================================================================== // Block 16: Application Startup