From 761acc7a41b7f881e537802519230e07193d68f1 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Sat, 28 Nov 2026 23:30:08 +0000 Subject: [PATCH] Accept the duplicate Swedish language code so activities can be published. The form offered Swedish as both se and sv, and se was rejected on save. The failure landed back on the address step, so it looked like a geolocation problem. Co-authored-by: Cursor --- app/Event.php | 18 +++++ app/Http/Controllers/EventController.php | 5 +- app/Http/Requests/EventRequest.php | 31 +++++++-- app/Providers/AppServiceProvider.php | 3 +- app/Queries/EventsQuery.php | 29 +++++--- .../js/components/activity-form/index.vue | 33 ++++++++-- tests/Feature/CreateEventTest.php | 66 +++++++++++++++++++ tests/Feature/UpdateEventTest.php | 34 ++++++++++ 8 files changed, 194 insertions(+), 25 deletions(-) diff --git a/app/Event.php b/app/Event.php index 0857e36e1..ccc871772 100644 --- a/app/Event.php +++ b/app/Event.php @@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Storage; @@ -187,6 +189,8 @@ public static function normalizeLanguageCode(?string $code): ?string 'tur' => 'tr', 'ell' => 'el', 'gre' => 'el', + // Country code that the language list also exposes as "Swedish". + 'se' => 'sv', ]; $code = $aliases[$code] ?? $code; @@ -194,6 +198,20 @@ public static function normalizeLanguageCode(?string $code): ?string return trans()->has('base.languages.'.$code) ? $code : null; } + /** + * Languages the activity form may offer. Matches EventRequest, which only + * accepts site locales. `se` is omitted because it duplicates Swedish (`sv`). + * + * @return array + */ + public static function activityLanguageOptions(): array + { + $allowed = array_flip(config('app.locales')); + $labels = array_intersect_key(Lang::get('base.languages'), $allowed); + + return Arr::sort($labels); + } + public function getUrlAttribute() { if (!empty($this->slug)) { return route('view_event', [ diff --git a/app/Http/Controllers/EventController.php b/app/Http/Controllers/EventController.php index ccc7fe76a..1268104b9 100755 --- a/app/Http/Controllers/EventController.php +++ b/app/Http/Controllers/EventController.php @@ -12,7 +12,6 @@ use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Mail; use Illuminate\View\View; use Illuminate\Http\JsonResponse; @@ -56,7 +55,7 @@ public function create(Request $request) // Redirect to locations chooser only when the user has stored locations $countries = \App\Country::all()->sortBy('name'); - $languages = Arr::sort(Lang::get('base.languages')); + $languages = Event::activityLanguageOptions(); $leading_teachers = $this->getLeadingTeachersTagsArray(); $hasLocations = auth()->user()->locations()->exists(); @@ -162,7 +161,7 @@ public function edit(Event $event): View ? 'en' : $event->language; - $languages = Arr::sort(Lang::get('base.languages')); + $languages = Event::activityLanguageOptions(); $leading_teachers = $this->getLeadingTeachersTagsArray(); diff --git a/app/Http/Requests/EventRequest.php b/app/Http/Requests/EventRequest.php index 2e22c0edd..7666b3e99 100644 --- a/app/Http/Requests/EventRequest.php +++ b/app/Http/Requests/EventRequest.php @@ -2,12 +2,12 @@ namespace App\Http\Requests; +use App\Event; use App\Rules\NotReservedEmailDomain; use App\Rules\NotSecurityProbeContent; use App\Rules\validAudience; use App\Rules\validTheme; use Illuminate\Foundation\Http\FormRequest; -use Illuminate\Support\Facades\Lang; use Illuminate\Validation\Rule; class EventRequest extends FormRequest @@ -25,8 +25,6 @@ public function authorize(): bool */ public function rules(): array { - //dd(Lang::get('base.languages')); - //$languages = array_keys(Lang::get('base.languages')); $languages = config('app.locales'); return [ @@ -58,9 +56,32 @@ protected function prepareForValidation(): void { $language = $this->input('language'); if (is_string($language)) { - $parts = array_values(array_filter(array_map('trim', explode(',', $language)))); - $this->merge(['language' => $parts]); + $language = array_values(array_filter(array_map('trim', explode(',', $language)))); } + + if (! is_array($language)) { + return; + } + + // The form used to offer Swedish twice (`se` and `sv`). `se` is not a + // site locale, so choosing that chip failed validation and the form + // bounced back to the address step. + $language = collect($language) + ->map(function ($code) { + if (! is_string($code)) { + return null; + } + + $normalized = Event::normalizeLanguageCode($code); + + return $normalized ?? trim($code); + }) + ->filter(fn ($code) => is_string($code) && $code !== '') + ->unique() + ->values() + ->all(); + + $this->merge(['language' => $language]); } public function messages(): array diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 453f76246..0ce06669c 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -16,7 +16,6 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Rules\Password; use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\Lang; use App\Services\Support\Gmail\GmailConnector; use App\Services\Support\Gmail\GmailOutboundService; use App\Services\Support\Gmail\GoogleGmailConnector; @@ -70,7 +69,7 @@ function ($view) { $view->with('audiences', $audiences); $view->with('activity_types', \App\ActivityType::list()); $view->with('countries', \App\Country::translated()); - $view->with('languages', Arr::sort(Lang::get('base.languages'))); + $view->with('languages', Event::activityLanguageOptions()); $view->with('active_countries', $activeCountries); $view->with('themes', $themes); } diff --git a/app/Queries/EventsQuery.php b/app/Queries/EventsQuery.php index 43c5b70a7..d8cdcf386 100644 --- a/app/Queries/EventsQuery.php +++ b/app/Queries/EventsQuery.php @@ -46,14 +46,7 @@ public static function store(Request $request) // 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['geoposition'] = self::normalizedGeoposition($request['geoposition'] ?? null) ?? '0,0'; [$latitude, $longitude] = array_map('trim', explode(',', $request['geoposition'], 2)); $request['latitude'] = $latitude; @@ -108,6 +101,9 @@ public static function update(Request $request, Event $event) //dd($event->status); + $request['geoposition'] = self::normalizedGeoposition($request['geoposition'] ?? null) + ?? self::normalizedGeoposition($event->geoposition) + ?? '0,0'; $request['latitude'] = explode(',', $request['geoposition'])[0]; $request['longitude'] = explode(',', $request['geoposition'])[1]; @@ -171,4 +167,21 @@ public static function trigger(?Country $country, $status = null) return $query->get(); } + + /** + * A coordinate pair the events table can store. Anything else (null, [], a + * typed address) is missing, not a pin. + */ + private static function normalizedGeoposition(mixed $geoposition): ?string + { + 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))) { + return null; + } + + return trim($geoposition); + } } diff --git a/resources/js/components/activity-form/index.vue b/resources/js/components/activity-form/index.vue index ec80ca2ea..5217c7e1e 100644 --- a/resources/js/components/activity-form/index.vue +++ b/resources/js/components/activity-form/index.vue @@ -342,6 +342,18 @@ export default { const handleGoMapPage = () => (window.location.href = '/events'); const handleReloadPage = () => window.location.reload(); + const stepForSaveErrors = (responseErrors) => { + const keys = Object.keys(responseErrors || {}); + const matches = (fields) => keys.some((key) => fields.some((field) => key === field || key.startsWith(`${field}.`))); + if (matches(['organizer', 'organizer_type', 'language', 'country_iso', 'event_url', 'user_email', 'contact_person', 'privacy'])) { + return 3; + } + if (matches(['audience', 'participants_count', 'males_count', 'females_count', 'other_count', 'ages', 'is_extracurricular_event', 'is_standard_school_curriculum', 'picture', 'leading_teacher_tag'])) { + return 2; + } + return 1; + }; + const handleSubmit = async () => { errors.value = {}; @@ -405,17 +417,24 @@ export default { } handleMoveStep(4); } catch (error) { - 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 = { + const raw = error.response?.data?.errors; + // Surface non-validation failures instead of bouncing to the address step. + const responseErrors = raw && Object.keys(raw).length + ? { ...raw } + : { title: [ error.response?.data?.message - || 'Something went wrong while saving. Please try again, or clear the address and leave it empty for online activities.', + || 'Something went wrong while saving. Please try again.', ], }; - } - step.value = 1; + Object.entries(responseErrors).forEach(([key, messages]) => { + const root = key.split('.')[0]; + if (root !== key && Array.isArray(messages)) { + responseErrors[root] = [...(responseErrors[root] || []), ...messages]; + } + }); + errors.value = responseErrors; + step.value = stepForSaveErrors(responseErrors); } finally { isSubmitting.value = false; diff --git a/tests/Feature/CreateEventTest.php b/tests/Feature/CreateEventTest.php index f65c2147d..186ed125a 100755 --- a/tests/Feature/CreateEventTest.php +++ b/tests/Feature/CreateEventTest.php @@ -348,6 +348,72 @@ public function an_authenticated_user_can_create_events_without_geoposition(): v $this->get($event->path())->assertSee('Dutch'); } + #[Test] + public function swedish_language_alias_can_be_published_for_an_in_person_activity(): void + { + $this->seed('RolesAndPermissionsSeeder'); + $this->seed('LeadingTeacherRoleSeeder'); + $this->signIn(); + auth()->user()->forceFill(['consent_given_at' => now()])->save(); + + $page = $this->get('/add'); + $page->assertOk(); + $page->assertSee('"sv":"Swedish"', false); + $page->assertDontSee('"se":', false); + + \App\Country::factory()->create([ + 'iso' => 'SE', + 'name' => 'Sweden', + 'latitude' => 62.1984, + 'longitude' => 14.8961, + ]); + \App\Audience::factory()->count(3)->create(); + \App\Theme::factory()->count(3)->create(); + + $event = \App\Event::factory()->make(); + $event->title = 'First LEGO League Finals 2026'; + $event->description = 'Scandinavian First LEGO League final in Lund.'; + $event->theme = '1'; + $event->audience = '2, 3'; + $event->privacy = true; + $event->language = ['en', 'se', 'da']; + $event->activity_type = 'open-in-person'; + $event->activity_format = 'competition'; + $event->duration = 'over-4'; + $event->location = 'The Loop, Rydbergs Torg 11, 224 84, Lund, SWE'; + $event->geoposition = '55.7304,13.2436'; + $event->country_iso = 'SE'; + $event->organizer = 'First LEGO League'; + $event->organizer_type = 'non profit'; + $event->ages = '6-9,10-12,13-15,16-18,19-25'; + $event->participants_count = 1300; + $event->males_count = 725; + $event->females_count = 575; + $event->other_count = null; + $event->is_extracurricular_event = false; + $event->is_use_resource = false; + $event->start_date = '2026-11-27 14:00:00'; + $event->end_date = '2026-11-28 17:30:00'; + $event->event_url = 'https://hjernekraft.org/se/skandinavisk-final-2026'; + $event->contact_person = 'mariann@firstscandinavia.org'; + $event->user_email = 'info@ecwt.eu'; + + $response = $this->postJson('/events', $event->toArray()); + + $response->assertCreated(); + + $stored = Event::where('title', 'First LEGO League Finals 2026')->first(); + $this->assertNotNull($stored); + $this->assertSame('en,sv,da', $stored->getRawOriginal('language')); + $this->assertSame('55.7304,13.2436', $stored->geoposition); + $this->assertSame('SE', $stored->country_iso); + $this->assertSame('The Loop, Rydbergs Torg 11, 224 84, Lund, SWE', $stored->location); + + $options = Event::activityLanguageOptions(); + $this->assertArrayHasKey('sv', $options); + $this->assertArrayNotHasKey('se', $options); + } + public function publishEvent($overrides = []) { $this->withExceptionHandling()->signIn(); diff --git a/tests/Feature/UpdateEventTest.php b/tests/Feature/UpdateEventTest.php index 4cdec8031..6f60b4a7b 100644 --- a/tests/Feature/UpdateEventTest.php +++ b/tests/Feature/UpdateEventTest.php @@ -53,6 +53,40 @@ public function event_can_be_updated_by_its_owner(): void } + #[Test] + public function updating_without_a_pin_keeps_the_existing_geoposition(): void + { + $this->seed('RolesAndPermissionsSeeder'); + $this->signIn(); + + $event = \App\Event::factory()->make(); + \App\Audience::factory()->count(3)->create(); + \App\Theme::factory()->count(3)->create(); + + $event->theme = '1'; + $event->tags = 'tag:foo,tag:bar'; + $event->audience = '2, 3'; + $event->privacy = true; + $event->geoposition = '55.7304,13.2436'; + + $this->post('/events', $event->toArray()); + + $event = Event::where('title', $event->title)->first(); + $event->title = 'Changed'; + $event->description = 'Changed description.'; + $event->theme = '1'; + $event->audience = '2, 3'; + $event->tags = 'tag:foo'; + $event->privacy = true; + $event->geoposition = null; + + $this->patch('/events/'.$event->id, $event->toArray()); + + $fresh = $event->fresh(); + $this->assertSame('Changed', $fresh->title); + $this->assertSame('55.7304,13.2436', $fresh->geoposition); + } + #[Test] public function event_can_be_updated_by_its_owner_when_approved(): void {