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
|
|
|
* @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
|
|
|
{ |
25
|
|
|
$form = $this->createForm(NewTagType::class, new Tag()); |
26
|
|
|
$form->handleRequest($request); |
27
|
|
|
|
28
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
29
|
|
|
$this->get('wallabag_core.tags_assigner')->assignTagsToEntry( |
30
|
|
|
$entry, |
31
|
|
|
$form->get('label')->getData() |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$em = $this->getDoctrine()->getManager(); |
35
|
|
|
$em->persist($entry); |
36
|
|
|
$em->flush(); |
37
|
|
|
|
38
|
|
|
$this->get('session')->getFlashBag()->add( |
39
|
|
|
'notice', |
40
|
|
|
'flashes.tag.notice.tag_added' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [ |
47
|
|
|
'form' => $form->createView(), |
48
|
|
|
'entry' => $entry, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
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(); |
64
|
|
|
|
65
|
|
|
// remove orphan tag in case no entries are associated to it |
66
|
|
|
if (0 === \count($tag->getEntries())) { |
67
|
|
|
$em->remove($tag); |
68
|
|
|
$em->flush(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true); |
72
|
|
|
|
73
|
|
|
return $this->redirect($redirectUrl); |
74
|
|
|
} |
75
|
|
|
|
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
|
|
|
{ |
85
|
|
|
$tags = $this->get('wallabag_core.tag_repository') |
86
|
|
|
->findAllFlatTagsWithNbEntries($this->getUser()->getId()); |
|
|
|
|
87
|
|
|
$nbEntriesUntagged = $this->get('wallabag_core.entry_repository') |
88
|
|
|
->countUntaggedEntriesByUser($this->getUser()->getId()); |
89
|
|
|
|
90
|
|
|
$renameForms = []; |
91
|
|
|
foreach ($tags as $tag) { |
92
|
|
|
$renameForms[$tag['id']] = $this->createForm(RenameTagType::class, new Tag())->createView(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [ |
96
|
|
|
'tags' => $tags, |
97
|
|
|
'renameForms' => $renameForms, |
98
|
|
|
'nbEntriesUntagged' => $nbEntriesUntagged, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
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
|
|
|
{ |
112
|
|
|
$entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId( |
113
|
|
|
$this->getUser()->getId(), |
114
|
|
|
$tag->getId() |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$pagerAdapter = new ArrayAdapter($entriesByTag); |
118
|
|
|
|
119
|
|
|
$entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter); |
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
|
|
|
|
132
|
|
|
return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [ |
133
|
|
|
'form' => null, |
134
|
|
|
'entries' => $entries, |
135
|
|
|
'currentPage' => $page, |
136
|
|
|
'tag' => $tag, |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Rename a given tag with a new label |
142
|
|
|
* Create a new tag with the new name and drop the old one. |
143
|
|
|
* |
144
|
|
|
* @Route("/tag/rename/{slug}", name="tag_rename") |
145
|
|
|
* @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) |
146
|
|
|
* |
147
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
148
|
|
|
*/ |
149
|
|
|
public function renameTagAction(Tag $tag, Request $request) |
150
|
|
|
{ |
151
|
|
|
$form = $this->createForm(RenameTagType::class, new Tag()); |
152
|
|
|
$form->handleRequest($request); |
153
|
|
|
|
154
|
|
|
$redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true); |
155
|
|
|
|
156
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
157
|
|
|
$newTag = new Tag(); |
158
|
|
|
$newTag->setLabel($form->get('label')->getData()); |
159
|
|
|
|
160
|
|
|
if ($newTag->getLabel() === $tag->getLabel()) { |
161
|
|
|
return $this->redirect($redirectUrl); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$tagFromRepo = $this->get('wallabag_core.tag_repository')->findOneByLabel($newTag->getLabel()); |
|
|
|
|
165
|
|
|
|
166
|
|
|
if (null !== $tagFromRepo) { |
167
|
|
|
$newTag = $tagFromRepo; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$entries = $this->get('wallabag_core.entry_repository')->findAllByTagId( |
171
|
|
|
$this->getUser()->getId(), |
172
|
|
|
$tag->getId() |
173
|
|
|
); |
174
|
|
|
foreach ($entries as $entry) { |
175
|
|
|
$this->get('wallabag_core.tags_assigner')->assignTagsToEntry( |
176
|
|
|
$entry, |
177
|
|
|
$newTag->getLabel(), |
178
|
|
|
[$newTag] |
179
|
|
|
); |
180
|
|
|
$entry->removeTag($tag); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->getDoctrine()->getManager()->flush(); |
184
|
|
|
|
185
|
|
|
$this->get('session')->getFlashBag()->add( |
186
|
|
|
'notice', |
187
|
|
|
'flashes.tag.notice.tag_renamed' |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->redirect($redirectUrl); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|