From c379bccad488b1ca16704f3d67fa3757907d66e6 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 15 Jan 2025 16:33:57 +0100 Subject: [PATCH] Create social issue export controller --- .../SocialIssuesExportController.php | 93 +++++++++++++++++++ .../config/services/controller.yaml | 3 + 2 files changed, 96 insertions(+) create mode 100644 src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php b/src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php new file mode 100644 index 000000000..5bdef7d45 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php @@ -0,0 +1,93 @@ + 'csv'])] + public function socialIssueList(Request $request, string $_format = 'csv'): StreamedResponse + { + if (!$this->security->isGranted('ROLE_ADMIN')) { + throw new AccessDeniedHttpException('Only ROLE_ADMIN can export this list'); + } + +// $socialIssues = $this->socialIssueRepository->findAllAsArray(); + $socialIssues = $this->socialIssueRepository->findAll(); + + $socialIssues = array_map(function ($issue) { + return [ + 'id' => $issue->getId(), + 'title' => $this->socialIssueRender->renderString($issue, []), + 'ordering' => $issue->getOrdering(), + 'desactivationDate' => $issue->getDesactivationDate(), + ]; + }, $socialIssues); + + $csv = Writer::createFromPath('php://temp', 'r+'); + $csv->insertOne( + array_map( + fn (string $e) => $this->translator->trans($e), + [ + 'Id', + 'Title', + 'Ordering', + 'goal.desactivationDate', + ] + ) + ); + + $csv->addFormatter(fn (array $row) => null !== ($row['desactivationDate'] ?? null) ? array_merge($row, ['desactivationDate' => $row['desactivationDate']->format('Y-m-d')]) : $row); + $csv->insertAll($socialIssues); + + return new StreamedResponse( + function () use ($csv) { + foreach ($csv->chunk(1024) as $chunk) { + echo $chunk; + flush(); + } + }, + Response::HTTP_OK, + [ + 'Content-Encoding' => 'none', + 'Content-Type' => 'text/csv; charset=UTF-8', + 'Content-Disposition' => 'attachment; users.csv', + ] + ); + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/controller.yaml b/src/Bundle/ChillPersonBundle/config/services/controller.yaml index 6d5efcfe1..faa938b8a 100644 --- a/src/Bundle/ChillPersonBundle/config/services/controller.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/controller.yaml @@ -71,3 +71,6 @@ services: Chill\PersonBundle\Controller\PersonSignatureController: tags: [ 'controller.service_arguments' ] + Chill\PersonBundle\Controller\SocialIssuesExportController: + tags: [ 'controller.service_arguments' ] +