FEATURE [datamapper][regroupment] make the datamapper work

This commit is contained in:
Julie Lenaerts 2023-02-07 15:36:57 +01:00
parent 5756a37178
commit 068311d071
5 changed files with 144 additions and 87 deletions

View File

@ -24,7 +24,7 @@ class Regroupment
/** /**
* @var Center * @var Center
* @ORM\ManyToMany( * @ORM\ManyToMany(
* targetEntity="Chill\MainBundle\Entity\Center" * targetEntity=Center::class
* ) * )
* @ORM\Id * @ORM\Id
*/ */
@ -52,7 +52,7 @@ class Regroupment
$this->centers = new ArrayCollection(); $this->centers = new ArrayCollection();
} }
public function getCenters(): ?Collection public function getCenters(): Collection
{ {
return $this->centers; return $this->centers;
} }

View File

@ -0,0 +1,77 @@
<?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\Center;
use Chill\MainBundle\Entity\Regroupment;
use Chill\MainBundle\Repository\RegroupmentRepository;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\FormInterface;
class ExportPickCenterDataMapper implements DataMapperInterface
{
protected RegroupmentRepository $regroupmentRepository;
/**
* @param array|Center[] $data
* @param $forms
* @return mixed
* @throws \Exception
*/
public function mapDataToForms($data, $forms)
{
throw new \Exception("not implemented");
/** @var array<string, FormInterface> $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<string, FormInterface> $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);
}
}

View File

@ -1,67 +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;
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,8 @@ use Chill\MainBundle\Center\GroupingCenterInterface;
use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Entity\Regroupment;
use Chill\MainBundle\Export\ExportManager; 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\Form\DataMapper\RegroupmentDataMapper;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 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) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setRequired('export_alias'); $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;
}
} }

View File

@ -0,0 +1,61 @@
<?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\Repository;
use Chill\MainBundle\Entity\Regroupment;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
final class RegroupmentRepository implements ObjectRepository
{
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->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;
}
}