add filter in page

This commit is contained in:
Simon Vieille 2021-10-06 14:41:59 +02:00
parent 79bdb31f3d
commit 4a1585334c
3 changed files with 44 additions and 2 deletions

View File

@ -9,6 +9,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class PageFilterType extends AbstractType
{
@ -47,6 +48,19 @@ class PageFilterType extends AbstractType
],
]
);
$builder->add(
'isAssociated',
ChoiceType::class,
[
'label' => 'Associated',
'choices' => [
'Anyway' => -1,
'No' => 0,
'Yes' => 1,
],
]
);
}
public function configureOptions(OptionsResolver $resolver)

View File

@ -16,9 +16,11 @@ class PageRepositoryQuery extends RepositoryQuery
public function __construct(PageRepository $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, 'p', $paginator);
$this->forcedFilterHandlers[] = 'isAssociated';
}
public function filterByNavigation(Navigation $navigation)
public function filterByNavigation(Navigation $navigation): self
{
return $this
->leftJoin('.nodes', 'node')
@ -29,7 +31,7 @@ class PageRepositoryQuery extends RepositoryQuery
;
}
public function filterById($id)
public function filterById($id): self
{
$this
->where('.id = :id')
@ -39,12 +41,37 @@ class PageRepositoryQuery extends RepositoryQuery
return $this;
}
protected function withAssociation(bool $isAssociated): self
{
$entities = $this->create()->find();
$ids = [];
foreach ($entities as $entity) {
if ($isAssociated && !$entity->getNodes()->isEmpty()) {
$ids[] = $entity->getId();
} elseif (!$isAssociated && $entity->getNodes()->isEmpty()) {
$ids[] = $entity->getId();
}
}
$this
->andWhere('.id IN (:ids)')
->setParameter(':ids', $ids)
;
return $this;
}
protected function filterHandler(string $name, $value)
{
if ('navigation' === $name) {
return $this->filterByNavigation($value);
}
if ('isAssociated' === $name && $value > -1) {
$this->withAssociation((bool) $value);
}
return parent::filterHandler($name, $value);
}
}

View File

@ -191,3 +191,4 @@
"Insert": "Insérer"
"Attributes": "Attributs"
"Choose": "Choisir"
"Associated": "Associé(e)"