participant = Participant::find($this->participantId); $this->event = $this->participant->event; $this->uploadFileAll(); if ($this->event->loadConfig('uploadGroup')) { $this->uploadFileGroup(); } $this->sendMail(); } private function sendMail(): void { Mail::send('confirm_'.$this->event->slug, ['data' => $this->participant->payload], function ($message) { $message->to($this->participant->email, $this->participant->firstname.' '.$this->participant->lastname); }); } private function uploadFileAll(): void { $filesystem = app(Filesystem::class)->client(); $orderBy = collect($this->event->loadConfig('orderBy'))->map(fn ($order) => "{$order['key']} {$order['direction']}")->implode(','); $groupBy = $this->event->loadConfig('groupBy'); $s = $this->newSpreadsheet(); if ($this->event->loadConfig('groupAll')) { $this->makeSheet($s, 'Alle', Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()); } if ($groupBy) { $participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()->groupBy(fn ($p) => data_get($p->payload, $groupBy)); foreach ($participants as $group => $groupParticipants) { $this->makeSheet($s, $group, $groupParticipants); } } $s = $this->addStat($s); $spreadsheetFile = $s->generate(); $filesystem->write($this->event->slug.'/anmeldungen-'.$this->event->slug.'.xlsx', file_get_contents($spreadsheetFile)); } private function uploadFileGroup(): void { $filesystem = app(Filesystem::class)->client(); $orderBy = collect($this->event->loadConfig('orderBy'))->map(fn ($order) => "{$order['key']} {$order['direction']}")->implode(','); $groupBy = $this->event->loadConfig('groupBy'); $participantGroup = data_get($this->participant->payload, $groupBy); $participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get() ->filter(fn ($p) => data_get($p->payload, $groupBy) === $participantGroup); $s = $this->newSpreadsheet(); $this->makeSheet($s, 'Alle', $participants); $s->headers([new SpreadsheetHeader('Stamm'), new SpreadsheetHeader('TN')]); $s = $this->addStat($s); $spreadsheetFile = $s->generate(); $filesystem->write($participantGroup.'/anmeldungen-'.$this->event->slug.'.xlsx', file_get_contents($spreadsheetFile)); } private function newSpreadsheet() { $headers = $this->getFields()->map(fn ($field) => new SpreadsheetHeader($field['label']))->toArray(); return (new Spreadsheet('Anmeldezahlen '.$this->event->title))->headers($headers); } private function addStat(Spreadsheet $s): Spreadsheet { $payload = collect([]); foreach ($this->event->loadConfig('stat') as $stat) { $stat = Participant::where('event_id', $this->event->id) ->get() ->groupBy(fn ($p) => data_get($p->payload, $stat)) ->mapWithKeys(fn ($participants, $groupKey) => [$groupKey => [$groupKey, $participants->count()]]) ->values(); $payload = $payload->merge($stat); } $s->sheet('Statistik', $payload->toArray()); return $s; } private function getfields() { return collect($this->event->loadConfig('fields')); } private function makeSheet(Spreadsheet $s, $group, $groupParticipants): Spreadsheet { $groupParticipants = $groupParticipants->map(function ($participant) { $payload = $participant->payload; $content = $this->getFields()->map(function ($field) use ($payload) { $type = $field['type']; $method = 'format'.ucfirst($type); return $this->{$method}(data_get($payload, $field['key'])); }); return $content->toArray(); })->toArray(); $s->sheet($group, $groupParticipants); return $s; } private function formatString(?string $input = null): string { return $input ?: ''; } private function formatDate(?string $input = null): string { if (!$input) { return ''; } return Carbon::parse($input)->format('d.m.Y'); } private function formatEnum(?array $input = []): string { if (!$input) { return ''; } return collect($input)->implode(', '); } private function formatBool($input = false): string { return true === $input ? 'Ja' : 'Nein'; } }