wi-events/jobs/ProcessSubmitJob.php

130 lines
3.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->uploadFile();
$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 uploadFile(): void
{
$filesystem = app(Filesystem::class)->client();
$headers = $this->getFields()->map(fn ($field) => new SpreadsheetHeader($field['label']))->toArray();
$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 = (new Spreadsheet('Anmeldezahlen '.$this->event->title))->headers($headers);
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 getfields()
{
return collect($this->event->loadConfig('fields'));
}
private function makeSheet(Spreadsheet $s, $group, $groupParticipants)
{
$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);
}
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';
}
}