murph-skeleton/core/Repository/Site/Page/PageRepositoryQuery.php

78 lines
1.9 KiB
PHP
Raw Normal View History

2021-03-24 12:27:07 +01:00
<?php
namespace App\Core\Repository\Site\Page;
2021-05-12 11:56:48 +02:00
use App\Core\Entity\Site\Navigation;
2021-03-24 12:27:07 +01:00
use App\Core\Repository\RepositoryQuery;
use Knp\Component\Pager\PaginatorInterface;
/**
* class PageRepositoryQuery.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class PageRepositoryQuery extends RepositoryQuery
{
public function __construct(PageRepository $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, 'p', $paginator);
2021-10-06 14:41:59 +02:00
$this->forcedFilterHandlers[] = 'isAssociated';
2021-03-24 12:27:07 +01:00
}
2021-10-06 14:41:59 +02:00
public function filterByNavigation(Navigation $navigation): self
2021-04-30 12:20:30 +02:00
{
return $this
->leftJoin('.nodes', 'node')
->leftJoin('node.menu', 'menu')
->leftJoin('menu.navigation', 'navigation')
->where('navigation.id = :navigationId')
2021-05-12 11:56:48 +02:00
->setParameter(':navigationId', $navigation->getId())
;
2021-04-30 12:20:30 +02:00
}
2021-10-06 14:41:59 +02:00
public function filterById($id): self
2021-03-24 12:27:07 +01:00
{
$this
->where('.id = :id')
->setParameter(':id', $id)
;
return $this;
}
2021-05-12 11:56:48 +02:00
2021-10-06 14:41:59 +02:00
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;
}
2021-05-12 11:56:48 +02:00
protected function filterHandler(string $name, $value)
{
if ('navigation' === $name) {
return $this->filterByNavigation($value);
}
2021-10-06 14:41:59 +02:00
if ('isAssociated' === $name && $value > -1) {
$this->withAssociation((bool) $value);
}
2021-05-12 11:56:48 +02:00
return parent::filterHandler($name, $value);
}
2021-03-24 12:27:07 +01:00
}