]>
Commit | Line | Data |
---|---|---|
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 | * | |
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 | $form = $this->createForm(NewTagType::class, new Tag()); | |
24 | $form->handleRequest($request); | |
25 | ||
26 | if ($form->isValid()) { | |
27 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry( | |
28 | $entry, | |
29 | $form->get('label')->getData() | |
30 | ); | |
31 | ||
32 | $em = $this->getDoctrine()->getManager(); | |
33 | $em->persist($entry); | |
34 | $em->flush(); | |
35 | ||
36 | $this->get('session')->getFlashBag()->add( | |
37 | 'notice', | |
38 | 'flashes.tag.notice.tag_added' | |
39 | ); | |
40 | ||
41 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
42 | } | |
43 | ||
44 | return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', array( | |
45 | 'form' => $form->createView(), | |
46 | 'entry' => $entry, | |
47 | )); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Removes tag from entry. | |
52 | * | |
53 | * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag") | |
54 | * | |
55 | * @return \Symfony\Component\HttpFoundation\Response | |
56 | */ | |
57 | public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag) | |
58 | { | |
59 | $entry->removeTag($tag); | |
60 | $em = $this->getDoctrine()->getManager(); | |
61 | $em->flush(); | |
62 | if (count($tag->getEntries()) == 0) { | |
63 | $em->remove($tag); | |
64 | } | |
65 | $em->flush(); | |
66 | ||
67 | return $this->redirect($request->headers->get('referer')); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Shows tags for current user. | |
72 | * | |
73 | * @Route("/tag/list", name="tag") | |
74 | * | |
75 | * @return \Symfony\Component\HttpFoundation\Response | |
76 | */ | |
77 | public function showTagAction() | |
78 | { | |
79 | $tags = $this->getDoctrine() | |
80 | ->getRepository('WallabagCoreBundle:Tag') | |
81 | ->findTags($this->getUser()->getId()); | |
82 | ||
83 | return $this->render( | |
84 | 'WallabagCoreBundle:Tag:tags.html.twig', | |
85 | array( | |
86 | 'tags' => $tags, | |
87 | ) | |
88 | ); | |
89 | } | |
90 | } |