Refactor SocialIssuesExportController to include method for exporting social actions

This commit is contained in:
Julie Lenaerts 2025-01-15 16:44:45 +01:00
parent edb51dd3cd
commit de6385ba21
2 changed files with 63 additions and 2 deletions

View File

@ -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',
]
);
}
}

View File

@ -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' ]