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\Tag
;
16 use Wallabag\UserBundle\Entity\User
;
18 class FeedController
extends Controller
21 * Shows unread entries for current user.
23 * @Route("/feed/{username}/{token}/unread/{page}", name="unread_feed", defaults={"page": 1})
24 * @Route("/{username}/{token}/unread.xml", defaults={"page": 1})
25 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
30 * @return \Symfony\Component\HttpFoundation\Response
32 public function showUnreadFeedAction(User
$user, $page)
34 return $this->showEntries('unread', $user, $page);
38 * Shows read entries for current user.
40 * @Route("/feed/{username}/{token}/archive/{page}", name="archive_feed", defaults={"page": 1})
41 * @Route("/{username}/{token}/archive.xml", defaults={"page": 1})
42 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
47 * @return \Symfony\Component\HttpFoundation\Response
49 public function showArchiveFeedAction(User
$user, $page)
51 return $this->showEntries('archive', $user, $page);
55 * Shows starred entries for current user.
57 * @Route("/feed/{username}/{token}/starred/{page}", name="starred_feed", defaults={"page": 1})
58 * @Route("/{username}/{token}/starred.xml", defaults={"page": 1})
59 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
64 * @return \Symfony\Component\HttpFoundation\Response
66 public function showStarredFeedAction(User
$user, $page)
68 return $this->showEntries('starred', $user, $page);
72 * Shows all entries for current user.
74 * @Route("/{username}/{token}/all.xml", name="all_feed", defaults={"_format"="xml"})
75 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
77 * @return \Symfony\Component\HttpFoundation\Response
79 public function showAllFeedAction(Request
$request, User
$user)
81 return $this->showEntries('all', $user, $request->query
->get('page', 1));
85 * Shows entries associated to a tag for current user.
87 * @Route("/{username}/{token}/tags/{slug}.xml", name="tag_feed", defaults={"_format"="xml"})
88 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
89 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
91 * @return \Symfony\Component\HttpFoundation\Response
93 public function showTagsFeedAction(Request
$request, User
$user, Tag
$tag)
95 $page = $request->query
->get('page', 1);
97 $url = $this->generateUrl(
100 'username' => $user->getUsername(),
101 'token' => $user->getConfig()->getFeedToken(),
102 'slug' => $tag->getSlug(),
104 UrlGeneratorInterface
::ABSOLUTE_URL
107 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
112 $pagerAdapter = new ArrayAdapter($entriesByTag);
114 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
119 if (null === $entries) {
120 throw $this->createNotFoundException('No entries found?');
124 $entries->setCurrentPage($page);
125 } catch (OutOfRangeCurrentPageException
$e) {
127 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
131 return $this->render(
132 '@WallabagCore/themes/common/Entry/entries.xml.twig',
136 'entries' => $entries,
137 'user' => $user->getUsername(),
138 'domainName' => $this->getParameter('domain_name'),
139 'version' => $this->getParameter('wallabag_core.version'),
140 'tag' => $tag->getSlug(),
142 new Response('', 200, ['Content-Type' => 'application/atom+xml'])
147 * Global method to retrieve entries depending on the given type
148 * It returns the response to be send.
150 * @param string $type Entries type: unread, starred or archive
154 * @return \Symfony\Component\HttpFoundation\Response
156 private function showEntries($type, User
$user, $page = 1)
158 $repository = $this->get('wallabag_core.entry_repository');
162 $qb = $repository->getBuilderForStarredByUser($user->getId());
165 $qb = $repository->getBuilderForArchiveByUser($user->getId());
168 $qb = $repository->getBuilderForUnreadByUser($user->getId());
171 $qb = $repository->getBuilderForAllByUser($user->getId());
174 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
177 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
178 $entries = new Pagerfanta($pagerAdapter);
180 $perPage = $user->getConfig()->getFeedLimit() ?: $this->getParameter('wallabag_core.Feed_limit');
181 $entries->setMaxPerPage($perPage);
183 $url = $this->generateUrl(
186 'username' => $user->getUsername(),
187 'token' => $user->getConfig()->getFeedToken(),
189 UrlGeneratorInterface
::ABSOLUTE_URL
193 $entries->setCurrentPage((int) $page);
194 } catch (OutOfRangeCurrentPageException
$e) {
196 return $this->redirect($url.'/'.$entries->getNbPages());
200 return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [
203 'entries' => $entries,
204 'user' => $user->getUsername(),
205 'domainName' => $this->getParameter('domain_name'),
206 'version' => $this->getParameter('wallabag_core.version'),
208 new Response('', 200, ['Content-Type' => 'application/atom+xml'])