api for grouping centers, select centers as group in "pick centers" step

for exports
This commit is contained in:
2019-01-28 15:21:31 +01:00
parent cf354cbccd
commit 722274964c
8 changed files with 179 additions and 5 deletions

View File

@@ -28,12 +28,14 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Center\GroupingCenterInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\CallbackTransformer;
use Doctrine\Common\Collections\Collection;
/**
* Pick centers amongst available centers for the user
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PickCenterType extends AbstractType
{
@@ -49,6 +51,12 @@ class PickCenterType extends AbstractType
*/
protected $exportManager;
/**
*
* @var GroupingCenterInterface[]
*/
protected $groupingCenters = [];
const CENTERS_IDENTIFIERS = 'c';
/**
@@ -87,10 +95,87 @@ class PickCenterType extends AbstractType
return $qb->where($qb->expr()->in('c.id', $ids));
},
'multiple' => true,
'expanded' => false,
'expanded' => true,
'choice_label' => function(Center $c) { return $c->getName(); },
'data' => $centers
'data' => count($this->groupingCenters) > 0 ? null : $centers
));
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()
]);
}
}
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); }
));
}
public function addGroupingCenter(GroupingCenterInterface $grouping)
{
$this->groupingCenters[md5($grouping->getName())] = $grouping;
}
protected function buildChoices($reachablesCenters, GroupingCenterInterface $gc)
{
$result = [];
foreach ($gc->getGroups() as $group) {
foreach ($gc->getCentersForGroup($group) as $center) {
if (\in_array($center, $reachablesCenters)) {
$result[$group] = $group;
}
}
}
return $result;
}
protected function transform($data, $centers)
{
return $data;
}
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);
}
}