Skip to content

Commit

Permalink
Add tag page
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 6, 2023
1 parent 5d6a53b commit fe1aeb8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/Controller/TagController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Controller;

use App\Repository\BookRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/tags')]
class TagController extends AbstractController
{
#[Route('/{page}', name: 'app_tags', requirements: ['page' => '\d+'])]
public function index(BookRepository $bookRepository, PaginatorInterface $paginator, int $page = 1): Response
{
$tags = $bookRepository->getAllTags();

$pagination = $paginator->paginate($tags, $page, 18);

return $this->render('group/index.html.twig', [
'pagination' => $pagination,
'page' => $page,
'type' => 'tags',
]);
}

#[Route('/{slug}/{page}', name: 'app_tags_detail', requirements: ['page' => '\d+'])]
public function detail(string $slug, BookRepository $bookRepository, PaginatorInterface $paginator, int $page = 1): Response
{
$tags = $bookRepository->getAllTags();

$slug = urldecode($slug);
dump($slug);
$tag = $tags[$slug] ?? null;

if (null === $tag) {
return $this->redirectToRoute('app_tags');
}

$pagination = $paginator->paginate(
$bookRepository->getByTagQuery($tag['item']),
$page,
18
);

return $this->render('author/detail.html.twig', [
'pagination' => $pagination,
'author' => $tag,
]);
}
}
2 changes: 1 addition & 1 deletion src/EventSubscriber/DisplayModeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DisplayModeSubscriber implements EventSubscriberInterface
{
private string $displayMode;
private string $displayMode='gallery';

public function onKernelRequest(RequestEvent $event): void
{
Expand Down
1 change: 1 addition & 0 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function createMainMenu(array $options): ItemInterface
$menu->addChild('Not read', ['route' => 'app_read', 'routeParameters' => ['read' => 0], ...$this->defaultAttr])->setExtra('icon', 'journal');
$menu->addChild('Series', ['route' => 'app_serie', ...$this->defaultAttr])->setExtra('icon', 'list');
$menu->addChild('Authors', ['route' => 'app_authors', ...$this->defaultAttr])->setExtra('icon', 'people-fill');
$menu->addChild('Tags', ['route' => 'app_tags', ...$this->defaultAttr])->setExtra('icon', 'tags-fill');
$menu->addChild('Unverified', ['route' => 'app_unverified', ...$this->defaultAttr])->setExtra('icon', 'question-circle-fill');
$menu->addChild('setting_divider', ['label' => 'Others'])->setExtra('divider', true);
$menu->addChild('Settings', ['route' => 'admin', ...$this->defaultAttr])->setExtra('icon', 'gear-fill');
Expand Down
9 changes: 9 additions & 0 deletions src/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public function getByAuthorQuery(string $author): Query
->getQuery();
}

public function getByTagQuery(string $tag): Query
{
return $this->createQueryBuilder('b')
->select('b')
->where('JSON_CONTAINS(b.tags, :tag)=1')
->setParameter('tag', json_encode([$tag]))
->getQuery();
}

public function getBySerieQuery(string $serieSlug): Query
{
return $this->createQueryBuilder('b')
Expand Down
2 changes: 1 addition & 1 deletion templates/group/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ul class="list-group my-2">
{% for item in pagination %}
<li class="list-group-item">
<a href="{{ path('app_'~type~'_detail',{slug:item.slug|default(item.item)}) }}">
<a href="{{ path('app_'~type~'_detail',{slug:item.slug|default(item.item)|url_encode}) }}">
{{ item.item }}
</a>
</li>
Expand Down

0 comments on commit fe1aeb8

Please sign in to comment.