]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/TagController.php
Merge pull request #2393 from wallabag/api-urls-exist
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / TagController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Pagerfanta\Adapter\ArrayAdapter;
6 use Pagerfanta\Exception\OutOfRangeCurrentPageException;
7 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9 use Symfony\Component\HttpFoundation\Request;
10 use Wallabag\CoreBundle\Entity\Entry;
11 use Wallabag\CoreBundle\Entity\Tag;
12 use Wallabag\CoreBundle\Form\Type\NewTagType;
13 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
14
15 class TagController extends Controller
16 {
17 /**
18 * @param Request $request
19 * @param Entry $entry
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 {
27 $form = $this->createForm(NewTagType::class, new Tag());
28 $form->handleRequest($request);
29
30 if ($form->isValid()) {
31 $this->get('wallabag_core.content_proxy')->assignTagsToEntry(
32 $entry,
33 $form->get('label')->getData()
34 );
35
36 $em = $this->getDoctrine()->getManager();
37 $em->persist($entry);
38 $em->flush();
39
40 $this->get('session')->getFlashBag()->add(
41 'notice',
42 'flashes.tag.notice.tag_added'
43 );
44
45 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
46 }
47
48 return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
49 'form' => $form->createView(),
50 'entry' => $entry,
51 ]);
52 }
53
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();
66
67 // remove orphan tag in case no entries are associated to it
68 if (count($tag->getEntries()) === 0) {
69 $em->remove($tag);
70 $em->flush();
71 }
72
73 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
74
75 return $this->redirect($redirectUrl);
76 }
77
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 {
87 $tags = $this->getDoctrine()
88 ->getRepository('WallabagCoreBundle:Tag')
89 ->findAllTagsWithEntries($this->getUser()->getId());
90
91 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
92 'tags' => $tags,
93 ]);
94 }
95
96 /**
97 * @param Tag $tag
98 * @param int $page
99 *
100 * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
101 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
102 *
103 * @return \Symfony\Component\HttpFoundation\Response
104 */
105 public function showEntriesForTagAction(Tag $tag, $page, Request $request)
106 {
107 $entriesByTag = $this->getDoctrine()
108 ->getRepository('WallabagCoreBundle:Entry')
109 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
110
111 $pagerAdapter = new ArrayAdapter($entriesByTag);
112
113 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
114 ->prepare($pagerAdapter, $page);
115
116 try {
117 $entries->setCurrentPage($page);
118 } catch (OutOfRangeCurrentPageException $e) {
119 if ($page > 1) {
120 return $this->redirect($this->generateUrl($request->get('_route'), [
121 'slug' => $tag->getSlug(),
122 'page' => $entries->getNbPages(),
123 ]), 302);
124 }
125 }
126
127 return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
128 'form' => null,
129 'entries' => $entries,
130 'currentPage' => $page,
131 ]);
132 }
133 }