wi-events/jobs/ProcessSubmitJob.php

150 lines
4.7 KiB
PHP

<?php
namespace Zoomyboy\Event\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;
use Zoomyboy\Event\Classes\Spreadsheet;
use Zoomyboy\Event\Classes\SpreadsheetHeader;
use Zoomyboy\Event\Models\Event;
use Zoomyboy\Event\Models\Participant;
use Zoomyboy\Owncloud\Classes\Filesystem;
class ProcessSubmitJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public Participant $participant;
public Event $event;
/**
* Create a new job instance.
*/
public function __construct(public int $participantId)
{
}
/**
* Execute the job.
*/
public function handle(): void
{
$this->participant = Participant::find($this->participantId);
$this->event = $this->participant->event;
$this->uploadFileAll();
$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');
$participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()->groupBy(fn ($p) => data_get($p->payload, $groupBy));
$s = $this->newSpreadsheet();
if ($this->event->loadConfig('groupAll')) {
$this->makeSheet($s, 'Alle', Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get());
}
foreach ($participants as $group => $groupParticipants) {
$this->makeSheet($s, $group, $groupParticipants);
}
$spreadsheetFile = $s->generate();
$filesystem->write($this->event->slug.'/anmeldungen.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);
$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 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';
}
}