mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
review: fix stuffs on socialAction and socialIssue filters:
* filters call static entity method * add renderString option to manage add_children * add tests on new entity method getDescendantsWithThisFor..() * rename function in repository * rename 'Select2..' classes by 'Pick..' and replace all occurences
This commit is contained in:
@@ -12,7 +12,7 @@ declare(strict_types=1);
|
||||
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Chill\MainBundle\Form\Type\Select2UserLocationType;
|
||||
use Chill\MainBundle\Form\Type\PickUserLocationType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
@@ -48,7 +48,7 @@ class AdministrativeLocationFilter implements FilterInterface
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('accepted_locations', Select2UserLocationType::class, [
|
||||
$builder->add('accepted_locations', PickUserLocationType::class, [
|
||||
'label' => 'Accepted locations',
|
||||
'label_attr' => [
|
||||
//'class' => 'd-none'
|
||||
|
@@ -15,7 +15,7 @@ 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\Form\Type\PickSocialActionType;
|
||||
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
@@ -23,10 +23,10 @@ use function in_array;
|
||||
|
||||
class SocialActionFilter implements FilterInterface
|
||||
{
|
||||
private SocialActionRender $actionRender;
|
||||
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
private SocialActionRender $actionRender;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
SocialActionRender $actionRender
|
||||
@@ -52,9 +52,9 @@ class SocialActionFilter implements FilterInterface
|
||||
|
||||
$clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions');
|
||||
|
||||
$qb ->andWhere($clause)
|
||||
$qb->andWhere($clause)
|
||||
->setParameter('socialactions',
|
||||
$this->addDescendantsActions($data['accepted_socialactions'])
|
||||
SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions'])->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ class SocialActionFilter implements FilterInterface
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('accepted_socialactions', Select2SocialActionType::class, [
|
||||
$builder->add('accepted_socialactions', PickSocialActionType::class, [
|
||||
'multiple' => true,
|
||||
]);
|
||||
}
|
||||
@@ -77,7 +77,9 @@ class SocialActionFilter implements FilterInterface
|
||||
$socialactions = $data['accepted_socialactions'];
|
||||
|
||||
foreach ($socialactions as $action) {
|
||||
$actions[] = $this->renderAction($action);
|
||||
$actions[] = $this->actionRender->renderString($action, [
|
||||
'show_and_children' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
return ['Filtered by socialactions: only %socialactions%', [
|
||||
@@ -89,45 +91,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ use Chill\MainBundle\Export\FilterInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
use Chill\PersonBundle\Form\Type\Select2SocialIssueType;
|
||||
use Chill\PersonBundle\Form\Type\PickSocialIssueType;
|
||||
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
@@ -56,9 +56,9 @@ class SocialIssueFilter implements FilterInterface
|
||||
|
||||
$clause = $qb->expr()->in('acpsocialissue.id', ':socialissues');
|
||||
|
||||
$qb ->andWhere($clause)
|
||||
$qb->andWhere($clause)
|
||||
->setParameter('socialissues',
|
||||
$this->addDescendantsIssues($data['accepted_socialissues'])
|
||||
SocialIssue::getDescendantsWithThisForIssues($data['accepted_socialissues'])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ class SocialIssueFilter implements FilterInterface
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('accepted_socialissues', Select2SocialIssueType::class, [
|
||||
$builder->add('accepted_socialissues', PickSocialIssueType::class, [
|
||||
'multiple' => true,
|
||||
]);
|
||||
}
|
||||
@@ -80,14 +80,10 @@ class SocialIssueFilter implements FilterInterface
|
||||
|
||||
$socialissues = $data['accepted_socialissues'];
|
||||
|
||||
foreach ($socialissues as $si) {
|
||||
/** @var SocialIssue $si */
|
||||
if (null === $si) {
|
||||
$issues[] = $this->translator->trans('Not given');
|
||||
} else {
|
||||
$issues[] = $this->renderIssue($si);
|
||||
|
||||
}
|
||||
foreach ($socialissues as $issue) {
|
||||
$issues[] = $this->socialIssueRender->renderString($issue, [
|
||||
'show_and_children' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
@@ -100,45 +96,4 @@ class SocialIssueFilter implements FilterInterface
|
||||
{
|
||||
return 'Filter by social issue';
|
||||
}
|
||||
|
||||
private function addDescendantsIssues($accepted_socialissues): array
|
||||
{
|
||||
$array = [];
|
||||
|
||||
foreach ($accepted_socialissues as $si) {
|
||||
/** @var SocialIssue $si */
|
||||
$array[] = $si;
|
||||
if (!$si->hasParent()) {
|
||||
$array = array_merge($array, $si->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 renderIssue(SocialIssue $si): string
|
||||
{
|
||||
$render_str = $this->socialIssueRender->renderString($si, []);
|
||||
|
||||
if (!$si->hasParent()) {
|
||||
if (count($si->getDescendants()) > 0) {
|
||||
return $render_str . " (et dérivés)";
|
||||
}
|
||||
}
|
||||
|
||||
return $render_str;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user