]>
Commit | Line | Data |
---|---|---|
3f3fbef1 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
7244d6cb NL |
7 | use Symfony\Component\HttpFoundation\Request; |
8 | use Wallabag\CoreBundle\Form\Type\NewTagType; | |
9 | use Wallabag\CoreBundle\Entity\Tag; | |
10 | use Wallabag\CoreBundle\Entity\Entry; | |
3f3fbef1 NL |
11 | |
12 | class 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(); |
7244d6cb NL |
24 | $form = $this->createForm(new NewTagType(), $tag); |
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', | |
46 | 'Tag added' | |
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 | ||
3f3fbef1 NL |
58 | /** |
59 | * Shows tags for current user. | |
60 | * | |
61 | * @Route("/tag/list", name="tag") | |
62 | * | |
63 | * @return \Symfony\Component\HttpFoundation\Response | |
64 | */ | |
65 | public function showTagAction() | |
66 | { | |
67 | $tags = $this->getDoctrine() | |
68 | ->getRepository('WallabagCoreBundle:Tag') | |
69 | ->findTags($this->getUser()->getId()); | |
70 | ||
71 | return $this->render( | |
72 | 'WallabagCoreBundle:Tag:tags.html.twig', | |
73 | array( | |
8ce32af6 | 74 | 'tags' => $tags, |
3f3fbef1 NL |
75 | ) |
76 | ); | |
77 | } | |
3f3fbef1 | 78 | } |