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
;
17 class EntryController
extends Controller
20 * Fetch content and update entry.
21 * In case it fails, entry will return to avod loosing the data.
24 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
28 private function updateEntry(Entry
$entry, $prefixMessage = 'entry_saved')
30 // put default title in case of fetching content failed
31 $entry->setTitle('No title found');
33 $message = 'flashes.entry.notice.'.$prefixMessage;
36 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
37 } catch (\Exception
$e) {
38 $this->get('logger')->error('Error while saving an entry', [
43 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
46 $this->get('session')->getFlashBag()->add('notice', $message);
52 * @param Request $request
54 * @Route("/new-entry", name="new_entry")
56 * @return \Symfony\Component\HttpFoundation\Response
58 public function addEntryFormAction(Request
$request)
60 $entry = new Entry($this->getUser());
62 $form = $this->createForm(NewEntryType
::class, $entry);
64 $form->handleRequest($request);
66 if ($form->isValid()) {
67 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
69 if (false !== $existingEntry) {
70 $this->get('session')->getFlashBag()->add(
72 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
75 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
78 $this->updateEntry($entry);
80 $em = $this->getDoctrine()->getManager();
84 return $this->redirect($this->generateUrl('homepage'));
87 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
88 'form' => $form->createView(),
93 * @param Request $request
95 * @Route("/bookmarklet", name="bookmarklet")
97 * @return \Symfony\Component\HttpFoundation\Response
99 public function addEntryViaBookmarkletAction(Request
$request)
101 $entry = new Entry($this->getUser());
102 $entry->setUrl($request->get('url'));
104 if (false === $this->checkIfEntryAlreadyExists($entry)) {
105 $this->updateEntry($entry);
107 $em = $this->getDoctrine()->getManager();
108 $em->persist($entry);
112 return $this->redirect($this->generateUrl('homepage'));
116 * @Route("/new", name="new")
118 * @return \Symfony\Component\HttpFoundation\Response
120 public function addEntryAction()
122 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
126 * Edit an entry content.
128 * @param Request $request
129 * @param Entry $entry
131 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
133 * @return \Symfony\Component\HttpFoundation\Response
135 public function editEntryAction(Request
$request, Entry
$entry)
137 $this->checkUserAction($entry);
139 $form = $this->createForm(EditEntryType
::class, $entry);
141 $form->handleRequest($request);
143 if ($form->isValid()) {
144 $em = $this->getDoctrine()->getManager();
145 $em->persist($entry);
148 $this->get('session')->getFlashBag()->add(
150 'flashes.entry.notice.entry_updated'
153 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
156 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
157 'form' => $form->createView(),
162 * Shows all entries for current user.
164 * @param Request $request
167 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
169 * @return \Symfony\Component\HttpFoundation\Response
171 public function showAllAction(Request
$request, $page)
173 return $this->showEntries('all', $request, $page);
177 * Shows unread entries for current user.
179 * @param Request $request
182 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
184 * @return \Symfony\Component\HttpFoundation\Response
186 public function showUnreadAction(Request
$request, $page)
188 // load the quickstart if no entry in database
189 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
190 return $this->redirect($this->generateUrl('quickstart'));
193 return $this->showEntries('unread', $request, $page);
197 * Shows read entries for current user.
199 * @param Request $request
202 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
204 * @return \Symfony\Component\HttpFoundation\Response
206 public function showArchiveAction(Request
$request, $page)
208 return $this->showEntries('archive', $request, $page);
212 * Shows starred entries for current user.
214 * @param Request $request
217 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
219 * @return \Symfony\Component\HttpFoundation\Response
221 public function showStarredAction(Request
$request, $page)
223 return $this->showEntries('starred', $request, $page);
227 * Global method to retrieve entries depending on the given type
228 * It returns the response to be send.
230 * @param string $type Entries type: unread, starred or archive
231 * @param Request $request
234 * @return \Symfony\Component\HttpFoundation\Response
236 private function showEntries($type, Request
$request, $page)
238 $repository = $this->get('wallabag_core.entry_repository');
242 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
246 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
250 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
254 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
258 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
262 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
265 $form = $this->createForm(EntryFilterType
::class);
267 if ($request->query
->has($form->getName())) {
268 // manually bind values from the request
269 $form->submit($request->query
->get($form->getName()));
271 // build the query from the given form object
272 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
275 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
277 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
278 ->prepare($pagerAdapter, $page);
281 $entries->setCurrentPage($page);
282 } catch (OutOfRangeCurrentPageException
$e) {
284 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
288 return $this->render(
289 'WallabagCoreBundle:Entry:entries.html.twig',
291 'form' => $form->createView(),
292 'entries' => $entries,
293 'currentPage' => $page,
299 * Shows entry content.
301 * @param Entry $entry
303 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
305 * @return \Symfony\Component\HttpFoundation\Response
307 public function viewAction(Entry
$entry)
309 $this->checkUserAction($entry);
311 return $this->render(
312 'WallabagCoreBundle:Entry:entry.html.twig',
319 * Refetch content from the website and make it readable again.
321 * @param Entry $entry
323 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
325 * @return \Symfony\Component\HttpFoundation\RedirectResponse
327 public function reloadAction(Entry
$entry)
329 $this->checkUserAction($entry);
331 $this->updateEntry($entry, 'entry_reloaded');
333 $em = $this->getDoctrine()->getManager();
334 $em->persist($entry);
337 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
341 * Changes read status for an entry.
343 * @param Request $request
344 * @param Entry $entry
346 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
348 * @return \Symfony\Component\HttpFoundation\RedirectResponse
350 public function toggleArchiveAction(Request
$request, Entry
$entry)
352 $this->checkUserAction($entry);
354 $entry->toggleArchive();
355 $this->getDoctrine()->getManager()->flush();
357 $message = 'flashes.entry.notice.entry_unarchived';
358 if ($entry->isArchived()) {
359 $message = 'flashes.entry.notice.entry_archived';
362 $this->get('session')->getFlashBag()->add(
367 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
369 return $this->redirect($redirectUrl);
373 * Changes starred status for an entry.
375 * @param Request $request
376 * @param Entry $entry
378 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
380 * @return \Symfony\Component\HttpFoundation\RedirectResponse
382 public function toggleStarAction(Request
$request, Entry
$entry)
384 $this->checkUserAction($entry);
386 $entry->toggleStar();
387 $this->getDoctrine()->getManager()->flush();
389 $message = 'flashes.entry.notice.entry_unstarred';
390 if ($entry->isStarred()) {
391 $message = 'flashes.entry.notice.entry_starred';
394 $this->get('session')->getFlashBag()->add(
399 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
401 return $this->redirect($redirectUrl);
405 * Deletes entry and redirect to the homepage or the last viewed page.
407 * @param Entry $entry
409 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
411 * @return \Symfony\Component\HttpFoundation\RedirectResponse
413 public function deleteEntryAction(Request
$request, Entry
$entry)
415 $this->checkUserAction($entry);
417 // generates the view url for this entry to check for redirection later
418 // to avoid redirecting to the deleted entry. Ugh.
419 $url = $this->generateUrl(
421 ['id' => $entry->getId()],
422 UrlGeneratorInterface
::ABSOLUTE_PATH
425 $em = $this->getDoctrine()->getManager();
429 $this->get('session')->getFlashBag()->add(
431 'flashes.entry.notice.entry_deleted'
434 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
435 $referer = $request->headers
->get('referer');
436 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
438 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
440 return $this->redirect($redirectUrl);
444 * Check if the logged user can manage the given entry.
446 * @param Entry $entry
448 private function checkUserAction(Entry
$entry)
450 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
451 throw $this->createAccessDeniedException('You can not access this entry.');
456 * Check for existing entry, if it exists, redirect to it with a message.
458 * @param Entry $entry
462 private function checkIfEntryAlreadyExists(Entry
$entry)
464 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
468 * Get public URL for entry (and generate it if necessary).
470 * @param Entry $entry
472 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
474 * @return \Symfony\Component\HttpFoundation\Response
476 public function shareAction(Entry
$entry)
478 $this->checkUserAction($entry);
480 if (null === $entry->getUuid()) {
481 $entry->generateUuid();
483 $em = $this->getDoctrine()->getManager();
484 $em->persist($entry);
488 return $this->redirect($this->generateUrl('share_entry', [
489 'uuid' => $entry->getUuid(),
494 * Disable public sharing for an entry.
496 * @param Entry $entry
498 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
500 * @return \Symfony\Component\HttpFoundation\Response
502 public function deleteShareAction(Entry
$entry)
504 $this->checkUserAction($entry);
508 $em = $this->getDoctrine()->getManager();
509 $em->persist($entry);
512 return $this->redirect($this->generateUrl('view', [
513 'id' => $entry->getId(),
518 * Ability to view a content publicly.
520 * @param Entry $entry
522 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
523 * @Cache(maxage="25200", smaxage="25200", public=true)
525 * @return \Symfony\Component\HttpFoundation\Response
527 public function shareEntryAction(Entry
$entry)
529 if (!$this->get('craue_config')->get('share_public')) {
530 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
533 return $this->render(
534 '@WallabagCore/themes/share.html.twig',
540 * Shows untagged articles for current user.
542 * @param Request $request
545 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
547 * @return \Symfony\Component\HttpFoundation\Response
549 public function showUntaggedEntriesAction(Request
$request, $page)
551 return $this->showEntries('untagged', $request, $page);