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\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
;
19 class EntryController
extends Controller
22 * Fetch content and update entry.
23 * In case it fails, entry will return to avod loosing the data.
26 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
30 private function updateEntry(Entry
$entry, $prefixMessage = 'entry_saved')
32 // put default title in case of fetching content failed
33 $entry->setTitle('No title found');
35 $message = 'flashes.entry.notice.'.$prefixMessage;
38 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
39 } catch (\Exception
$e) {
40 $this->get('logger')->error('Error while saving an entry', [
45 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
48 $this->get('session')->getFlashBag()->add('notice', $message);
54 * @param Request $request
56 * @Route("/new-entry", name="new_entry")
58 * @return \Symfony\Component\HttpFoundation\Response
60 public function addEntryFormAction(Request
$request)
62 $entry = new Entry($this->getUser());
64 $form = $this->createForm(NewEntryType
::class, $entry);
66 $form->handleRequest($request);
68 if ($form->isValid()) {
69 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
71 if (false !== $existingEntry) {
72 $this->get('session')->getFlashBag()->add(
74 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
77 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
80 $this->updateEntry($entry);
82 $em = $this->getDoctrine()->getManager();
86 // entry saved, dispatch event about it!
87 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
89 return $this->redirect($this->generateUrl('homepage'));
92 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
93 'form' => $form->createView(),
98 * @param Request $request
100 * @Route("/bookmarklet", name="bookmarklet")
102 * @return \Symfony\Component\HttpFoundation\Response
104 public function addEntryViaBookmarkletAction(Request
$request)
106 $entry = new Entry($this->getUser());
107 $entry->setUrl($request->get('url'));
109 if (false === $this->checkIfEntryAlreadyExists($entry)) {
110 $this->updateEntry($entry);
112 $em = $this->getDoctrine()->getManager();
113 $em->persist($entry);
116 // entry saved, dispatch event about it!
117 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
120 return $this->redirect($this->generateUrl('homepage'));
124 * @Route("/new", name="new")
126 * @return \Symfony\Component\HttpFoundation\Response
128 public function addEntryAction()
130 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
134 * Edit an entry content.
136 * @param Request $request
137 * @param Entry $entry
139 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
141 * @return \Symfony\Component\HttpFoundation\Response
143 public function editEntryAction(Request
$request, Entry
$entry)
145 $this->checkUserAction($entry);
147 $form = $this->createForm(EditEntryType
::class, $entry);
149 $form->handleRequest($request);
151 if ($form->isValid()) {
152 $em = $this->getDoctrine()->getManager();
153 $em->persist($entry);
156 $this->get('session')->getFlashBag()->add(
158 'flashes.entry.notice.entry_updated'
161 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
164 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
165 'form' => $form->createView(),
170 * Shows all entries for current user.
172 * @param Request $request
175 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
177 * @return \Symfony\Component\HttpFoundation\Response
179 public function showAllAction(Request
$request, $page)
181 return $this->showEntries('all', $request, $page);
185 * Shows unread entries for current user.
187 * @param Request $request
190 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
192 * @return \Symfony\Component\HttpFoundation\Response
194 public function showUnreadAction(Request
$request, $page)
196 // load the quickstart if no entry in database
197 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
198 return $this->redirect($this->generateUrl('quickstart'));
201 return $this->showEntries('unread', $request, $page);
205 * Shows read entries for current user.
207 * @param Request $request
210 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
212 * @return \Symfony\Component\HttpFoundation\Response
214 public function showArchiveAction(Request
$request, $page)
216 return $this->showEntries('archive', $request, $page);
220 * Shows starred entries for current user.
222 * @param Request $request
225 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
227 * @return \Symfony\Component\HttpFoundation\Response
229 public function showStarredAction(Request
$request, $page)
231 return $this->showEntries('starred', $request, $page);
235 * Global method to retrieve entries depending on the given type
236 * It returns the response to be send.
238 * @param string $type Entries type: unread, starred or archive
239 * @param Request $request
242 * @return \Symfony\Component\HttpFoundation\Response
244 private function showEntries($type, Request
$request, $page)
246 $repository = $this->get('wallabag_core.entry_repository');
250 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
254 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
258 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
262 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
266 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
270 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
273 $form = $this->createForm(EntryFilterType
::class);
275 if ($request->query
->has($form->getName())) {
276 // manually bind values from the request
277 $form->submit($request->query
->get($form->getName()));
279 // build the query from the given form object
280 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
283 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
285 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
286 ->prepare($pagerAdapter, $page);
289 $entries->setCurrentPage($page);
290 } catch (OutOfRangeCurrentPageException
$e) {
292 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
296 return $this->render(
297 'WallabagCoreBundle:Entry:entries.html.twig',
299 'form' => $form->createView(),
300 'entries' => $entries,
301 'currentPage' => $page,
307 * Shows entry content.
309 * @param Entry $entry
311 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
313 * @return \Symfony\Component\HttpFoundation\Response
315 public function viewAction(Entry
$entry)
317 $this->checkUserAction($entry);
319 return $this->render(
320 'WallabagCoreBundle:Entry:entry.html.twig',
327 * Refetch content from the website and make it readable again.
329 * @param Entry $entry
331 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
333 * @return \Symfony\Component\HttpFoundation\RedirectResponse
335 public function reloadAction(Entry
$entry)
337 $this->checkUserAction($entry);
339 $this->updateEntry($entry, 'entry_reloaded');
341 // if refreshing entry failed, don't save it
342 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
343 $bag = $this->get('session')->getFlashBag();
345 $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
347 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
350 $em = $this->getDoctrine()->getManager();
351 $em->persist($entry);
354 // entry saved, dispatch event about it!
355 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
357 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
361 * Changes read status for an entry.
363 * @param Request $request
364 * @param Entry $entry
366 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
368 * @return \Symfony\Component\HttpFoundation\RedirectResponse
370 public function toggleArchiveAction(Request
$request, Entry
$entry)
372 $this->checkUserAction($entry);
374 $entry->toggleArchive();
375 $this->getDoctrine()->getManager()->flush();
377 $message = 'flashes.entry.notice.entry_unarchived';
378 if ($entry->isArchived()) {
379 $message = 'flashes.entry.notice.entry_archived';
382 $this->get('session')->getFlashBag()->add(
387 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
389 return $this->redirect($redirectUrl);
393 * Changes starred status for an entry.
395 * @param Request $request
396 * @param Entry $entry
398 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
400 * @return \Symfony\Component\HttpFoundation\RedirectResponse
402 public function toggleStarAction(Request
$request, Entry
$entry)
404 $this->checkUserAction($entry);
406 $entry->toggleStar();
407 $this->getDoctrine()->getManager()->flush();
409 $message = 'flashes.entry.notice.entry_unstarred';
410 if ($entry->isStarred()) {
411 $message = 'flashes.entry.notice.entry_starred';
414 $this->get('session')->getFlashBag()->add(
419 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
421 return $this->redirect($redirectUrl);
425 * Deletes entry and redirect to the homepage or the last viewed page.
427 * @param Entry $entry
429 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
431 * @return \Symfony\Component\HttpFoundation\RedirectResponse
433 public function deleteEntryAction(Request
$request, Entry
$entry)
435 $this->checkUserAction($entry);
437 // generates the view url for this entry to check for redirection later
438 // to avoid redirecting to the deleted entry. Ugh.
439 $url = $this->generateUrl(
441 ['id' => $entry->getId()],
442 UrlGeneratorInterface
::ABSOLUTE_PATH
445 // entry deleted, dispatch event about it!
446 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
448 $em = $this->getDoctrine()->getManager();
452 $this->get('session')->getFlashBag()->add(
454 'flashes.entry.notice.entry_deleted'
457 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
458 $referer = $request->headers
->get('referer');
459 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
461 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
463 return $this->redirect($redirectUrl);
467 * Check if the logged user can manage the given entry.
469 * @param Entry $entry
471 private function checkUserAction(Entry
$entry)
473 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
474 throw $this->createAccessDeniedException('You can not access this entry.');
479 * Check for existing entry, if it exists, redirect to it with a message.
481 * @param Entry $entry
485 private function checkIfEntryAlreadyExists(Entry
$entry)
487 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
491 * Get public URL for entry (and generate it if necessary).
493 * @param Entry $entry
495 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
497 * @return \Symfony\Component\HttpFoundation\Response
499 public function shareAction(Entry
$entry)
501 $this->checkUserAction($entry);
503 if (null === $entry->getUuid()) {
504 $entry->generateUuid();
506 $em = $this->getDoctrine()->getManager();
507 $em->persist($entry);
511 return $this->redirect($this->generateUrl('share_entry', [
512 'uuid' => $entry->getUuid(),
517 * Disable public sharing for an entry.
519 * @param Entry $entry
521 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
523 * @return \Symfony\Component\HttpFoundation\Response
525 public function deleteShareAction(Entry
$entry)
527 $this->checkUserAction($entry);
531 $em = $this->getDoctrine()->getManager();
532 $em->persist($entry);
535 return $this->redirect($this->generateUrl('view', [
536 'id' => $entry->getId(),
541 * Ability to view a content publicly.
543 * @param Entry $entry
545 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
546 * @Cache(maxage="25200", smaxage="25200", public=true)
548 * @return \Symfony\Component\HttpFoundation\Response
550 public function shareEntryAction(Entry
$entry)
552 if (!$this->get('craue_config')->get('share_public')) {
553 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
556 return $this->render(
557 '@WallabagCore/themes/common/Entry/share.html.twig',
563 * Shows untagged articles for current user.
565 * @param Request $request
568 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
570 * @return \Symfony\Component\HttpFoundation\Response
572 public function showUntaggedEntriesAction(Request
$request, $page)
574 return $this->showEntries('untagged', $request, $page);