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