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