Skip to content
Merged

Dev #2838

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
170 changes: 88 additions & 82 deletions app/Imports/GenericEventsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
Expand All @@ -16,11 +17,9 @@ class GenericEventsImport extends BaseEventsImport implements ToModel, WithCusto
{
public function parseDate($value)
{
// Handle Excel dates or normal string dates
if (is_numeric($value)) {
return Date::excelToDateTimeObject($value)->format('Y-m-d H:i:s');
}

try {
return Carbon::parse($value)->format('Y-m-d H:i:s');
} catch (\Exception $e) {
Expand All @@ -33,7 +32,7 @@ public function model(array $row): ?Model
{
Log::info('Importing row:', $row);

// Validate required fields
// required fields
if (
empty($row['activity_title']) ||
empty($row['name_of_organisation']) ||
Expand All @@ -48,100 +47,107 @@ public function model(array $row): ?Model
return null;
}

// Resolve creator_id
// resolve creator_id (if that column exists)
$creatorId = null;
if (!empty($row['creator_id'])) {
if (is_numeric($row['creator_id'])) {
$creatorId = (int) $row['creator_id'];
} else {
$creatorId = User::where('email', trim($row['creator_id']))->value('id');
}
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');
}
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']) : '',
'user_email' => !empty($row['contact_email']) ? trim($row['contact_email']) : '',
'creator_id' => $creatorId,
'country_iso' => strtoupper(trim($row['country'])),
'picture' => !empty($row['image_path']) ? trim($row['image_path']) : '',
'pub_date' => now(),
'created' => now(),
'updated' => now(),
'codeweek_for_all_participation_code' => '', // You can make this configurable
'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' => !empty($row['longitude']) ? trim($row['longitude']) : '',
'latitude' => !empty($row['latitude']) ? trim($row['latitude']) : '',
'language' => !empty($row['language']) ? strtolower(explode('_', trim($row['language']))[0]) : 'en',
'mass_added_for' => 'Excel',
'recurring_event' => isset($row['recurring_event'])
? $this->validateSingleChoice($row['recurring_event'], Event::RECURRING_EVENTS)
: null,

'males_count' => isset($row['males_count']) ? (int) $row['males_count'] : null,
'females_count' => isset($row['females_count']) ? (int) $row['females_count'] : null,
'other_count' => isset($row['other_count']) ? (int) $row['other_count'] : null,

'is_extracurricular_event' => isset($row['is_extracurricular_event'])
? $this->parseBool($row['is_extracurricular_event'])
: false,

'is_standard_school_curriculum' => isset($row['is_standard_school_curriculum'])
? $this->parseBool($row['is_standard_school_curriculum'])
: false,

'is_use_resource' => isset($row['is_use_resource'])
? $this->parseBool($row['is_use_resource'])
: false,

'activity_format' => isset($row['activity_format'])
? $this->validateMultiChoice($row['activity_format'], Event::ACTIVITY_FORMATS)
: [],

'ages' => isset($row['ages'])
? $this->validateMultiChoice($row['ages'], Event::AGES)
: [],
$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' => isset($row['address']) ? trim($row['address']) : 'online',
'event_url' => isset($row['organiser_website']) ? trim($row['organiser_website']) : '',
'user_email' => isset($row['contact_email']) ? trim($row['contact_email']) : '',
'creator_id' => $creatorId,
'country_iso' => strtoupper(trim($row['country'])),
'picture' => isset($row['image_path']) ? trim($row['image_path']) : '',
'pub_date' => now(),
'created' => now(),
'updated' => now(),
'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' => (isset($row['latitude'], $row['longitude']) && $row['latitude'] !== '' && $row['longitude'] !== '')
? $row['latitude'] . ',' . $row['longitude']
: '',
'longitude' => isset($row['longitude']) ? trim($row['longitude']) : '',
'latitude' => isset($row['latitude']) ? trim($row['latitude']) : '',
'language' => isset($row['language'])
? strtolower(explode('_', trim($row['language']))[0])
: 'en',
];

// optional single‐choice / boolean / counts
if (isset($row['recurring_event'])) {
$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];
}
}
foreach (['is_extracurricular_event','is_standard_school_curriculum','is_use_resource'] as $b) {
if (isset($row[$b])) {
$attrs[$b] = $this->parseBool($row[$b]);
}
}

'duration' => isset($row['duration'])
? $this->validateSingleChoice($row['duration'], Event::DURATIONS)
: null,
// multi‐choice arrays
if (isset($row['activity_format'])) {
$attrs['activity_format'] = $this->validateMultiChoice($row['activity_format'], Event::ACTIVITY_FORMATS);
}
if (isset($row['ages'])) {
$attrs['ages'] = $this->validateMultiChoice($row['ages'], Event::AGES);
}
if (isset($row['duration'])) {
$attrs['duration'] = $this->validateSingleChoice($row['duration'], Event::DURATIONS);
}
if (isset($row['recurring_type'])) {
$attrs['recurring_type'] = $this->validateSingleChoice($row['recurring_type'], Event::RECURRING_TYPES);
}

'recurring_type' => isset($row['recurring_type'])
? $this->validateSingleChoice($row['recurring_type'], Event::RECURRING_TYPES)
: null,
]);
// contact_person only if the *DB* has that column and we have a contact_email
if (Schema::hasColumn('events','contact_person') && isset($row['contact_email']) && $row['contact_email'] !== '') {
$attrs['contact_person'] = trim($row['contact_email']);
}

try {
$event = new Event($attrs);
$event->save();

// Audiences
if (!empty($row['audience_comma_separated_ids'])) {
$audiences = array_unique(array_map('trim', explode(',', $row['audience_comma_separated_ids'])));
$audiences = array_filter($audiences, fn($id) => is_numeric($id) && $id > 0 && $id <= 100);
if (!empty($audiences)) {
$event->audiences()->attach($audiences);
// pivot attaches
if (isset($row['audience_comma_separated_ids'])) {
$aud = array_filter(
array_unique(
array_map('trim', explode(',', $row['audience_comma_separated_ids']))
),
fn($id) => is_numeric($id) && $id > 0 && $id <= 100
);
if ($aud) {
$event->audiences()->attach($aud);
}
}

// Themes
if (!empty($row['theme_comma_separated_ids'])) {
$validThemeIds = $this->validateThemes($row['theme_comma_separated_ids'] ?? '');
if (count($validThemeIds) > 0) {
$event->themes()->attach($validThemeIds);
if (isset($row['theme_comma_separated_ids'])) {
$themes = $this->validateThemes($row['theme_comma_separated_ids']);
if (count($themes)) {
$event->themes()->attach($themes);
}
}

return $event;
} catch (\Exception $e) {
Log::error('Event import failed: ' . $e->getMessage());
Log::error('Event import failed: '.$e->getMessage());
return null;
}
}
Expand Down
Loading