FEATURE [datamapper][regroupment] moved datamapper to seperate class. Still not working

This commit is contained in:
Julie Lenaerts 2023-02-07 10:23:19 +01:00
parent 0ace1c1f6a
commit fb9b9b9226
3 changed files with 76 additions and 116 deletions

View File

@ -0,0 +1,67 @@
<?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;
class RegroupmentDataMapper implements DataMapperInterface
{
private $regroupment;
public function __construct(?Regroupment $regroupment = null)
{
$this->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();
}
}
}
}

View File

@ -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;
}*/
}

View File

@ -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