Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions app/Queries/EventsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ public static function store(Request $request)
$request['geoposition'] = '0,0';
}

if (is_null($request['geoposition'])) {
$request['geoposition'] = '0,0';
// Frontend may send [] / '' when address autocomplete failed to resolve coords
// (e.g. ArcGIS blank Country for Kosovo). Treat those as missing.
$geoposition = $request['geoposition'] ?? null;
if (is_array($geoposition)) {
$geoposition = implode(',', array_filter($geoposition, static fn ($v) => $v !== null && $v !== ''));
}
if (! is_string($geoposition) || ! preg_match('/^-?\d+(\.\d+)?\s*,\s*-?\d+(\.\d+)?$/', trim($geoposition))) {
$geoposition = '0,0';
}
$request['geoposition'] = $geoposition;

$request['latitude'] = explode(',', $request['geoposition'])[0];
$request['longitude'] = explode(',', $request['geoposition'])[1];
[$latitude, $longitude] = array_map('trim', explode(',', $request['geoposition'], 2));
$request['latitude'] = $latitude;
$request['longitude'] = $longitude;

if (empty($request['codeweek_for_all_participation_code'])) {
$codeweek_4_all_generated_code = 'cw'.Carbon::now()->format('y').'-'.str_random(5);
Expand Down
16 changes: 13 additions & 3 deletions resources/assets/js/components/AutoCompleteGeo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,28 @@
url: baseURL + "&singleLine=" + item.name + "&magicKey=" + item.magicKey + "&outFields=Country",
success: function (res) {
var candidate = res.candidates[0];
if (!candidate || !candidate.location) {
return;
}
me.geoposition = [candidate.location.y, candidate.location.x];
if (window.map){
map.setView(me.geoposition,16);
}
var countryIso2 = me.findCountry(candidate.attributes.Country).iso2;
$("#id_country").val(countryIso2);
// ArcGIS returns blank Country for disputed regions (e.g. Kosovo).
var countryIso2 = me.findCountry(candidate.attributes && candidate.attributes.Country);
if (countryIso2) {
$("#id_country").val(countryIso2);
}
}
});
}
},
findCountry(iso3) {
return allCountries.find(country => country.iso3 === iso3);
if (!iso3) {
return undefined;
}
var match = allCountries.find(country => country.iso3 === iso3);
return match ? match.iso2 : undefined;
},
getLabel (item) {
if (item && item.name) {
Expand Down
1 change: 1 addition & 0 deletions resources/assets/js/components/allCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const allCountries = [
["VI","VIR"],
["WF","WLF"],
["EH","ESH"],
["XK","XKX"],
["YE","YEM"],
["ZM","ZMB"],
["ZW","ZWE"],
Expand Down
78 changes: 49 additions & 29 deletions resources/js/components/AutocompleteGeo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,60 @@ export default {
});

const itemSelected = (selectedItem) => {
emit('onChange', { location: selectedItem?.name || '' });

if (selectedItem && selectedItem.name && selectedItem.magicKey) {
const baseURL = "/api/proxy/geocode"; // Update to your Laravel endpoint
axios.get(baseURL, {
params: {
singleLine: selectedItem.name,
magicKey: selectedItem.magicKey
}
}).then(response => {
const candidate = response.data.candidates[0];
localGeoposition.value = [candidate.location.y, candidate.location.x];
if (window.map) {
window.map.setView([candidate.location.y, candidate.location.x], 16);
}
const countryIso2 = findCountry(candidate.attributes.Country).iso2;

emit('onChange', {
location: selectedItem?.name || '',
geoposition: [candidate.location.y, candidate.location.x],
country_iso: countryIso2 || '',
});

if (document.getElementById('id_country')) {
document.getElementById('id_country').value = countryIso2;
}
}).catch(error => {
console.error('Error:', error);
});
if (!selectedItem?.name) {
return;
}

// Location text only — do not clear country/geoposition before geocode resolves.
// ArcGIS returns blank Country for disputed regions (e.g. Kosovo); that must not wipe the form.
emit('onChange', { location: selectedItem.name });

if (!selectedItem.magicKey) {
return;
}

const baseURL = "/api/proxy/geocode";
axios.get(baseURL, {
params: {
singleLine: selectedItem.name,
magicKey: selectedItem.magicKey
}
}).then(response => {
const candidate = response.data?.candidates?.[0];
if (!candidate?.location) {
return;
}

const geoposition = [candidate.location.y, candidate.location.x];
localGeoposition.value = geoposition;
if (window.map) {
window.map.setView(geoposition, 16);
}

const countryIso2 = findCountry(candidate.attributes?.Country)?.iso2;
const payload = {
location: selectedItem.name,
geoposition,
};
if (countryIso2) {
payload.country_iso = countryIso2;
}

emit('onChange', payload);

if (countryIso2 && document.getElementById('id_country')) {
document.getElementById('id_country').value = countryIso2;
}
}).catch(error => {
console.error('Error:', error);
});
};


const findCountry = (iso3) => {
if (!iso3) {
return undefined;
}
return allCountries.find(country => country.iso3 === iso3);
};

Expand Down
10 changes: 8 additions & 2 deletions resources/js/components/activity-form/FormStep1.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ export default {

const handleLocationChange = ({ location, geoposition, country_iso }) => {
props.formValues.location = location || '';
props.formValues.geoposition = geoposition;
props.formValues.country_iso = country_iso;
// Only update fields that the geocoder actually resolved. Blank ArcGIS Country
// (e.g. Kosovo) must not clear a country the user already selected.
if (geoposition !== undefined) {
props.formValues.geoposition = geoposition;
}
if (country_iso !== undefined) {
props.formValues.country_iso = country_iso;
}
props.formValues.locationSelected = true;
props.formValues.locationDirty = true;
};
Expand Down
18 changes: 16 additions & 2 deletions resources/js/components/activity-form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,12 @@ export default {
activity_format: values.activity_format?.join(','),
activity_type: values.activity_type,
location: values.location,
geoposition: values.geoposition?.join(',') || [],
// Empty array becomes [] and makes EventsQuery explode() 500 — send null instead.
geoposition: Array.isArray(values.geoposition) && values.geoposition.length
? values.geoposition.join(',')
: (typeof values.geoposition === 'string' && values.geoposition
? values.geoposition
: null),
duration: values.duration,
start_date: values.start_date,
end_date: values.end_date,
Expand Down Expand Up @@ -400,7 +405,16 @@ export default {
}
handleMoveStep(4);
} catch (error) {
errors.value = error.response?.data?.errors;
errors.value = error.response?.data?.errors || {};
// Surface non-validation failures (e.g. 500) instead of silently jumping to step 1.
if (!error.response?.data?.errors) {
errors.value = {
title: [
error.response?.data?.message
|| 'Something went wrong while saving. Please try again, or clear the address and leave it empty for online activities.',
],
};
}
step.value = 1;
}
finally {
Expand Down
1 change: 1 addition & 0 deletions resources/js/components/allCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const allCountries = [
["VI","VIR"],
["WF","WLF"],
["EH","ESH"],
["XK","XKX"],
["YE","YEM"],
["ZM","ZMB"],
["ZW","ZWE"],
Expand Down
Loading