]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
WIP
[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;
bf6c0346 12use Wallabag\CoreBundle\Event\Activity\Actions\Entry\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()) {
6bc6fb1f 32 $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
bf6c0346
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
bf6c0346
TC
70 $this->get('event_dispatcher')->dispatch(EntryTaggedEvent::NAME, new EntryTaggedEvent($entry, $tag), true);
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
5dbf3f23 78 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
af497a64
NL
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 {
25203e50
JB
92 $repository = $this->get('wallabag_core.entry_repository');
93 $tags = $this->get('wallabag_core.tag_repository')
28bb4890
JB
94 ->findAllTags($this->getUser()->getId());
95
96 $flatTags = [];
97
b0de88f7 98 foreach ($tags as $tag) {
25203e50
JB
99 $nbEntries = $repository->countAllEntriesByUserIdAndTagId(
100 $this->getUser()->getId(),
101 $tag->getId()
102 );
28bb4890
JB
103
104 $flatTags[] = [
b0de88f7
JB
105 'id' => $tag->getId(),
106 'label' => $tag->getLabel(),
107 'slug' => $tag->getSlug(),
28bb4890
JB
108 'nbEntries' => $nbEntries,
109 ];
110 }
faa86e06
JB
111
112 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
28bb4890 113 'tags' => $flatTags,
faa86e06 114 ]);
3f3fbef1 115 }
891456ba
NL
116
117 /**
118 * @param Tag $tag
119 * @param int $page
120 *
121 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
122 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
123 *
124 * @return \Symfony\Component\HttpFoundation\Response
125 */
126 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
127 {
25203e50
JB
128 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
129 $this->getUser()->getId(),
130 $tag->getId()
131 );
267e8d63
NL
132
133 $pagerAdapter = new ArrayAdapter($entriesByTag);
891456ba 134
53da8ad8 135 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
891456ba
NL
136
137 try {
138 $entries->setCurrentPage($page);
139 } catch (OutOfRangeCurrentPageException $e) {
140 if ($page > 1) {
141 return $this->redirect($this->generateUrl($request->get('_route'), [
142 'slug' => $tag->getSlug(),
143 'page' => $entries->getNbPages(),
144 ]), 302);
145 }
146 }
147
82fc3290 148 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
faa86e06
JB
149 'form' => null,
150 'entries' => $entries,
151 'currentPage' => $page,
bd40f1af 152 'tag' => $tag,
faa86e06 153 ]);
891456ba 154 }
3f3fbef1 155}