]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
Merge pull request #2306 from wallabag/redis-rabbit-check
[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;
3f3fbef1
NL
7use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7244d6cb 9use Symfony\Component\HttpFoundation\Request;
7244d6cb 10use Wallabag\CoreBundle\Entity\Entry;
619cc453
JB
11use Wallabag\CoreBundle\Entity\Tag;
12use Wallabag\CoreBundle\Form\Type\NewTagType;
891456ba 13use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
3f3fbef1
NL
14
15class TagController extends Controller
16{
7244d6cb
NL
17 /**
18 * @param Request $request
4094ea47 19 * @param Entry $entry
7244d6cb
NL
20 *
21 * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
22 *
23 * @return \Symfony\Component\HttpFoundation\Response
24 */
25 public function addTagFormAction(Request $request, Entry $entry)
26 {
2baca964 27 $form = $this->createForm(NewTagType::class, new Tag());
7244d6cb
NL
28 $form->handleRequest($request);
29
30 if ($form->isValid()) {
2baca964
JB
31 $this->get('wallabag_core.content_proxy')->assignTagsToEntry(
32 $entry,
33 $form->get('label')->getData()
34 );
7244d6cb
NL
35
36 $em = $this->getDoctrine()->getManager();
2baca964 37 $em->persist($entry);
7244d6cb
NL
38 $em->flush();
39
40 $this->get('session')->getFlashBag()->add(
41 'notice',
4204a06b 42 'flashes.tag.notice.tag_added'
7244d6cb
NL
43 );
44
4094ea47 45 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
7244d6cb
NL
46 }
47
4094ea47 48 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
7244d6cb
NL
49 'form' => $form->createView(),
50 'entry' => $entry,
4094ea47 51 ]);
7244d6cb
NL
52 }
53
567421af
TC
54 /**
55 * Removes tag from entry.
56 *
57 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
58 *
59 * @return \Symfony\Component\HttpFoundation\Response
60 */
61 public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
62 {
63 $entry->removeTag($tag);
64 $em = $this->getDoctrine()->getManager();
65 $em->flush();
66 if (count($tag->getEntries()) == 0) {
67 $em->remove($tag);
68 }
69 $em->flush();
70
af497a64
NL
71 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
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 {
85 $tags = $this->getDoctrine()
86 ->getRepository('WallabagCoreBundle:Tag')
429d86f3
NL
87 ->findAllTags($this->getUser()->getId())
88 ->getQuery()
89 ->getResult();
3f3fbef1
NL
90
91 return $this->render(
92 'WallabagCoreBundle:Tag:tags.html.twig',
4094ea47 93 [
8ce32af6 94 'tags' => $tags,
4094ea47 95 ]
3f3fbef1
NL
96 );
97 }
891456ba
NL
98
99 /**
100 * @param Tag $tag
101 * @param int $page
102 *
103 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
104 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
105 *
106 * @return \Symfony\Component\HttpFoundation\Response
107 */
108 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
109 {
267e8d63
NL
110 $entriesByTag = $this->getDoctrine()
111 ->getRepository('WallabagCoreBundle:Entry')
112 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
113
114 $pagerAdapter = new ArrayAdapter($entriesByTag);
891456ba
NL
115
116 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
117 ->prepare($pagerAdapter, $page);
118
119 try {
120 $entries->setCurrentPage($page);
121 } catch (OutOfRangeCurrentPageException $e) {
122 if ($page > 1) {
123 return $this->redirect($this->generateUrl($request->get('_route'), [
124 'slug' => $tag->getSlug(),
125 'page' => $entries->getNbPages(),
126 ]), 302);
127 }
128 }
129
130 return $this->render(
131 'WallabagCoreBundle:Entry:entries.html.twig',
132 [
133 'form' => null,
134 'entries' => $entries,
135 'currentPage' => $page,
136 ]
137 );
138 }
3f3fbef1 139}