mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
[export form] decouple data from PickCenter form
This commit is contained in:
parent
77f8cf0e1a
commit
d1e1b1c4ce
@ -13,6 +13,7 @@ namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\SavedExport;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Export\ExportFormHelper;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Form\SavedExportType;
|
||||
use Chill\MainBundle\Form\Type\Export\ExportType;
|
||||
@ -86,7 +87,8 @@ class ExportController extends AbstractController
|
||||
LoggerInterface $logger,
|
||||
SessionInterface $session,
|
||||
TranslatorInterface $translator,
|
||||
EntityManagerInterface $entityManager
|
||||
EntityManagerInterface $entityManager,
|
||||
private readonly ExportFormHelper $exportFormHelper,
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->redis = $chillRedis;
|
||||
@ -293,10 +295,15 @@ class ExportController extends AbstractController
|
||||
$isGenerate = strpos($step, 'generate_') === 0;
|
||||
|
||||
$builder = $this->formFactory
|
||||
->createNamedBuilder(null, FormType::class, [], [
|
||||
->createNamedBuilder(
|
||||
null,
|
||||
FormType::class,
|
||||
$this->exportFormHelper->getDefaultData($step, $exportManager->getExport($alias)),
|
||||
[
|
||||
'method' => $isGenerate ? 'GET' : 'POST',
|
||||
'csrf_protection' => $isGenerate ? false : true,
|
||||
]);
|
||||
]
|
||||
);
|
||||
|
||||
// TODO: add a condition to be able to select a regroupment of centers?
|
||||
|
||||
@ -341,7 +348,7 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function exportFormStep(Request $request, $export, $alias)
|
||||
private function exportFormStep(Request $request, $export, $alias)
|
||||
{
|
||||
$exportManager = $this->exportManager;
|
||||
|
||||
@ -405,7 +412,7 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function formatterFormStep(Request $request, $export, $alias)
|
||||
private function formatterFormStep(Request $request, $export, $alias)
|
||||
{
|
||||
// check we have data from the previous step (export step)
|
||||
$data = $this->session->get('export_step', null);
|
||||
@ -462,7 +469,7 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse
|
||||
*/
|
||||
protected function forwardToGenerate(Request $request, $export, $alias)
|
||||
private function forwardToGenerate(Request $request, $export, $alias)
|
||||
{
|
||||
$dataCenters = $this->session->get('centers_step_raw', null);
|
||||
$dataFormatter = $this->session->get('formatter_step_raw', null);
|
||||
@ -494,7 +501,7 @@ class ExportController extends AbstractController
|
||||
return $this->redirectToRoute('chill_main_export_download', ['key' => $key, 'alias' => $alias]);
|
||||
}
|
||||
|
||||
protected function rebuildData($key)
|
||||
private function rebuildData($key)
|
||||
{
|
||||
$rawData = $this->rebuildRawData($key);
|
||||
|
||||
@ -527,7 +534,7 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function selectCentersStep(Request $request, $export, $alias)
|
||||
private function selectCentersStep(Request $request, $export, $alias)
|
||||
{
|
||||
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
|
37
src/Bundle/ChillMainBundle/Export/ExportFormHelper.php
Normal file
37
src/Bundle/ChillMainBundle/Export/ExportFormHelper.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?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\MainBundle\Export;
|
||||
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
|
||||
|
||||
final readonly class ExportFormHelper
|
||||
{
|
||||
public function __construct(
|
||||
private ExportManager $exportManager,
|
||||
private AuthorizationHelperForCurrentUserInterface $authorizationHelper,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<"centers"> $steps
|
||||
*/
|
||||
public function getDefaultData(string $step, ExportInterface $export): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if ($step === 'centers') {
|
||||
$data['centers'] = $this->authorizationHelper->getReachableCenters($export->requiredRole());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -22,7 +22,10 @@ use function count;
|
||||
|
||||
class ExportPickCenterDataMapper implements DataMapperInterface
|
||||
{
|
||||
protected RegroupmentRepository $regroupmentRepository;
|
||||
public function __construct(
|
||||
private RegroupmentRepository $regroupmentRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
public function mapDataToForms($data, $forms): void
|
||||
{
|
||||
@ -37,15 +40,15 @@ class ExportPickCenterDataMapper implements DataMapperInterface
|
||||
|
||||
foreach ($this->regroupmentRepository->findAll() as $regroupment) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$contained, $notContained] = $regroupment->getCenters()->partition(static fn (Center $center): bool => false);
|
||||
[$contained, $notContained] = $regroupment->getCenters()->partition(static fn (int $id, Center $center): bool => false);
|
||||
|
||||
if (0 === count($notContained)) {
|
||||
$pickedRegroupment[] = $regroupment;
|
||||
}
|
||||
}
|
||||
|
||||
$form['regroupment']->setData($pickedRegroupment);
|
||||
$form['centers']->setData($data);
|
||||
$form['regroupment']->setData([]);
|
||||
$form['center']->setData($data);
|
||||
}
|
||||
|
||||
public function mapFormsToData($forms, &$data): void
|
||||
|
@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\Regroupment;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper;
|
||||
use Chill\MainBundle\Repository\RegroupmentRepository;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
@ -33,22 +34,18 @@ final class PickCenterType extends AbstractType
|
||||
{
|
||||
public const CENTERS_IDENTIFIERS = 'c';
|
||||
|
||||
private AuthorizationHelperInterface $authorizationHelper;
|
||||
private AuthorizationHelperForCurrentUserInterface $authorizationHelper;
|
||||
|
||||
private ExportManager $exportManager;
|
||||
|
||||
private RegroupmentRepository $regroupmentRepository;
|
||||
|
||||
private UserInterface $user;
|
||||
|
||||
public function __construct(
|
||||
TokenStorageInterface $tokenStorage,
|
||||
ExportManager $exportManager,
|
||||
RegroupmentRepository $regroupmentRepository,
|
||||
AuthorizationHelperInterface $authorizationHelper
|
||||
AuthorizationHelperForCurrentUserInterface $authorizationHelper
|
||||
) {
|
||||
$this->exportManager = $exportManager;
|
||||
$this->user = $tokenStorage->getToken()->getUser();
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->regroupmentRepository = $regroupmentRepository;
|
||||
}
|
||||
@ -57,18 +54,16 @@ final class PickCenterType extends AbstractType
|
||||
{
|
||||
$export = $this->exportManager->getExport($options['export_alias']);
|
||||
$centers = $this->authorizationHelper->getReachableCenters(
|
||||
$this->user,
|
||||
$export->requiredRole()
|
||||
);
|
||||
|
||||
$builder->add('center', EntityType::class, [
|
||||
'class' => Center::class,
|
||||
'label' => 'center',
|
||||
'choices' => $centers,
|
||||
'label' => 'center',
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choice_label' => static fn (Center $c) => $c->getName(),
|
||||
'data' => $centers,
|
||||
]);
|
||||
|
||||
if (count($this->regroupmentRepository->findAllActive()) > 0) {
|
||||
@ -82,7 +77,7 @@ final class PickCenterType extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->setDataMapper(new ExportPickCenterDataMapper());
|
||||
$builder->setDataMapper(new ExportPickCenterDataMapper($this->regroupmentRepository));
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
|
@ -6,6 +6,8 @@ services:
|
||||
Chill\MainBundle\Export\Helper\:
|
||||
resource: '../../Export/Helper'
|
||||
|
||||
Chill\MainBundle\Export\ExportFormHelper: ~
|
||||
|
||||
chill.main.export_element_validator:
|
||||
class: Chill\MainBundle\Validator\Constraints\Export\ExportElementConstraintValidator
|
||||
tags:
|
||||
|
Loading…
x
Reference in New Issue
Block a user