From 5bd6187f4a0ff44a0215457819f4faa1a84b5ea2 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 15 Jul 2024 23:49:28 +0200 Subject: [PATCH] Move Creation of excel document to own action --- .../Actions/CreateExcelDocumentAction.php | 68 +++++++++++++++++++ app/Form/Actions/ExportSyncAction.php | 54 +-------------- 2 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 app/Form/Actions/CreateExcelDocumentAction.php diff --git a/app/Form/Actions/CreateExcelDocumentAction.php b/app/Form/Actions/CreateExcelDocumentAction.php new file mode 100644 index 00000000..1f8b006c --- /dev/null +++ b/app/Form/Actions/CreateExcelDocumentAction.php @@ -0,0 +1,68 @@ +form = $form; + + return file_get_contents($this->allSheet($participants)->compile($this->tempPath())); + } + + /** + * @param Collection $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(); + } +} diff --git a/app/Form/Actions/ExportSyncAction.php b/app/Form/Actions/ExportSyncAction.php index d9a9e65f..044fdd78 100644 --- a/app/Form/Actions/ExportSyncAction.php +++ b/app/Form/Actions/ExportSyncAction.php @@ -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 $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(); - } }