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

This commit is contained in:
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\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Form\Type\Select2SocialActionType;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
@@ -52,7 +53,9 @@ class SocialActionFilter implements FilterInterface
$clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions');
$qb ->andWhere($clause)
->setParameter('socialactions', $data['accepted_socialactions']);
->setParameter('socialactions',
$this->addDescendantsActions($data['accepted_socialactions'])
);
}
public function applyOn(): string
@@ -69,14 +72,16 @@ class SocialActionFilter implements FilterInterface
public function describeAction($data, $format = 'string'): array
{
$socialactions = [];
$actions = [];
foreach ($data['accepted_socialactions'] as $sa) {
$socialactions[] = $this->actionRender->renderString($sa, []);
$socialactions = $data['accepted_socialactions'];
foreach ($socialactions as $action) {
$actions[] = $this->renderAction($action);
}
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';
}
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;
}
}