3 namespace Wallabag\CoreBundle\Controller
;
5 use Pagerfanta\Adapter\ArrayAdapter
;
6 use Pagerfanta\Exception\OutOfRangeCurrentPageException
;
7 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
8 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
9 use Symfony\Component\HttpFoundation\Request
;
10 use Wallabag\CoreBundle\Entity\Entry
;
11 use Wallabag\CoreBundle\Entity\Tag
;
12 use Wallabag\CoreBundle\Event\EntryTaggedEvent
;
13 use Wallabag\CoreBundle\Form\Type\NewTagType
;
14 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
16 class TagController
extends Controller
19 * @param Request $request
22 * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
24 * @return \Symfony\Component\HttpFoundation\Response
26 public function addTagFormAction(Request
$request, Entry
$entry)
28 $form = $this->createForm(NewTagType
::class, new Tag());
29 $form->handleRequest($request);
31 if ($form->isSubmitted() && $form->isValid()) {
32 $tags = $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
34 $form->get('label')->getData()
37 $em = $this->getDoctrine()->getManager();
41 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent
::NAME
, new EntryTaggedEvent($entry, $tags));
43 $this->get('session')->getFlashBag()->add(
45 'flashes.tag.notice.tag_added'
48 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
51 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
52 'form' => $form->createView(),
58 * Removes tag from entry.
60 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
62 * @return \Symfony\Component\HttpFoundation\Response
64 public function removeTagFromEntry(Request
$request, Entry
$entry, Tag
$tag)
66 $entry->removeTag($tag);
67 $em = $this->getDoctrine()->getManager();
70 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent
::NAME
, new EntryTaggedEvent($entry, $tag));
72 // remove orphan tag in case no entries are associated to it
73 if (count($tag->getEntries()) === 0) {
78 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
80 return $this->redirect($redirectUrl);
84 * Shows tags for current user.
86 * @Route("/tag/list", name="tag")
88 * @return \Symfony\Component\HttpFoundation\Response
90 public function showTagAction()
92 $tags = $this->getDoctrine()
93 ->getRepository('WallabagCoreBundle:Tag')
94 ->findAllTags($this->getUser()->getId());
98 foreach ($tags as $tag) {
99 $nbEntries = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag->getId());
104 'id' => $tag->getId(),
105 'label' => $tag->getLabel(),
106 'slug' => $tag->getSlug(),
107 'nbEntries' => $nbEntries,
111 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
120 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
121 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
123 * @return \Symfony\Component\HttpFoundation\Response
125 public function showEntriesForTagAction(Tag
$tag, $page, Request
$request)
127 $entriesByTag = $this->getDoctrine()
128 ->getRepository('WallabagCoreBundle:Entry')
129 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
131 $pagerAdapter = new ArrayAdapter($entriesByTag);
133 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
134 ->prepare($pagerAdapter, $page);
137 $entries->setCurrentPage($page);
138 } catch (OutOfRangeCurrentPageException
$e) {
140 return $this->redirect($this->generateUrl($request->get('_route'), [
141 'slug' => $tag->getSlug(),
142 'page' => $entries->getNbPages(),
147 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
149 'entries' => $entries,
150 'currentPage' => $page,
151 'tag' => $tag->getSlug(),