]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
Split tags with commas from UI
[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)) {
5a4ee4ae
NL
35 $this->get('wallabag_core.content_proxy')->assignTagsToEntry(
36 $entry,
37 $tag
38 );
7244d6cb 39 $em->persist($tag);
fc732227 40 } elseif (!$existingTag->hasEntry($entry)) {
5a4ee4ae
NL
41 $this->get('wallabag_core.content_proxy')->assignTagsToEntry(
42 $entry,
43 $existingTag
44 );
fc732227 45 $em->persist($existingTag);
7244d6cb
NL
46 }
47
48 $em->flush();
49
50 $this->get('session')->getFlashBag()->add(
51 'notice',
4204a06b 52 'flashes.tag.notice.tag_added'
7244d6cb
NL
53 );
54
55 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
56 }
57
58 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', array(
59 'form' => $form->createView(),
60 'entry' => $entry,
61 ));
62 }
63
567421af
TC
64 /**
65 * Removes tag from entry.
66 *
67 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
68 *
69 * @return \Symfony\Component\HttpFoundation\Response
70 */
71 public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
72 {
73 $entry->removeTag($tag);
74 $em = $this->getDoctrine()->getManager();
75 $em->flush();
76 if (count($tag->getEntries()) == 0) {
77 $em->remove($tag);
78 }
79 $em->flush();
80
81 return $this->redirect($request->headers->get('referer'));
82 }
83
3f3fbef1
NL
84 /**
85 * Shows tags for current user.
86 *
87 * @Route("/tag/list", name="tag")
88 *
89 * @return \Symfony\Component\HttpFoundation\Response
90 */
91 public function showTagAction()
92 {
93 $tags = $this->getDoctrine()
94 ->getRepository('WallabagCoreBundle:Tag')
95 ->findTags($this->getUser()->getId());
96
97 return $this->render(
98 'WallabagCoreBundle:Tag:tags.html.twig',
99 array(
8ce32af6 100 'tags' => $tags,
3f3fbef1
NL
101 )
102 );
103 }
3f3fbef1 104}