3 namespace Wallabag\CoreBundle\Controller
;
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
;
19 class RssController
extends Controller
22 * Shows unread entries for current user.
24 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
25 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
27 * @return \Symfony\Component\HttpFoundation\Response
29 public function showUnreadRSSAction(Request
$request, User
$user)
31 return $this->showEntries('unread', $user, $request->query
->get('page', 1));
35 * Shows read entries for current user.
37 * @Route("/{username}/{token}/archive.xml", name="archive_rss", defaults={"_format"="xml"})
38 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
40 * @return \Symfony\Component\HttpFoundation\Response
42 public function showArchiveRSSAction(Request
$request, User
$user)
44 return $this->showEntries('archive', $user, $request->query
->get('page', 1));
48 * Shows starred entries for current user.
50 * @Route("/{username}/{token}/starred.xml", name="starred_rss", defaults={"_format"="xml"})
51 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
53 * @return \Symfony\Component\HttpFoundation\Response
55 public function showStarredRSSAction(Request
$request, User
$user)
57 return $this->showEntries('starred', $user, $request->query
->get('page', 1));
61 * Shows all entries for current user.
63 * @Route("/{username}/{token}/all.xml", name="all_rss", defaults={"_format"="xml"})
64 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
66 * @return \Symfony\Component\HttpFoundation\Response
68 public function showAllRSSAction(Request
$request, User
$user)
70 return $this->showEntries('all', $user, $request->query
->get('page', 1));
74 * Shows entries associated to a tag for current user.
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"}})
80 * @return \Symfony\Component\HttpFoundation\Response
82 public function showTagsAction(Request
$request, User
$user, Tag
$tag)
84 $page = $request->query
->get('page', 1);
86 $url = $this->generateUrl(
89 'username' => $user->getUsername(),
90 'token' => $user->getConfig()->getRssToken(),
91 'slug' => $tag->getSlug(),
93 UrlGeneratorInterface
::ABSOLUTE_URL
96 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
101 $pagerAdapter = new ArrayAdapter($entriesByTag);
103 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
108 if (null === $entries) {
109 throw $this->createNotFoundException('No entries found?');
113 $entries->setCurrentPage($page);
114 } catch (OutOfRangeCurrentPageException
$e) {
116 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
120 return $this->render(
121 '@WallabagCore/themes/common/Entry/entries.xml.twig',
123 'url_html' => $this->generateUrl('tag_entries', ['slug' => $tag->getSlug()], UrlGeneratorInterface
::ABSOLUTE_URL
),
124 'type' => 'tag (' . $tag->getLabel() . ')',
126 'entries' => $entries,
128 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
133 * Global method to retrieve entries depending on the given type
134 * It returns the response to be send.
136 * @param string $type Entries type: unread, starred or archive
140 * @return \Symfony\Component\HttpFoundation\Response
142 private function showEntries($type, User
$user, $page = 1)
144 $repository = $this->get('wallabag_core.entry_repository');
148 $qb = $repository->getBuilderForStarredByUser($user->getId());
151 $qb = $repository->getBuilderForArchiveByUser($user->getId());
154 $qb = $repository->getBuilderForUnreadByUser($user->getId());
157 $qb = $repository->getBuilderForAllByUser($user->getId());
160 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
163 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
164 $entries = new Pagerfanta($pagerAdapter);
166 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
167 $entries->setMaxPerPage($perPage);
169 $url = $this->generateUrl(
172 'username' => $user->getUsername(),
173 'token' => $user->getConfig()->getRssToken(),
175 UrlGeneratorInterface
::ABSOLUTE_URL
179 $entries->setCurrentPage((int) $page);
180 } catch (OutOfRangeCurrentPageException
$e) {
182 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
186 return $this->render(
187 '@WallabagCore/themes/common/Entry/entries.xml.twig',
189 'url_html' => $this->generateUrl($type, [], UrlGeneratorInterface
::ABSOLUTE_URL
),
192 'entries' => $entries,
194 new Response('', 200, ['Content-Type' => 'application/rss+xml'])