]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
Merge pull request #1540 from wallabag/v2-fix-delete
[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
NL
7use Symfony\Component\HttpFoundation\Request;
8use Wallabag\CoreBundle\Form\Type\NewTagType;
9use Wallabag\CoreBundle\Entity\Tag;
10use Wallabag\CoreBundle\Entity\Entry;
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 {
23 $tag = new Tag($this->getUser());
24 $form = $this->createForm(new NewTagType(), $tag);
25 $form->handleRequest($request);
26
27 if ($form->isValid()) {
28 $existingTag = $this->getDoctrine()
29 ->getRepository('WallabagCoreBundle:Tag')
30 ->findOneByLabelAndUserId($tag->getLabel(), $this->getUser()->getId());
31
32 $em = $this->getDoctrine()->getManager();
33
34 if (is_null($existingTag)) {
35 $entry->addTag($tag);
36 $em->persist($tag);
37 } else {
38 if (!$existingTag->hasEntry($entry)) {
39 $entry->addTag($existingTag);
40 $em->persist($existingTag);
41 }
42 }
43
44 $em->flush();
45
46 $this->get('session')->getFlashBag()->add(
47 'notice',
48 'Tag added'
49 );
50
51 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
52 }
53
54 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', array(
55 'form' => $form->createView(),
56 'entry' => $entry,
57 ));
58 }
59
3f3fbef1
NL
60 /**
61 * Shows tags for current user.
62 *
63 * @Route("/tag/list", name="tag")
64 *
65 * @return \Symfony\Component\HttpFoundation\Response
66 */
67 public function showTagAction()
68 {
69 $tags = $this->getDoctrine()
70 ->getRepository('WallabagCoreBundle:Tag')
71 ->findTags($this->getUser()->getId());
72
73 return $this->render(
74 'WallabagCoreBundle:Tag:tags.html.twig',
75 array(
8ce32af6 76 'tags' => $tags,
3f3fbef1
NL
77 )
78 );
79 }
3f3fbef1 80}