3333use App \Helpers \ZIPBinaryFileResponseFacade ;
3434use App \Message \PaymentOrder \PaymentOrderDeletedNotification ;
3535use App \Services \EmailConfirmation \ConfirmationEmailSender ;
36+ use App \Services \PaymentOrder \CSVExporter ;
3637use App \Services \PaymentOrderMailLinkGenerator ;
3738use App \Services \PaymentReferenceGenerator ;
3839use Doctrine \ORM \EntityManagerInterface ;
6566use EasyCorp \Bundle \EasyAdminBundle \Registry \DashboardControllerRegistry ;
6667use EasyCorp \Bundle \EasyAdminBundle \Router \AdminUrlGenerator ;
6768use RuntimeException ;
69+ use Symfony \Component \HttpFoundation \BinaryFileResponse ;
70+ use Symfony \Component \HttpFoundation \HeaderUtils ;
6871use Symfony \Component \HttpFoundation \Response ;
6972use Symfony \Component \Messenger \MessageBusInterface ;
7073
@@ -74,11 +77,12 @@ final class PaymentOrderCrudController extends AbstractCrudController
7477{
7578 public function __construct (
7679 private readonly PaymentOrderMailLinkGenerator $ mailToGenerator ,
77- private EntityManagerInterface $ entityManager ,
80+ private readonly EntityManagerInterface $ entityManager ,
7881 private readonly ConfirmationEmailSender $ confirmationEmailSender ,
7982 private readonly AdminUrlGenerator $ adminURLGenerator ,
8083 private readonly MessageBusInterface $ messageBus ,
8184 private readonly PaymentReferenceGenerator $ paymentReferenceGenerator ,
85+ private readonly CSVExporter $ CSVExporter ,
8286 )
8387 {
8488 }
@@ -88,6 +92,24 @@ public static function getEntityFqcn(): string
8892 return PaymentOrder::class;
8993 }
9094
95+ #[AdminAction(routePath: '/action-csv-export ' , routeName: 'admin_action_payment_order_csv_export ' , methods: ['POST ' ])]
96+ public function csvExport (BatchActionDto $ batchActionDto ): Response
97+ {
98+ $ this ->denyAccessUnlessGranted ('ROLE_SHOW_PAYMENT_ORDERS ' );
99+
100+ $ entities = $ this ->entityManager ->getRepository (PaymentOrder::class)->findBy (['id ' => $ batchActionDto ->getEntityIds ()]);
101+
102+ $ csv = $ this ->CSVExporter ->export ($ entities );
103+ $ response = new Response ($ csv );
104+ $ response ->headers ->set ('Content-type ' , 'text/csv ' );
105+ $ response ->headers ->set ('Content-length ' , (string ) strlen ($ csv ));
106+ $ response ->headers ->set ('Cache-Control ' , 'private ' );
107+ $ disposition = HeaderUtils::makeDisposition ("attachment " , "payment_orders.csv " , "payment_orders.csv " );
108+ $ response ->headers ->set ('Content-Disposition ' , $ disposition );
109+
110+ return $ response ;
111+ }
112+
91113 #[AdminAction(routePath: '/action-sepa-xml-export ' , routeName: 'admin_action_payment_order_sepa_xml_export ' , methods: ['POST ' ])]
92114 public function sepaXMLExport (BatchActionDto $ batchActionDto ): Response
93115 {
@@ -165,9 +187,9 @@ public function configureCrud(Crud $crud): Crud
165187 return $ crud
166188
167189 //Set validation groups for new and edit forms
168- ->setFormOptions ([
169- 'validation_groups ' => ['Default ' , 'backend ' ],
170- ])
190+ ->setFormOptions ([
191+ 'validation_groups ' => ['Default ' , 'backend ' ],
192+ ])
171193
172194 ->setEntityLabelInSingular ('payment_order.label ' )
173195 ->setEntityLabelInPlural ('payment_order.labelp ' )
@@ -250,17 +272,30 @@ public function configureActions(Actions $actions): Actions
250272 }
251273
252274 //if ($this->isGranted('ROLE_EXPORT_PAYMENT_ORDERS_REFERENCES')) {
253- $ actions ->addBatchAction (Action::new ('referencesExport ' , 'payment.order.action.export.export_references ' )
254- ->linkToCrudAction ('referencesExport ' )
255- ->addCssClass ('btn btn-primary ' )
256- //Together with some backend.js logic, this attribute will prevent the modal from showing
257- ->setHtmlAttributes ([
258- 'data-action-batch-no-confirm ' => 'true ' ,
259- ])
260- ->setIcon ('fas fa-file-invoice ' )
261- );
275+ $ actions ->addBatchAction (Action::new ('referencesExport ' , 'payment.order.action.export.export_references ' )
276+ ->linkToCrudAction ('referencesExport ' )
277+ ->addCssClass ('btn btn-primary ' )
278+ //Together with some backend.js logic, this attribute will prevent the modal from showing
279+ ->setHtmlAttributes ([
280+ 'data-action-batch-no-confirm ' => 'true ' ,
281+ ])
282+ ->setIcon ('fas fa-file-invoice ' )
283+ );
262284 //}
263285
286+ if ($ this ->isGranted ('ROLE_EXPORT_PAYMENT_ORDERS ' )) {
287+ $ actions ->addBatchAction (Action::new ('csvExport ' , 'CSV Export ' )
288+ ->linkToCrudAction ('csvExport ' )
289+ ->addCssClass ('btn btn-secondary ' )
290+ //Together with some backend.js logic, this attribute will prevent the modal from showing
291+ ->setHtmlAttributes ([
292+ 'data-action-batch-no-confirm ' => 'true ' ,
293+ ])
294+ ->setIcon ('fas fa-file-csv ' )
295+ );
296+ }
297+
298+
264299 $ actions ->setPermissions ([
265300 Action::INDEX => 'ROLE_SHOW_PAYMENT_ORDERS ' ,
266301 Action::DETAIL => 'ROLE_SHOW_PAYMENT_ORDERS ' ,
0 commit comments