]>
Commit | Line | Data |
---|---|---|
0c83fd59 J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
18c38dff | 5 | use Pagerfanta\Adapter\ArrayAdapter; |
f808b016 | 6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
8670250a | 7 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; |
619cc453 | 8 | use Pagerfanta\Pagerfanta; |
0c83fd59 | 9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
619cc453 | 10 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
f808b016 | 11 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8670250a | 12 | use Symfony\Component\HttpFoundation\Request; |
18c38dff | 13 | use Symfony\Component\HttpFoundation\Response; |
f808b016 | 14 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
0c83fd59 | 15 | use Wallabag\CoreBundle\Entity\Entry; |
18c38dff | 16 | use Wallabag\CoreBundle\Entity\Tag; |
619cc453 | 17 | use Wallabag\UserBundle\Entity\User; |
0c83fd59 J |
18 | |
19 | class RssController extends Controller | |
20 | { | |
21 | /** | |
4346a860 | 22 | * Shows unread entries for current user. |
0c83fd59 | 23 | * |
47e2d609 | 24 | * @Route("/feed/{username}/{token}/unread/{page}", name="unread_rss", defaults={"page": 1}) |
1210dae1 | 25 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") |
0c83fd59 | 26 | * |
47e2d609 TC |
27 | * @param User $user |
28 | * @param $page | |
0c83fd59 J |
29 | * @return \Symfony\Component\HttpFoundation\Response |
30 | */ | |
47e2d609 | 31 | public function showUnreadRSSAction(User $user, $page) |
0c83fd59 | 32 | { |
47e2d609 | 33 | return $this->showEntries('unread', $user, $page); |
0c83fd59 J |
34 | } |
35 | ||
36 | /** | |
4346a860 | 37 | * Shows read entries for current user. |
0c83fd59 | 38 | * |
47e2d609 | 39 | * @Route("/feed/{username}/{token}/archive/{page}", name="archive_rss", defaults={"page": 1}) |
1210dae1 | 40 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") |
0c83fd59 | 41 | * |
47e2d609 TC |
42 | * @param User $user |
43 | * @param $page | |
0c83fd59 J |
44 | * @return \Symfony\Component\HttpFoundation\Response |
45 | */ | |
47e2d609 | 46 | public function showArchiveRSSAction(User $user, $page) |
0c83fd59 | 47 | { |
47e2d609 | 48 | return $this->showEntries('archive', $user, $page); |
0c83fd59 J |
49 | } |
50 | ||
51 | /** | |
4346a860 | 52 | * Shows starred entries for current user. |
0c83fd59 | 53 | * |
47e2d609 | 54 | * @Route("/feed/{username}/{token}/starred/{page}", name="starred_rss", defaults={"page": 1}) |
1210dae1 | 55 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") |
0c83fd59 | 56 | * |
47e2d609 TC |
57 | * @param User $user |
58 | * @param $page | |
0c83fd59 J |
59 | * @return \Symfony\Component\HttpFoundation\Response |
60 | */ | |
47e2d609 | 61 | public function showStarredRSSAction(User $user, $page) |
0c83fd59 | 62 | { |
47e2d609 | 63 | return $this->showEntries('starred', $user, $page); |
0ab7404f JB |
64 | } |
65 | ||
bd40f1af TC |
66 | /** |
67 | * Shows all entries for current user. | |
68 | * | |
69 | * @Route("/{username}/{token}/all.xml", name="all_rss", defaults={"_format"="xml"}) | |
70 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
71 | * | |
72 | * @return \Symfony\Component\HttpFoundation\Response | |
73 | */ | |
74 | public function showAllRSSAction(Request $request, User $user) | |
75 | { | |
76 | return $this->showEntries('all', $user, $request->query->get('page', 1)); | |
77 | } | |
78 | ||
18c38dff JB |
79 | /** |
80 | * Shows entries associated to a tag for current user. | |
81 | * | |
82 | * @Route("/{username}/{token}/tags/{slug}.xml", name="tag_rss", defaults={"_format"="xml"}) | |
83 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
84 | * @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) | |
85 | * | |
86 | * @return \Symfony\Component\HttpFoundation\Response | |
87 | */ | |
88 | public function showTagsAction(Request $request, User $user, Tag $tag) | |
89 | { | |
90 | $page = $request->query->get('page', 1); | |
91 | ||
92 | $url = $this->generateUrl( | |
93 | 'tag_rss', | |
94 | [ | |
95 | 'username' => $user->getUsername(), | |
96 | 'token' => $user->getConfig()->getRssToken(), | |
97 | 'slug' => $tag->getSlug(), | |
98 | ], | |
99 | UrlGeneratorInterface::ABSOLUTE_URL | |
100 | ); | |
101 | ||
102 | $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId( | |
103 | $user->getId(), | |
104 | $tag->getId() | |
105 | ); | |
106 | ||
107 | $pagerAdapter = new ArrayAdapter($entriesByTag); | |
108 | ||
109 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare( | |
110 | $pagerAdapter, | |
111 | $user | |
112 | ); | |
113 | ||
114 | if (null === $entries) { | |
115 | throw $this->createNotFoundException('No entries found?'); | |
116 | } | |
117 | ||
118 | try { | |
119 | $entries->setCurrentPage($page); | |
120 | } catch (OutOfRangeCurrentPageException $e) { | |
121 | if ($page > 1) { | |
f808b016 | 122 | return $this->redirect($url . '?page=' . $entries->getNbPages(), 302); |
18c38dff JB |
123 | } |
124 | } | |
125 | ||
126 | return $this->render( | |
127 | '@WallabagCore/themes/common/Entry/entries.xml.twig', | |
128 | [ | |
129 | 'url_html' => $this->generateUrl('tag_entries', ['slug' => $tag->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL), | |
f808b016 | 130 | 'type' => 'tag (' . $tag->getLabel() . ')', |
18c38dff JB |
131 | 'url' => $url, |
132 | 'entries' => $entries, | |
133 | ], | |
134 | new Response('', 200, ['Content-Type' => 'application/rss+xml']) | |
135 | ); | |
136 | } | |
137 | ||
0ab7404f JB |
138 | /** |
139 | * Global method to retrieve entries depending on the given type | |
140 | * It returns the response to be send. | |
141 | * | |
142 | * @param string $type Entries type: unread, starred or archive | |
143 | * @param User $user | |
8670250a | 144 | * @param int $page |
0ab7404f JB |
145 | * |
146 | * @return \Symfony\Component\HttpFoundation\Response | |
147 | */ | |
8670250a | 148 | private function showEntries($type, User $user, $page = 1) |
0ab7404f | 149 | { |
25203e50 | 150 | $repository = $this->get('wallabag_core.entry_repository'); |
0ab7404f JB |
151 | |
152 | switch ($type) { | |
153 | case 'starred': | |
154 | $qb = $repository->getBuilderForStarredByUser($user->getId()); | |
155 | break; | |
0ab7404f JB |
156 | case 'archive': |
157 | $qb = $repository->getBuilderForArchiveByUser($user->getId()); | |
158 | break; | |
0ab7404f JB |
159 | case 'unread': |
160 | $qb = $repository->getBuilderForUnreadByUser($user->getId()); | |
161 | break; | |
bd40f1af TC |
162 | case 'all': |
163 | $qb = $repository->getBuilderForAllByUser($user->getId()); | |
164 | break; | |
0ab7404f JB |
165 | default: |
166 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
167 | } | |
0c83fd59 | 168 | |
5a5da369 | 169 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); |
26864574 NL |
170 | $entries = new Pagerfanta($pagerAdapter); |
171 | ||
67c99849 | 172 | $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit'); |
9fb6ac83 FG |
173 | $entries->setMaxPerPage($perPage); |
174 | ||
8670250a | 175 | $url = $this->generateUrl( |
f808b016 | 176 | $type . '_rss', |
8670250a JB |
177 | [ |
178 | 'username' => $user->getUsername(), | |
179 | 'token' => $user->getConfig()->getRssToken(), | |
180 | ], | |
181 | UrlGeneratorInterface::ABSOLUTE_URL | |
182 | ); | |
183 | ||
184 | try { | |
185 | $entries->setCurrentPage((int) $page); | |
186 | } catch (OutOfRangeCurrentPageException $e) { | |
187 | if ($page > 1) { | |
47e2d609 | 188 | return $this->redirect($url.'/'.$entries->getNbPages()); |
8670250a JB |
189 | } |
190 | } | |
191 | ||
47e2d609 TC |
192 | return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [ |
193 | 'type' => $type, | |
194 | 'url' => $url, | |
195 | 'entries' => $entries, | |
196 | 'user' => $user->getUsername(), | |
197 | 'domainName' => $this->getParameter('domain_name'), | |
198 | 'version' => $this->getParameter('wallabag_core.version'), | |
199 | ]); | |
0c83fd59 J |
200 | } |
201 | } |