]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/FeedController.php
Added URL compatibility
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / FeedController.php
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\Tag;
16 use Wallabag\UserBundle\Entity\User;
17
18 class FeedController extends Controller
19 {
20 /**
21 * Shows unread entries for current user.
22 *
23 * @Route("/feed/{username}/{token}/unread/{page}", name="unread_feed", defaults={"page": 1})
24 * @Route("/{username}/{token}/unread.xml", defaults={"page": 1})
25 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
26 *
27 * @param User $user
28 * @param $page
29 *
30 * @return \Symfony\Component\HttpFoundation\Response
31 */
32 public function showUnreadFeedAction(User $user, $page)
33 {
34 return $this->showEntries('unread', $user, $page);
35 }
36
37 /**
38 * Shows read entries for current user.
39 *
40 * @Route("/feed/{username}/{token}/archive/{page}", name="archive_feed", defaults={"page": 1})
41 * @Route("/{username}/{token}/archive.xml", defaults={"page": 1})
42 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
43 *
44 * @param User $user
45 * @param $page
46 *
47 * @return \Symfony\Component\HttpFoundation\Response
48 */
49 public function showArchiveFeedAction(User $user, $page)
50 {
51 return $this->showEntries('archive', $user, $page);
52 }
53
54 /**
55 * Shows starred entries for current user.
56 *
57 * @Route("/feed/{username}/{token}/starred/{page}", name="starred_feed", defaults={"page": 1})
58 * @Route("/{username}/{token}/starred.xml", defaults={"page": 1})
59 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
60 *
61 * @param User $user
62 * @param $page
63 *
64 * @return \Symfony\Component\HttpFoundation\Response
65 */
66 public function showStarredFeedAction(User $user, $page)
67 {
68 return $this->showEntries('starred', $user, $page);
69 }
70
71 /**
72 * Shows all entries for current user.
73 *
74 * @Route("/{username}/{token}/all.xml", name="all_feed", defaults={"_format"="xml"})
75 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
76 *
77 * @return \Symfony\Component\HttpFoundation\Response
78 */
79 public function showAllFeedAction(Request $request, User $user)
80 {
81 return $this->showEntries('all', $user, $request->query->get('page', 1));
82 }
83
84 /**
85 * Shows entries associated to a tag for current user.
86 *
87 * @Route("/{username}/{token}/tags/{slug}.xml", name="tag_feed", defaults={"_format"="xml"})
88 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
89 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
90 *
91 * @return \Symfony\Component\HttpFoundation\Response
92 */
93 public function showTagsFeedAction(Request $request, User $user, Tag $tag)
94 {
95 $page = $request->query->get('page', 1);
96
97 $url = $this->generateUrl(
98 'tag_feed',
99 [
100 'username' => $user->getUsername(),
101 'token' => $user->getConfig()->getFeedToken(),
102 'slug' => $tag->getSlug(),
103 ],
104 UrlGeneratorInterface::ABSOLUTE_URL
105 );
106
107 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
108 $user->getId(),
109 $tag->getId()
110 );
111
112 $pagerAdapter = new ArrayAdapter($entriesByTag);
113
114 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
115 $pagerAdapter,
116 $user
117 );
118
119 if (null === $entries) {
120 throw $this->createNotFoundException('No entries found?');
121 }
122
123 try {
124 $entries->setCurrentPage($page);
125 } catch (OutOfRangeCurrentPageException $e) {
126 if ($page > 1) {
127 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
128 }
129 }
130
131 return $this->render(
132 '@WallabagCore/themes/common/Entry/entries.xml.twig',
133 [
134 'type' => 'tag',
135 'url' => $url,
136 'entries' => $entries,
137 'user' => $user->getUsername(),
138 'domainName' => $this->getParameter('domain_name'),
139 'version' => $this->getParameter('wallabag_core.version'),
140 'tag' => $tag->getSlug(),
141 ],
142 new Response('', 200, ['Content-Type' => 'application/atom+xml'])
143 );
144 }
145
146 /**
147 * Global method to retrieve entries depending on the given type
148 * It returns the response to be send.
149 *
150 * @param string $type Entries type: unread, starred or archive
151 * @param User $user
152 * @param int $page
153 *
154 * @return \Symfony\Component\HttpFoundation\Response
155 */
156 private function showEntries($type, User $user, $page = 1)
157 {
158 $repository = $this->get('wallabag_core.entry_repository');
159
160 switch ($type) {
161 case 'starred':
162 $qb = $repository->getBuilderForStarredByUser($user->getId());
163 break;
164 case 'archive':
165 $qb = $repository->getBuilderForArchiveByUser($user->getId());
166 break;
167 case 'unread':
168 $qb = $repository->getBuilderForUnreadByUser($user->getId());
169 break;
170 case 'all':
171 $qb = $repository->getBuilderForAllByUser($user->getId());
172 break;
173 default:
174 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
175 }
176
177 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
178 $entries = new Pagerfanta($pagerAdapter);
179
180 $perPage = $user->getConfig()->getFeedLimit() ?: $this->getParameter('wallabag_core.Feed_limit');
181 $entries->setMaxPerPage($perPage);
182
183 $url = $this->generateUrl(
184 $type . '_feed',
185 [
186 'username' => $user->getUsername(),
187 'token' => $user->getConfig()->getFeedToken(),
188 ],
189 UrlGeneratorInterface::ABSOLUTE_URL
190 );
191
192 try {
193 $entries->setCurrentPage((int) $page);
194 } catch (OutOfRangeCurrentPageException $e) {
195 if ($page > 1) {
196 return $this->redirect($url.'/'.$entries->getNbPages());
197 }
198 }
199
200 return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [
201 'type' => $type,
202 'url' => $url,
203 'entries' => $entries,
204 'user' => $user->getUsername(),
205 'domainName' => $this->getParameter('domain_name'),
206 'version' => $this->getParameter('wallabag_core.version'),
207 ],
208 new Response('', 200, ['Content-Type' => 'application/atom+xml'])
209 );
210 }
211 }