]>
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 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | |
17 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
18 | use Wallabag\CoreBundle\Form\Type\SearchEntryType; | |
19 | ||
20 | class EntryController extends Controller | |
21 | { | |
22 | /** | |
23 | * @param Request $request | |
24 | * @param int $page | |
25 | * | |
26 | * @Route("/search/{page}", name="search", defaults={"page" = "1"}) | |
27 | * | |
28 | * @return \Symfony\Component\HttpFoundation\Response | |
29 | */ | |
30 | public function searchFormAction(Request $request, $page) | |
31 | { | |
32 | $form = $this->createForm(SearchEntryType::class); | |
33 | ||
34 | $form->handleRequest($request); | |
35 | ||
36 | if ($form->isValid()) { | |
37 | return $this->showEntries('search', $request, $page); | |
38 | } | |
39 | ||
40 | return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [ | |
41 | 'form' => $form->createView(), | |
42 | ]); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Fetch content and update entry. | |
47 | * In case it fails, entry will return to avod loosing the data. | |
48 | * | |
49 | * @param Entry $entry | |
50 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | |
51 | * | |
52 | * @return Entry | |
53 | */ | |
54 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') | |
55 | { | |
56 | // put default title in case of fetching content failed | |
57 | $entry->setTitle('No title found'); | |
58 | ||
59 | $message = 'flashes.entry.notice.'.$prefixMessage; | |
60 | ||
61 | try { | |
62 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
63 | } catch (\Exception $e) { | |
64 | $this->get('logger')->error('Error while saving an entry', [ | |
65 | 'exception' => $e, | |
66 | 'entry' => $entry, | |
67 | ]); | |
68 | ||
69 | $message = 'flashes.entry.notice.'.$prefixMessage.'_failed'; | |
70 | } | |
71 | ||
72 | $this->get('session')->getFlashBag()->add('notice', $message); | |
73 | ||
74 | return $entry; | |
75 | } | |
76 | ||
77 | /** | |
78 | * @param Request $request | |
79 | * | |
80 | * @Route("/new-entry", name="new_entry") | |
81 | * | |
82 | * @return \Symfony\Component\HttpFoundation\Response | |
83 | */ | |
84 | public function addEntryFormAction(Request $request) | |
85 | { | |
86 | $entry = new Entry($this->getUser()); | |
87 | ||
88 | $form = $this->createForm(NewEntryType::class, $entry); | |
89 | ||
90 | $form->handleRequest($request); | |
91 | ||
92 | if ($form->isValid()) { | |
93 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); | |
94 | ||
95 | if (false !== $existingEntry) { | |
96 | $this->get('session')->getFlashBag()->add( | |
97 | 'notice', | |
98 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) | |
99 | ); | |
100 | ||
101 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); | |
102 | } | |
103 | ||
104 | $this->updateEntry($entry); | |
105 | ||
106 | $em = $this->getDoctrine()->getManager(); | |
107 | $em->persist($entry); | |
108 | $em->flush(); | |
109 | ||
110 | // entry saved, dispatch event about it! | |
111 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
112 | ||
113 | return $this->redirect($this->generateUrl('homepage')); | |
114 | } | |
115 | ||
116 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [ | |
117 | 'form' => $form->createView(), | |
118 | ]); | |
119 | } | |
120 | ||
121 | /** | |
122 | * @param Request $request | |
123 | * | |
124 | * @Route("/bookmarklet", name="bookmarklet") | |
125 | * | |
126 | * @return \Symfony\Component\HttpFoundation\Response | |
127 | */ | |
128 | public function addEntryViaBookmarkletAction(Request $request) | |
129 | { | |
130 | $entry = new Entry($this->getUser()); | |
131 | $entry->setUrl($request->get('url')); | |
132 | ||
133 | if (false === $this->checkIfEntryAlreadyExists($entry)) { | |
134 | $this->updateEntry($entry); | |
135 | ||
136 | $em = $this->getDoctrine()->getManager(); | |
137 | $em->persist($entry); | |
138 | $em->flush(); | |
139 | ||
140 | // entry saved, dispatch event about it! | |
141 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
142 | } | |
143 | ||
144 | return $this->redirect($this->generateUrl('homepage')); | |
145 | } | |
146 | ||
147 | /** | |
148 | * @Route("/new", name="new") | |
149 | * | |
150 | * @return \Symfony\Component\HttpFoundation\Response | |
151 | */ | |
152 | public function addEntryAction() | |
153 | { | |
154 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Edit an entry content. | |
159 | * | |
160 | * @param Request $request | |
161 | * @param Entry $entry | |
162 | * | |
163 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
164 | * | |
165 | * @return \Symfony\Component\HttpFoundation\Response | |
166 | */ | |
167 | public function editEntryAction(Request $request, Entry $entry) | |
168 | { | |
169 | $this->checkUserAction($entry); | |
170 | ||
171 | $form = $this->createForm(EditEntryType::class, $entry); | |
172 | ||
173 | $form->handleRequest($request); | |
174 | ||
175 | if ($form->isValid()) { | |
176 | $em = $this->getDoctrine()->getManager(); | |
177 | $em->persist($entry); | |
178 | $em->flush(); | |
179 | ||
180 | $this->get('session')->getFlashBag()->add( | |
181 | 'notice', | |
182 | 'flashes.entry.notice.entry_updated' | |
183 | ); | |
184 | ||
185 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
186 | } | |
187 | ||
188 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ | |
189 | 'form' => $form->createView(), | |
190 | ]); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Shows all entries for current user. | |
195 | * | |
196 | * @param Request $request | |
197 | * @param int $page | |
198 | * | |
199 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
200 | * | |
201 | * @return \Symfony\Component\HttpFoundation\Response | |
202 | */ | |
203 | public function showAllAction(Request $request, $page) | |
204 | { | |
205 | return $this->showEntries('all', $request, $page); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Shows unread entries for current user. | |
210 | * | |
211 | * @param Request $request | |
212 | * @param int $page | |
213 | * | |
214 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
215 | * | |
216 | * @return \Symfony\Component\HttpFoundation\Response | |
217 | */ | |
218 | public function showUnreadAction(Request $request, $page) | |
219 | { | |
220 | // load the quickstart if no entry in database | |
221 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
222 | return $this->redirect($this->generateUrl('quickstart')); | |
223 | } | |
224 | ||
225 | return $this->showEntries('unread', $request, $page); | |
226 | } | |
227 | ||
228 | /** | |
229 | * Shows read entries for current user. | |
230 | * | |
231 | * @param Request $request | |
232 | * @param int $page | |
233 | * | |
234 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
235 | * | |
236 | * @return \Symfony\Component\HttpFoundation\Response | |
237 | */ | |
238 | public function showArchiveAction(Request $request, $page) | |
239 | { | |
240 | return $this->showEntries('archive', $request, $page); | |
241 | } | |
242 | ||
243 | /** | |
244 | * Shows starred entries for current user. | |
245 | * | |
246 | * @param Request $request | |
247 | * @param int $page | |
248 | * | |
249 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
250 | * | |
251 | * @return \Symfony\Component\HttpFoundation\Response | |
252 | */ | |
253 | public function showStarredAction(Request $request, $page) | |
254 | { | |
255 | return $this->showEntries('starred', $request, $page); | |
256 | } | |
257 | ||
258 | /** | |
259 | * Global method to retrieve entries depending on the given type | |
260 | * It returns the response to be send. | |
261 | * | |
262 | * @param string $type Entries type: unread, starred or archive | |
263 | * @param Request $request | |
264 | * @param int $page | |
265 | * | |
266 | * @return \Symfony\Component\HttpFoundation\Response | |
267 | */ | |
268 | private function showEntries($type, Request $request, $page) | |
269 | { | |
270 | $repository = $this->get('wallabag_core.entry_repository'); | |
271 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); | |
272 | ||
273 | switch ($type) { | |
274 | case 'search': | |
275 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm); | |
276 | ||
277 | break; | |
278 | case 'untagged': | |
279 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | |
280 | ||
281 | break; | |
282 | case 'starred': | |
283 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
284 | break; | |
285 | ||
286 | case 'archive': | |
287 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
288 | break; | |
289 | ||
290 | case 'unread': | |
291 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
292 | break; | |
293 | ||
294 | case 'all': | |
295 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
296 | break; | |
297 | ||
298 | default: | |
299 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
300 | } | |
301 | ||
302 | $form = $this->createForm(EntryFilterType::class); | |
303 | ||
304 | if ($request->query->has($form->getName())) { | |
305 | // manually bind values from the request | |
306 | $form->submit($request->query->get($form->getName())); | |
307 | ||
308 | // build the query from the given form object | |
309 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
310 | } | |
311 | ||
312 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
313 | ||
314 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') | |
315 | ->prepare($pagerAdapter, $page); | |
316 | ||
317 | try { | |
318 | $entries->setCurrentPage($page); | |
319 | } catch (OutOfRangeCurrentPageException $e) { | |
320 | if ($page > 1) { | |
321 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | |
322 | } | |
323 | } | |
324 | ||
325 | return $this->render( | |
326 | 'WallabagCoreBundle:Entry:entries.html.twig', [ | |
327 | 'form' => $form->createView(), | |
328 | 'entries' => $entries, | |
329 | 'currentPage' => $page, | |
330 | 'searchTerm' => $searchTerm, | |
331 | ] | |
332 | ); | |
333 | } | |
334 | ||
335 | /** | |
336 | * Shows entry content. | |
337 | * | |
338 | * @param Entry $entry | |
339 | * | |
340 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
341 | * | |
342 | * @return \Symfony\Component\HttpFoundation\Response | |
343 | */ | |
344 | public function viewAction(Entry $entry) | |
345 | { | |
346 | $this->checkUserAction($entry); | |
347 | ||
348 | return $this->render( | |
349 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
350 | ['entry' => $entry] | |
351 | ); | |
352 | } | |
353 | ||
354 | /** | |
355 | * Reload an entry. | |
356 | * Refetch content from the website and make it readable again. | |
357 | * | |
358 | * @param Entry $entry | |
359 | * | |
360 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
361 | * | |
362 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
363 | */ | |
364 | public function reloadAction(Entry $entry) | |
365 | { | |
366 | $this->checkUserAction($entry); | |
367 | ||
368 | $this->updateEntry($entry, 'entry_reloaded'); | |
369 | ||
370 | // if refreshing entry failed, don't save it | |
371 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
372 | $bag = $this->get('session')->getFlashBag(); | |
373 | $bag->clear(); | |
374 | $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed'); | |
375 | ||
376 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
377 | } | |
378 | ||
379 | $em = $this->getDoctrine()->getManager(); | |
380 | $em->persist($entry); | |
381 | $em->flush(); | |
382 | ||
383 | // entry saved, dispatch event about it! | |
384 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
385 | ||
386 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
387 | } | |
388 | ||
389 | /** | |
390 | * Changes read status for an entry. | |
391 | * | |
392 | * @param Request $request | |
393 | * @param Entry $entry | |
394 | * | |
395 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
396 | * | |
397 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
398 | */ | |
399 | public function toggleArchiveAction(Request $request, Entry $entry) | |
400 | { | |
401 | $this->checkUserAction($entry); | |
402 | ||
403 | $entry->toggleArchive(); | |
404 | $this->getDoctrine()->getManager()->flush(); | |
405 | ||
406 | $message = 'flashes.entry.notice.entry_unarchived'; | |
407 | if ($entry->isArchived()) { | |
408 | $message = 'flashes.entry.notice.entry_archived'; | |
409 | } | |
410 | ||
411 | $this->get('session')->getFlashBag()->add( | |
412 | 'notice', | |
413 | $message | |
414 | ); | |
415 | ||
416 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
417 | ||
418 | return $this->redirect($redirectUrl); | |
419 | } | |
420 | ||
421 | /** | |
422 | * Changes starred status for an entry. | |
423 | * | |
424 | * @param Request $request | |
425 | * @param Entry $entry | |
426 | * | |
427 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
428 | * | |
429 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
430 | */ | |
431 | public function toggleStarAction(Request $request, Entry $entry) | |
432 | { | |
433 | $this->checkUserAction($entry); | |
434 | ||
435 | $entry->toggleStar(); | |
436 | $this->getDoctrine()->getManager()->flush(); | |
437 | ||
438 | $message = 'flashes.entry.notice.entry_unstarred'; | |
439 | if ($entry->isStarred()) { | |
440 | $message = 'flashes.entry.notice.entry_starred'; | |
441 | } | |
442 | ||
443 | $this->get('session')->getFlashBag()->add( | |
444 | 'notice', | |
445 | $message | |
446 | ); | |
447 | ||
448 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
449 | ||
450 | return $this->redirect($redirectUrl); | |
451 | } | |
452 | ||
453 | /** | |
454 | * Deletes entry and redirect to the homepage or the last viewed page. | |
455 | * | |
456 | * @param Entry $entry | |
457 | * | |
458 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
459 | * | |
460 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
461 | */ | |
462 | public function deleteEntryAction(Request $request, Entry $entry) | |
463 | { | |
464 | $this->checkUserAction($entry); | |
465 | ||
466 | // generates the view url for this entry to check for redirection later | |
467 | // to avoid redirecting to the deleted entry. Ugh. | |
468 | $url = $this->generateUrl( | |
469 | 'view', | |
470 | ['id' => $entry->getId()], | |
471 | UrlGeneratorInterface::ABSOLUTE_PATH | |
472 | ); | |
473 | ||
474 | // entry deleted, dispatch event about it! | |
475 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
476 | ||
477 | $em = $this->getDoctrine()->getManager(); | |
478 | $em->remove($entry); | |
479 | $em->flush(); | |
480 | ||
481 | $this->get('session')->getFlashBag()->add( | |
482 | 'notice', | |
483 | 'flashes.entry.notice.entry_deleted' | |
484 | ); | |
485 | ||
486 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) | |
487 | $referer = $request->headers->get('referer'); | |
488 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | |
489 | ||
490 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
491 | ||
492 | return $this->redirect($redirectUrl); | |
493 | } | |
494 | ||
495 | /** | |
496 | * Check if the logged user can manage the given entry. | |
497 | * | |
498 | * @param Entry $entry | |
499 | */ | |
500 | private function checkUserAction(Entry $entry) | |
501 | { | |
502 | if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) { | |
503 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
504 | } | |
505 | } | |
506 | ||
507 | /** | |
508 | * Check for existing entry, if it exists, redirect to it with a message. | |
509 | * | |
510 | * @param Entry $entry | |
511 | * | |
512 | * @return Entry|bool | |
513 | */ | |
514 | private function checkIfEntryAlreadyExists(Entry $entry) | |
515 | { | |
516 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
517 | } | |
518 | ||
519 | /** | |
520 | * Get public URL for entry (and generate it if necessary). | |
521 | * | |
522 | * @param Entry $entry | |
523 | * | |
524 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") | |
525 | * | |
526 | * @return \Symfony\Component\HttpFoundation\Response | |
527 | */ | |
528 | public function shareAction(Entry $entry) | |
529 | { | |
530 | $this->checkUserAction($entry); | |
531 | ||
532 | if (null === $entry->getUuid()) { | |
533 | $entry->generateUuid(); | |
534 | ||
535 | $em = $this->getDoctrine()->getManager(); | |
536 | $em->persist($entry); | |
537 | $em->flush(); | |
538 | } | |
539 | ||
540 | return $this->redirect($this->generateUrl('share_entry', [ | |
541 | 'uuid' => $entry->getUuid(), | |
542 | ])); | |
543 | } | |
544 | ||
545 | /** | |
546 | * Disable public sharing for an entry. | |
547 | * | |
548 | * @param Entry $entry | |
549 | * | |
550 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") | |
551 | * | |
552 | * @return \Symfony\Component\HttpFoundation\Response | |
553 | */ | |
554 | public function deleteShareAction(Entry $entry) | |
555 | { | |
556 | $this->checkUserAction($entry); | |
557 | ||
558 | $entry->cleanUuid(); | |
559 | ||
560 | $em = $this->getDoctrine()->getManager(); | |
561 | $em->persist($entry); | |
562 | $em->flush(); | |
563 | ||
564 | return $this->redirect($this->generateUrl('view', [ | |
565 | 'id' => $entry->getId(), | |
566 | ])); | |
567 | } | |
568 | ||
569 | /** | |
570 | * Ability to view a content publicly. | |
571 | * | |
572 | * @param Entry $entry | |
573 | * | |
574 | * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry") | |
575 | * @Cache(maxage="25200", smaxage="25200", public=true) | |
576 | * | |
577 | * @return \Symfony\Component\HttpFoundation\Response | |
578 | */ | |
579 | public function shareEntryAction(Entry $entry) | |
580 | { | |
581 | if (!$this->get('craue_config')->get('share_public')) { | |
582 | throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.'); | |
583 | } | |
584 | ||
585 | return $this->render( | |
586 | '@WallabagCore/themes/common/Entry/share.html.twig', | |
587 | ['entry' => $entry] | |
588 | ); | |
589 | } | |
590 | ||
591 | /** | |
592 | * Shows untagged articles for current user. | |
593 | * | |
594 | * @param Request $request | |
595 | * @param int $page | |
596 | * | |
597 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | |
598 | * | |
599 | * @return \Symfony\Component\HttpFoundation\Response | |
600 | */ | |
601 | public function showUntaggedEntriesAction(Request $request, $page) | |
602 | { | |
603 | return $this->showEntries('untagged', $request, $page); | |
604 | } | |
605 | } |