Implement tag filtering

This commit is contained in:
Tim
2025-05-10 20:44:13 +02:00
parent e2bd1a7c3b
commit b8ae8bb8a7
9 changed files with 90 additions and 34 deletions

View File

@ -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,
];
}
}

View File

@ -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();
}

View File

@ -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]);
}
}

View 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;
}
}