]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
Show untagged entries count on tag list
[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;
f808b016 7use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
3f3fbef1 8use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7244d6cb 9use Symfony\Component\HttpFoundation\Request;
115de64e 10use Symfony\Component\Routing\Annotation\Route;
7244d6cb 11use Wallabag\CoreBundle\Entity\Entry;
619cc453
JB
12use Wallabag\CoreBundle\Entity\Tag;
13use Wallabag\CoreBundle\Form\Type\NewTagType;
be326a22 14use Wallabag\CoreBundle\Form\Type\RenameTagType;
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
41 $this->get('session')->getFlashBag()->add(
42 'notice',
4204a06b 43 'flashes.tag.notice.tag_added'
7244d6cb
NL
44 );
45
4094ea47 46 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
7244d6cb
NL
47 }
48
4094ea47 49 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
7244d6cb
NL
50 'form' => $form->createView(),
51 'entry' => $entry,
4094ea47 52 ]);
7244d6cb
NL
53 }
54
567421af
TC
55 /**
56 * Removes tag from entry.
57 *
58 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
59 *
60 * @return \Symfony\Component\HttpFoundation\Response
61 */
62 public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
63 {
64 $entry->removeTag($tag);
65 $em = $this->getDoctrine()->getManager();
66 $em->flush();
ac8cf632
JB
67
68 // remove orphan tag in case no entries are associated to it
2a1ceb67 69 if (0 === \count($tag->getEntries())) {
567421af 70 $em->remove($tag);
ac8cf632 71 $em->flush();
567421af 72 }
567421af 73
5dbf3f23 74 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
af497a64
NL
75
76 return $this->redirect($redirectUrl);
567421af
TC
77 }
78
3f3fbef1
NL
79 /**
80 * Shows tags for current user.
81 *
82 * @Route("/tag/list", name="tag")
83 *
84 * @return \Symfony\Component\HttpFoundation\Response
85 */
86 public function showTagAction()
87 {
25203e50 88 $tags = $this->get('wallabag_core.tag_repository')
935e9fff 89 ->findAllFlatTagsWithNbEntries($this->getUser()->getId());
ad51743e
KD
90 $untagged = $this->get('wallabag_core.entry_repository')
91 ->countUntaggedEntriesForUser($this->getUser()->getId());
faa86e06 92
b846c1e4
SH
93 $renameForms = [];
94 foreach ($tags as $tag) {
95 $renameForms[$tag['id']] = $this->createForm(RenameTagType::class, new Tag())->createView();
96 }
97
faa86e06 98 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
935e9fff 99 'tags' => $tags,
b846c1e4 100 'renameForms' => $renameForms,
ad51743e 101 'untagged' => $untagged,
faa86e06 102 ]);
3f3fbef1 103 }
891456ba
NL
104
105 /**
106 * @param Tag $tag
107 * @param int $page
108 *
109 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
110 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
111 *
112 * @return \Symfony\Component\HttpFoundation\Response
113 */
114 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
115 {
25203e50
JB
116 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
117 $this->getUser()->getId(),
118 $tag->getId()
119 );
267e8d63
NL
120
121 $pagerAdapter = new ArrayAdapter($entriesByTag);
891456ba 122
53da8ad8 123 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
891456ba
NL
124
125 try {
126 $entries->setCurrentPage($page);
127 } catch (OutOfRangeCurrentPageException $e) {
128 if ($page > 1) {
129 return $this->redirect($this->generateUrl($request->get('_route'), [
130 'slug' => $tag->getSlug(),
131 'page' => $entries->getNbPages(),
132 ]), 302);
133 }
134 }
135
82fc3290 136 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
faa86e06
JB
137 'form' => null,
138 'entries' => $entries,
139 'currentPage' => $page,
bd40f1af 140 'tag' => $tag,
faa86e06 141 ]);
891456ba 142 }
be326a22
SH
143
144 /**
145 * Rename a given tag with a new label
146 * Create a new tag with the new name and drop the old one.
147 *
148 * @param Tag $tag
149 * @param Request $request
150 *
151 * @Route("/tag/rename/{slug}", name="tag_rename")
152 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
153 *
154 * @return \Symfony\Component\HttpFoundation\Response
155 */
156 public function renameTagAction(Tag $tag, Request $request)
157 {
158 $form = $this->createForm(RenameTagType::class, new Tag());
159 $form->handleRequest($request);
160
161 if ($form->isSubmitted() && $form->isValid()) {
162 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId(
163 $this->getUser()->getId(),
164 $tag->getId()
165 );
166 foreach ($entries as $entry) {
167 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
168 $entry,
169 $form->get('label')->getData()
170 );
171 $entry->removeTag($tag);
172 }
173
174 $em = $this->getDoctrine()->getManager();
175 $em->flush();
176 }
177
178 $this->get('session')->getFlashBag()->add(
179 'notice',
180 'flashes.tag.notice.tag_renamed'
181 );
182
183 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
184
185 return $this->redirect($redirectUrl);
186 }
3f3fbef1 187}