]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/TagController.php
Create a new Tag action to rename tags.
[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\ParamConverter;
8 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10 use Symfony\Component\HttpFoundation\Request;
11 use Wallabag\CoreBundle\Entity\Entry;
12 use Wallabag\CoreBundle\Entity\Tag;
13 use Wallabag\CoreBundle\Form\Type\NewTagType;
14 use Wallabag\CoreBundle\Form\Type\RenameTagType;
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 $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('session')->getFlashBag()->add(
42 'notice',
43 'flashes.tag.notice.tag_added'
44 );
45
46 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
47 }
48
49 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
50 'form' => $form->createView(),
51 'entry' => $entry,
52 ]);
53 }
54
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();
67
68 // remove orphan tag in case no entries are associated to it
69 if (0 === \count($tag->getEntries())) {
70 $em->remove($tag);
71 $em->flush();
72 }
73
74 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
75
76 return $this->redirect($redirectUrl);
77 }
78
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 {
88 $tags = $this->get('wallabag_core.tag_repository')
89 ->findAllFlatTagsWithNbEntries($this->getUser()->getId());
90
91 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
92 'tags' => $tags,
93 ]);
94 }
95
96 /**
97 * @param Tag $tag
98 * @param int $page
99 *
100 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
101 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
102 *
103 * @return \Symfony\Component\HttpFoundation\Response
104 */
105 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
106 {
107 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
108 $this->getUser()->getId(),
109 $tag->getId()
110 );
111
112 $pagerAdapter = new ArrayAdapter($entriesByTag);
113
114 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
115
116 try {
117 $entries->setCurrentPage($page);
118 } catch (OutOfRangeCurrentPageException $e) {
119 if ($page > 1) {
120 return $this->redirect($this->generateUrl($request->get('_route'), [
121 'slug' => $tag->getSlug(),
122 'page' => $entries->getNbPages(),
123 ]), 302);
124 }
125 }
126
127 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
128 'form' => null,
129 'entries' => $entries,
130 'currentPage' => $page,
131 'tag' => $tag,
132 ]);
133 }
134
135 /**
136 * Rename a given tag with a new label
137 * Create a new tag with the new name and drop the old one.
138 *
139 * @param Tag $tag
140 * @param Request $request
141 *
142 * @Route("/tag/rename/{slug}", name="tag_rename")
143 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
144 *
145 * @return \Symfony\Component\HttpFoundation\Response
146 */
147 public function renameTagAction(Tag $tag, Request $request)
148 {
149 $form = $this->createForm(RenameTagType::class, new Tag());
150 $form->handleRequest($request);
151
152 if ($form->isSubmitted() && $form->isValid()) {
153 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId(
154 $this->getUser()->getId(),
155 $tag->getId()
156 );
157 foreach ($entries as $entry) {
158 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
159 $entry,
160 $form->get('label')->getData()
161 );
162 $entry->removeTag($tag);
163 }
164
165 $em = $this->getDoctrine()->getManager();
166 $em->flush();
167 }
168
169 $this->get('session')->getFlashBag()->add(
170 'notice',
171 'flashes.tag.notice.tag_renamed'
172 );
173
174 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
175
176 return $this->redirect($redirectUrl);
177 }
178 }