]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
8 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
10 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
11 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
12 | use Wallabag\CoreBundle\Filter\EntryFilterType; | |
13 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
14 | use Pagerfanta\Pagerfanta; | |
15 | ||
16 | class EntryController extends Controller | |
17 | { | |
18 | /** | |
19 | * @param Entry $entry | |
20 | */ | |
21 | private function updateEntry(Entry $entry) | |
22 | { | |
23 | try { | |
24 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
25 | $em = $this->getDoctrine()->getManager(); | |
26 | $em->persist($entry); | |
27 | $em->flush(); | |
28 | } catch (\Exception $e) { | |
29 | return false; | |
30 | } | |
31 | ||
32 | return true; | |
33 | } | |
34 | ||
35 | /** | |
36 | * @param Request $request | |
37 | * | |
38 | * @Route("/new-entry", name="new_entry") | |
39 | * | |
40 | * @return \Symfony\Component\HttpFoundation\Response | |
41 | */ | |
42 | public function addEntryFormAction(Request $request) | |
43 | { | |
44 | $entry = new Entry($this->getUser()); | |
45 | ||
46 | $form = $this->createForm(new NewEntryType(), $entry); | |
47 | ||
48 | $form->handleRequest($request); | |
49 | ||
50 | if ($form->isValid()) { | |
51 | $this->updateEntry($entry); | |
52 | $this->get('session')->getFlashBag()->add( | |
53 | 'notice', | |
54 | 'Entry saved' | |
55 | ); | |
56 | ||
57 | return $this->redirect($this->generateUrl('homepage')); | |
58 | } | |
59 | ||
60 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( | |
61 | 'form' => $form->createView(), | |
62 | )); | |
63 | } | |
64 | ||
65 | /** | |
66 | * @param Request $request | |
67 | * | |
68 | * @Route("/bookmarklet", name="bookmarklet") | |
69 | * | |
70 | * @return \Symfony\Component\HttpFoundation\Response | |
71 | */ | |
72 | public function addEntryViaBookmarklet(Request $request) | |
73 | { | |
74 | $entry = new Entry($this->getUser()); | |
75 | $entry->setUrl($request->get('url')); | |
76 | $this->updateEntry($entry); | |
77 | ||
78 | return $this->redirect($this->generateUrl('homepage')); | |
79 | } | |
80 | ||
81 | /** | |
82 | * @param Request $request | |
83 | * | |
84 | * @Route("/new", name="new") | |
85 | * | |
86 | * @return \Symfony\Component\HttpFoundation\Response | |
87 | */ | |
88 | public function addEntryAction(Request $request) | |
89 | { | |
90 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Edit an entry content. | |
95 | * | |
96 | * @param Request $request | |
97 | * @param Entry $entry | |
98 | * | |
99 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
100 | * | |
101 | * @return \Symfony\Component\HttpFoundation\Response | |
102 | */ | |
103 | public function editEntryAction(Request $request, Entry $entry) | |
104 | { | |
105 | $this->checkUserAction($entry); | |
106 | ||
107 | $form = $this->createForm(new EditEntryType(), $entry); | |
108 | ||
109 | $form->handleRequest($request); | |
110 | ||
111 | if ($form->isValid()) { | |
112 | $em = $this->getDoctrine()->getManager(); | |
113 | $em->persist($entry); | |
114 | $em->flush(); | |
115 | ||
116 | $this->get('session')->getFlashBag()->add( | |
117 | 'notice', | |
118 | 'Entry updated' | |
119 | ); | |
120 | ||
121 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
122 | } | |
123 | ||
124 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
125 | 'form' => $form->createView(), | |
126 | )); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Shows all entries for current user. | |
131 | * | |
132 | * @param Request $request | |
133 | * @param int $page | |
134 | * | |
135 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
136 | * | |
137 | * @return \Symfony\Component\HttpFoundation\Response | |
138 | */ | |
139 | public function showAllAction(Request $request, $page) | |
140 | { | |
141 | return $this->showEntries('all', $request, $page); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Shows unread entries for current user. | |
146 | * | |
147 | * @param Request $request | |
148 | * @param int $page | |
149 | * | |
150 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
151 | * | |
152 | * @return \Symfony\Component\HttpFoundation\Response | |
153 | */ | |
154 | public function showUnreadAction(Request $request, $page) | |
155 | { | |
156 | return $this->showEntries('unread', $request, $page); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Shows read entries for current user. | |
161 | * | |
162 | * @param Request $request | |
163 | * @param int $page | |
164 | * | |
165 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
166 | * | |
167 | * @return \Symfony\Component\HttpFoundation\Response | |
168 | */ | |
169 | public function showArchiveAction(Request $request, $page) | |
170 | { | |
171 | return $this->showEntries('archive', $request, $page); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Shows starred entries for current user. | |
176 | * | |
177 | * @param Request $request | |
178 | * @param int $page | |
179 | * | |
180 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
181 | * | |
182 | * @return \Symfony\Component\HttpFoundation\Response | |
183 | */ | |
184 | public function showStarredAction(Request $request, $page) | |
185 | { | |
186 | return $this->showEntries('starred', $request, $page); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Global method to retrieve entries depending on the given type | |
191 | * It returns the response to be send. | |
192 | * | |
193 | * @param string $type Entries type: unread, starred or archive | |
194 | * @param Request $request | |
195 | * @param int $page | |
196 | * | |
197 | * @return \Symfony\Component\HttpFoundation\Response | |
198 | */ | |
199 | private function showEntries($type, Request $request, $page) | |
200 | { | |
201 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | |
202 | ||
203 | switch ($type) { | |
204 | case 'starred': | |
205 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
206 | break; | |
207 | ||
208 | case 'archive': | |
209 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
210 | break; | |
211 | ||
212 | case 'unread': | |
213 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
214 | break; | |
215 | ||
216 | case 'all': | |
217 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
218 | break; | |
219 | ||
220 | default: | |
221 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
222 | } | |
223 | ||
224 | $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser())); | |
225 | ||
226 | if ($request->query->has($form->getName())) { | |
227 | // manually bind values from the request | |
228 | $form->submit($request->query->get($form->getName())); | |
229 | ||
230 | // build the query from the given form object | |
231 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
232 | } | |
233 | ||
234 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
235 | $entries = new Pagerfanta($pagerAdapter); | |
236 | ||
237 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
238 | $entries->setCurrentPage($page); | |
239 | ||
240 | return $this->render( | |
241 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
242 | array( | |
243 | 'form' => $form->createView(), | |
244 | 'entries' => $entries, | |
245 | 'currentPage' => $page, | |
246 | ) | |
247 | ); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Shows entry content. | |
252 | * | |
253 | * @param Entry $entry | |
254 | * | |
255 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
256 | * | |
257 | * @return \Symfony\Component\HttpFoundation\Response | |
258 | */ | |
259 | public function viewAction(Entry $entry) | |
260 | { | |
261 | $this->checkUserAction($entry); | |
262 | ||
263 | return $this->render( | |
264 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
265 | array('entry' => $entry) | |
266 | ); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Reload an entry. | |
271 | * Refetch content from the website and make it readable again. | |
272 | * | |
273 | * @param Entry $entry | |
274 | * | |
275 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
276 | * | |
277 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
278 | */ | |
279 | public function reloadAction(Entry $entry) | |
280 | { | |
281 | $this->checkUserAction($entry); | |
282 | ||
283 | $message = 'Entry reloaded'; | |
284 | if (false === $this->updateEntry($entry)) { | |
285 | $message = 'Failed to reload entry'; | |
286 | } | |
287 | ||
288 | $this->get('session')->getFlashBag()->add( | |
289 | 'notice', | |
290 | $message | |
291 | ); | |
292 | ||
293 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
294 | } | |
295 | ||
296 | /** | |
297 | * Changes read status for an entry. | |
298 | * | |
299 | * @param Request $request | |
300 | * @param Entry $entry | |
301 | * | |
302 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
303 | * | |
304 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
305 | */ | |
306 | public function toggleArchiveAction(Request $request, Entry $entry) | |
307 | { | |
308 | $this->checkUserAction($entry); | |
309 | ||
310 | $entry->toggleArchive(); | |
311 | $this->getDoctrine()->getManager()->flush(); | |
312 | ||
313 | $this->get('session')->getFlashBag()->add( | |
314 | 'notice', | |
315 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') | |
316 | ); | |
317 | ||
318 | return $this->redirect($request->headers->get('referer')); | |
319 | } | |
320 | ||
321 | /** | |
322 | * Changes favorite status for an entry. | |
323 | * | |
324 | * @param Request $request | |
325 | * @param Entry $entry | |
326 | * | |
327 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
328 | * | |
329 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
330 | */ | |
331 | public function toggleStarAction(Request $request, Entry $entry) | |
332 | { | |
333 | $this->checkUserAction($entry); | |
334 | ||
335 | $entry->toggleStar(); | |
336 | $this->getDoctrine()->getManager()->flush(); | |
337 | ||
338 | $this->get('session')->getFlashBag()->add( | |
339 | 'notice', | |
340 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') | |
341 | ); | |
342 | ||
343 | return $this->redirect($request->headers->get('referer')); | |
344 | } | |
345 | ||
346 | /** | |
347 | * Deletes entry and redirect to the homepage or the last viewed page. | |
348 | * | |
349 | * @param Entry $entry | |
350 | * | |
351 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
352 | * | |
353 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
354 | */ | |
355 | public function deleteEntryAction(Request $request, Entry $entry) | |
356 | { | |
357 | $this->checkUserAction($entry); | |
358 | ||
359 | // generates the view url for this entry to check for redirection later | |
360 | // to avoid redirecting to the deleted entry. Ugh. | |
361 | $url = $this->generateUrl( | |
362 | 'view', | |
363 | array('id' => $entry->getId()), | |
364 | UrlGeneratorInterface::ABSOLUTE_URL | |
365 | ); | |
366 | ||
367 | $em = $this->getDoctrine()->getManager(); | |
368 | $em->remove($entry); | |
369 | $em->flush(); | |
370 | ||
371 | $this->get('session')->getFlashBag()->add( | |
372 | 'notice', | |
373 | 'Entry deleted' | |
374 | ); | |
375 | ||
376 | // don't redirect user to the deleted entry | |
377 | return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); | |
378 | } | |
379 | ||
380 | /** | |
381 | * Check if the logged user can manage the given entry. | |
382 | * | |
383 | * @param Entry $entry | |
384 | */ | |
385 | private function checkUserAction(Entry $entry) | |
386 | { | |
387 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
388 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
389 | } | |
390 | } | |
391 | } |