aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Controller/TagController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/TagController.php')
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php71
1 files changed, 63 insertions, 8 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index 8645fb44..5acc6852 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -2,12 +2,15 @@
2 2
3namespace Wallabag\CoreBundle\Controller; 3namespace Wallabag\CoreBundle\Controller;
4 4
5use Pagerfanta\Adapter\ArrayAdapter;
6use Pagerfanta\Exception\OutOfRangeCurrentPageException;
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 7use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller; 8use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
8use Wallabag\CoreBundle\Entity\Entry; 10use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag; 11use Wallabag\CoreBundle\Entity\Tag;
10use Wallabag\CoreBundle\Form\Type\NewTagType; 12use Wallabag\CoreBundle\Form\Type\NewTagType;
13use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
11 14
12class TagController extends Controller 15class TagController extends Controller
13{ 16{
@@ -60,10 +63,12 @@ class TagController extends Controller
60 $entry->removeTag($tag); 63 $entry->removeTag($tag);
61 $em = $this->getDoctrine()->getManager(); 64 $em = $this->getDoctrine()->getManager();
62 $em->flush(); 65 $em->flush();
63 if (count($tag->getEntries()) == 0) { 66
67 // remove orphan tag in case no entries are associated to it
68 if (count($tag->getEntries()) === 0) {
64 $em->remove($tag); 69 $em->remove($tag);
70 $em->flush();
65 } 71 }
66 $em->flush();
67 72
68 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); 73 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
69 74
@@ -83,11 +88,61 @@ class TagController extends Controller
83 ->getRepository('WallabagCoreBundle:Tag') 88 ->getRepository('WallabagCoreBundle:Tag')
84 ->findAllTags($this->getUser()->getId()); 89 ->findAllTags($this->getUser()->getId());
85 90
86 return $this->render( 91 $flatTags = [];
87 'WallabagCoreBundle:Tag:tags.html.twig', 92
88 [ 93 foreach ($tags as $key => $tag) {
89 'tags' => $tags, 94 $nbEntries = $this->getDoctrine()
90 ] 95 ->getRepository('WallabagCoreBundle:Entry')
91 ); 96 ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag['id']);
97
98 $flatTags[] = [
99 'id' => $tag['id'],
100 'label' => $tag['label'],
101 'slug' => $tag['slug'],
102 'nbEntries' => $nbEntries,
103 ];
104 }
105
106 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
107 'tags' => $flatTags,
108 ]);
109 }
110
111 /**
112 * @param Tag $tag
113 * @param int $page
114 *
115 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
116 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
117 *
118 * @return \Symfony\Component\HttpFoundation\Response
119 */
120 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
121 {
122 $entriesByTag = $this->getDoctrine()
123 ->getRepository('WallabagCoreBundle:Entry')
124 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
125
126 $pagerAdapter = new ArrayAdapter($entriesByTag);
127
128 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
129 ->prepare($pagerAdapter, $page);
130
131 try {
132 $entries->setCurrentPage($page);
133 } catch (OutOfRangeCurrentPageException $e) {
134 if ($page > 1) {
135 return $this->redirect($this->generateUrl($request->get('_route'), [
136 'slug' => $tag->getSlug(),
137 'page' => $entries->getNbPages(),
138 ]), 302);
139 }
140 }
141
142 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
143 'form' => null,
144 'entries' => $entries,
145 'currentPage' => $page,
146 ]);
92 } 147 }
93} 148}