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