Skip to content

Commit 390c05c

Browse files
committed
#2227 Upload Coding Activities EU Code Week Site
1 parent dffa66e commit 390c05c

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Console\Commands\excel;
4+
5+
use App\Imports\CodingActivitiesEUCodeWeekSiteImport;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Log;
8+
use Maatwebsite\Excel\Facades\Excel;
9+
10+
class CodingActivitiesEUCodeWeekSite extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'excel:coding_activities_eu';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Coding Activities EU CodeWeek Site From Excel File';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
parent::__construct();
34+
}
35+
36+
/**
37+
* Execute the console command.
38+
*/
39+
public function handle(): void
40+
{
41+
Log::info('Loading Coding Activities EU CodeWeek Site');
42+
43+
Excel::import(
44+
new CodingActivitiesEUCodeWeekSiteImport(),
45+
'events.23_24.xlsx',
46+
'excel'
47+
);
48+
}
49+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace App\Imports;
4+
5+
use App\Event;
6+
use Carbon\Carbon;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Support\Facades\Log;
9+
use Maatwebsite\Excel\Concerns\ToModel;
10+
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
11+
use Maatwebsite\Excel\Concerns\WithHeadingRow;
12+
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
13+
use PhpOffice\PhpSpreadsheet\Shared\Date;
14+
15+
class CodingActivitiesEUCodeWeekSiteImport extends DefaultValueBinder implements ToModel, WithCustomValueBinder, WithHeadingRow
16+
{
17+
public function parseDate($date)
18+
{
19+
if(strpos($date, '.') !== false) {
20+
return Date::excelToDateTimeObject($date);
21+
}
22+
23+
$return = [];
24+
25+
$date = substr($date, 0, strpos($date, ' '));
26+
$date = trim($date);
27+
28+
$arr = explode('/', $date);
29+
30+
if(!empty($arr)) {
31+
$return[] = $arr[2].'-'.$arr[1].'-'.$arr[0];
32+
}
33+
34+
$time = substr($date, strpos($date, ' ') + 1);
35+
$time = trim($time);
36+
37+
if(!empty($time)) {
38+
$return[] = $time;
39+
}
40+
41+
return implode(' ', $return);
42+
}
43+
44+
public function model(array $row): ?Model
45+
{
46+
47+
$event = new Event([
48+
'status' => 'APPROVED',
49+
'title' => $row['activity_title'],
50+
'slug' => str_slug($row['activity_title']),
51+
'organizer' => $row['name_of_organisation'],
52+
'description' => $row['description'],
53+
'organizer_type' => $row['type_of_organisation'],
54+
'activity_type' => $row['activity_type'],
55+
'location' => isset($row['address']) ? $row['address'] : 'online',
56+
'event_url' => $row['organiser_website'],
57+
'contact_person' => !empty($row['contact_email']) ? $row['contact_email'] : '',
58+
'user_email' => '',
59+
'creator_id' => 132942,
60+
'country_iso' => $row['country'],
61+
'picture' => isset($row['image_path']) ? $row['image_path'] : '',
62+
'pub_date' => now(),
63+
'created' => now(),
64+
'updated' => now(),
65+
'codeweek_for_all_participation_code' => '',
66+
'start_date' => $this->parseDate($row['start_date']),
67+
'end_date' => $this->parseDate($row['end_date']),
68+
'geoposition' => $row['latitude'].','.$row['longitude'],
69+
'longitude' => $row['longitude'],
70+
'latitude' => $row['latitude'],
71+
'language' => $row['language'],
72+
'approved_by' => 19588,
73+
'mass_added_for' => 'Excel',
74+
]);
75+
76+
$event->save();
77+
78+
$event->audiences()->attach(explode(',', $row['audience_comma_separated_ids']));
79+
$event->themes()->attach(explode(',', $row['theme_comma_separated_ids']));
80+
81+
return $event;
82+
83+
}
84+
}

resources/excel/events.23_24.xlsx

14 KB
Binary file not shown.

0 commit comments

Comments
 (0)