From 068311d071330e788ca1c4b9b342b62b13b304b6 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 15:36:57 +0100 Subject: [PATCH] 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; + } +}