]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Pagerfanta\Pagerfanta; | |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
10 | use Symfony\Component\HttpFoundation\Request; | |
11 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
12 | use Wallabag\CoreBundle\Entity\Entry; | |
13 | use Wallabag\UserBundle\Entity\User; | |
14 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
15 | ||
16 | class RssController extends Controller | |
17 | { | |
18 | /** | |
19 | * Shows unread entries for current user. | |
20 | * | |
21 | * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"}) | |
22 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
23 | * | |
24 | * @return \Symfony\Component\HttpFoundation\Response | |
25 | */ | |
26 | public function showUnreadAction(Request $request, User $user) | |
27 | { | |
28 | return $this->showEntries('unread', $user, $request->query->get('page', 1)); | |
29 | } | |
30 | ||
31 | /** | |
32 | * Shows read entries for current user. | |
33 | * | |
34 | * @Route("/{username}/{token}/archive.xml", name="archive_rss") | |
35 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
36 | * | |
37 | * @return \Symfony\Component\HttpFoundation\Response | |
38 | */ | |
39 | public function showArchiveAction(Request $request, User $user) | |
40 | { | |
41 | return $this->showEntries('archive', $user, $request->query->get('page', 1)); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Shows starred entries for current user. | |
46 | * | |
47 | * @Route("/{username}/{token}/starred.xml", name="starred_rss") | |
48 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
49 | * | |
50 | * @return \Symfony\Component\HttpFoundation\Response | |
51 | */ | |
52 | public function showStarredAction(Request $request, User $user) | |
53 | { | |
54 | return $this->showEntries('starred', $user, $request->query->get('page', 1)); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Global method to retrieve entries depending on the given type | |
59 | * It returns the response to be send. | |
60 | * | |
61 | * @param string $type Entries type: unread, starred or archive | |
62 | * @param User $user | |
63 | * @param int $page | |
64 | * | |
65 | * @return \Symfony\Component\HttpFoundation\Response | |
66 | */ | |
67 | private function showEntries($type, User $user, $page = 1) | |
68 | { | |
69 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | |
70 | ||
71 | switch ($type) { | |
72 | case 'starred': | |
73 | $qb = $repository->getBuilderForStarredByUser($user->getId()); | |
74 | break; | |
75 | ||
76 | case 'archive': | |
77 | $qb = $repository->getBuilderForArchiveByUser($user->getId()); | |
78 | break; | |
79 | ||
80 | case 'unread': | |
81 | $qb = $repository->getBuilderForUnreadByUser($user->getId()); | |
82 | break; | |
83 | ||
84 | default: | |
85 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
86 | } | |
87 | ||
88 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); | |
89 | $entries = new Pagerfanta($pagerAdapter); | |
90 | ||
91 | $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit'); | |
92 | $entries->setMaxPerPage($perPage); | |
93 | ||
94 | $url = $this->generateUrl( | |
95 | $type.'_rss', | |
96 | [ | |
97 | 'username' => $user->getUsername(), | |
98 | 'token' => $user->getConfig()->getRssToken(), | |
99 | ], | |
100 | UrlGeneratorInterface::ABSOLUTE_URL | |
101 | ); | |
102 | ||
103 | try { | |
104 | $entries->setCurrentPage((int) $page); | |
105 | } catch (OutOfRangeCurrentPageException $e) { | |
106 | if ($page > 1) { | |
107 | return $this->redirect($url.'?page='.$entries->getNbPages(), 302); | |
108 | } | |
109 | } | |
110 | ||
111 | return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [ | |
112 | 'type' => $type, | |
113 | 'url' => $url, | |
114 | 'entries' => $entries, | |
115 | ]); | |
116 | } | |
117 | } |