]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/TagController.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / TagController.php
CommitLineData
3f3fbef1
NL
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
891456ba
NL
5use Pagerfanta\Adapter\ArrayAdapter;
6use Pagerfanta\Exception\OutOfRangeCurrentPageException;
f808b016 7use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
3f3fbef1
NL
8use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7244d6cb 10use Symfony\Component\HttpFoundation\Request;
7244d6cb 11use Wallabag\CoreBundle\Entity\Entry;
619cc453
JB
12use Wallabag\CoreBundle\Entity\Tag;
13use Wallabag\CoreBundle\Form\Type\NewTagType;
3f3fbef1
NL
14
15class TagController extends Controller
16{
7244d6cb
NL
17 /**
18 * @param Request $request
4094ea47 19 * @param Entry $entry
7244d6cb
NL
20 *
21 * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
22 *
23 * @return \Symfony\Component\HttpFoundation\Response
24 */
25 public function addTagFormAction(Request $request, Entry $entry)
26 {
2baca964 27 $form = $this->createForm(NewTagType::class, new Tag());
7244d6cb
NL
28 $form->handleRequest($request);
29
21e7ccef 30 if ($form->isSubmitted() && $form->isValid()) {
6bc6fb1f 31 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
2baca964
JB
32 $entry,
33 $form->get('label')->getData()
34 );
7244d6cb
NL
35
36 $em = $this->getDoctrine()->getManager();
2baca964 37 $em->persist($entry);
7244d6cb
NL
38 $em->flush();
39
40 $this->get('session')->getFlashBag()->add(
41 'notice',
4204a06b 42 'flashes.tag.notice.tag_added'
7244d6cb
NL
43 );
44
4094ea47 45 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
7244d6cb
NL
46 }
47
4094ea47 48 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
7244d6cb
NL
49 'form' => $form->createView(),
50 'entry' => $entry,
4094ea47 51 ]);
7244d6cb
NL
52 }
53
567421af
TC
54 /**
55 * Removes tag from entry.
56 *
57 * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
58 *
59 * @return \Symfony\Component\HttpFoundation\Response
60 */
61 public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
62 {
63 $entry->removeTag($tag);
64 $em = $this->getDoctrine()->getManager();
65 $em->flush();
ac8cf632
JB
66
67 // remove orphan tag in case no entries are associated to it
68 if (count($tag->getEntries()) === 0) {
567421af 69 $em->remove($tag);
ac8cf632 70 $em->flush();
567421af 71 }
567421af 72
5dbf3f23 73 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
af497a64
NL
74
75 return $this->redirect($redirectUrl);
567421af
TC
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 {
25203e50
JB
87 $repository = $this->get('wallabag_core.entry_repository');
88 $tags = $this->get('wallabag_core.tag_repository')
28bb4890
JB
89 ->findAllTags($this->getUser()->getId());
90
91 $flatTags = [];
92
b0de88f7 93 foreach ($tags as $tag) {
25203e50
JB
94 $nbEntries = $repository->countAllEntriesByUserIdAndTagId(
95 $this->getUser()->getId(),
96 $tag->getId()
97 );
28bb4890
JB
98
99 $flatTags[] = [
b0de88f7
JB
100 'id' => $tag->getId(),
101 'label' => $tag->getLabel(),
102 'slug' => $tag->getSlug(),
28bb4890
JB
103 'nbEntries' => $nbEntries,
104 ];
105 }
faa86e06
JB
106
107 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
28bb4890 108 'tags' => $flatTags,
faa86e06 109 ]);
3f3fbef1 110 }
891456ba
NL
111
112 /**
113 * @param Tag $tag
114 * @param int $page
115 *
116 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
117 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
118 *
119 * @return \Symfony\Component\HttpFoundation\Response
120 */
121 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
122 {
25203e50
JB
123 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
124 $this->getUser()->getId(),
125 $tag->getId()
126 );
267e8d63
NL
127
128 $pagerAdapter = new ArrayAdapter($entriesByTag);
891456ba 129
53da8ad8 130 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
891456ba
NL
131
132 try {
133 $entries->setCurrentPage($page);
134 } catch (OutOfRangeCurrentPageException $e) {
135 if ($page > 1) {
136 return $this->redirect($this->generateUrl($request->get('_route'), [
137 'slug' => $tag->getSlug(),
138 'page' => $entries->getNbPages(),
139 ]), 302);
140 }
141 }
142
82fc3290 143 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
faa86e06
JB
144 'form' => null,
145 'entries' => $entries,
146 'currentPage' => $page,
bd40f1af 147 'tag' => $tag,
faa86e06 148 ]);
891456ba 149 }
3f3fbef1 150}