]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/TagController.php
Save changes
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / TagController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
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;
15
16 class TagController extends Controller
17 {
18 /**
19 * @param Request $request
20 * @param Entry $entry
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 {
28 $form = $this->createForm(NewTagType::class, new Tag());
29 $form->handleRequest($request);
30
31 if ($form->isSubmitted() && $form->isValid()) {
32 $tags = $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
33 $entry,
34 $form->get('label')->getData()
35 );
36
37 $em = $this->getDoctrine()->getManager();
38 $em->persist($entry);
39 $em->flush();
40
41 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent::NAME, new EntryTaggedEvent($entry, $tags));
42
43 $this->get('session')->getFlashBag()->add(
44 'notice',
45 'flashes.tag.notice.tag_added'
46 );
47
48 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
49 }
50
51 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
52 'form' => $form->createView(),
53 'entry' => $entry,
54 ]);
55 }
56
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();
69
70 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent::NAME, new EntryTaggedEvent($entry, $tag));
71
72 // remove orphan tag in case no entries are associated to it
73 if (count($tag->getEntries()) === 0) {
74 $em->remove($tag);
75 $em->flush();
76 }
77
78 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
79
80 return $this->redirect($redirectUrl);
81 }
82
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')
94 ->findAllTags($this->getUser()->getId());
95
96 $flatTags = [];
97
98 foreach ($tags as $tag) {
99 $nbEntries = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag->getId());
102
103 $flatTags[] = [
104 'id' => $tag->getId(),
105 'label' => $tag->getLabel(),
106 'slug' => $tag->getSlug(),
107 'nbEntries' => $nbEntries,
108 ];
109 }
110
111 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
112 'tags' => $flatTags,
113 ]);
114 }
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 {
127 $entriesByTag = $this->getDoctrine()
128 ->getRepository('WallabagCoreBundle:Entry')
129 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
130
131 $pagerAdapter = new ArrayAdapter($entriesByTag);
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
147 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
148 'form' => null,
149 'entries' => $entries,
150 'currentPage' => $page,
151 'tag' => $tag->getSlug(),
152 ]);
153 }
154 }