]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/TagController.php
Merge pull request #1925 from wallabag/fix-redirect-without-referer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / TagController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 use Symfony\Component\HttpFoundation\Request;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\CoreBundle\Entity\Tag;
10 use Wallabag\CoreBundle\Form\Type\NewTagType;
11
12 class TagController extends Controller
13 {
14 /**
15 * @param Request $request
16 * @param Entry $entry
17 *
18 * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
19 *
20 * @return \Symfony\Component\HttpFoundation\Response
21 */
22 public function addTagFormAction(Request $request, Entry $entry)
23 {
24 $form = $this->createForm(NewTagType::class, new Tag());
25 $form->handleRequest($request);
26
27 if ($form->isValid()) {
28 $this->get('wallabag_core.content_proxy')->assignTagsToEntry(
29 $entry,
30 $form->get('label')->getData()
31 );
32
33 $em = $this->getDoctrine()->getManager();
34 $em->persist($entry);
35 $em->flush();
36
37 $this->get('session')->getFlashBag()->add(
38 'notice',
39 'flashes.tag.notice.tag_added'
40 );
41
42 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
43 }
44
45 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
46 'form' => $form->createView(),
47 'entry' => $entry,
48 ]);
49 }
50
51 /**
52 * Removes tag from entry.
53 *
54 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
55 *
56 * @return \Symfony\Component\HttpFoundation\Response
57 */
58 public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
59 {
60 $entry->removeTag($tag);
61 $em = $this->getDoctrine()->getManager();
62 $em->flush();
63 if (count($tag->getEntries()) == 0) {
64 $em->remove($tag);
65 }
66 $em->flush();
67
68 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
69
70 return $this->redirect($redirectUrl);
71 }
72
73 /**
74 * Shows tags for current user.
75 *
76 * @Route("/tag/list", name="tag")
77 *
78 * @return \Symfony\Component\HttpFoundation\Response
79 */
80 public function showTagAction()
81 {
82 $tags = $this->getDoctrine()
83 ->getRepository('WallabagCoreBundle:Tag')
84 ->findTags($this->getUser()->getId());
85
86 return $this->render(
87 'WallabagCoreBundle:Tag:tags.html.twig',
88 [
89 'tags' => $tags,
90 ]
91 );
92 }
93 }