From 7f9e045d5db27f0b627f1d8bef1433c073d63a28 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 26 Jan 2023 11:28:13 +0100 Subject: [PATCH 01/11] FEATURE [regroupment][exports] first commit to implement regroupment entity in exports --- .../ChillMainBundle/Controller/ExportController.php | 2 ++ .../Form/Type/Export/PickRegroupmentType.php | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php diff --git a/src/Bundle/ChillMainBundle/Controller/ExportController.php b/src/Bundle/ChillMainBundle/Controller/ExportController.php index 84bf80c6b..caeec8327 100644 --- a/src/Bundle/ChillMainBundle/Controller/ExportController.php +++ b/src/Bundle/ChillMainBundle/Controller/ExportController.php @@ -298,6 +298,8 @@ class ExportController extends AbstractController 'csrf_protection' => $isGenerate ? false : true, ]); + // TODO: add a condition to be able to select a regroupment of centers? + if ('centers' === $step || 'generate_centers' === $step) { $builder->add('centers', PickCenterType::class, [ 'export_alias' => $alias, diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php new file mode 100644 index 000000000..e60388fd3 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php @@ -0,0 +1,10 @@ + Date: Fri, 3 Feb 2023 12:29:38 +0100 Subject: [PATCH 02/11] FEATURE [regroupment][form] integrate in regroupment admin entity into PickCenterType, datamapping not working --- .../Form/Type/Export/PickCenterType.php | 92 ++++++++++++++++--- .../views/Export/new_centers_step.html.twig | 52 ++++++----- 2 files changed, 105 insertions(+), 39 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 20db0b535..f2cda1628 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -13,13 +13,17 @@ namespace Chill\MainBundle\Form\Type\Export; use Chill\MainBundle\Center\GroupingCenterInterface; use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; +use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; @@ -34,7 +38,7 @@ use function in_array; /** * Pick centers amongst available centers for the user. */ -class PickCenterType extends AbstractType +class PickCenterType extends AbstractType implements DataMapperInterface { public const CENTERS_IDENTIFIERS = 'c'; @@ -42,10 +46,10 @@ class PickCenterType extends AbstractType protected ExportManager $exportManager; - /** - * @var array|GroupingCenterInterface[] - */ - protected array $groupingCenters = []; +// /** +// * @var array|GroupingCenterInterface[] +// */ +// protected array $groupingCenters = []; protected UserInterface $user; @@ -72,8 +76,9 @@ class PickCenterType extends AbstractType $export->requiredRole() ); - $builder->add(self::CENTERS_IDENTIFIERS, EntityType::class, [ + $builder->add('center', EntityType::class, [ 'class' => Center::class, + 'label' => 'center', 'choices' => $centers, 'multiple' => true, 'expanded' => true, @@ -81,9 +86,19 @@ class PickCenterType extends AbstractType return $c->getName(); }, 'data' => count($this->groupingCenters) > 0 ? null : $centers, - ]); + ]) + ->add('regroupment', EntityType::class, [ + 'class' => Regroupment::class, + 'label' => 'regroupment', + 'multiple' => true, + 'expanded' => true, + 'choice_label' => static function (Regroupment $r) { + return $r->getName(); + }, + ]) + ->setDataMapper($this); - if (count($this->groupingCenters) > 0) { +/* if (count($this->groupingCenters) > 0) { $groupingBuilder = $builder->create('g', null, [ 'compound' => true, ]); @@ -105,18 +120,65 @@ class PickCenterType extends AbstractType if ($groupingBuilder->count() > 0) { $builder->add($groupingBuilder); } - } + }*/ - $builder->addModelTransformer(new CallbackTransformer( +/* $builder->addModelTransformer(new CallbackTransformer( function ($data) use ($centers) { return $this->transform($data, $centers); }, function ($data) use ($centers) { return $this->reverseTransform($data, $centers); } - )); + ));*/ } + public function mapDataToForms($viewData, $forms) + { + if (null === $viewData) { + return; + } + + if (!$viewData instanceof Center || !$viewData instanceof Regroupment) { + throw new UnexpectedTypeException($viewData, [Center::class, Regroupment::class]); + } + + $forms = iterator_to_array($forms); + + $forms['centers']->setData($viewData->getCenters()); + + } + + public function mapFormsToData($forms, &$viewData) + { + $forms = iterator_to_array($forms); + + $centersArray = []; + + array_push($centersArray, $forms['center']); + + dump($forms['regroupment']); + +// array_push($centersArray, ) + +// $viewData = array_merge($centersArray, $forms['regroupment']); + + $viewData = $forms['regroupment']; + dump($forms); + dump($viewData); + + } + + /* public function buildView(FormView $view, FormInterface $form, array $options) + { + $export = $this->exportManager->getExport($options['export_alias']); + $centers = $this->authorizationHelper->getReachableCenters( + $this->user, + $export->requiredRole() + ); + + $view->vars['is_hidden'] = count($centers) <= 1; + }*/ + public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('export_alias'); @@ -137,7 +199,7 @@ class PickCenterType extends AbstractType return $result; } - protected function reverseTransform($data, $centers) +/* protected function reverseTransform($data, $centers) { $picked = $data[self::CENTERS_IDENTIFIERS] instanceof \Doctrine\Common\Collections\Collection ? @@ -159,10 +221,10 @@ class PickCenterType extends AbstractType } return array_unique($picked); - } + }*/ - protected function transform($data, $centers) +/* protected function transform($data, $centers) { return $data; - } + }*/ } diff --git a/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig index 8874744c4..a73913154 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig @@ -1,5 +1,5 @@ {# - * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, / * * This program is free software: you can redistribute it and/or modify @@ -22,39 +22,43 @@ {% block content %}
- + {{ include('@ChillMain/Export/_breadcrumb.html.twig') }} - +

{{ export.title|trans }}

- +

{{ export.description|trans }}

- + {{ form_start(form) }} - +
- +

{{ 'Pick centers'|trans }}

- +

{{ 'The export will contains only data from the picked centers.'|trans }} {{ 'This will eventually restrict your possibilities in filtering the data.'|trans }}

- - {{ form_widget(form.centers.c) }} - - {% if form.centers.children.g is defined %} - -

{{ 'Pick aggregated centers'|trans }}

- - {% for f in form.centers.children.g.children %} - {{ form_row(f) }} - {% endfor %} - - {% endif %} - + +

{{ 'Center'|trans }}

+ {{ form_widget(form.centers.center) }} + +

{{ 'Pick aggregated centers'|trans }}

+ {{ form_widget(form.centers.regroupment) }} + +{# {% if form.centers.children.g is defined %}#} +{##} +{#

{{ 'Pick aggregated centers'|trans }}

#} +{##} +{# {% for f in form.centers.children.g.children %}#} +{# {{ form_row(f) }}#} +{# {% endfor %}#} +{##} +{# {% endif %}#} +
- +

{{ form_widget(form.submit, { 'attr' : { 'class' : 'btn btn-action btn-create' }, 'label' : 'Go to export options' } ) }}

- + {{ form_end(form) }} - +
{% endblock content %} From 5756a371786d62882d51f448d63b996a2aad122f Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 10:23:19 +0100 Subject: [PATCH 03/11] FEATURE [datamapper][regroupment] moved datamapper to seperate class. Still not working --- .../Form/DataMapper/RegroupmentDataMapper.php | 67 ++++++++++ .../Form/Type/Export/PickCenterType.php | 121 +----------------- .../ChillMainBundle/config/services/form.yaml | 4 + 3 files changed, 76 insertions(+), 116 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php new file mode 100644 index 000000000..958e4c013 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php @@ -0,0 +1,67 @@ +regroupment = $regroupment; + } + + public function mapDataToForms($data, $forms) + { + $forms = iterator_to_array($forms); + + if ($this->regroupment instanceof Regroupment) { + $forms['regroupment']->setData($this->regroupment->getCenters()); + + dump($forms['regroupment']); + + return; + } + + if (null === $data) { + return; + } + + if ($data instanceof Regroupment) { + $forms['regroupment']->setData($data); + } + } + + public function mapFormsToData($forms, &$data) + { + $forms = iterator_to_array($forms); + + if (isset($forms['regroupment'])) { + if ($this->regroupment instanceof Regroupment) { + $data = $this->regroupment; + } else { + $data = []; + + foreach ($forms['regroupment']->getData() as $key => $regroupment) + { + dump($regroupment->getCenters()); + $data[$key] = $regroupment->getCenters(); + dump($data); + } +// $data = $forms['regroupment']->getData(); + } + } + } +} diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index f2cda1628..1390809a5 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -15,6 +15,7 @@ use Chill\MainBundle\Center\GroupingCenterInterface; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; +use Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -38,7 +39,7 @@ use function in_array; /** * Pick centers amongst available centers for the user. */ -class PickCenterType extends AbstractType implements DataMapperInterface +class PickCenterType extends AbstractType { public const CENTERS_IDENTIFIERS = 'c'; @@ -46,11 +47,6 @@ class PickCenterType extends AbstractType implements DataMapperInterface protected ExportManager $exportManager; -// /** -// * @var array|GroupingCenterInterface[] -// */ -// protected array $groupingCenters = []; - protected UserInterface $user; public function __construct( @@ -86,7 +82,7 @@ class PickCenterType extends AbstractType implements DataMapperInterface return $c->getName(); }, 'data' => count($this->groupingCenters) > 0 ? null : $centers, - ]) + ]) ->add('regroupment', EntityType::class, [ 'class' => Regroupment::class, 'label' => 'regroupment', @@ -95,90 +91,11 @@ class PickCenterType extends AbstractType implements DataMapperInterface 'choice_label' => static function (Regroupment $r) { return $r->getName(); }, - ]) - ->setDataMapper($this); - -/* if (count($this->groupingCenters) > 0) { - $groupingBuilder = $builder->create('g', null, [ - 'compound' => true, ]); - foreach ($this->groupingCenters as $key => $gc) { - $choices = $this->buildChoices($centers, $gc); - - if (count($choices) > 0) { - $groupingBuilder->add($key, ChoiceType::class, [ - 'choices' => $choices, - 'multiple' => true, - 'expanded' => true, - 'label' => $gc->getName(), - 'required' => false, - ]); - } - } - - if ($groupingBuilder->count() > 0) { - $builder->add($groupingBuilder); - } - }*/ - -/* $builder->addModelTransformer(new CallbackTransformer( - function ($data) use ($centers) { - return $this->transform($data, $centers); - }, - function ($data) use ($centers) { - return $this->reverseTransform($data, $centers); - } - ));*/ + $builder->setDataMapper(new RegroupmentDataMapper()); } - - public function mapDataToForms($viewData, $forms) - { - if (null === $viewData) { - return; - } - - if (!$viewData instanceof Center || !$viewData instanceof Regroupment) { - throw new UnexpectedTypeException($viewData, [Center::class, Regroupment::class]); - } - - $forms = iterator_to_array($forms); - - $forms['centers']->setData($viewData->getCenters()); - - } - - public function mapFormsToData($forms, &$viewData) - { - $forms = iterator_to_array($forms); - - $centersArray = []; - - array_push($centersArray, $forms['center']); - - dump($forms['regroupment']); - -// array_push($centersArray, ) - -// $viewData = array_merge($centersArray, $forms['regroupment']); - - $viewData = $forms['regroupment']; - dump($forms); - dump($viewData); - - } - - /* public function buildView(FormView $view, FormInterface $form, array $options) - { - $export = $this->exportManager->getExport($options['export_alias']); - $centers = $this->authorizationHelper->getReachableCenters( - $this->user, - $export->requiredRole() - ); - - $view->vars['is_hidden'] = count($centers) <= 1; - }*/ - + public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('export_alias'); @@ -199,32 +116,4 @@ class PickCenterType extends AbstractType implements DataMapperInterface return $result; } -/* protected function reverseTransform($data, $centers) - { - $picked = $data[self::CENTERS_IDENTIFIERS] - instanceof \Doctrine\Common\Collections\Collection ? - $data[self::CENTERS_IDENTIFIERS]->toArray() - : - $data[self::CENTERS_IDENTIFIERS]; - - if (array_key_exists('g', $data)) { - foreach ($data['g'] as $gcid => $group) { - $picked = - array_merge( - array_intersect( - $this->groupingCenters[$gcid]->getCentersForGroup($group), - $centers - ), - $picked - ); - } - } - - return array_unique($picked); - }*/ - -/* protected function transform($data, $centers) - { - return $data; - }*/ } diff --git a/src/Bundle/ChillMainBundle/config/services/form.yaml b/src/Bundle/ChillMainBundle/config/services/form.yaml index 8157c27e9..f306658fd 100644 --- a/src/Bundle/ChillMainBundle/config/services/form.yaml +++ b/src/Bundle/ChillMainBundle/config/services/form.yaml @@ -138,6 +138,10 @@ services: autowire: true autoconfigure: true + Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper: + autowire: true + autoconfigure: true + Chill\MainBundle\Form\RegroupmentType: autowire: true autoconfigure: true From 068311d071330e788ca1c4b9b342b62b13b304b6 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 15:36:57 +0100 Subject: [PATCH 04/11] FEATURE [datamapper][regroupment] make the datamapper work --- .../ChillMainBundle/Entity/Regroupment.php | 4 +- .../DataMapper/ExportPickCenterDataMapper.php | 77 +++++++++++++++++++ .../Form/DataMapper/RegroupmentDataMapper.php | 67 ---------------- .../Form/Type/Export/PickCenterType.php | 22 +----- .../Repository/RegroupmentRepository.php | 61 +++++++++++++++ 5 files changed, 144 insertions(+), 87 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php delete mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php create mode 100644 src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php diff --git a/src/Bundle/ChillMainBundle/Entity/Regroupment.php b/src/Bundle/ChillMainBundle/Entity/Regroupment.php index 5faffd17c..5de02ade6 100644 --- a/src/Bundle/ChillMainBundle/Entity/Regroupment.php +++ b/src/Bundle/ChillMainBundle/Entity/Regroupment.php @@ -24,7 +24,7 @@ class Regroupment /** * @var Center * @ORM\ManyToMany( - * targetEntity="Chill\MainBundle\Entity\Center" + * targetEntity=Center::class * ) * @ORM\Id */ @@ -52,7 +52,7 @@ class Regroupment $this->centers = new ArrayCollection(); } - public function getCenters(): ?Collection + public function getCenters(): Collection { return $this->centers; } diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php new file mode 100644 index 000000000..884f8cb78 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php @@ -0,0 +1,77 @@ + $form */ + $form = iterator_to_array($forms); + + $pickedRegroupment = []; + foreach ($this->regroupmentRepository->findAll() as $regroupment) { + [$contained, $notContained] = $regroupment->getCenters()->partition(function (Center $center) { + + }); + + if (0 === count($notContained)) { + $pickedRegroupment[] = $regroupment; + } + } + + $form['regroupment']->setData($pickedRegroupment); + $form['centers']->setData($data); + } + + /** + * @param iterable $forms + * @param array $data + * @return void + */ + public function mapFormsToData($forms, &$data) + { + /** @var array $forms */ + $forms = iterator_to_array($forms); + + $centers = []; + + foreach ($forms['center']->getData() as $center) { + $centers[spl_object_hash($center)] = $center; + } + + foreach ($forms['regroupment']->getData() as $regroupment) { + /** @var Regroupment $regroupment */ + foreach ($regroupment->getCenters() as $center) { + $centers[spl_object_hash($center)] = $center; + } + } + + $data = array_values($centers); + } +} diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php deleted file mode 100644 index 958e4c013..000000000 --- a/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php +++ /dev/null @@ -1,67 +0,0 @@ -regroupment = $regroupment; - } - - public function mapDataToForms($data, $forms) - { - $forms = iterator_to_array($forms); - - if ($this->regroupment instanceof Regroupment) { - $forms['regroupment']->setData($this->regroupment->getCenters()); - - dump($forms['regroupment']); - - return; - } - - if (null === $data) { - return; - } - - if ($data instanceof Regroupment) { - $forms['regroupment']->setData($data); - } - } - - public function mapFormsToData($forms, &$data) - { - $forms = iterator_to_array($forms); - - if (isset($forms['regroupment'])) { - if ($this->regroupment instanceof Regroupment) { - $data = $this->regroupment; - } else { - $data = []; - - foreach ($forms['regroupment']->getData() as $key => $regroupment) - { - dump($regroupment->getCenters()); - $data[$key] = $regroupment->getCenters(); - dump($data); - } -// $data = $forms['regroupment']->getData(); - } - } - } -} diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 1390809a5..674f42aa7 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -15,6 +15,8 @@ use Chill\MainBundle\Center\GroupingCenterInterface; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; +use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper; +use Chill\MainBundle\Form\DataMapper\PickCenterDataMapper; use Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; @@ -93,27 +95,11 @@ class PickCenterType extends AbstractType }, ]); - $builder->setDataMapper(new RegroupmentDataMapper()); + $builder->setDataMapper(new ExportPickCenterDataMapper()); } - + public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('export_alias'); } - - protected function buildChoices($reachablesCenters, GroupingCenterInterface $gc) - { - $result = []; - - foreach ($gc->getGroups() as $group) { - foreach ($gc->getCentersForGroup($group) as $center) { - if (in_array($center, $reachablesCenters, true)) { - $result[$group] = $group; - } - } - } - - return $result; - } - } diff --git a/src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php b/src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php new file mode 100644 index 000000000..6e84ef9b7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php @@ -0,0 +1,61 @@ +repository = $entityManager->getRepository(Regroupment::class); + } + + public function find($id, $lockMode = null, $lockVersion = null): ?Regroupment + { + return $this->repository->find($id, $lockMode, $lockVersion); + } + + /** + * @return Regroupment[] + */ + public function findAll(): array + { + return $this->repository->findAll(); + } + + /** + * @param mixed|null $limit + * @param mixed|null $offset + * + * @return Regroupment[] + */ + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria, ?array $orderBy = null): ?Regroupment + { + return $this->repository->findOneBy($criteria, $orderBy); + } + + public function getClassName() + { + return Regroupment::class; + } +} From b5ec0919e769c454010a684b86c3e06c3a52ec5a Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 15:39:36 +0100 Subject: [PATCH 05/11] FEATURE [datamapper][regroupment] minor fix --- .../DataMapper/ExportPickCenterDataMapper.php | 35 +++++++++++-------- .../Form/Type/Export/PickCenterType.php | 12 ------- .../Form/Type/Export/PickRegroupmentType.php | 10 +++++- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php index 884f8cb78..0d21b0adb 100644 --- a/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php @@ -14,8 +14,10 @@ namespace Chill\MainBundle\Form\DataMapper; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Repository\RegroupmentRepository; +use Exception; use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\FormInterface; +use function count; class ExportPickCenterDataMapper implements DataMapperInterface { @@ -24,34 +26,39 @@ class ExportPickCenterDataMapper implements DataMapperInterface /** * @param array|Center[] $data * @param $forms + * + * @throws Exception + * * @return mixed - * @throws \Exception */ public function mapDataToForms($data, $forms) { - throw new \Exception("not implemented"); + if (null === $data) { + return; + } - /** @var array $form */ - $form = iterator_to_array($forms); + /** @var array $form */ + $form = iterator_to_array($forms); - $pickedRegroupment = []; - foreach ($this->regroupmentRepository->findAll() as $regroupment) { - [$contained, $notContained] = $regroupment->getCenters()->partition(function (Center $center) { + $pickedRegroupment = []; - }); + foreach ($this->regroupmentRepository->findAll() as $regroupment) { + [$contained, $notContained] = $regroupment->getCenters()->partition(static function (Center $center) { + }); - if (0 === count($notContained)) { - $pickedRegroupment[] = $regroupment; - } - } + if (0 === count($notContained)) { + $pickedRegroupment[] = $regroupment; + } + } - $form['regroupment']->setData($pickedRegroupment); - $form['centers']->setData($data); + $form['regroupment']->setData($pickedRegroupment); + $form['centers']->setData($data); } /** * @param iterable $forms * @param array $data + * * @return void */ public function mapFormsToData($forms, &$data) diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 674f42aa7..17768544b 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -16,27 +16,15 @@ use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper; -use Chill\MainBundle\Form\DataMapper\PickCenterDataMapper; -use Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\CallbackTransformer; -use Symfony\Component\Form\DataMapperInterface; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\User\UserInterface; -use function array_intersect; -use function array_key_exists; -use function array_merge; -use function array_unique; use function count; -use function in_array; /** * Pick centers amongst available centers for the user. diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php index e60388fd3..7b04d2ebd 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php @@ -1,10 +1,18 @@ Date: Thu, 26 Jan 2023 11:28:13 +0100 Subject: [PATCH 06/11] FEATURE [regroupment][exports] first commit to implement regroupment entity in exports --- .../ChillMainBundle/Controller/ExportController.php | 2 ++ .../Form/Type/Export/PickRegroupmentType.php | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php diff --git a/src/Bundle/ChillMainBundle/Controller/ExportController.php b/src/Bundle/ChillMainBundle/Controller/ExportController.php index 84bf80c6b..caeec8327 100644 --- a/src/Bundle/ChillMainBundle/Controller/ExportController.php +++ b/src/Bundle/ChillMainBundle/Controller/ExportController.php @@ -298,6 +298,8 @@ class ExportController extends AbstractController 'csrf_protection' => $isGenerate ? false : true, ]); + // TODO: add a condition to be able to select a regroupment of centers? + if ('centers' === $step || 'generate_centers' === $step) { $builder->add('centers', PickCenterType::class, [ 'export_alias' => $alias, diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php new file mode 100644 index 000000000..e60388fd3 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php @@ -0,0 +1,10 @@ + Date: Fri, 3 Feb 2023 12:29:38 +0100 Subject: [PATCH 07/11] FEATURE [regroupment][form] integrate in regroupment admin entity into PickCenterType, datamapping not working --- .../Form/Type/Export/PickCenterType.php | 92 ++++++++++++++++--- .../views/Export/new_centers_step.html.twig | 52 ++++++----- 2 files changed, 105 insertions(+), 39 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 20db0b535..f2cda1628 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -13,13 +13,17 @@ namespace Chill\MainBundle\Form\Type\Export; use Chill\MainBundle\Center\GroupingCenterInterface; use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; +use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; @@ -34,7 +38,7 @@ use function in_array; /** * Pick centers amongst available centers for the user. */ -class PickCenterType extends AbstractType +class PickCenterType extends AbstractType implements DataMapperInterface { public const CENTERS_IDENTIFIERS = 'c'; @@ -42,10 +46,10 @@ class PickCenterType extends AbstractType protected ExportManager $exportManager; - /** - * @var array|GroupingCenterInterface[] - */ - protected array $groupingCenters = []; +// /** +// * @var array|GroupingCenterInterface[] +// */ +// protected array $groupingCenters = []; protected UserInterface $user; @@ -72,8 +76,9 @@ class PickCenterType extends AbstractType $export->requiredRole() ); - $builder->add(self::CENTERS_IDENTIFIERS, EntityType::class, [ + $builder->add('center', EntityType::class, [ 'class' => Center::class, + 'label' => 'center', 'choices' => $centers, 'multiple' => true, 'expanded' => true, @@ -81,9 +86,19 @@ class PickCenterType extends AbstractType return $c->getName(); }, 'data' => count($this->groupingCenters) > 0 ? null : $centers, - ]); + ]) + ->add('regroupment', EntityType::class, [ + 'class' => Regroupment::class, + 'label' => 'regroupment', + 'multiple' => true, + 'expanded' => true, + 'choice_label' => static function (Regroupment $r) { + return $r->getName(); + }, + ]) + ->setDataMapper($this); - if (count($this->groupingCenters) > 0) { +/* if (count($this->groupingCenters) > 0) { $groupingBuilder = $builder->create('g', null, [ 'compound' => true, ]); @@ -105,18 +120,65 @@ class PickCenterType extends AbstractType if ($groupingBuilder->count() > 0) { $builder->add($groupingBuilder); } - } + }*/ - $builder->addModelTransformer(new CallbackTransformer( +/* $builder->addModelTransformer(new CallbackTransformer( function ($data) use ($centers) { return $this->transform($data, $centers); }, function ($data) use ($centers) { return $this->reverseTransform($data, $centers); } - )); + ));*/ } + public function mapDataToForms($viewData, $forms) + { + if (null === $viewData) { + return; + } + + if (!$viewData instanceof Center || !$viewData instanceof Regroupment) { + throw new UnexpectedTypeException($viewData, [Center::class, Regroupment::class]); + } + + $forms = iterator_to_array($forms); + + $forms['centers']->setData($viewData->getCenters()); + + } + + public function mapFormsToData($forms, &$viewData) + { + $forms = iterator_to_array($forms); + + $centersArray = []; + + array_push($centersArray, $forms['center']); + + dump($forms['regroupment']); + +// array_push($centersArray, ) + +// $viewData = array_merge($centersArray, $forms['regroupment']); + + $viewData = $forms['regroupment']; + dump($forms); + dump($viewData); + + } + + /* public function buildView(FormView $view, FormInterface $form, array $options) + { + $export = $this->exportManager->getExport($options['export_alias']); + $centers = $this->authorizationHelper->getReachableCenters( + $this->user, + $export->requiredRole() + ); + + $view->vars['is_hidden'] = count($centers) <= 1; + }*/ + public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('export_alias'); @@ -137,7 +199,7 @@ class PickCenterType extends AbstractType return $result; } - protected function reverseTransform($data, $centers) +/* protected function reverseTransform($data, $centers) { $picked = $data[self::CENTERS_IDENTIFIERS] instanceof \Doctrine\Common\Collections\Collection ? @@ -159,10 +221,10 @@ class PickCenterType extends AbstractType } return array_unique($picked); - } + }*/ - protected function transform($data, $centers) +/* protected function transform($data, $centers) { return $data; - } + }*/ } diff --git a/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig index 8874744c4..a73913154 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig @@ -1,5 +1,5 @@ {# - * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, / * * This program is free software: you can redistribute it and/or modify @@ -22,39 +22,43 @@ {% block content %}
- + {{ include('@ChillMain/Export/_breadcrumb.html.twig') }} - +

{{ export.title|trans }}

- +

{{ export.description|trans }}

- + {{ form_start(form) }} - +
- +

{{ 'Pick centers'|trans }}

- +

{{ 'The export will contains only data from the picked centers.'|trans }} {{ 'This will eventually restrict your possibilities in filtering the data.'|trans }}

- - {{ form_widget(form.centers.c) }} - - {% if form.centers.children.g is defined %} - -

{{ 'Pick aggregated centers'|trans }}

- - {% for f in form.centers.children.g.children %} - {{ form_row(f) }} - {% endfor %} - - {% endif %} - + +

{{ 'Center'|trans }}

+ {{ form_widget(form.centers.center) }} + +

{{ 'Pick aggregated centers'|trans }}

+ {{ form_widget(form.centers.regroupment) }} + +{# {% if form.centers.children.g is defined %}#} +{##} +{#

{{ 'Pick aggregated centers'|trans }}

#} +{##} +{# {% for f in form.centers.children.g.children %}#} +{# {{ form_row(f) }}#} +{# {% endfor %}#} +{##} +{# {% endif %}#} +
- +

{{ form_widget(form.submit, { 'attr' : { 'class' : 'btn btn-action btn-create' }, 'label' : 'Go to export options' } ) }}

- + {{ form_end(form) }} - +
{% endblock content %} From fb9b9b922606afcfed058a42c7ef1c4fdc562fd1 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 10:23:19 +0100 Subject: [PATCH 08/11] FEATURE [datamapper][regroupment] moved datamapper to seperate class. Still not working --- .../Form/DataMapper/RegroupmentDataMapper.php | 67 ++++++++++ .../Form/Type/Export/PickCenterType.php | 121 +----------------- .../ChillMainBundle/config/services/form.yaml | 4 + 3 files changed, 76 insertions(+), 116 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php new file mode 100644 index 000000000..958e4c013 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php @@ -0,0 +1,67 @@ +regroupment = $regroupment; + } + + public function mapDataToForms($data, $forms) + { + $forms = iterator_to_array($forms); + + if ($this->regroupment instanceof Regroupment) { + $forms['regroupment']->setData($this->regroupment->getCenters()); + + dump($forms['regroupment']); + + return; + } + + if (null === $data) { + return; + } + + if ($data instanceof Regroupment) { + $forms['regroupment']->setData($data); + } + } + + public function mapFormsToData($forms, &$data) + { + $forms = iterator_to_array($forms); + + if (isset($forms['regroupment'])) { + if ($this->regroupment instanceof Regroupment) { + $data = $this->regroupment; + } else { + $data = []; + + foreach ($forms['regroupment']->getData() as $key => $regroupment) + { + dump($regroupment->getCenters()); + $data[$key] = $regroupment->getCenters(); + dump($data); + } +// $data = $forms['regroupment']->getData(); + } + } + } +} diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index f2cda1628..1390809a5 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -15,6 +15,7 @@ use Chill\MainBundle\Center\GroupingCenterInterface; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; +use Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -38,7 +39,7 @@ use function in_array; /** * Pick centers amongst available centers for the user. */ -class PickCenterType extends AbstractType implements DataMapperInterface +class PickCenterType extends AbstractType { public const CENTERS_IDENTIFIERS = 'c'; @@ -46,11 +47,6 @@ class PickCenterType extends AbstractType implements DataMapperInterface protected ExportManager $exportManager; -// /** -// * @var array|GroupingCenterInterface[] -// */ -// protected array $groupingCenters = []; - protected UserInterface $user; public function __construct( @@ -86,7 +82,7 @@ class PickCenterType extends AbstractType implements DataMapperInterface return $c->getName(); }, 'data' => count($this->groupingCenters) > 0 ? null : $centers, - ]) + ]) ->add('regroupment', EntityType::class, [ 'class' => Regroupment::class, 'label' => 'regroupment', @@ -95,90 +91,11 @@ class PickCenterType extends AbstractType implements DataMapperInterface 'choice_label' => static function (Regroupment $r) { return $r->getName(); }, - ]) - ->setDataMapper($this); - -/* if (count($this->groupingCenters) > 0) { - $groupingBuilder = $builder->create('g', null, [ - 'compound' => true, ]); - foreach ($this->groupingCenters as $key => $gc) { - $choices = $this->buildChoices($centers, $gc); - - if (count($choices) > 0) { - $groupingBuilder->add($key, ChoiceType::class, [ - 'choices' => $choices, - 'multiple' => true, - 'expanded' => true, - 'label' => $gc->getName(), - 'required' => false, - ]); - } - } - - if ($groupingBuilder->count() > 0) { - $builder->add($groupingBuilder); - } - }*/ - -/* $builder->addModelTransformer(new CallbackTransformer( - function ($data) use ($centers) { - return $this->transform($data, $centers); - }, - function ($data) use ($centers) { - return $this->reverseTransform($data, $centers); - } - ));*/ + $builder->setDataMapper(new RegroupmentDataMapper()); } - - public function mapDataToForms($viewData, $forms) - { - if (null === $viewData) { - return; - } - - if (!$viewData instanceof Center || !$viewData instanceof Regroupment) { - throw new UnexpectedTypeException($viewData, [Center::class, Regroupment::class]); - } - - $forms = iterator_to_array($forms); - - $forms['centers']->setData($viewData->getCenters()); - - } - - public function mapFormsToData($forms, &$viewData) - { - $forms = iterator_to_array($forms); - - $centersArray = []; - - array_push($centersArray, $forms['center']); - - dump($forms['regroupment']); - -// array_push($centersArray, ) - -// $viewData = array_merge($centersArray, $forms['regroupment']); - - $viewData = $forms['regroupment']; - dump($forms); - dump($viewData); - - } - - /* public function buildView(FormView $view, FormInterface $form, array $options) - { - $export = $this->exportManager->getExport($options['export_alias']); - $centers = $this->authorizationHelper->getReachableCenters( - $this->user, - $export->requiredRole() - ); - - $view->vars['is_hidden'] = count($centers) <= 1; - }*/ - + public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('export_alias'); @@ -199,32 +116,4 @@ class PickCenterType extends AbstractType implements DataMapperInterface return $result; } -/* protected function reverseTransform($data, $centers) - { - $picked = $data[self::CENTERS_IDENTIFIERS] - instanceof \Doctrine\Common\Collections\Collection ? - $data[self::CENTERS_IDENTIFIERS]->toArray() - : - $data[self::CENTERS_IDENTIFIERS]; - - if (array_key_exists('g', $data)) { - foreach ($data['g'] as $gcid => $group) { - $picked = - array_merge( - array_intersect( - $this->groupingCenters[$gcid]->getCentersForGroup($group), - $centers - ), - $picked - ); - } - } - - return array_unique($picked); - }*/ - -/* protected function transform($data, $centers) - { - return $data; - }*/ } diff --git a/src/Bundle/ChillMainBundle/config/services/form.yaml b/src/Bundle/ChillMainBundle/config/services/form.yaml index 8157c27e9..f306658fd 100644 --- a/src/Bundle/ChillMainBundle/config/services/form.yaml +++ b/src/Bundle/ChillMainBundle/config/services/form.yaml @@ -138,6 +138,10 @@ services: autowire: true autoconfigure: true + Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper: + autowire: true + autoconfigure: true + Chill\MainBundle\Form\RegroupmentType: autowire: true autoconfigure: true From 1a44a516c26055ef9cc03110921525b81dcab810 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 15:36:57 +0100 Subject: [PATCH 09/11] FEATURE [datamapper][regroupment] make the datamapper work --- .../ChillMainBundle/Entity/Regroupment.php | 4 +- .../DataMapper/ExportPickCenterDataMapper.php | 77 +++++++++++++++++++ .../Form/DataMapper/RegroupmentDataMapper.php | 67 ---------------- .../Form/Type/Export/PickCenterType.php | 22 +----- .../Repository/RegroupmentRepository.php | 61 +++++++++++++++ 5 files changed, 144 insertions(+), 87 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php delete mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php create mode 100644 src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php diff --git a/src/Bundle/ChillMainBundle/Entity/Regroupment.php b/src/Bundle/ChillMainBundle/Entity/Regroupment.php index 5faffd17c..5de02ade6 100644 --- a/src/Bundle/ChillMainBundle/Entity/Regroupment.php +++ b/src/Bundle/ChillMainBundle/Entity/Regroupment.php @@ -24,7 +24,7 @@ class Regroupment /** * @var Center * @ORM\ManyToMany( - * targetEntity="Chill\MainBundle\Entity\Center" + * targetEntity=Center::class * ) * @ORM\Id */ @@ -52,7 +52,7 @@ class Regroupment $this->centers = new ArrayCollection(); } - public function getCenters(): ?Collection + public function getCenters(): Collection { return $this->centers; } diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php new file mode 100644 index 000000000..884f8cb78 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php @@ -0,0 +1,77 @@ + $form */ + $form = iterator_to_array($forms); + + $pickedRegroupment = []; + foreach ($this->regroupmentRepository->findAll() as $regroupment) { + [$contained, $notContained] = $regroupment->getCenters()->partition(function (Center $center) { + + }); + + if (0 === count($notContained)) { + $pickedRegroupment[] = $regroupment; + } + } + + $form['regroupment']->setData($pickedRegroupment); + $form['centers']->setData($data); + } + + /** + * @param iterable $forms + * @param array $data + * @return void + */ + public function mapFormsToData($forms, &$data) + { + /** @var array $forms */ + $forms = iterator_to_array($forms); + + $centers = []; + + foreach ($forms['center']->getData() as $center) { + $centers[spl_object_hash($center)] = $center; + } + + foreach ($forms['regroupment']->getData() as $regroupment) { + /** @var Regroupment $regroupment */ + foreach ($regroupment->getCenters() as $center) { + $centers[spl_object_hash($center)] = $center; + } + } + + $data = array_values($centers); + } +} diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php deleted file mode 100644 index 958e4c013..000000000 --- a/src/Bundle/ChillMainBundle/Form/DataMapper/RegroupmentDataMapper.php +++ /dev/null @@ -1,67 +0,0 @@ -regroupment = $regroupment; - } - - public function mapDataToForms($data, $forms) - { - $forms = iterator_to_array($forms); - - if ($this->regroupment instanceof Regroupment) { - $forms['regroupment']->setData($this->regroupment->getCenters()); - - dump($forms['regroupment']); - - return; - } - - if (null === $data) { - return; - } - - if ($data instanceof Regroupment) { - $forms['regroupment']->setData($data); - } - } - - public function mapFormsToData($forms, &$data) - { - $forms = iterator_to_array($forms); - - if (isset($forms['regroupment'])) { - if ($this->regroupment instanceof Regroupment) { - $data = $this->regroupment; - } else { - $data = []; - - foreach ($forms['regroupment']->getData() as $key => $regroupment) - { - dump($regroupment->getCenters()); - $data[$key] = $regroupment->getCenters(); - dump($data); - } -// $data = $forms['regroupment']->getData(); - } - } - } -} diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 1390809a5..674f42aa7 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -15,6 +15,8 @@ use Chill\MainBundle\Center\GroupingCenterInterface; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; +use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper; +use Chill\MainBundle\Form\DataMapper\PickCenterDataMapper; use Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; @@ -93,27 +95,11 @@ class PickCenterType extends AbstractType }, ]); - $builder->setDataMapper(new RegroupmentDataMapper()); + $builder->setDataMapper(new ExportPickCenterDataMapper()); } - + public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('export_alias'); } - - protected function buildChoices($reachablesCenters, GroupingCenterInterface $gc) - { - $result = []; - - foreach ($gc->getGroups() as $group) { - foreach ($gc->getCentersForGroup($group) as $center) { - if (in_array($center, $reachablesCenters, true)) { - $result[$group] = $group; - } - } - } - - return $result; - } - } diff --git a/src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php b/src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php new file mode 100644 index 000000000..6e84ef9b7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Repository/RegroupmentRepository.php @@ -0,0 +1,61 @@ +repository = $entityManager->getRepository(Regroupment::class); + } + + public function find($id, $lockMode = null, $lockVersion = null): ?Regroupment + { + return $this->repository->find($id, $lockMode, $lockVersion); + } + + /** + * @return Regroupment[] + */ + public function findAll(): array + { + return $this->repository->findAll(); + } + + /** + * @param mixed|null $limit + * @param mixed|null $offset + * + * @return Regroupment[] + */ + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria, ?array $orderBy = null): ?Regroupment + { + return $this->repository->findOneBy($criteria, $orderBy); + } + + public function getClassName() + { + return Regroupment::class; + } +} From a4e21b783482449ada97fa361987414f33b8526f Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 15:39:36 +0100 Subject: [PATCH 10/11] FEATURE [datamapper][regroupment] minor fix --- .../DataMapper/ExportPickCenterDataMapper.php | 35 +++++++++++-------- .../Form/Type/Export/PickCenterType.php | 12 ------- .../Form/Type/Export/PickRegroupmentType.php | 10 +++++- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php index 884f8cb78..0d21b0adb 100644 --- a/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/ExportPickCenterDataMapper.php @@ -14,8 +14,10 @@ namespace Chill\MainBundle\Form\DataMapper; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Repository\RegroupmentRepository; +use Exception; use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\FormInterface; +use function count; class ExportPickCenterDataMapper implements DataMapperInterface { @@ -24,34 +26,39 @@ class ExportPickCenterDataMapper implements DataMapperInterface /** * @param array|Center[] $data * @param $forms + * + * @throws Exception + * * @return mixed - * @throws \Exception */ public function mapDataToForms($data, $forms) { - throw new \Exception("not implemented"); + if (null === $data) { + return; + } - /** @var array $form */ - $form = iterator_to_array($forms); + /** @var array $form */ + $form = iterator_to_array($forms); - $pickedRegroupment = []; - foreach ($this->regroupmentRepository->findAll() as $regroupment) { - [$contained, $notContained] = $regroupment->getCenters()->partition(function (Center $center) { + $pickedRegroupment = []; - }); + foreach ($this->regroupmentRepository->findAll() as $regroupment) { + [$contained, $notContained] = $regroupment->getCenters()->partition(static function (Center $center) { + }); - if (0 === count($notContained)) { - $pickedRegroupment[] = $regroupment; - } - } + if (0 === count($notContained)) { + $pickedRegroupment[] = $regroupment; + } + } - $form['regroupment']->setData($pickedRegroupment); - $form['centers']->setData($data); + $form['regroupment']->setData($pickedRegroupment); + $form['centers']->setData($data); } /** * @param iterable $forms * @param array $data + * * @return void */ public function mapFormsToData($forms, &$data) diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 674f42aa7..17768544b 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -16,27 +16,15 @@ use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper; -use Chill\MainBundle\Form\DataMapper\PickCenterDataMapper; -use Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\CallbackTransformer; -use Symfony\Component\Form\DataMapperInterface; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\User\UserInterface; -use function array_intersect; -use function array_key_exists; -use function array_merge; -use function array_unique; use function count; -use function in_array; /** * Pick centers amongst available centers for the user. diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php index e60388fd3..7b04d2ebd 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php @@ -1,10 +1,18 @@ Date: Wed, 1 Mar 2023 17:10:59 +0100 Subject: [PATCH 11/11] Fixes for feature "Regroupment of center" * allow more than 15 character in regroupment name * remove unused methods in PickCenterType * show only active Regroupment in form * remove dead code and commented code --- .../ChillMainBundle/Entity/Regroupment.php | 2 +- .../Form/Type/Export/PickCenterType.php | 21 ++++++++------- .../Form/Type/Export/PickRegroupmentType.php | 18 ------------- .../Repository/RegroupmentRepository.php | 5 ++++ .../views/Export/new_centers_step.html.twig | 10 ------- .../migrations/Version20230301155213.php | 26 +++++++++++++++++++ 6 files changed, 43 insertions(+), 39 deletions(-) delete mode 100644 src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20230301155213.php diff --git a/src/Bundle/ChillMainBundle/Entity/Regroupment.php b/src/Bundle/ChillMainBundle/Entity/Regroupment.php index 5de02ade6..96953abf5 100644 --- a/src/Bundle/ChillMainBundle/Entity/Regroupment.php +++ b/src/Bundle/ChillMainBundle/Entity/Regroupment.php @@ -43,7 +43,7 @@ class Regroupment private bool $isActive = true; /** - * @ORM\Column(type="string", length=15, options={"default": ""}, nullable=false) + * @ORM\Column(type="text", options={"default": ""}, nullable=false) */ private string $name = ''; diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php index 17768544b..55157b1ab 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Export/PickCenterType.php @@ -16,6 +16,7 @@ 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\AuthorizationHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -29,29 +30,28 @@ use function count; /** * Pick centers amongst available centers for the user. */ -class PickCenterType extends AbstractType +final class PickCenterType extends AbstractType { public const CENTERS_IDENTIFIERS = 'c'; - protected AuthorizationHelperInterface $authorizationHelper; + private AuthorizationHelperInterface $authorizationHelper; - protected ExportManager $exportManager; + private ExportManager $exportManager; - protected UserInterface $user; + private RegroupmentRepository $regroupmentRepository; + + private UserInterface $user; public function __construct( TokenStorageInterface $tokenStorage, ExportManager $exportManager, + RegroupmentRepository $regroupmentRepository, AuthorizationHelperInterface $authorizationHelper ) { $this->exportManager = $exportManager; $this->user = $tokenStorage->getToken()->getUser(); $this->authorizationHelper = $authorizationHelper; - } - - public function addGroupingCenter(GroupingCenterInterface $grouping) - { - $this->groupingCenters[md5($grouping->getName())] = $grouping; + $this->regroupmentRepository = $regroupmentRepository; } public function buildForm(FormBuilderInterface $builder, array $options) @@ -71,13 +71,14 @@ class PickCenterType extends AbstractType 'choice_label' => static function (Center $c) { return $c->getName(); }, - 'data' => count($this->groupingCenters) > 0 ? null : $centers, + 'data' => $centers, ]) ->add('regroupment', EntityType::class, [ 'class' => Regroupment::class, 'label' => 'regroupment', 'multiple' => true, 'expanded' => true, + 'choices' => $this->regroupmentRepository->findAllActive(), 'choice_label' => static function (Regroupment $r) { return $r->getName(); }, diff --git a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php b/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php deleted file mode 100644 index 7b04d2ebd..000000000 --- a/src/Bundle/ChillMainBundle/Form/Type/Export/PickRegroupmentType.php +++ /dev/null @@ -1,18 +0,0 @@ -repository->findAll(); } + public function findAllActive(): array + { + return $this->repository->findBy(['isActive' => true], ['name' => 'ASC']); + } + /** * @param mixed|null $limit * @param mixed|null $offset diff --git a/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig index a73913154..e08ec84d9 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Export/new_centers_step.html.twig @@ -44,16 +44,6 @@

{{ 'Pick aggregated centers'|trans }}

{{ form_widget(form.centers.regroupment) }} -{# {% if form.centers.children.g is defined %}#} -{##} -{#

{{ 'Pick aggregated centers'|trans }}

#} -{##} -{# {% for f in form.centers.children.g.children %}#} -{# {{ form_row(f) }}#} -{# {% endfor %}#} -{##} -{# {% endif %}#} -

{{ form_widget(form.submit, { 'attr' : { 'class' : 'btn btn-action btn-create' }, 'label' : 'Go to export options' } ) }}

diff --git a/src/Bundle/ChillMainBundle/migrations/Version20230301155213.php b/src/Bundle/ChillMainBundle/migrations/Version20230301155213.php new file mode 100644 index 000000000..6d93b71d9 --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20230301155213.php @@ -0,0 +1,26 @@ +addSql('ALTER TABLE regroupment ALTER name TYPE TEXT'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE regroupment ALTER name TYPE VARCHAR(15)'); + } +}