|
| 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 | +} |
0 commit comments