3 namespace Wallabag\CoreBundle\Controller
;
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\JsonResponse
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Symfony\Component\HttpFoundation\Response
;
12 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
13 use Wallabag\CoreBundle\Entity\Entry
;
14 use Wallabag\CoreBundle\Form\Type\EntryFilterType
;
15 use Wallabag\CoreBundle\Form\Type\EditEntryType
;
16 use Wallabag\CoreBundle\Form\Type\NewEntryType
;
17 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache
;
18 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
19 use Wallabag\CoreBundle\Event\EntryDeletedEvent
;
20 use Wallabag\CoreBundle\Form\Type\SearchEntryType
;
22 class EntryController
extends Controller
25 * @param Request $request
28 * @Route("/search/{page}", name="search", defaults={"page" = 1})
30 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
31 * because this controller is also called inside the layout template without any page as argument
33 * @return \Symfony\Component\HttpFoundation\Response
35 public function searchFormAction(Request
$request, $page = 1, $currentRoute = null)
37 // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template)
38 if (null === $currentRoute && $request->query
->has('currentRoute')) {
39 $currentRoute = $request->query
->get('currentRoute');
42 $form = $this->createForm(SearchEntryType
::class);
44 $form->handleRequest($request);
46 if ($form->isSubmitted() && $form->isValid()) {
47 return $this->showEntries('search', $request, $page);
50 return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [
51 'form' => $form->createView(),
52 'currentRoute' => $currentRoute,
57 * Fetch content and update entry.
58 * In case it fails, $entry->getContent will return an error message.
61 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
63 private function updateEntry(Entry
$entry, $prefixMessage = 'entry_saved')
65 $message = 'flashes.entry.notice.'.$prefixMessage;
68 $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
69 } catch (\Exception
$e) {
70 $this->get('logger')->error('Error while saving an entry', [
75 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
78 $this->get('session')->getFlashBag()->add('notice', $message);
82 * @param Request $request
84 * @Route("/new-entry", name="new_entry")
86 * @return \Symfony\Component\HttpFoundation\Response
88 public function addEntryFormAction(Request
$request)
90 $entry = new Entry($this->getUser());
92 $form = $this->createForm(NewEntryType
::class, $entry);
94 $form->handleRequest($request);
96 if ($form->isSubmitted() && $form->isValid()) {
97 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
99 if (false !== $existingEntry) {
100 $this->get('session')->getFlashBag()->add(
102 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
105 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
108 $this->updateEntry($entry);
110 $em = $this->getDoctrine()->getManager();
111 $em->persist($entry);
114 // entry saved, dispatch event about it!
115 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
117 return $this->redirect($this->generateUrl('homepage'));
120 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
121 'form' => $form->createView(),
126 * @param Request $request
128 * @Route("/bookmarklet", name="bookmarklet")
130 * @return \Symfony\Component\HttpFoundation\Response
132 public function addEntryViaBookmarkletAction(Request
$request)
134 $entry = new Entry($this->getUser());
135 $entry->setUrl($request->get('url'));
137 if (false === $this->checkIfEntryAlreadyExists($entry)) {
138 $this->updateEntry($entry);
140 $em = $this->getDoctrine()->getManager();
141 $em->persist($entry);
144 // entry saved, dispatch event about it!
145 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
148 return $this->redirect($this->generateUrl('homepage'));
152 * @Route("/new", name="new")
154 * @return \Symfony\Component\HttpFoundation\Response
156 public function addEntryAction()
158 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
162 * Edit an entry content.
164 * @param Request $request
165 * @param Entry $entry
167 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
169 * @return \Symfony\Component\HttpFoundation\Response
171 public function editEntryAction(Request
$request, Entry
$entry)
173 $this->checkUserAction($entry);
175 $form = $this->createForm(EditEntryType
::class, $entry);
177 $form->handleRequest($request);
179 if ($form->isSubmitted() && $form->isValid()) {
180 $em = $this->getDoctrine()->getManager();
181 $em->persist($entry);
184 $this->get('session')->getFlashBag()->add(
186 'flashes.entry.notice.entry_updated'
189 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
192 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
193 'form' => $form->createView(),
198 * Shows all entries for current user.
200 * @param Request $request
203 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
205 * @return \Symfony\Component\HttpFoundation\Response
207 public function showAllAction(Request
$request, $page)
209 return $this->showEntries('all', $request, $page);
213 * Shows unread entries for current user.
215 * @param Request $request
218 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
220 * @return \Symfony\Component\HttpFoundation\Response
222 public function showUnreadAction(Request
$request, $page)
224 // load the quickstart if no entry in database
225 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUser($this->getUser()->getId()) == 0) {
226 return $this->redirect($this->generateUrl('quickstart'));
229 return $this->showEntries('unread', $request, $page);
233 * Shows read entries for current user.
235 * @param Request $request
238 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
240 * @return \Symfony\Component\HttpFoundation\Response
242 public function showArchiveAction(Request
$request, $page)
244 return $this->showEntries('archive', $request, $page);
248 * Shows starred entries for current user.
250 * @param Request $request
253 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
255 * @return \Symfony\Component\HttpFoundation\Response
257 public function showStarredAction(Request
$request, $page)
259 return $this->showEntries('starred', $request, $page);
263 * Global method to retrieve entries depending on the given type
264 * It returns the response to be send.
266 * @param string $type Entries type: unread, starred or archive
267 * @param Request $request
270 * @return \Symfony\Component\HttpFoundation\Response
272 private function showEntries($type, Request
$request, $page)
274 $repository = $this->get('wallabag_core.entry_repository');
275 $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
276 $currentRoute = (!is_null($request->query
->get('currentRoute')) ? $request->query
->get('currentRoute') : '');
280 $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute);
284 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
288 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
292 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
296 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
300 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
304 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
307 $form = $this->createForm(EntryFilterType
::class);
309 if ($request->query
->has($form->getName())) {
310 // manually bind values from the request
311 $form->submit($request->query
->get($form->getName()));
313 // build the query from the given form object
314 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
317 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
319 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
322 $entries->setCurrentPage($page);
323 } catch (OutOfRangeCurrentPageException
$e) {
325 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
329 return $this->render(
330 'WallabagCoreBundle:Entry:entries.html.twig', [
331 'form' => $form->createView(),
332 'entries' => $entries,
333 'currentPage' => $page,
334 'searchTerm' => $searchTerm,
340 * Shows entry content.
342 * @param Entry $entry
344 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
346 * @return \Symfony\Component\HttpFoundation\Response
348 public function viewAction(Entry
$entry)
350 $this->checkUserAction($entry);
352 return $this->render(
353 'WallabagCoreBundle:Entry:entry.html.twig',
360 * Refetch content from the website and make it readable again.
362 * @param Entry $entry
364 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
366 * @return \Symfony\Component\HttpFoundation\RedirectResponse
368 public function reloadAction(Entry
$entry)
370 $this->checkUserAction($entry);
372 $this->updateEntry($entry, 'entry_reloaded');
374 // if refreshing entry failed, don't save it
375 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
376 $bag = $this->get('session')->getFlashBag();
378 $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
380 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
383 $em = $this->getDoctrine()->getManager();
384 $em->persist($entry);
387 // entry saved, dispatch event about it!
388 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
390 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
394 * Changes read status for an entry.
396 * @param Request $request
397 * @param Entry $entry
399 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
401 * @return \Symfony\Component\HttpFoundation\RedirectResponse
403 public function toggleArchiveAction(Request
$request, Entry
$entry)
405 $this->checkUserAction($entry);
407 $entry->toggleArchive();
408 $this->getDoctrine()->getManager()->flush();
410 $message = 'flashes.entry.notice.entry_unarchived';
411 if ($entry->isArchived()) {
412 $message = 'flashes.entry.notice.entry_archived';
415 $this->get('session')->getFlashBag()->add(
420 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
422 return $this->redirect($redirectUrl);
426 * Changes starred status for an entry.
428 * @param Request $request
429 * @param Entry $entry
431 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
433 * @return \Symfony\Component\HttpFoundation\RedirectResponse
435 public function toggleStarAction(Request
$request, Entry
$entry)
437 $this->checkUserAction($entry);
439 $entry->toggleStar();
440 $this->getDoctrine()->getManager()->flush();
442 $message = 'flashes.entry.notice.entry_unstarred';
443 if ($entry->isStarred()) {
444 $message = 'flashes.entry.notice.entry_starred';
447 $this->get('session')->getFlashBag()->add(
452 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
454 return $this->redirect($redirectUrl);
458 * Deletes entry and redirect to the homepage or the last viewed page.
460 * @param Entry $entry
462 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
464 * @return \Symfony\Component\HttpFoundation\RedirectResponse
466 public function deleteEntryAction(Request
$request, Entry
$entry)
468 $this->checkUserAction($entry);
470 // generates the view url for this entry to check for redirection later
471 // to avoid redirecting to the deleted entry. Ugh.
472 $url = $this->generateUrl(
474 ['id' => $entry->getId()],
475 UrlGeneratorInterface
::ABSOLUTE_PATH
478 // entry deleted, dispatch event about it!
479 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
481 $em = $this->getDoctrine()->getManager();
485 $this->get('session')->getFlashBag()->add(
487 'flashes.entry.notice.entry_deleted'
490 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
491 $referer = $request->headers
->get('referer');
492 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
494 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
496 return $this->redirect($redirectUrl);
500 * Check if the logged user can manage the given entry.
502 * @param Entry $entry
504 private function checkUserAction(Entry
$entry)
506 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
507 throw $this->createAccessDeniedException('You can not access this entry.');
512 * Check for existing entry, if it exists, redirect to it with a message.
514 * @param Entry $entry
518 private function checkIfEntryAlreadyExists(Entry
$entry)
520 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
524 * Get public URL for entry (and generate it if necessary).
526 * @param Entry $entry
528 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
530 * @return \Symfony\Component\HttpFoundation\Response
532 public function shareAction(Entry
$entry)
534 $this->checkUserAction($entry);
536 if (null === $entry->getUid()) {
537 $entry->generateUid();
539 $em = $this->getDoctrine()->getManager();
540 $em->persist($entry);
544 return $this->redirect($this->generateUrl('share_entry', [
545 'uid' => $entry->getUid(),
550 * Disable public sharing for an entry.
552 * @param Entry $entry
554 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
556 * @return \Symfony\Component\HttpFoundation\Response
558 public function deleteShareAction(Entry
$entry)
560 $this->checkUserAction($entry);
564 $em = $this->getDoctrine()->getManager();
565 $em->persist($entry);
568 return $this->redirect($this->generateUrl('view', [
569 'id' => $entry->getId(),
574 * Ability to view a content publicly.
576 * @param Entry $entry
578 * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry")
579 * @Cache(maxage="25200", smaxage="25200", public=true)
581 * @return \Symfony\Component\HttpFoundation\Response
583 public function shareEntryAction(Entry
$entry)
585 if (!$this->get('craue_config')->get('share_public')) {
586 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
589 return $this->render(
590 '@WallabagCore/themes/common/Entry/share.html.twig',
596 * Shows untagged articles for current user.
598 * @param Request $request
601 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
603 * @return \Symfony\Component\HttpFoundation\Response
605 public function showUntaggedEntriesAction(Request
$request, $page)
607 return $this->showEntries('untagged', $request, $page);
611 * Get the progress of an entry.
613 * @param Entry $entry
615 * @Route("/progress/{entry}", name="get_progress")
617 * @return JsonResponse
619 public function getEntriesProgressAction(Entry
$entry)
621 $this->checkUserAction($entry);
623 $json = $this->get('serializer')->serialize($entry->getProgress(), 'json');
625 return (new JsonResponse())->setJson($json);
629 * Set the progress of an entry.
631 * @param Entry $entry
632 * @param int $progress
634 * @Route("/set-progress/{entry}", name="set_progress")
636 * @return JsonResponse
638 public function setEntriesProgressAction(Entry
$entry, $progress)
640 $this->checkUserAction($entry);
641 $response = new JsonResponse();
643 if (is_null($progress)) {
644 $response->setStatusCode(Response
::HTTP_BAD_REQUEST
);
646 $progress = (int) $progress;
647 if ($progress >= 0 && $progress <= 100) {
648 $entry->setProgress($progress);
649 $response->setStatusCode(Response
::HTTP_OK
);