]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Controller/TagController.php
Merge remote-tracking branch 'origin/master' into 2.2
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / TagController.php
index 16d14d7974059aa9fa9d9e2966315cad7f3845ee..a3e70fd0aeabf2661d15560b1e40142836fd2357 100644 (file)
@@ -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,10 +63,12 @@ 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();
 
         $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
 
@@ -81,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(),
+        ]);
     }
 }