X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FController%2FTagController.php;h=a3e70fd0aeabf2661d15560b1e40142836fd2357;hb=5a619812ca3eb05a82a023ccdaee13501eb8d45f;hp=e8e9ecbee0893f2620e3191ff9ea562e67b93a85;hpb=f2e5fdc3666a2a6525b4202ab48df05efeebaf5c;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index e8e9ecbe..a3e70fd0 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 { @@ -60,12 +63,16 @@ class TagController extends Controller $entry->removeTag($tag); $em = $this->getDoctrine()->getManager(); $em->flush(); - if (count($tag->getEntries()) == 0) { + + // remove orphan tag in case no entries are associated to it + if (count($tag->getEntries()) === 0) { $em->remove($tag); + $em->flush(); } - $em->flush(); - return $this->redirect($request->headers->get('referer')); + $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); + + return $this->redirect($redirectUrl); } /** @@ -79,13 +86,64 @@ class TagController extends Controller { $tags = $this->getDoctrine() ->getRepository('WallabagCoreBundle:Tag') - ->findTags($this->getUser()->getId()); - - return $this->render( - 'WallabagCoreBundle:Tag:tags.html.twig', - [ - 'tags' => $tags, - ] - ); + ->findAllTags($this->getUser()->getId()); + + $flatTags = []; + + foreach ($tags as $tag) { + $nbEntries = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag->getId()); + + $flatTags[] = [ + 'id' => $tag->getId(), + 'label' => $tag->getLabel(), + 'slug' => $tag->getSlug(), + 'nbEntries' => $nbEntries, + ]; + } + + return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [ + 'tags' => $flatTags, + ]); + } + + /** + * @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) + { + $entriesByTag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->findAllByTagId($this->getUser()->getId(), $tag->getId()); + + $pagerAdapter = new ArrayAdapter($entriesByTag); + + $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, + 'tag' => $tag->getLabel(), + ]); } }