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