diff --git a/app/Form/Actions/ExportAction.php b/app/Form/Actions/ExportAction.php new file mode 100644 index 00000000..54000524 --- /dev/null +++ b/app/Form/Actions/ExportAction.php @@ -0,0 +1,38 @@ +insertOne($form->getFields()->names()); + + foreach ($form->participants as $participant) { + $csv->insertOne($participant->getFields()->presentValues()); + } + + return $csv->toString(); + } + + public function asController(Form $form, ActionRequest $request): StreamedResponse + { + $contents = $this->handle($form); + + $filename = 'tn-' . $form->slug . '.csv'; + Storage::disk('temp')->put($filename, $contents); + + return Storage::disk('temp')->download($filename); + } +} diff --git a/app/Form/Data/FieldCollection.php b/app/Form/Data/FieldCollection.php index 73742ca3..31673e7c 100644 --- a/app/Form/Data/FieldCollection.php +++ b/app/Form/Data/FieldCollection.php @@ -92,6 +92,22 @@ class FieldCollection extends Collection return $attributes->toArray(); } + /** + * @return array + */ + public function names(): array + { + return $this->map(fn ($field) => $field->name)->toArray(); + } + + /** + * @return array + */ + public function presentValues(): array + { + return $this->map(fn ($field) => $field->presentRaw())->toArray(); + } + private function findBySpecialType(SpecialType $specialType): ?Field { return $this->first(fn ($field) => $field->specialType === $specialType); diff --git a/app/Form/Resources/FormResource.php b/app/Form/Resources/FormResource.php index bfea4a41..58964496 100644 --- a/app/Form/Resources/FormResource.php +++ b/app/Form/Resources/FormResource.php @@ -51,7 +51,8 @@ class FormResource extends JsonResource 'update' => route('form.update', ['form' => $this->getModel()]), 'destroy' => route('form.destroy', ['form' => $this->getModel()]), 'is_dirty' => route('form.is-dirty', ['form' => $this->getModel()]), - 'frontend' => str(app(FormSettings::class)->registerUrl)->replace('{slug}', $this->slug) + 'frontend' => str(app(FormSettings::class)->registerUrl)->replace('{slug}', $this->slug), + 'export' => route('form.export', ['form' => $this->getModel()]), ] ]; } diff --git a/resources/js/views/form/Index.vue b/resources/js/views/form/Index.vue index 03891882..40e24888 100644 --- a/resources/js/views/form/Index.vue +++ b/resources/js/views/form/Index.vue @@ -154,6 +154,7 @@ + diff --git a/routes/web.php b/routes/web.php index 6f372761..7c8b5352 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,6 +19,7 @@ use App\Invoice\Actions\InvoiceStoreAction; use App\Course\Actions\CourseUpdateAction; use App\Dashboard\Actions\IndexAction as DashboardIndexAction; use App\Efz\ShowEfzDocumentAction; +use App\Form\Actions\ExportAction as ActionsExportAction; use App\Form\Actions\FormDestroyAction; use App\Form\Actions\FormIndexAction; use App\Group\Actions\GroupBulkstoreAction; @@ -153,6 +154,7 @@ Route::group(['middleware' => 'auth:web'], function (): void { // ------------------------------------ form ----------------------------------- Route::get('/formtemplate', FormtemplateIndexAction::class)->name('formtemplate.index'); + Route::get('/form/{form}/export', ActionsExportAction::class)->name('form.export'); Route::get('/form', FormIndexAction::class)->name('form.index'); Route::patch('/form/{form}', FormUpdateAction::class)->name('form.update'); Route::delete('/form/{form}', FormDestroyAction::class)->name('form.destroy'); diff --git a/tests/EndToEnd/Form/FormIndexActionTest.php b/tests/EndToEnd/Form/FormIndexActionTest.php index 80d93c34..1120929f 100644 --- a/tests/EndToEnd/Form/FormIndexActionTest.php +++ b/tests/EndToEnd/Form/FormIndexActionTest.php @@ -52,6 +52,7 @@ class FormIndexActionTest extends FormTestCase ->assertInertiaPath('data.data.0.registration_from', '2023-05-06 04:00:00') ->assertInertiaPath('data.data.0.registration_until', '2023-04-01 05:00:00') ->assertInertiaPath('data.data.0.links.participant_index', route('form.participant.index', ['form' => $form])) + ->assertInertiaPath('data.data.0.links.export', route('form.export', ['form' => $form])) ->assertInertiaPath('data.meta.links.store', route('form.store')) ->assertInertiaPath('data.meta.links.formtemplate_index', route('formtemplate.index')) ->assertInertiaPath('data.meta.templates.0.name', 'tname') diff --git a/tests/Feature/Form/ParticipantExportActionTest.php b/tests/Feature/Form/ParticipantExportActionTest.php new file mode 100644 index 00000000..ab2577f9 --- /dev/null +++ b/tests/Feature/Form/ParticipantExportActionTest.php @@ -0,0 +1,43 @@ +login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->has(Participant::factory()->data(['stufe' => 'Pfadfinder', 'vorname' => 'Max', 'select' => ['A', 'B']])) + ->sections([ + FormtemplateSectionRequest::new()->fields([ + $this->textField('vorname')->name('Vorname'), + $this->checkboxesField('select')->name('Abcselect')->options(['A', 'B', 'C']), + $this->dropdownField('stufe')->name('Stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']), + ]), + ]) + ->name('ZEM 2024') + ->create(); + + $this->get(route('form.export', ['form' => $form]))->assertDownload('tn-zem-2024.csv'); + $contents = Storage::disk('temp')->get('tn-zem-2024.csv'); + $this->assertTrue(str_contains($contents, 'Max')); + $this->assertTrue(str_contains($contents, 'A, B')); + $this->assertTrue(str_contains($contents, 'Pfadfinder')); + $this->assertTrue(str_contains($contents, 'Stufe')); + $this->assertTrue(str_contains($contents, 'Abcselect')); + } +}