Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2227 Upload Coding Activities EU Code Week Site #2322

Merged
merged 1 commit into from
Nov 8, 2024
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
49 changes: 49 additions & 0 deletions app/Console/Commands/excel/CodingActivitiesEUCodeWeekSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands\excel;

use App\Imports\CodingActivitiesEUCodeWeekSiteImport;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class CodingActivitiesEUCodeWeekSite extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'excel:coding_activities_eu';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Coding Activities EU CodeWeek Site From Excel File';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*/
public function handle(): void
{
Log::info('Loading Coding Activities EU CodeWeek Site');

Excel::import(
new CodingActivitiesEUCodeWeekSiteImport(),
'events.23_24.xlsx',
'excel'
);
}
}
84 changes: 84 additions & 0 deletions app/Imports/CodingActivitiesEUCodeWeekSiteImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Imports;

use App\Event;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class CodingActivitiesEUCodeWeekSiteImport extends DefaultValueBinder implements ToModel, WithCustomValueBinder, WithHeadingRow
{
public function parseDate($date)
{
if(strpos($date, '.') !== false) {
return Date::excelToDateTimeObject($date);
}

$return = [];

$date = substr($date, 0, strpos($date, ' '));
$date = trim($date);

$arr = explode('/', $date);

if(!empty($arr)) {
$return[] = $arr[2].'-'.$arr[1].'-'.$arr[0];
}

$time = substr($date, strpos($date, ' ') + 1);
$time = trim($time);

if(!empty($time)) {
$return[] = $time;
}

return implode(' ', $return);
}

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' => '',
'start_date' => $this->parseDate($row['start_date']),
'end_date' => $this->parseDate($row['end_date']),
'geoposition' => $row['latitude'].','.$row['longitude'],
'longitude' => $row['longitude'],
'latitude' => $row['latitude'],
'language' => $row['language'],
'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;

}
}
Binary file added resources/excel/events.23_24.xlsx
Binary file not shown.
Loading