]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/RssController.php
Moved total messages from import
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
CommitLineData
0c83fd59
J
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
619cc453 5use Pagerfanta\Adapter\DoctrineORMAdapter;
8670250a 6use Pagerfanta\Exception\OutOfRangeCurrentPageException;
619cc453 7use Pagerfanta\Pagerfanta;
0c83fd59 8use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
619cc453 9use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8670250a 10use Symfony\Component\HttpFoundation\Request;
0c83fd59 11use Symfony\Bundle\FrameworkBundle\Controller\Controller;
0c83fd59 12use Wallabag\CoreBundle\Entity\Entry;
619cc453 13use Wallabag\UserBundle\Entity\User;
8670250a 14use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
0c83fd59
J
15
16class RssController extends Controller
17{
18 /**
4346a860 19 * Shows unread entries for current user.
0c83fd59
J
20 *
21 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
1210dae1 22 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
23 *
24 * @return \Symfony\Component\HttpFoundation\Response
25 */
8670250a 26 public function showUnreadAction(Request $request, User $user)
0c83fd59 27 {
8670250a 28 return $this->showEntries('unread', $user, $request->query->get('page', 1));
0c83fd59
J
29 }
30
31 /**
4346a860 32 * Shows read entries for current user.
0c83fd59
J
33 *
34 * @Route("/{username}/{token}/archive.xml", name="archive_rss")
1210dae1 35 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
36 *
37 * @return \Symfony\Component\HttpFoundation\Response
38 */
8670250a 39 public function showArchiveAction(Request $request, User $user)
0c83fd59 40 {
8670250a 41 return $this->showEntries('archive', $user, $request->query->get('page', 1));
0c83fd59
J
42 }
43
44 /**
4346a860 45 * Shows starred entries for current user.
0c83fd59
J
46 *
47 * @Route("/{username}/{token}/starred.xml", name="starred_rss")
1210dae1 48 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
49 *
50 * @return \Symfony\Component\HttpFoundation\Response
51 */
8670250a 52 public function showStarredAction(Request $request, User $user)
0c83fd59 53 {
8670250a 54 return $this->showEntries('starred', $user, $request->query->get('page', 1));
0ab7404f
JB
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
8670250a 63 * @param int $page
0ab7404f
JB
64 *
65 * @return \Symfony\Component\HttpFoundation\Response
66 */
8670250a 67 private function showEntries($type, User $user, $page = 1)
0ab7404f
JB
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 }
0c83fd59 87
26864574
NL
88 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
89 $entries = new Pagerfanta($pagerAdapter);
90
67c99849 91 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
9fb6ac83
FG
92 $entries->setMaxPerPage($perPage);
93
8670250a
JB
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
2ff9991a 111 return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [
0ab7404f 112 'type' => $type,
8670250a 113 'url' => $url,
0c83fd59 114 'entries' => $entries,
4094ea47 115 ]);
0c83fd59
J
116 }
117}