]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
11 | use Wallabag\CoreBundle\Entity\Entry; | |
12 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | |
13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
15 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | |
16 | ||
17 | class EntryController extends Controller | |
18 | { | |
19 | /** | |
20 | * @param Entry $entry | |
21 | */ | |
22 | private function updateEntry(Entry $entry) | |
23 | { | |
24 | try { | |
25 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
26 | ||
27 | $em = $this->getDoctrine()->getManager(); | |
28 | $em->persist($entry); | |
29 | $em->flush(); | |
30 | } catch (\Exception $e) { | |
31 | $this->get('logger')->error('Error while saving an entry', [ | |
32 | 'exception' => $e, | |
33 | 'entry' => $entry, | |
34 | ]); | |
35 | ||
36 | return false; | |
37 | } | |
38 | ||
39 | return true; | |
40 | } | |
41 | ||
42 | /** | |
43 | * @param Request $request | |
44 | * | |
45 | * @Route("/new-entry", name="new_entry") | |
46 | * | |
47 | * @return \Symfony\Component\HttpFoundation\Response | |
48 | */ | |
49 | public function addEntryFormAction(Request $request) | |
50 | { | |
51 | $entry = new Entry($this->getUser()); | |
52 | ||
53 | $form = $this->createForm(NewEntryType::class, $entry); | |
54 | ||
55 | $form->handleRequest($request); | |
56 | ||
57 | if ($form->isValid()) { | |
58 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); | |
59 | ||
60 | if (false !== $existingEntry) { | |
61 | $this->get('session')->getFlashBag()->add( | |
62 | 'notice', | |
63 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) | |
64 | ); | |
65 | ||
66 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); | |
67 | } | |
68 | ||
69 | $message = 'flashes.entry.notice.entry_saved'; | |
70 | if (false === $this->updateEntry($entry)) { | |
71 | $message = 'flashes.entry.notice.entry_saved_failed'; | |
72 | } | |
73 | ||
74 | $this->get('session')->getFlashBag()->add('notice', $message); | |
75 | ||
76 | return $this->redirect($this->generateUrl('homepage')); | |
77 | } | |
78 | ||
79 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [ | |
80 | 'form' => $form->createView(), | |
81 | ]); | |
82 | } | |
83 | ||
84 | /** | |
85 | * @param Request $request | |
86 | * | |
87 | * @Route("/bookmarklet", name="bookmarklet") | |
88 | * | |
89 | * @return \Symfony\Component\HttpFoundation\Response | |
90 | */ | |
91 | public function addEntryViaBookmarkletAction(Request $request) | |
92 | { | |
93 | $entry = new Entry($this->getUser()); | |
94 | $entry->setUrl($request->get('url')); | |
95 | ||
96 | if (false === $this->checkIfEntryAlreadyExists($entry)) { | |
97 | $this->updateEntry($entry); | |
98 | } | |
99 | ||
100 | return $this->redirect($this->generateUrl('homepage')); | |
101 | } | |
102 | ||
103 | /** | |
104 | * @Route("/new", name="new") | |
105 | * | |
106 | * @return \Symfony\Component\HttpFoundation\Response | |
107 | */ | |
108 | public function addEntryAction() | |
109 | { | |
110 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Edit an entry content. | |
115 | * | |
116 | * @param Request $request | |
117 | * @param Entry $entry | |
118 | * | |
119 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
120 | * | |
121 | * @return \Symfony\Component\HttpFoundation\Response | |
122 | */ | |
123 | public function editEntryAction(Request $request, Entry $entry) | |
124 | { | |
125 | $this->checkUserAction($entry); | |
126 | ||
127 | $form = $this->createForm(EditEntryType::class, $entry); | |
128 | ||
129 | $form->handleRequest($request); | |
130 | ||
131 | if ($form->isValid()) { | |
132 | $em = $this->getDoctrine()->getManager(); | |
133 | $em->persist($entry); | |
134 | $em->flush(); | |
135 | ||
136 | $this->get('session')->getFlashBag()->add( | |
137 | 'notice', | |
138 | 'flashes.entry.notice.entry_updated' | |
139 | ); | |
140 | ||
141 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
142 | } | |
143 | ||
144 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ | |
145 | 'form' => $form->createView(), | |
146 | ]); | |
147 | } | |
148 | ||
149 | /** | |
150 | * Shows all entries for current user. | |
151 | * | |
152 | * @param Request $request | |
153 | * @param int $page | |
154 | * | |
155 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
156 | * | |
157 | * @return \Symfony\Component\HttpFoundation\Response | |
158 | */ | |
159 | public function showAllAction(Request $request, $page) | |
160 | { | |
161 | return $this->showEntries('all', $request, $page); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Shows unread entries for current user. | |
166 | * | |
167 | * @param Request $request | |
168 | * @param int $page | |
169 | * | |
170 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
171 | * | |
172 | * @return \Symfony\Component\HttpFoundation\Response | |
173 | */ | |
174 | public function showUnreadAction(Request $request, $page) | |
175 | { | |
176 | // load the quickstart if no entry in database | |
177 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
178 | return $this->redirect($this->generateUrl('quickstart')); | |
179 | } | |
180 | ||
181 | return $this->showEntries('unread', $request, $page); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Shows read entries for current user. | |
186 | * | |
187 | * @param Request $request | |
188 | * @param int $page | |
189 | * | |
190 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
191 | * | |
192 | * @return \Symfony\Component\HttpFoundation\Response | |
193 | */ | |
194 | public function showArchiveAction(Request $request, $page) | |
195 | { | |
196 | return $this->showEntries('archive', $request, $page); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Shows starred entries for current user. | |
201 | * | |
202 | * @param Request $request | |
203 | * @param int $page | |
204 | * | |
205 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
206 | * | |
207 | * @return \Symfony\Component\HttpFoundation\Response | |
208 | */ | |
209 | public function showStarredAction(Request $request, $page) | |
210 | { | |
211 | return $this->showEntries('starred', $request, $page); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Global method to retrieve entries depending on the given type | |
216 | * It returns the response to be send. | |
217 | * | |
218 | * @param string $type Entries type: unread, starred or archive | |
219 | * @param Request $request | |
220 | * @param int $page | |
221 | * | |
222 | * @return \Symfony\Component\HttpFoundation\Response | |
223 | */ | |
224 | private function showEntries($type, Request $request, $page) | |
225 | { | |
226 | $repository = $this->get('wallabag_core.entry_repository'); | |
227 | ||
228 | switch ($type) { | |
229 | case 'starred': | |
230 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
231 | break; | |
232 | ||
233 | case 'archive': | |
234 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
235 | break; | |
236 | ||
237 | case 'unread': | |
238 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
239 | break; | |
240 | ||
241 | case 'all': | |
242 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
243 | break; | |
244 | ||
245 | default: | |
246 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
247 | } | |
248 | ||
249 | $form = $this->createForm(EntryFilterType::class); | |
250 | ||
251 | if ($request->query->has($form->getName())) { | |
252 | // manually bind values from the request | |
253 | $form->submit($request->query->get($form->getName())); | |
254 | ||
255 | // build the query from the given form object | |
256 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
257 | } | |
258 | ||
259 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
260 | ||
261 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') | |
262 | ->prepare($pagerAdapter, $page); | |
263 | ||
264 | try { | |
265 | $entries->setCurrentPage($page); | |
266 | } catch (OutOfRangeCurrentPageException $e) { | |
267 | if ($page > 1) { | |
268 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | |
269 | } | |
270 | } | |
271 | ||
272 | return $this->render( | |
273 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
274 | [ | |
275 | 'form' => $form->createView(), | |
276 | 'entries' => $entries, | |
277 | 'currentPage' => $page, | |
278 | ] | |
279 | ); | |
280 | } | |
281 | ||
282 | /** | |
283 | * Shows entry content. | |
284 | * | |
285 | * @param Entry $entry | |
286 | * | |
287 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
288 | * | |
289 | * @return \Symfony\Component\HttpFoundation\Response | |
290 | */ | |
291 | public function viewAction(Entry $entry) | |
292 | { | |
293 | $this->checkUserAction($entry); | |
294 | ||
295 | $this->generateEntryUuid($entry); | |
296 | ||
297 | return $this->render( | |
298 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
299 | ['entry' => $entry] | |
300 | ); | |
301 | } | |
302 | ||
303 | /** | |
304 | * Reload an entry. | |
305 | * Refetch content from the website and make it readable again. | |
306 | * | |
307 | * @param Entry $entry | |
308 | * | |
309 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
310 | * | |
311 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
312 | */ | |
313 | public function reloadAction(Entry $entry) | |
314 | { | |
315 | $this->checkUserAction($entry); | |
316 | ||
317 | $message = 'flashes.entry.notice.entry_reloaded'; | |
318 | if (false === $this->updateEntry($entry)) { | |
319 | $message = 'flashes.entry.notice.entry_reload_failed'; | |
320 | } | |
321 | ||
322 | $this->get('session')->getFlashBag()->add( | |
323 | 'notice', | |
324 | $message | |
325 | ); | |
326 | ||
327 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
328 | } | |
329 | ||
330 | /** | |
331 | * Changes read status for an entry. | |
332 | * | |
333 | * @param Request $request | |
334 | * @param Entry $entry | |
335 | * | |
336 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
337 | * | |
338 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
339 | */ | |
340 | public function toggleArchiveAction(Request $request, Entry $entry) | |
341 | { | |
342 | $this->checkUserAction($entry); | |
343 | ||
344 | $entry->toggleArchive(); | |
345 | $this->getDoctrine()->getManager()->flush(); | |
346 | ||
347 | $message = 'flashes.entry.notice.entry_unarchived'; | |
348 | if ($entry->isArchived()) { | |
349 | $message = 'flashes.entry.notice.entry_archived'; | |
350 | } | |
351 | ||
352 | $this->get('session')->getFlashBag()->add( | |
353 | 'notice', | |
354 | $message | |
355 | ); | |
356 | ||
357 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
358 | ||
359 | return $this->redirect($redirectUrl); | |
360 | } | |
361 | ||
362 | /** | |
363 | * Changes starred status for an entry. | |
364 | * | |
365 | * @param Request $request | |
366 | * @param Entry $entry | |
367 | * | |
368 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
369 | * | |
370 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
371 | */ | |
372 | public function toggleStarAction(Request $request, Entry $entry) | |
373 | { | |
374 | $this->checkUserAction($entry); | |
375 | ||
376 | $entry->toggleStar(); | |
377 | $this->getDoctrine()->getManager()->flush(); | |
378 | ||
379 | $message = 'flashes.entry.notice.entry_unstarred'; | |
380 | if ($entry->isStarred()) { | |
381 | $message = 'flashes.entry.notice.entry_starred'; | |
382 | } | |
383 | ||
384 | $this->get('session')->getFlashBag()->add( | |
385 | 'notice', | |
386 | $message | |
387 | ); | |
388 | ||
389 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
390 | ||
391 | return $this->redirect($redirectUrl); | |
392 | } | |
393 | ||
394 | /** | |
395 | * Deletes entry and redirect to the homepage or the last viewed page. | |
396 | * | |
397 | * @param Entry $entry | |
398 | * | |
399 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
400 | * | |
401 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
402 | */ | |
403 | public function deleteEntryAction(Request $request, Entry $entry) | |
404 | { | |
405 | $this->checkUserAction($entry); | |
406 | ||
407 | // generates the view url for this entry to check for redirection later | |
408 | // to avoid redirecting to the deleted entry. Ugh. | |
409 | $url = $this->generateUrl( | |
410 | 'view', | |
411 | ['id' => $entry->getId()], | |
412 | UrlGeneratorInterface::ABSOLUTE_PATH | |
413 | ); | |
414 | ||
415 | $em = $this->getDoctrine()->getManager(); | |
416 | $em->remove($entry); | |
417 | $em->flush(); | |
418 | ||
419 | $this->get('session')->getFlashBag()->add( | |
420 | 'notice', | |
421 | 'flashes.entry.notice.entry_deleted' | |
422 | ); | |
423 | ||
424 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) | |
425 | $referer = $request->headers->get('referer'); | |
426 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | |
427 | ||
428 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
429 | ||
430 | return $this->redirect($redirectUrl); | |
431 | } | |
432 | ||
433 | /** | |
434 | * Check if the logged user can manage the given entry. | |
435 | * | |
436 | * @param Entry $entry | |
437 | */ | |
438 | private function checkUserAction(Entry $entry) | |
439 | { | |
440 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
441 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
442 | } | |
443 | } | |
444 | ||
445 | /** | |
446 | * Check for existing entry, if it exists, redirect to it with a message. | |
447 | * | |
448 | * @param Entry $entry | |
449 | * | |
450 | * @return Entry|bool | |
451 | */ | |
452 | private function checkIfEntryAlreadyExists(Entry $entry) | |
453 | { | |
454 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
455 | } | |
456 | ||
457 | /** | |
458 | * Share entry content. | |
459 | * | |
460 | * @param Entry $entry | |
461 | * | |
462 | * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share") | |
463 | * @Cache(maxage="25200", public=true) | |
464 | * | |
465 | * @return \Symfony\Component\HttpFoundation\Response | |
466 | */ | |
467 | public function shareEntryAction(Entry $entry) | |
468 | { | |
469 | return $this->render( | |
470 | '@WallabagCore/themes/share.html.twig', | |
471 | array('entry' => $entry) | |
472 | ); | |
473 | } | |
474 | ||
475 | /** | |
476 | * @param Entry $entry | |
477 | */ | |
478 | private function generateEntryUuid(Entry $entry) | |
479 | { | |
480 | $entry->generateUuid(); | |
481 | $em = $this->getDoctrine()->getManager(); | |
482 | $em->persist($entry); | |
483 | $em->flush(); | |
484 | } | |
485 | } |