mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-28 01:25:00 +00:00
Compute allowed centers and regroupment at the time of generating the export
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
<?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\Form\DataMapper;
|
||||
|
||||
use Chill\MainBundle\Entity\Regroupment;
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
final readonly class ExportPickCenterDataMapper implements DataMapperInterface
|
||||
{
|
||||
public function mapDataToForms($viewData, \Traversable $forms): void
|
||||
{
|
||||
if (null === $viewData) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var array<string, FormInterface> $form */
|
||||
$form = iterator_to_array($forms);
|
||||
|
||||
$form['center']->setData($viewData);
|
||||
|
||||
// NOTE: we do not map back the regroupments
|
||||
}
|
||||
|
||||
public function mapFormsToData(\Traversable $forms, &$viewData): void
|
||||
{
|
||||
/** @var array<string, FormInterface> $forms */
|
||||
$forms = iterator_to_array($forms);
|
||||
|
||||
$centers = [];
|
||||
|
||||
foreach ($forms['center']->getData() as $center) {
|
||||
$centers[spl_object_hash($center)] = $center;
|
||||
}
|
||||
|
||||
if (\array_key_exists('regroupment', $forms)) {
|
||||
/** @var Regroupment $regroupment */
|
||||
foreach ($forms['regroupment']->getData() as $regroupment) {
|
||||
foreach ($regroupment->getCenters() as $center) {
|
||||
$centers[spl_object_hash($center)] = $center;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$viewData = array_values($centers);
|
||||
}
|
||||
}
|
@@ -14,9 +14,9 @@ namespace Chill\MainBundle\Form\Type\Export;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
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\Service\Regroupement\RegroupementFiltering;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
@@ -27,27 +27,26 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
*/
|
||||
final class PickCenterType extends AbstractType
|
||||
{
|
||||
public const CENTERS_IDENTIFIERS = 'c';
|
||||
|
||||
public function __construct(
|
||||
private readonly ExportManager $exportManager,
|
||||
private readonly RegroupmentRepository $regroupmentRepository,
|
||||
private readonly AuthorizationHelperForCurrentUserInterface $authorizationHelper,
|
||||
private readonly RegroupementFiltering $regroupementFiltering,
|
||||
) {}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$export = $this->exportManager->getExport($options['export_alias']);
|
||||
$centers = $this->authorizationHelper->getReachableCenters(
|
||||
$export->requiredRole()
|
||||
$export->requiredRole(),
|
||||
);
|
||||
|
||||
$centersActive = array_filter($centers, fn (Center $c) => $c->getIsActive());
|
||||
|
||||
// order alphabetically
|
||||
usort($centersActive, fn (Center $a, Center $b) => $a->getCenter() <=> $b->getName());
|
||||
usort($centersActive, fn (Center $a, Center $b) => $a->getName() <=> $b->getName());
|
||||
|
||||
$builder->add('center', EntityType::class, [
|
||||
$builder->add('centers', EntityType::class, [
|
||||
'class' => Center::class,
|
||||
'choices' => $centersActive,
|
||||
'label' => 'center',
|
||||
@@ -56,18 +55,22 @@ final class PickCenterType extends AbstractType
|
||||
'choice_label' => static fn (Center $c) => $c->getName(),
|
||||
]);
|
||||
|
||||
if (\count($this->regroupmentRepository->findAllActive()) > 0) {
|
||||
$builder->add('regroupment', EntityType::class, [
|
||||
$groups = $this->regroupementFiltering
|
||||
->filterContainsAtLeastOneCenter($this->regroupmentRepository->findAllActive(), $centersActive);
|
||||
|
||||
// order alphabetically
|
||||
usort($groups, fn (Regroupment $a, Regroupment $b) => $a->getName() <=> $b->getName());
|
||||
|
||||
if (\count($groups) > 0) {
|
||||
$builder->add('regroupments', EntityType::class, [
|
||||
'class' => Regroupment::class,
|
||||
'label' => 'regroupment',
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choices' => $this->regroupmentRepository->findAllActive(),
|
||||
'choices' => $groups,
|
||||
'choice_label' => static fn (Regroupment $r) => $r->getName(),
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->setDataMapper(new ExportPickCenterDataMapper());
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
|
Reference in New Issue
Block a user