]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/RssController.php
Correction indent. L538
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
CommitLineData
0c83fd59
J
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
18c38dff 5use Pagerfanta\Adapter\ArrayAdapter;
f808b016 6use Pagerfanta\Adapter\DoctrineORMAdapter;
8670250a 7use Pagerfanta\Exception\OutOfRangeCurrentPageException;
619cc453 8use Pagerfanta\Pagerfanta;
0c83fd59 9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
619cc453 10use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
f808b016 11use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8670250a 12use Symfony\Component\HttpFoundation\Request;
18c38dff 13use Symfony\Component\HttpFoundation\Response;
f808b016 14use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
0c83fd59 15use Wallabag\CoreBundle\Entity\Entry;
18c38dff 16use Wallabag\CoreBundle\Entity\Tag;
619cc453 17use Wallabag\UserBundle\Entity\User;
0c83fd59
J
18
19class RssController extends Controller
20{
21 /**
4346a860 22 * Shows unread entries for current user.
0c83fd59
J
23 *
24 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
1210dae1 25 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
26 *
27 * @return \Symfony\Component\HttpFoundation\Response
28 */
bd40f1af 29 public function showUnreadRSSAction(Request $request, User $user)
0c83fd59 30 {
8670250a 31 return $this->showEntries('unread', $user, $request->query->get('page', 1));
0c83fd59
J
32 }
33
34 /**
4346a860 35 * Shows read entries for current user.
0c83fd59 36 *
18c38dff 37 * @Route("/{username}/{token}/archive.xml", name="archive_rss", defaults={"_format"="xml"})
1210dae1 38 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
39 *
40 * @return \Symfony\Component\HttpFoundation\Response
41 */
bd40f1af 42 public function showArchiveRSSAction(Request $request, User $user)
0c83fd59 43 {
8670250a 44 return $this->showEntries('archive', $user, $request->query->get('page', 1));
0c83fd59
J
45 }
46
47 /**
4346a860 48 * Shows starred entries for current user.
0c83fd59 49 *
18c38dff 50 * @Route("/{username}/{token}/starred.xml", name="starred_rss", defaults={"_format"="xml"})
1210dae1 51 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
52 *
53 * @return \Symfony\Component\HttpFoundation\Response
54 */
bd40f1af 55 public function showStarredRSSAction(Request $request, User $user)
0c83fd59 56 {
8670250a 57 return $this->showEntries('starred', $user, $request->query->get('page', 1));
0ab7404f
JB
58 }
59
bd40f1af
TC
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
18c38dff
JB
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) {
f808b016 116 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
18c38dff
JB
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),
f808b016 124 'type' => 'tag (' . $tag->getLabel() . ')',
18c38dff
JB
125 'url' => $url,
126 'entries' => $entries,
127 ],
128 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
129 );
130 }
131
0ab7404f
JB
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
8670250a 138 * @param int $page
0ab7404f
JB
139 *
140 * @return \Symfony\Component\HttpFoundation\Response
141 */
8670250a 142 private function showEntries($type, User $user, $page = 1)
0ab7404f 143 {
25203e50 144 $repository = $this->get('wallabag_core.entry_repository');
0ab7404f
JB
145
146 switch ($type) {
147 case 'starred':
148 $qb = $repository->getBuilderForStarredByUser($user->getId());
149 break;
0ab7404f
JB
150 case 'archive':
151 $qb = $repository->getBuilderForArchiveByUser($user->getId());
152 break;
0ab7404f
JB
153 case 'unread':
154 $qb = $repository->getBuilderForUnreadByUser($user->getId());
155 break;
bd40f1af
TC
156 case 'all':
157 $qb = $repository->getBuilderForAllByUser($user->getId());
158 break;
0ab7404f
JB
159 default:
160 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
161 }
0c83fd59 162
5a5da369 163 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
26864574
NL
164 $entries = new Pagerfanta($pagerAdapter);
165
67c99849 166 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
9fb6ac83
FG
167 $entries->setMaxPerPage($perPage);
168
8670250a 169 $url = $this->generateUrl(
f808b016 170 $type . '_rss',
8670250a
JB
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) {
f808b016 182 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
8670250a
JB
183 }
184 }
185
18c38dff
JB
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 );
0c83fd59
J
196 }
197}