3 namespace Wallabag\CoreBundle\Controller
;
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
;
16 class TagController
extends Controller
19 * @param Request $request
22 * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
24 * @return \Symfony\Component\HttpFoundation\Response
26 public function addTagFormAction(Request
$request, Entry
$entry)
28 $form = $this->createForm(NewTagType
::class, new Tag());
29 $form->handleRequest($request);
31 if ($form->isSubmitted() && $form->isValid()) {
32 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
34 $form->get('label')->getData()
37 $em = $this->getDoctrine()->getManager();
41 $this->get('session')->getFlashBag()->add(
43 'flashes.tag.notice.tag_added'
46 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
49 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
50 'form' => $form->createView(),
56 * Removes tag from entry.
58 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
60 * @return \Symfony\Component\HttpFoundation\Response
62 public function removeTagFromEntry(Request
$request, Entry
$entry, Tag
$tag)
64 $entry->removeTag($tag);
65 $em = $this->getDoctrine()->getManager();
68 // remove orphan tag in case no entries are associated to it
69 if (0 === \
count($tag->getEntries())) {
74 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'), '', true);
76 return $this->redirect($redirectUrl);
80 * Shows tags for current user.
82 * @Route("/tag/list", name="tag")
84 * @return \Symfony\Component\HttpFoundation\Response
86 public function showTagAction()
88 $tags = $this->get('wallabag_core.tag_repository')
89 ->findAllFlatTagsWithNbEntries($this->getUser()->getId());
90 $untagged = $this->get('wallabag_core.entry_repository')
91 ->countUntaggedEntriesForUser($this->getUser()->getId());
94 foreach ($tags as $tag) {
95 $renameForms[$tag['id']] = $this->createForm(RenameTagType
::class, new Tag())->createView();
98 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
100 'renameForms' => $renameForms,
101 'untagged' => $untagged,
109 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
110 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
112 * @return \Symfony\Component\HttpFoundation\Response
114 public function showEntriesForTagAction(Tag
$tag, $page, Request
$request)
116 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
117 $this->getUser()->getId(),
121 $pagerAdapter = new ArrayAdapter($entriesByTag);
123 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
126 $entries->setCurrentPage($page);
127 } catch (OutOfRangeCurrentPageException
$e) {
129 return $this->redirect($this->generateUrl($request->get('_route'), [
130 'slug' => $tag->getSlug(),
131 'page' => $entries->getNbPages(),
136 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
138 'entries' => $entries,
139 'currentPage' => $page,
145 * Rename a given tag with a new label
146 * Create a new tag with the new name and drop the old one.
149 * @param Request $request
151 * @Route("/tag/rename/{slug}", name="tag_rename")
152 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
154 * @return \Symfony\Component\HttpFoundation\Response
156 public function renameTagAction(Tag
$tag, Request
$request)
158 $form = $this->createForm(RenameTagType
::class, new Tag());
159 $form->handleRequest($request);
161 if ($form->isSubmitted() && $form->isValid()) {
162 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId(
163 $this->getUser()->getId(),
166 foreach ($entries as $entry) {
167 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
169 $form->get('label')->getData()
171 $entry->removeTag($tag);
174 $em = $this->getDoctrine()->getManager();
178 $this->get('session')->getFlashBag()->add(
180 'flashes.tag.notice.tag_renamed'
183 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'), '', true);
185 return $this->redirect($redirectUrl);