diff --git a/app/Console/Commands/api/GermanTraits.php b/app/Console/Commands/api/GermanTraits.php index a447692db..6b8af4505 100644 --- a/app/Console/Commands/api/GermanTraits.php +++ b/app/Console/Commands/api/GermanTraits.php @@ -15,7 +15,6 @@ private function loadJson($url) { $client = new \GuzzleHttp\Client(); $guzzle = $client->request('get', $url); - return json_decode($guzzle->getBody(), true); } @@ -52,7 +51,7 @@ private function createRSSItem($json, $city): void $RSSitem->user_email = $item['user']['email'] ?? ''; $RSSitem->user_publicEmail = $item['user']['publicEmail'] ?? ''; $RSSitem->user_type = $item['user']['type']['identifier'] ?? - ($item['user']['type'] ?? 'invite-in-person'); + ($item['user']['type'] ?? 'invite-in-person'); $RSSitem->user_website = $item['user']['www'] ?? ''; // Safely get type data @@ -96,8 +95,8 @@ private function createGermanEvent($city): void 'lastname' => '', 'username' => $this->organizer, 'password' => bcrypt(Str::random()), - ]); - + ] + ); } $event = new Event([ @@ -124,7 +123,7 @@ private function createGermanEvent($city): void 'end_date' => $this->eventEndDate, 'longitude' => $this->longitude, 'latitude' => $this->latitude, - 'geoposition' => $this->latitude.','.$this->longitude, + 'geoposition' => $this->latitude . ',' . $this->longitude, 'language' => 'de', ]); @@ -151,6 +150,5 @@ private function createGermanEvent($city): void $event->tags()->sync($tagsArray); } - } } diff --git a/app/Console/Commands/excel/CoderDojoEvents.php b/app/Console/Commands/excel/CoderDojoEvents.php index 717875857..9b402007a 100644 --- a/app/Console/Commands/excel/CoderDojoEvents.php +++ b/app/Console/Commands/excel/CoderDojoEvents.php @@ -42,8 +42,7 @@ public function handle(): void Excel::import( new CoderDojoEventsImport(), - 'events.xlsx', - 'excel' + resource_path('excel/20250113 Events CoderDojo Belgium January - February 2025.xlsx') ); } } diff --git a/app/Imports/CoderDojoEventsImport.php b/app/Imports/CoderDojoEventsImport.php index 13bc6b74c..367457081 100644 --- a/app/Imports/CoderDojoEventsImport.php +++ b/app/Imports/CoderDojoEventsImport.php @@ -17,47 +17,82 @@ public function parseDate($date) { $arr = explode(',', $date); array_shift($arr); - return implode($arr); } public function model(array $row): ?Model { - $event = new Event([ - 'status' => 'APPROVED', - 'title' => $row['activity_title'], - 'slug' => str_slug($row['activity_title']), - 'organizer' => $row['name_of_organisation'], - 'description' => $row['description'], - 'organizer_type' => $row['type_of_organisation'], - 'activity_type' => $row['activity_type'], - 'location' => isset($row['address']) ? $row['address'] : 'online', - 'event_url' => $row['organiser_website'], - 'contact_person' => !empty($row['contact_email']) ? $row['contact_email'] : '', - 'user_email' => '', - 'creator_id' => 132942, - 'country_iso' => $row['country'], - 'picture' => isset($row['image_path']) ? $row['image_path'] : '', - 'pub_date' => now(), - 'created' => now(), - 'updated' => now(), - 'codeweek_for_all_participation_code' => 'cw20-coderdojo-eu', - 'start_date' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['start_date']), - 'end_date' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['end_date']), - 'geoposition' => $row['latitude'].','.$row['longitude'], - 'longitude' => $row['longitude'], - 'latitude' => $row['latitude'], - 'language' => isset($row['language']) ? $row['language'] : 'nl', - 'approved_by' => 19588, - 'mass_added_for' => 'Excel', - ]); - - $event->save(); - - $event->audiences()->attach(explode(',', $row['audience_comma_separated_ids'])); - $event->themes()->attach(explode(',', $row['theme_comma_separated_ids'])); - - return $event; + // Validate required fields + if ( + empty($row['activity_title']) || + empty($row['name_of_organisation']) || + empty($row['description']) || + empty($row['type_of_organisation']) || + empty($row['activity_type']) || + empty($row['country']) || + empty($row['start_date']) || + empty($row['end_date']) + ) { + Log::error('Missing required fields in row'); + return null; + } + + try { + $event = new Event([ + 'status' => 'APPROVED', + 'title' => trim($row['activity_title']), + 'slug' => str_slug(trim($row['activity_title'])), + 'organizer' => trim($row['name_of_organisation']), + 'description' => trim($row['description']), + 'organizer_type' => trim($row['type_of_organisation']), + 'activity_type' => trim($row['activity_type']), + 'location' => !empty($row['address']) ? trim($row['address']) : 'online', + 'event_url' => !empty($row['organiser_website']) ? trim($row['organiser_website']) : '', + 'contact_person' => !empty($row['contact_email']) ? trim($row['contact_email']) : '', + 'user_email' => '', + 'creator_id' => 132942, + 'country_iso' => trim($row['country']), + 'picture' => !empty($row['image_path']) ? trim($row['image_path']) : '', + 'pub_date' => now(), + 'created' => now(), + 'updated' => now(), + 'codeweek_for_all_participation_code' => 'cw20-coderdojo-eu', + 'start_date' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['start_date']), + 'end_date' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['end_date']), + 'geoposition' => (!empty($row['latitude']) && !empty($row['longitude'])) ? $row['latitude'] . ',' . $row['longitude'] : '', + 'longitude' => !empty($row['longitude']) ? trim($row['longitude']) : '', + 'latitude' => !empty($row['latitude']) ? trim($row['latitude']) : '', + 'language' => !empty($row['language']) ? trim($row['language']) : 'nl', + 'approved_by' => 19588, + 'mass_added_for' => 'Excel', + ]); + + $event->save(); + + if (!empty($row['audience_comma_separated_ids'])) { + $audiences = array_unique(array_map('trim', explode(',', $row['audience_comma_separated_ids']))); + $audiences = array_filter($audiences, function ($id) { + return is_numeric($id) && $id > 0 && $id <= 100; + }); + if (!empty($audiences)) { + $event->audiences()->attach($audiences); + } + } + + if (!empty($row['theme_comma_separated_ids'])) { + $themes = array_unique(array_map('trim', explode(',', $row['theme_comma_separated_ids']))); + $themes = array_filter($themes, function ($id) { + return is_numeric($id) && $id > 0 && $id <= 100; + }); + if (!empty($themes)) { + $event->themes()->attach($themes); + } + } + return $event; + } catch (\Exception $e) { + Log::error('Event import failed: ' . $e->getMessage()); + return null; + } } } diff --git a/resources/excel/20250113 Events CoderDojo Belgium January - February 2025.xlsx b/resources/excel/20250113 Events CoderDojo Belgium January - February 2025.xlsx new file mode 100644 index 000000000..5981dd04d Binary files /dev/null and b/resources/excel/20250113 Events CoderDojo Belgium January - February 2025.xlsx differ