]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\ArrayAdapter; | |
6 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
7 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
8 | use Pagerfanta\Pagerfanta; | |
9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
10 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
11 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
12 | use Symfony\Component\HttpFoundation\Request; | |
13 | use Symfony\Component\HttpFoundation\Response; | |
14 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
15 | use Wallabag\CoreBundle\Entity\Entry; | |
16 | use Wallabag\CoreBundle\Entity\Tag; | |
17 | use Wallabag\UserBundle\Entity\User; | |
18 | ||
19 | class RssController extends Controller | |
20 | { | |
21 | /** | |
22 | * Shows unread entries for current user. | |
23 | * | |
24 | * @Route("/feed/{username}/{token}/unread/{page}", name="unread_rss", defaults={"page": 1}) | |
25 | * @Route("/{username}/{token}/unread.xml", defaults={"page": 1}) | |
26 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
27 | * | |
28 | * @param User $user | |
29 | * @param $page | |
30 | * | |
31 | * @return \Symfony\Component\HttpFoundation\Response | |
32 | */ | |
33 | public function showUnreadRSSAction(User $user, $page) | |
34 | { | |
35 | return $this->showEntries('unread', $user, $page); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Shows read entries for current user. | |
40 | * | |
41 | * @Route("/feed/{username}/{token}/archive/{page}", name="archive_rss", defaults={"page": 1}) | |
42 | * @Route("/{username}/{token}/archive.xml", defaults={"page": 1}) | |
43 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
44 | * | |
45 | * @param User $user | |
46 | * @param $page | |
47 | * | |
48 | * @return \Symfony\Component\HttpFoundation\Response | |
49 | */ | |
50 | public function showArchiveRSSAction(User $user, $page) | |
51 | { | |
52 | return $this->showEntries('archive', $user, $page); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Shows starred entries for current user. | |
57 | * | |
58 | * @Route("/feed/{username}/{token}/starred/{page}", name="starred_rss", defaults={"page": 1}) | |
59 | * @Route("/{username}/{token}/starred.xml", defaults={"page": 1}) | |
60 | * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter") | |
61 | * | |
62 | * @param User $user | |
63 | * @param $page | |
64 | * | |
65 | * @return \Symfony\Component\HttpFoundation\Response | |
66 | */ | |
67 | public function showStarredRSSAction(User $user, $page) | |
68 | { | |
69 | return $this->showEntries('starred', $user, $page); | |
70 | } | |
71 | ||
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 | ||
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) { | |
128 | return $this->redirect($url . '?page=' . $entries->getNbPages(), 302); | |
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), | |
136 | 'type' => 'tag (' . $tag->getLabel() . ')', | |
137 | 'url' => $url, | |
138 | 'entries' => $entries, | |
139 | ], | |
140 | new Response('', 200, ['Content-Type' => 'application/rss+xml']) | |
141 | ); | |
142 | } | |
143 | ||
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 | |
150 | * @param int $page | |
151 | * | |
152 | * @return \Symfony\Component\HttpFoundation\Response | |
153 | */ | |
154 | private function showEntries($type, User $user, $page = 1) | |
155 | { | |
156 | $repository = $this->get('wallabag_core.entry_repository'); | |
157 | ||
158 | switch ($type) { | |
159 | case 'starred': | |
160 | $qb = $repository->getBuilderForStarredByUser($user->getId()); | |
161 | break; | |
162 | case 'archive': | |
163 | $qb = $repository->getBuilderForArchiveByUser($user->getId()); | |
164 | break; | |
165 | case 'unread': | |
166 | $qb = $repository->getBuilderForUnreadByUser($user->getId()); | |
167 | break; | |
168 | case 'all': | |
169 | $qb = $repository->getBuilderForAllByUser($user->getId()); | |
170 | break; | |
171 | default: | |
172 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
173 | } | |
174 | ||
175 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); | |
176 | $entries = new Pagerfanta($pagerAdapter); | |
177 | ||
178 | $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit'); | |
179 | $entries->setMaxPerPage($perPage); | |
180 | ||
181 | $url = $this->generateUrl( | |
182 | $type . '_rss', | |
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) { | |
194 | return $this->redirect($url.'/'.$entries->getNbPages()); | |
195 | } | |
196 | } | |
197 | ||
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 | ]); | |
206 | } | |
207 | } |