diff --git a/src/Dto/SnipFilterRequest.php b/src/Dto/SnipFilterRequest.php index e9b8dae..6e73503 100644 --- a/src/Dto/SnipFilterRequest.php +++ b/src/Dto/SnipFilterRequest.php @@ -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, ]; } } \ No newline at end of file diff --git a/src/Repository/SnipRepository.php b/src/Repository/SnipRepository.php index 0326184..72c23d9 100644 --- a/src/Repository/SnipRepository.php +++ b/src/Repository/SnipRepository.php @@ -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(); } diff --git a/src/Repository/TagRepository.php b/src/Repository/TagRepository.php index 5a30f64..fe6bead 100644 --- a/src/Repository/TagRepository.php +++ b/src/Repository/TagRepository.php @@ -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 @@ -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]); + } } diff --git a/src/Twig/Extension/SnipFilterExtension.php b/src/Twig/Extension/SnipFilterExtension.php new file mode 100644 index 0000000..04d1020 --- /dev/null +++ b/src/Twig/Extension/SnipFilterExtension.php @@ -0,0 +1,35 @@ + ['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; + } + +} diff --git a/templates/base/two.column.html.twig b/templates/base/two.column.6-6.html.twig similarity index 100% rename from templates/base/two.column.html.twig rename to templates/base/two.column.6-6.html.twig diff --git a/templates/base/two.column.8-4.html.twig b/templates/base/two.column.8-4.html.twig new file mode 100644 index 0000000..7312b87 --- /dev/null +++ b/templates/base/two.column.8-4.html.twig @@ -0,0 +1,17 @@ +{% extends 'base/container.html.twig' %} + +{% block container %} +
+
+ {% if title is defined %}

{{ title }}

{% endif %} +
+
+
+
+ {% block column1 %}{% endblock %} +
+
+ {% block column2 %}{% endblock %} +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/snip/index.html.twig b/templates/snip/index.html.twig index 2e1e74c..886abc1 100644 --- a/templates/snip/index.html.twig +++ b/templates/snip/index.html.twig @@ -1,4 +1,4 @@ -{% extends 'base/two.column.html.twig' %} +{% extends 'base/two.column.8-4.html.twig' %} {% set title = 'My Snips' %} @@ -15,6 +15,9 @@ {{ include('snip/badge.html.twig', {snip: snip}) }} {{ snip }} + {% for tag in snip.tags %} + {{ tag }} + {% endfor %} {% endfor %} @@ -24,12 +27,12 @@ {% block column2 %}

Filters

-
Ordering
+
Sort by
- {% for sortOption in ['name', 'date'] %} + {% for sortOption in snipSortOptions() %} - Sort by {{ sortOption|capitalize }} + {{ sortOption|capitalize }} {% endfor %}
@@ -37,11 +40,22 @@
Visibility
- {% for visibilityOption in ['all', 'visible', 'hidden', 'archived'] %} + {% for visibilityOption in snipFilterOptions() %} Show {{ visibilityOption|capitalize }} {% endfor %}
+ +
+
Tags
+
+ {% for key,tagOption in snipTagOptions() %} + + {{ tagOption|capitalize }} + + {% endfor %} +
{% endblock %} \ No newline at end of file diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index 4e71f4c..2edb94f 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -37,10 +37,7 @@

{{ include('snip/badge.html.twig', {snip: snip}) }} - {{ snip }} #{{ snip.id }}
- {% for tag in snip.tags %} - {{ tag }} - {% endfor %} + {{ snip }} #{{ snip.id }}

{{ content|raw }} @@ -53,6 +50,9 @@ Created at {{ snip.activeVersion.id.dateTime|date('Y-m-d H:i:s') }} {{ include('user/badge.html.twig', {user: snip.createdBy}) }} + {% for tag in snip.tags %} + {{ tag }} + {% endfor %}

diff --git a/templates/user/profile.html.twig b/templates/user/profile.html.twig index df7dc5c..32e6b73 100644 --- a/templates/user/profile.html.twig +++ b/templates/user/profile.html.twig @@ -1,4 +1,4 @@ -{% extends 'base/two.column.html.twig' %} +{% extends 'base/two.column.6-6.html.twig' %} {% set title = app.user.name %}