issue641: same for actions: take children, propose only associated and sort them by ordering

This commit is contained in:
Mathieu Jaumotte 2022-10-16 20:06:18 +02:00
parent 6b1155b9d8
commit 71f989f00e
3 changed files with 77 additions and 7 deletions

View File

@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Form\Type\Select2SocialActionType;
use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Chill\PersonBundle\Templating\Entity\SocialActionRender;
@ -52,7 +53,9 @@ class SocialActionFilter implements FilterInterface
$clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions'); $clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions');
$qb ->andWhere($clause) $qb ->andWhere($clause)
->setParameter('socialactions', $data['accepted_socialactions']); ->setParameter('socialactions',
$this->addDescendantsActions($data['accepted_socialactions'])
);
} }
public function applyOn(): string public function applyOn(): string
@ -69,14 +72,16 @@ class SocialActionFilter implements FilterInterface
public function describeAction($data, $format = 'string'): array public function describeAction($data, $format = 'string'): array
{ {
$socialactions = []; $actions = [];
foreach ($data['accepted_socialactions'] as $sa) { $socialactions = $data['accepted_socialactions'];
$socialactions[] = $this->actionRender->renderString($sa, []);
foreach ($socialactions as $action) {
$actions[] = $this->renderAction($action);
} }
return ['Filtered by socialactions: only %socialactions%', [ return ['Filtered by socialactions: only %socialactions%', [
'%socialactions%' => implode(', ou ', $socialactions), '%socialactions%' => implode(', ou ', $actions),
]]; ]];
} }
@ -84,4 +89,45 @@ class SocialActionFilter implements FilterInterface
{ {
return 'Filter by socialaction'; return 'Filter by socialaction';
} }
private function addDescendantsActions($accepted_socialactions): array
{
$array = [];
foreach ($accepted_socialactions as $action) {
/** @var SocialAction $action */
$array[] = $action;
if (!$action->hasParent()) {
$array = array_merge($array, $action->getDescendants()->toArray());
}
}
return $this->removeDuplicate($array);
}
private function removeDuplicate(array $array): array
{
$ids = array_map(static function ($item) {
return $item->getId();
}, $array);
$unique_ids = array_unique($ids);
return array_values(
array_intersect_key($array, $unique_ids)
);
}
private function renderAction(SocialAction $action): string
{
$render_str = $this->actionRender->renderString($action, []);
if (!$action->hasParent()) {
if (count($action->getDescendants()) > 0) {
return $render_str . " (et dérivés)";
}
}
return $render_str;
}
} }

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Form\Type; namespace Chill\PersonBundle\Form\Type;
use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository;
use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -21,9 +22,14 @@ class Select2SocialActionType extends AbstractType
{ {
private SocialActionRender $actionRender; private SocialActionRender $actionRender;
public function __construct(SocialActionRender $actionRender) private SocialActionRepository $actionRepository;
{
public function __construct(
SocialActionRender $actionRender,
SocialActionRepository $actionRepository
) {
$this->actionRender = $actionRender; $this->actionRender = $actionRender;
$this->actionRepository = $actionRepository;
} }
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
@ -31,6 +37,7 @@ class Select2SocialActionType extends AbstractType
$resolver $resolver
->setDefaults([ ->setDefaults([
'class' => SocialAction::class, 'class' => SocialAction::class,
'choices' => $this->actionRepository->findActionsNotDesactivated(),
'choice_label' => function (SocialAction $sa) { 'choice_label' => function (SocialAction $sa) {
return $this->actionRender->renderString($sa, []); return $this->actionRender->renderString($sa, []);
}, },

View File

@ -67,4 +67,21 @@ final class SocialActionRepository implements ObjectRepository
{ {
return SocialAction::class; return SocialAction::class;
} }
public function findActionsNotDesactivated(): array
{
return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult();
}
private function buildQueryWithDesactivatedDateCriteria(): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('sa');
$qb ->where('sa.desactivationDate is null')
->orWhere('sa.desactivationDate > :now')
->orderBy('sa.ordering', 'ASC')
->setParameter('now', new \DateTime('now'));
return $qb;
}
} }