From 891456ba9a592a200d8b23029e8f4514d9803080 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 14 Apr 2016 15:03:22 +0200 Subject: [PATCH] Links on each tag in Tags view --- .../CoreBundle/Controller/EntryController.php | 6 +-- .../Controller/ExportController.php | 2 +- .../CoreBundle/Controller/TagController.php | 40 +++++++++++++++++++ .../Helper/PreparePagerForEntries.php | 34 ++++++++++++++++ .../CoreBundle/Resources/config/services.yml | 6 +++ .../themes/material/Entry/entries.html.twig | 4 +- .../views/themes/material/Tag/tags.html.twig | 2 +- 7 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index ccdf9406..93db0d6c 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -4,7 +4,6 @@ namespace Wallabag\CoreBundle\Controller; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Exception\OutOfRangeCurrentPageException; -use Pagerfanta\Pagerfanta; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; @@ -257,9 +256,10 @@ class EntryController extends Controller } $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); + $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') + ->prepare($pagerAdapter, $page); + try { $entries->setCurrentPage($page); } catch (OutOfRangeCurrentPageException $e) { diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php index 944c755d..959b308d 100644 --- a/src/Wallabag/CoreBundle/Controller/ExportController.php +++ b/src/Wallabag/CoreBundle/Controller/ExportController.php @@ -46,7 +46,7 @@ class ExportController extends Controller * * @Route("/export/{category}.{format}", name="export_entries", requirements={ * "format": "epub|mobi|pdf|json|xml|txt|csv", - * "category": "all|unread|starred|archive" + * "category": "all|unread|starred|archive|tag_entries" * }) * * @return \Symfony\Component\HttpFoundation\Response diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index 8645fb44..b6514ea6 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -2,12 +2,15 @@ namespace Wallabag\CoreBundle\Controller; +use Pagerfanta\Adapter\ArrayAdapter; +use Pagerfanta\Exception\OutOfRangeCurrentPageException; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Form\Type\NewTagType; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; class TagController extends Controller { @@ -90,4 +93,41 @@ class TagController extends Controller ] ); } + + /** + * @param Tag $tag + * @param int $page + * + * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"}) + * @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showEntriesForTagAction(Tag $tag, $page, Request $request) + { + $pagerAdapter = new ArrayAdapter($tag->getEntries()->toArray()); + + $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') + ->prepare($pagerAdapter, $page); + + try { + $entries->setCurrentPage($page); + } catch (OutOfRangeCurrentPageException $e) { + if ($page > 1) { + return $this->redirect($this->generateUrl($request->get('_route'), [ + 'slug' => $tag->getSlug(), + 'page' => $entries->getNbPages(), + ]), 302); + } + } + + return $this->render( + 'WallabagCoreBundle:Entry:entries.html.twig', + [ + 'form' => null, + 'entries' => $entries, + 'currentPage' => $page, + ] + ); + } } diff --git a/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php b/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php new file mode 100644 index 00000000..f9066bee --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php @@ -0,0 +1,34 @@ +user = $token->getToken()->getUser(); + $this->router = $router; + } + + /** + * @param AdapterInterface $adapter + * @param int $page + * + * @return null|Pagerfanta + */ + public function prepare(AdapterInterface $adapter, $page = 1) + { + $entries = new Pagerfanta($adapter); + $entries->setMaxPerPage($this->user->getConfig()->getItemsPerPage()); + + return $entries; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index f8835198..e95ef452 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -119,3 +119,9 @@ services: class: Wallabag\CoreBundle\Helper\Redirect arguments: - "@router" + + wallabag_core.helper.prepare_pager_for_entries: + class: Wallabag\CoreBundle\Helper\PreparePagerForEntries + arguments: + - "@security.token_storage" + - "@router" diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig index eca8924e..a0a0b02f 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig @@ -122,6 +122,7 @@ + {% if form is not null %}
@@ -205,5 +206,6 @@
- {% include "WallabagCoreBundle:Entry:pager.html.twig" with {'entries': entries} %} + {% endif %} + {% endblock %} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig index d958c4b8..9495f543 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig @@ -9,7 +9,7 @@
{% endblock %} -- 2.41.0