From 805a0a2493666fa5dfce9927e5655fe2e1242e44 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Tue, 1 Jul 2025 11:03:31 +0100 Subject: [PATCH] Add example Excel file and importer update --- app/Imports/GenericEventsImport.php | 155 ++++++++++++++++++---------- 1 file changed, 99 insertions(+), 56 deletions(-) diff --git a/app/Imports/GenericEventsImport.php b/app/Imports/GenericEventsImport.php index 4caaab1fd..8e9ce84f7 100644 --- a/app/Imports/GenericEventsImport.php +++ b/app/Imports/GenericEventsImport.php @@ -15,10 +15,21 @@ class GenericEventsImport extends BaseEventsImport implements ToModel, WithCustomValueBinder, WithHeadingRow { + protected function normalizeRow(array $row): array + { + foreach ($row as $k => $v) { + if (is_float($v) && floor($v) === $v) { + $row[$k] = (int) $v; + } + } + return $row; + } + public function parseDate($value) { if (is_numeric($value)) { - return Date::excelToDateTimeObject($value)->format('Y-m-d H:i:s'); + return Date::excelToDateTimeObject($value) + ->format('Y-m-d H:i:s'); } try { return Carbon::parse($value)->format('Y-m-d H:i:s'); @@ -30,6 +41,7 @@ public function parseDate($value) public function model(array $row): ?Model { + $row = $this->normalizeRow($row); Log::info('Importing row:', $row); // required fields @@ -47,109 +59,140 @@ public function model(array $row): ?Model return null; } - // resolve creator_id + // + // 1) resolve creator_id — only by explicit ID or by local-part of contact_email + // $creatorId = null; - if (isset($row['creator_id']) && $row['creator_id'] !== '') { - $creatorId = is_numeric($row['creator_id']) - ? (int)$row['creator_id'] - : User::where('email', trim($row['creator_id']))->value('id'); + + // 1a) explicit numeric + if (isset($row['creator_id']) && is_int($row['creator_id'])) { + $creatorId = $row['creator_id']; + } + // 1b) explicit email in that column + elseif (!empty($row['creator_id']) && filter_var($row['creator_id'], FILTER_VALIDATE_EMAIL)) { + $creatorId = User::where('email', trim($row['creator_id']))->value('id'); } + // 1c) contact_email—only match on the local-part before the “@” + elseif (!empty($row['contact_email']) && filter_var($row['contact_email'], FILTER_VALIDATE_EMAIL)) { + [$local,] = explode('@', trim($row['contact_email']), 2); + // look for any user whose email starts with that same local-part + $creatorId = User::where('email', 'like', $local.'@%')->value('id'); + } + + // leave $creatorId null if no match — you said that’s OK - // build a clean attribute array + // + // 2) build attributes + // $attrs = [ - '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' => trim($row['address'] ?? 'online'), - 'event_url' => trim($row['organiser_website'] ?? ''), - 'user_email' => trim($row['contact_email'] ?? ''), - 'creator_id' => $creatorId, - 'country_iso' => strtoupper(trim($row['country'])), - 'picture' => trim($row['image_path'] ?? ''), - 'participants_count' => isset($row['participants_count']) ? (int)$row['participants_count'] : null, - 'mass_added_for' => 'Excel', - 'start_date' => $this->parseDate($row['start_date']), - 'end_date' => $this->parseDate($row['end_date']), - 'geoposition' => (!empty($row['latitude']) && !empty($row['longitude'])) - ? "{$row['latitude']},{$row['longitude']}" - : '', - 'longitude' => trim($row['longitude'] ?? ''), - 'latitude' => trim($row['latitude'] ?? ''), - 'language' => isset($row['language']) - ? strtolower(explode('_', trim($row['language']))[0]) - : 'en', + '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' => trim($row['address'] ?? 'online'), + 'event_url' => trim($row['organiser_website'] ?? ''), + 'user_email' => trim($row['contact_email'] ?? ''), + 'creator_id' => $creatorId, + 'country_iso' => strtoupper(trim($row['country'])), + 'picture' => trim($row['image_path'] ?? ''), + 'participants_count' => isset($row['participants_count']) + ? (int)$row['participants_count'] + : null, + 'mass_added_for' => 'Excel', + 'start_date' => $this->parseDate($row['start_date']), + 'end_date' => $this->parseDate($row['end_date']), + 'geoposition' => (! empty($row['latitude']) && ! empty($row['longitude'])) + ? "{$row['latitude']},{$row['longitude']}" + : '', + 'longitude' => trim($row['longitude'] ?? ''), + 'latitude' => trim($row['latitude'] ?? ''), + 'language' => isset($row['language']) + ? strtolower(explode('_', trim($row['language']))[0]) + : 'en', + 'pub_date' => now(), + 'created' => now(), + 'updated' => now(), ]; - // optional single/boolean/count fields + // optional singles & counts if (isset($row['recurring_event'])) { - $attrs['recurring_event'] = $this->validateSingleChoice($row['recurring_event'], Event::RECURRING_EVENTS); + $attrs['recurring_event'] = $this->validateSingleChoice( + $row['recurring_event'], + Event::RECURRING_EVENTS + ); } foreach (['males_count','females_count','other_count'] as $c) { if (isset($row[$c])) { $attrs[$c] = (int)$row[$c]; } } + + // optional booleans foreach (['is_extracurricular_event','is_standard_school_curriculum','is_use_resource'] as $b) { if (isset($row[$b])) { $attrs[$b] = $this->parseBool($row[$b]); } } - // multi-choice + // multi‐choice arrays if (isset($row['activity_format'])) { - $attrs['activity_format'] = $this->validateMultiChoice($row['activity_format'], Event::ACTIVITY_FORMATS); + $attrs['activity_format'] = $this->validateMultiChoice( + $row['activity_format'], + Event::ACTIVITY_FORMATS + ); } if (isset($row['ages'])) { - $attrs['ages'] = $this->validateMultiChoice($row['ages'], Event::AGES); + $attrs['ages'] = $this->validateMultiChoice( + $row['ages'], + Event::AGES + ); } if (isset($row['duration'])) { - $attrs['duration'] = $this->validateSingleChoice($row['duration'], Event::DURATIONS); + $attrs['duration'] = $this->validateSingleChoice( + $row['duration'], + Event::DURATIONS + ); } if (isset($row['recurring_type'])) { - $attrs['recurring_type'] = $this->validateSingleChoice($row['recurring_type'], Event::RECURRING_TYPES); + $attrs['recurring_type'] = $this->validateSingleChoice( + $row['recurring_type'], + Event::RECURRING_TYPES + ); } // contact_person fallback - if (Schema::hasColumn('events','contact_person') && !empty($row['contact_email'])) { + if (Schema::hasColumn('events','contact_person') && ! empty($row['contact_email'])) { $attrs['contact_person'] = trim($row['contact_email']); } + // persist & attach try { - // bypass fillable: assign property‐by‐property $event = new Event; - foreach ($attrs as $key => $value) { - $event->$key = $value; + foreach ($attrs as $k => $v) { + $event->$k = $v; } $event->save(); - // pivot attaches - if (!empty($row['audience_comma_separated_ids'])) { + if (! empty($row['audience_comma_separated_ids'])) { $aud = array_filter( array_unique(explode(',', $row['audience_comma_separated_ids'])), - fn($i)=> is_numeric($i) && $i > 0 && $i <= 100 + fn($i) => is_numeric($i) && $i > 0 && $i <= 100 ); $event->audiences()->attach($aud); } - if (!empty($row['theme_comma_separated_ids'])) { + + if (! empty($row['theme_comma_separated_ids'])) { $themes = $this->validateThemes($row['theme_comma_separated_ids']); $event->themes()->attach($themes); } return $event; - } catch (\Exception $e) { - // at least now you’ll see the real exception in your log - Log::error('Event import failed', [ - 'message' => $e->getMessage(), - 'row' => $row, - 'attrs' => $attrs, - 'trace' => $e->getTraceAsString(), - ]); - return null; + Log::error('Event import failed: '.$e->getMessage(), $attrs); + return null; } } }