From de6385ba21d1e0522f81809a17e2e47de05b11f9 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 15 Jan 2025 16:44:45 +0100 Subject: [PATCH] Refactor SocialIssuesExportController to include method for exporting social actions --- ...ler.php => SocialWorkExportController.php} | 63 ++++++++++++++++++- .../config/services/controller.yaml | 2 +- 2 files changed, 63 insertions(+), 2 deletions(-) rename src/Bundle/ChillPersonBundle/Controller/{SocialIssuesExportController.php => SocialWorkExportController.php} (56%) diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWorkExportController.php similarity index 56% rename from src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php rename to src/Bundle/ChillPersonBundle/Controller/SocialWorkExportController.php index 9320c9483..d79805620 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialIssuesExportController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWorkExportController.php @@ -11,7 +11,9 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; +use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository; use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository; +use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use League\Csv\CannotInsertRecord; use League\Csv\Exception; @@ -26,13 +28,15 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Security; use Symfony\Contracts\Translation\TranslatorInterface; -class SocialIssuesExportController extends AbstractController +class SocialWorkExportController extends AbstractController { public function __construct( private SocialIssueRepository $socialIssueRepository, + private SocialActionRepository $socialActionRepository, private Security $security, private TranslatorInterface $translator, private SocialIssueRender $socialIssueRender, + private SocialActionRender $socialActionRender, ) {} /** @@ -89,4 +93,61 @@ class SocialIssuesExportController extends AbstractController ] ); } + + /** + * @throws UnavailableStream + * @throws CannotInsertRecord + * @throws Exception + */ + #[Route(path: '/{_locale}/admin/social-work/social-action/export/list.{_format}', name: 'chill_person_social_action_export_list', requirements: ['_format' => 'csv'])] + public function socialActionList(Request $request, string $_format = 'csv'): StreamedResponse + { + if (!$this->security->isGranted('ROLE_ADMIN')) { + throw new AccessDeniedHttpException('Only ROLE_ADMIN can export this list'); + } + + $socialActions = $this->socialActionRepository->findAll(); + + $socialActions = array_map(function ($action) { + return [ + 'id' => $action->getId(), + 'title' => $this->socialActionRender->renderString($action, []), + 'desactivationDate' => $action->getDesactivationDate(), + 'socialIssue' => $this->socialIssueRender->renderString($action->getIssue(), []), + 'ordering' => $action->getOrdering(), + ]; + }, $socialActions); + + $csv = Writer::createFromPath('php://temp', 'r+'); + $csv->insertOne( + array_map( + fn (string $e) => $this->translator->trans($e), + [ + 'Id', + 'Title', + 'goal.desactivationDate', + 'Social issue', + 'Ordering', + ] + ) + ); + + $csv->addFormatter(fn (array $row) => null !== ($row['desactivationDate'] ?? null) ? array_merge($row, ['desactivationDate' => $row['desactivationDate']->format('Y-m-d')]) : $row); + $csv->insertAll($socialActions); + + 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 faa938b8a..efa30c803 100644 --- a/src/Bundle/ChillPersonBundle/config/services/controller.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/controller.yaml @@ -71,6 +71,6 @@ services: Chill\PersonBundle\Controller\PersonSignatureController: tags: [ 'controller.service_arguments' ] - Chill\PersonBundle\Controller\SocialIssuesExportController: + Chill\PersonBundle\Controller\SocialWorkExportController: tags: [ 'controller.service_arguments' ]