Implement tag filtering
This commit is contained in:
@ -15,6 +15,7 @@ readonly class SnipFilterRequest implements CachableDtoInterface
|
||||
public function __construct(
|
||||
public ?string $visibility = self::VISIBILITY_VISIBLE,
|
||||
public ?string $sort = self::SORT_DATE,
|
||||
public ?string $tag = null,
|
||||
) {}
|
||||
|
||||
public function toArray(): array
|
||||
@ -22,6 +23,7 @@ readonly class SnipFilterRequest implements CachableDtoInterface
|
||||
return [
|
||||
'visibility' => $this->visibility,
|
||||
'sort' => $this->sort,
|
||||
'tag' => $this->tag,
|
||||
];
|
||||
}
|
||||
}
|
@ -79,6 +79,13 @@ class SnipRepository extends ServiceEntityRepository
|
||||
throw new \InvalidArgumentException('Invalid sort option: ', $request->sort);
|
||||
}
|
||||
|
||||
if ($request->tag) {
|
||||
$qb->innerJoin('s.tags', 't')
|
||||
->andWhere('t.name = :tag')
|
||||
->setParameter('tag', $request->tag)
|
||||
;
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace App\Repository;
|
||||
use App\Entity\Tag;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Tag>
|
||||
@ -22,28 +23,8 @@ class TagRepository extends ServiceEntityRepository
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Tag[] Returns an array of Tag objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('t')
|
||||
// ->andWhere('t.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('t.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Tag
|
||||
// {
|
||||
// return $this->createQueryBuilder('t')
|
||||
// ->andWhere('t.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
public function findAllByUser(UserInterface $user): array
|
||||
{
|
||||
return $this->findBy(['user' => $user]);
|
||||
}
|
||||
}
|
||||
|
35
src/Twig/Extension/SnipFilterExtension.php
Normal file
35
src/Twig/Extension/SnipFilterExtension.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Twig\Extension;
|
||||
|
||||
use App\Repository\TagRepository;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class SnipFilterExtension extends AbstractExtension
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TagRepository $tagRepository,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('snipSortOptions', fn() => ['name', 'date']),
|
||||
new TwigFunction('snipFilterOptions', fn() => ['all', 'visible', 'hidden', 'archived']),
|
||||
new TwigFunction('snipTagOptions', fn() => $this->getSnipTagOptions()),
|
||||
];
|
||||
}
|
||||
|
||||
private function getSnipTagOptions(): array
|
||||
{
|
||||
$tags[null] = 'All tags';
|
||||
foreach ($this->tagRepository->findAllByUser($this->security->getUser()) as $tag) {
|
||||
$tags[(string)$tag] = (string)$tag;
|
||||
}
|
||||
return $tags;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user