]>
Commit | Line | Data |
---|---|---|
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 Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Symfony\Component\Routing\Annotation\Route; | |
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 | $renameForms = []; | |
92 | foreach ($tags as $tag) { | |
93 | $renameForms[$tag['id']] = $this->createForm(RenameTagType::class, new Tag())->createView(); | |
94 | } | |
95 | ||
96 | return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [ | |
97 | 'tags' => $tags, | |
98 | 'renameForms' => $renameForms, | |
99 | ]); | |
100 | } | |
101 | ||
102 | /** | |
103 | * @param Tag $tag | |
104 | * @param int $page | |
105 | * | |
106 | * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"}) | |
107 | * @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) | |
108 | * | |
109 | * @return \Symfony\Component\HttpFoundation\Response | |
110 | */ | |
111 | public function showEntriesForTagAction(Tag $tag, $page, Request $request) | |
112 | { | |
113 | $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId( | |
114 | $this->getUser()->getId(), | |
115 | $tag->getId() | |
116 | ); | |
117 | ||
118 | $pagerAdapter = new ArrayAdapter($entriesByTag); | |
119 | ||
120 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter); | |
121 | ||
122 | try { | |
123 | $entries->setCurrentPage($page); | |
124 | } catch (OutOfRangeCurrentPageException $e) { | |
125 | if ($page > 1) { | |
126 | return $this->redirect($this->generateUrl($request->get('_route'), [ | |
127 | 'slug' => $tag->getSlug(), | |
128 | 'page' => $entries->getNbPages(), | |
129 | ]), 302); | |
130 | } | |
131 | } | |
132 | ||
133 | return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [ | |
134 | 'form' => null, | |
135 | 'entries' => $entries, | |
136 | 'currentPage' => $page, | |
137 | 'tag' => $tag, | |
138 | ]); | |
139 | } | |
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 | * | |
145 | * @param Tag $tag | |
146 | * @param Request $request | |
147 | * | |
148 | * @Route("/tag/rename/{slug}", name="tag_rename") | |
149 | * @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) | |
150 | * | |
151 | * @return \Symfony\Component\HttpFoundation\Response | |
152 | */ | |
153 | public function renameTagAction(Tag $tag, Request $request) | |
154 | { | |
155 | $form = $this->createForm(RenameTagType::class, new Tag()); | |
156 | $form->handleRequest($request); | |
157 | ||
158 | if ($form->isSubmitted() && $form->isValid()) { | |
159 | $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId( | |
160 | $this->getUser()->getId(), | |
161 | $tag->getId() | |
162 | ); | |
163 | foreach ($entries as $entry) { | |
164 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry( | |
165 | $entry, | |
166 | $form->get('label')->getData() | |
167 | ); | |
168 | $entry->removeTag($tag); | |
169 | } | |
170 | ||
171 | $em = $this->getDoctrine()->getManager(); | |
172 | $em->flush(); | |
173 | } | |
174 | ||
175 | $this->get('session')->getFlashBag()->add( | |
176 | 'notice', | |
177 | 'flashes.tag.notice.tag_renamed' | |
178 | ); | |
179 | ||
180 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true); | |
181 | ||
182 | return $this->redirect($redirectUrl); | |
183 | } | |
184 | } |