]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
Save changes
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / TagController.php
CommitLineData
3f3fbef1
NL
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
891456ba
NL
5use Pagerfanta\Adapter\ArrayAdapter;
6use Pagerfanta\Exception\OutOfRangeCurrentPageException;
3f3fbef1
NL
7use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7244d6cb 9use Symfony\Component\HttpFoundation\Request;
7244d6cb 10use Wallabag\CoreBundle\Entity\Entry;
619cc453 11use Wallabag\CoreBundle\Entity\Tag;
61351218 12use Wallabag\CoreBundle\Event\EntryTaggedEvent;
619cc453 13use Wallabag\CoreBundle\Form\Type\NewTagType;
891456ba 14use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
3f3fbef1
NL
15
16class TagController extends Controller
17{
7244d6cb
NL
18 /**
19 * @param Request $request
4094ea47 20 * @param Entry $entry
7244d6cb
NL
21 *
22 * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
23 *
24 * @return \Symfony\Component\HttpFoundation\Response
25 */
26 public function addTagFormAction(Request $request, Entry $entry)
27 {
2baca964 28 $form = $this->createForm(NewTagType::class, new Tag());
7244d6cb
NL
29 $form->handleRequest($request);
30
21e7ccef 31 if ($form->isSubmitted() && $form->isValid()) {
61351218 32 $tags = $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
2baca964
JB
33 $entry,
34 $form->get('label')->getData()
35 );
7244d6cb
NL
36
37 $em = $this->getDoctrine()->getManager();
2baca964 38 $em->persist($entry);
7244d6cb
NL
39 $em->flush();
40
61351218
TC
41 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent::NAME, new EntryTaggedEvent($entry, $tags));
42
7244d6cb
NL
43 $this->get('session')->getFlashBag()->add(
44 'notice',
4204a06b 45 'flashes.tag.notice.tag_added'
7244d6cb
NL
46 );
47
4094ea47 48 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
7244d6cb
NL
49 }
50
4094ea47 51 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
7244d6cb
NL
52 'form' => $form->createView(),
53 'entry' => $entry,
4094ea47 54 ]);
7244d6cb
NL
55 }
56
567421af
TC
57 /**
58 * Removes tag from entry.
59 *
60 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
61 *
62 * @return \Symfony\Component\HttpFoundation\Response
63 */
64 public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
65 {
66 $entry->removeTag($tag);
67 $em = $this->getDoctrine()->getManager();
68 $em->flush();
ac8cf632 69
61351218
TC
70 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent::NAME, new EntryTaggedEvent($entry, $tag));
71
ac8cf632
JB
72 // remove orphan tag in case no entries are associated to it
73 if (count($tag->getEntries()) === 0) {
567421af 74 $em->remove($tag);
ac8cf632 75 $em->flush();
567421af 76 }
567421af 77
af497a64
NL
78 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
79
80 return $this->redirect($redirectUrl);
567421af
TC
81 }
82
3f3fbef1
NL
83 /**
84 * Shows tags for current user.
85 *
86 * @Route("/tag/list", name="tag")
87 *
88 * @return \Symfony\Component\HttpFoundation\Response
89 */
90 public function showTagAction()
91 {
92 $tags = $this->getDoctrine()
93 ->getRepository('WallabagCoreBundle:Tag')
28bb4890
JB
94 ->findAllTags($this->getUser()->getId());
95
96 $flatTags = [];
97
b0de88f7 98 foreach ($tags as $tag) {
28bb4890
JB
99 $nbEntries = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
b0de88f7 101 ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag->getId());
28bb4890
JB
102
103 $flatTags[] = [
b0de88f7
JB
104 'id' => $tag->getId(),
105 'label' => $tag->getLabel(),
106 'slug' => $tag->getSlug(),
28bb4890
JB
107 'nbEntries' => $nbEntries,
108 ];
109 }
faa86e06
JB
110
111 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
28bb4890 112 'tags' => $flatTags,
faa86e06 113 ]);
3f3fbef1 114 }
891456ba
NL
115
116 /**
117 * @param Tag $tag
118 * @param int $page
119 *
120 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
121 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
122 *
123 * @return \Symfony\Component\HttpFoundation\Response
124 */
125 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
126 {
267e8d63
NL
127 $entriesByTag = $this->getDoctrine()
128 ->getRepository('WallabagCoreBundle:Entry')
129 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
130
131 $pagerAdapter = new ArrayAdapter($entriesByTag);
891456ba
NL
132
133 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
134 ->prepare($pagerAdapter, $page);
135
136 try {
137 $entries->setCurrentPage($page);
138 } catch (OutOfRangeCurrentPageException $e) {
139 if ($page > 1) {
140 return $this->redirect($this->generateUrl($request->get('_route'), [
141 'slug' => $tag->getSlug(),
142 'page' => $entries->getNbPages(),
143 ]), 302);
144 }
145 }
146
82fc3290 147 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
faa86e06
JB
148 'form' => null,
149 'entries' => $entries,
150 'currentPage' => $page,
c8de7ab9 151 'tag' => $tag->getSlug(),
faa86e06 152 ]);
891456ba 153 }
3f3fbef1 154}