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