From fb9b9b922606afcfed058a42c7ef1c4fdc562fd1 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Feb 2023 10:23:19 +0100 Subject: [PATCH] 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