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
18 changes: 18 additions & 0 deletions app/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -187,13 +189,29 @@ 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;

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<string, string>
*/
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', [
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down
31 changes: 26 additions & 5 deletions app/Http/Requests/EventRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
29 changes: 21 additions & 8 deletions app/Queries/EventsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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);
}
}
33 changes: 26 additions & 7 deletions resources/js/components/activity-form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions tests/Feature/CreateEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('&quot;sv&quot;:&quot;Swedish&quot;', false);
$page->assertDontSee('&quot;se&quot;:', 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();
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/UpdateEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading