]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/RssController.php
Replace kernel.root_dir by kernel.project_dir
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Pagerfanta\Adapter\ArrayAdapter;
6 use Pagerfanta\Adapter\DoctrineORMAdapter;
7 use Pagerfanta\Exception\OutOfRangeCurrentPageException;
8 use Pagerfanta\Pagerfanta;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12 use Symfony\Component\HttpFoundation\Request;
13 use Symfony\Component\HttpFoundation\Response;
14 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15 use Wallabag\CoreBundle\Entity\Entry;
16 use Wallabag\CoreBundle\Entity\Tag;
17 use Wallabag\UserBundle\Entity\User;
18
19 class RssController extends Controller
20 {
21 /**
22 * Shows unread entries for current user.
23 *
24 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
25 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
26 *
27 * @return \Symfony\Component\HttpFoundation\Response
28 */
29 public function showUnreadRSSAction(Request $request, User $user)
30 {
31 return $this->showEntries('unread', $user, $request->query->get('page', 1));
32 }
33
34 /**
35 * Shows read entries for current user.
36 *
37 * @Route("/{username}/{token}/archive.xml", name="archive_rss", defaults={"_format"="xml"})
38 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
39 *
40 * @return \Symfony\Component\HttpFoundation\Response
41 */
42 public function showArchiveRSSAction(Request $request, User $user)
43 {
44 return $this->showEntries('archive', $user, $request->query->get('page', 1));
45 }
46
47 /**
48 * Shows starred entries for current user.
49 *
50 * @Route("/{username}/{token}/starred.xml", name="starred_rss", defaults={"_format"="xml"})
51 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
52 *
53 * @return \Symfony\Component\HttpFoundation\Response
54 */
55 public function showStarredRSSAction(Request $request, User $user)
56 {
57 return $this->showEntries('starred', $user, $request->query->get('page', 1));
58 }
59
60 /**
61 * Shows all entries for current user.
62 *
63 * @Route("/{username}/{token}/all.xml", name="all_rss", defaults={"_format"="xml"})
64 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
65 *
66 * @return \Symfony\Component\HttpFoundation\Response
67 */
68 public function showAllRSSAction(Request $request, User $user)
69 {
70 return $this->showEntries('all', $user, $request->query->get('page', 1));
71 }
72
73 /**
74 * Shows entries associated to a tag for current user.
75 *
76 * @Route("/{username}/{token}/tags/{slug}.xml", name="tag_rss", defaults={"_format"="xml"})
77 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
78 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
79 *
80 * @return \Symfony\Component\HttpFoundation\Response
81 */
82 public function showTagsAction(Request $request, User $user, Tag $tag)
83 {
84 $page = $request->query->get('page', 1);
85
86 $url = $this->generateUrl(
87 'tag_rss',
88 [
89 'username' => $user->getUsername(),
90 'token' => $user->getConfig()->getRssToken(),
91 'slug' => $tag->getSlug(),
92 ],
93 UrlGeneratorInterface::ABSOLUTE_URL
94 );
95
96 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
97 $user->getId(),
98 $tag->getId()
99 );
100
101 $pagerAdapter = new ArrayAdapter($entriesByTag);
102
103 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
104 $pagerAdapter,
105 $user
106 );
107
108 if (null === $entries) {
109 throw $this->createNotFoundException('No entries found?');
110 }
111
112 try {
113 $entries->setCurrentPage($page);
114 } catch (OutOfRangeCurrentPageException $e) {
115 if ($page > 1) {
116 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
117 }
118 }
119
120 return $this->render(
121 '@WallabagCore/themes/common/Entry/entries.xml.twig',
122 [
123 'url_html' => $this->generateUrl('tag_entries', ['slug' => $tag->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),
124 'type' => 'tag (' . $tag->getLabel() . ')',
125 'url' => $url,
126 'entries' => $entries,
127 ],
128 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
129 );
130 }
131
132 /**
133 * Global method to retrieve entries depending on the given type
134 * It returns the response to be send.
135 *
136 * @param string $type Entries type: unread, starred or archive
137 * @param User $user
138 * @param int $page
139 *
140 * @return \Symfony\Component\HttpFoundation\Response
141 */
142 private function showEntries($type, User $user, $page = 1)
143 {
144 $repository = $this->get('wallabag_core.entry_repository');
145
146 switch ($type) {
147 case 'starred':
148 $qb = $repository->getBuilderForStarredByUser($user->getId());
149 break;
150 case 'archive':
151 $qb = $repository->getBuilderForArchiveByUser($user->getId());
152 break;
153 case 'unread':
154 $qb = $repository->getBuilderForUnreadByUser($user->getId());
155 break;
156 case 'all':
157 $qb = $repository->getBuilderForAllByUser($user->getId());
158 break;
159 default:
160 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
161 }
162
163 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
164 $entries = new Pagerfanta($pagerAdapter);
165
166 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
167 $entries->setMaxPerPage($perPage);
168
169 $url = $this->generateUrl(
170 $type . '_rss',
171 [
172 'username' => $user->getUsername(),
173 'token' => $user->getConfig()->getRssToken(),
174 ],
175 UrlGeneratorInterface::ABSOLUTE_URL
176 );
177
178 try {
179 $entries->setCurrentPage((int) $page);
180 } catch (OutOfRangeCurrentPageException $e) {
181 if ($page > 1) {
182 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
183 }
184 }
185
186 return $this->render(
187 '@WallabagCore/themes/common/Entry/entries.xml.twig',
188 [
189 'url_html' => $this->generateUrl($type, [], UrlGeneratorInterface::ABSOLUTE_URL),
190 'type' => $type,
191 'url' => $url,
192 'entries' => $entries,
193 ],
194 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
195 );
196 }
197 }