Move Creation of excel document to own action
This commit is contained in:
parent
b67fa85539
commit
5bd6187f4a
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form\Actions;
|
||||
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Models\Participant;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Lorisleiva\Actions\Concerns\AsAction;
|
||||
use Zoomyboy\TableDocument\SheetData;
|
||||
use Zoomyboy\TableDocument\TableDocumentData;
|
||||
|
||||
class CreateExcelDocumentAction
|
||||
{
|
||||
use AsAction;
|
||||
|
||||
public Form $form;
|
||||
|
||||
public function handle(Form $form, Collection $participants): string
|
||||
{
|
||||
$this->form = $form;
|
||||
|
||||
return file_get_contents($this->allSheet($participants)->compile($this->tempPath()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Participant> $participants
|
||||
*/
|
||||
private function allSheet(Collection $participants): TableDocumentData
|
||||
{
|
||||
$document = TableDocumentData::from(['title' => 'Anmeldungen für ' . $this->form->name, 'sheets' => []]);
|
||||
$headers = $this->form->getFields()->map(fn ($field) => $field->name)->toArray();
|
||||
|
||||
$document->addSheet(SheetData::from([
|
||||
'header' => $headers,
|
||||
'data' => $participants
|
||||
->map(fn ($participant) => $this->form->getFields()->map(fn ($field) => $participant->getFields()->find($field)->presentRaw())->toArray())
|
||||
->toArray(),
|
||||
'name' => 'Alle',
|
||||
]));
|
||||
|
||||
if ($this->form->export->groupBy) {
|
||||
$groups = $participants->groupBy(fn ($participant) => $participant->getFields()->findByKey($this->form->export->groupBy)->presentRaw());
|
||||
|
||||
foreach ($groups as $name => $participants) {
|
||||
$document->addSheet(SheetData::from([
|
||||
'header' => $headers,
|
||||
'data' => $participants
|
||||
->map(fn ($participant) => $this->form->getFields()->map(fn ($field) => $participant->getFields()->find($field)->presentRaw())->toArray())
|
||||
->toArray(),
|
||||
'name' => $name,
|
||||
]));
|
||||
}
|
||||
|
||||
$document->addSheet(SheetData::from([
|
||||
'header' => ['Wert', 'Anzahl'],
|
||||
'data' => $groups->map(fn ($participants, $name) => [$name, (string) count($participants)])->toArray(),
|
||||
'name' => 'Statistik',
|
||||
]));
|
||||
}
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
private function tempPath(): string
|
||||
{
|
||||
return sys_get_temp_dir() . '/' . str()->uuid()->toString();
|
||||
}
|
||||
}
|
|
@ -3,12 +3,8 @@
|
|||
namespace App\Form\Actions;
|
||||
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Models\Participant;
|
||||
use App\Group;
|
||||
use Illuminate\Support\Collection;
|
||||
use Lorisleiva\Actions\Concerns\AsAction;
|
||||
use Zoomyboy\TableDocument\SheetData;
|
||||
use Zoomyboy\TableDocument\TableDocumentData;
|
||||
|
||||
class ExportSyncAction
|
||||
{
|
||||
|
@ -18,15 +14,13 @@ class ExportSyncAction
|
|||
|
||||
public function handle(Form $form): void
|
||||
{
|
||||
$this->form = $form;
|
||||
|
||||
if (!$form->export->root) {
|
||||
return;
|
||||
}
|
||||
|
||||
$storage = $form->export->root->getStorage();
|
||||
|
||||
$storage->put($form->export->root->resource . '/Anmeldungen ' . $form->name . '.xlsx', file_get_contents($this->allSheet($this->form->participants)->compile($this->tempPath())));
|
||||
$storage->put($form->export->root->resource . '/Anmeldungen ' . $form->name . '.xlsx', CreateExcelDocumentAction::run($form, $form->participants));
|
||||
|
||||
if ($form->export->toGroupField) {
|
||||
foreach ($form->participants->groupBy(fn ($participant) => $participant->data[$form->export->toGroupField]) as $groupId => $participants) {
|
||||
|
@ -35,7 +29,7 @@ class ExportSyncAction
|
|||
continue;
|
||||
}
|
||||
|
||||
$group->fileshare->getStorage()->put($group->fileshare->resource . '/Anmeldungen ' . $form->name . '.xlsx', file_get_contents($this->allSheet($participants)->compile($this->tempPath())));
|
||||
$group->fileshare->getStorage()->put($group->fileshare->resource . '/Anmeldungen ' . $form->name . '.xlsx', CreateExcelDocumentAction::run($form, $participants));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,48 +38,4 @@ class ExportSyncAction
|
|||
{
|
||||
$this->handle(Form::find($formId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Participant> $participants
|
||||
*/
|
||||
private function allSheet(Collection $participants): TableDocumentData
|
||||
{
|
||||
$document = TableDocumentData::from(['title' => 'Anmeldungen für ' . $this->form->name, 'sheets' => []]);
|
||||
$headers = $this->form->getFields()->map(fn ($field) => $field->name)->toArray();
|
||||
|
||||
$document->addSheet(SheetData::from([
|
||||
'header' => $headers,
|
||||
'data' => $participants
|
||||
->map(fn ($participant) => $this->form->getFields()->map(fn ($field) => $participant->getFields()->find($field)->presentRaw())->toArray())
|
||||
->toArray(),
|
||||
'name' => 'Alle',
|
||||
]));
|
||||
|
||||
if ($this->form->export->groupBy) {
|
||||
$groups = $participants->groupBy(fn ($participant) => $participant->getFields()->findByKey($this->form->export->groupBy)->presentRaw());
|
||||
|
||||
foreach ($groups as $name => $participants) {
|
||||
$document->addSheet(SheetData::from([
|
||||
'header' => $headers,
|
||||
'data' => $participants
|
||||
->map(fn ($participant) => $this->form->getFields()->map(fn ($field) => $participant->getFields()->find($field)->presentRaw())->toArray())
|
||||
->toArray(),
|
||||
'name' => $name,
|
||||
]));
|
||||
}
|
||||
|
||||
$document->addSheet(SheetData::from([
|
||||
'header' => ['Wert', 'Anzahl'],
|
||||
'data' => $groups->map(fn ($participants, $name) => [$name, (string) count($participants)])->toArray(),
|
||||
'name' => 'Statistik',
|
||||
]));
|
||||
}
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
private function tempPath(): string
|
||||
{
|
||||
return sys_get_temp_dir() . '/' . str()->uuid()->toString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue