mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Create social issue export controller
This commit is contained in:
parent
bd9ad8a569
commit
c379bccad4
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository;
|
||||
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
|
||||
use League\Csv\CannotInsertRecord;
|
||||
use League\Csv\Exception;
|
||||
use League\Csv\UnavailableStream;
|
||||
use League\Csv\Writer;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class SocialIssuesExportController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private SocialIssueRepository $socialIssueRepository,
|
||||
private Security $security,
|
||||
private TranslatorInterface $translator,
|
||||
private SocialIssueRender $socialIssueRender,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws UnavailableStream
|
||||
* @throws CannotInsertRecord
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Route(path: '/{_locale}/admin/social-work/social-issue/export/list.{_format}', name: 'chill_person_social_issue_export_list', requirements: ['_format' => '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',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
@ -71,3 +71,6 @@ services:
|
||||
Chill\PersonBundle\Controller\PersonSignatureController:
|
||||
tags: [ 'controller.service_arguments' ]
|
||||
|
||||
Chill\PersonBundle\Controller\SocialIssuesExportController:
|
||||
tags: [ 'controller.service_arguments' ]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user