mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-22 23:53:50 +00:00
FEATURE [datamapper][regroupment] make the datamapper work
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user