From 5d64089b2dda4fbeb36d63516dc5e44a911bc55a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Fri, 16 Feb 2024 21:53:59 +0100 Subject: [PATCH] wip --- composer.json | 3 +- src/DataTable.php | 12 ++++++++ src/Exporter/Form/Type/ExportDataType.php | 30 ++++++++++++-------- src/Request/HttpFoundationRequestHandler.php | 2 ++ src/Type/DataTableType.php | 3 +- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index da398be0..1cac812a 100755 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "symfony/mime": "^6.0", "symfony/twig-bundle": "^6.0", "twig/extra-bundle": "^3.6", - "twig/intl-extra": "^3.6" + "twig/intl-extra": "^3.6", + "symfony/validator": "^6.0" }, "require-dev": { "roave/security-advisories": "dev-latest", diff --git a/src/DataTable.php b/src/DataTable.php index 4aa7dfaa..b377872d 100755 --- a/src/DataTable.php +++ b/src/DataTable.php @@ -41,6 +41,7 @@ use Kreyu\Bundle\DataTableBundle\Query\ResultSetInterface; use Kreyu\Bundle\DataTableBundle\Sorting\SortingData; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; class DataTable implements DataTableInterface { @@ -731,6 +732,17 @@ public function createPersonalizationFormBuilder(?DataTableView $view = null): F ); } + public function getExportForm(): FormInterface + { + return $this->exportForm ??= $this->config->getExportFormFactory()->createNamed( + name: $this->config->getExportParameterName(), + type: ExportDataType::class, + options: [ + 'exporters' => $this->getExporters(), + ], + ); + } + public function createExportFormBuilder(?DataTableView $view = null): FormBuilderInterface { if (!$this->config->isExportingEnabled()) { diff --git a/src/Exporter/Form/Type/ExportDataType.php b/src/Exporter/Form/Type/ExportDataType.php index 767eab00..13684b1d 100755 --- a/src/Exporter/Form/Type/ExportDataType.php +++ b/src/Exporter/Form/Type/ExportDataType.php @@ -12,13 +12,22 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Validator\Constraints; class ExportDataType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('filename', TextType::class) + ->add('filename', TextType::class, [ + 'required' => true, + 'constraints' => [ + new Constraints\NotBlank(), + new Constraints\Callback(function (string $filename): bool { + // TODO: return str_contains($filename, ''); + }) + ], + ]) ->add('exporter', ChoiceType::class, [ 'choices' => array_flip(array_map( fn (ExporterInterface $exporter) => $exporter->getConfig()->getOption('label', $exporter->getName()), @@ -34,16 +43,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void public function configureOptions(OptionsResolver $resolver): void { - $resolver->setDefaults([ - 'data_class' => ExportData::class, - 'translation_domain' => 'KreyuDataTable', - 'exporters' => [], - ]); - - $resolver->setAllowedTypes('exporters', ExporterInterface::class.'[]'); - - // TODO: Remove deprecated default filename option - $resolver->setDefault('default_filename', null); - $resolver->setDeprecated('default_filename', 'kreyu/data-table-bundle', '0.14', 'The "%name%" option is deprecated.'); + $resolver + ->setDefaults([ + 'data_class' => ExportData::class, + 'translation_domain' => 'KreyuDataTable', + 'exporters' => [], + ]) + ->setAllowedTypes('exporters', ExporterInterface::class.'[]') + ; } } diff --git a/src/Request/HttpFoundationRequestHandler.php b/src/Request/HttpFoundationRequestHandler.php index a5b9de8d..3fd50e8f 100755 --- a/src/Request/HttpFoundationRequestHandler.php +++ b/src/Request/HttpFoundationRequestHandler.php @@ -113,6 +113,8 @@ private function export(DataTableInterface $dataTable, Request $request): void return; } + $dataTable->getExportForm()->handleRequest($request); + $form = $dataTable->createExportFormBuilder()->getForm(); $form->handleRequest($request); diff --git a/src/Type/DataTableType.php b/src/Type/DataTableType.php index da3c7ae3..04994c81 100755 --- a/src/Type/DataTableType.php +++ b/src/Type/DataTableType.php @@ -375,8 +375,7 @@ private function createPersonalizationFormView(DataTableView $view, DataTableInt private function createExportFormView(DataTableView $view, DataTableInterface $dataTable): FormView { - $form = $dataTable->createExportFormBuilder()->getForm(); - $form->setData($dataTable->getExportData()); + $form = $dataTable->getExportForm(); return $this->createFormView($form, $view, $dataTable); }