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 // if refreshing entry failed, don't save it
334 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
335 $bag = $this->get('session')->getFlashBag();
337 $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
339 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
342 $em = $this->getDoctrine()->getManager();
343 $em->persist($entry);
346 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
350 * Changes read status for an entry.
352 * @param Request $request
353 * @param Entry $entry
355 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
357 * @return \Symfony\Component\HttpFoundation\RedirectResponse
359 public function toggleArchiveAction(Request
$request, Entry
$entry)
361 $this->checkUserAction($entry);
363 $entry->toggleArchive();
364 $this->getDoctrine()->getManager()->flush();
366 $message = 'flashes.entry.notice.entry_unarchived';
367 if ($entry->isArchived()) {
368 $message = 'flashes.entry.notice.entry_archived';
371 $this->get('session')->getFlashBag()->add(
376 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
378 return $this->redirect($redirectUrl);
382 * Changes starred status for an entry.
384 * @param Request $request
385 * @param Entry $entry
387 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
389 * @return \Symfony\Component\HttpFoundation\RedirectResponse
391 public function toggleStarAction(Request
$request, Entry
$entry)
393 $this->checkUserAction($entry);
395 $entry->toggleStar();
396 $this->getDoctrine()->getManager()->flush();
398 $message = 'flashes.entry.notice.entry_unstarred';
399 if ($entry->isStarred()) {
400 $message = 'flashes.entry.notice.entry_starred';
403 $this->get('session')->getFlashBag()->add(
408 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
410 return $this->redirect($redirectUrl);
414 * Deletes entry and redirect to the homepage or the last viewed page.
416 * @param Entry $entry
418 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
420 * @return \Symfony\Component\HttpFoundation\RedirectResponse
422 public function deleteEntryAction(Request
$request, Entry
$entry)
424 $this->checkUserAction($entry);
426 // generates the view url for this entry to check for redirection later
427 // to avoid redirecting to the deleted entry. Ugh.
428 $url = $this->generateUrl(
430 ['id' => $entry->getId()],
431 UrlGeneratorInterface
::ABSOLUTE_PATH
434 $em = $this->getDoctrine()->getManager();
438 $this->get('session')->getFlashBag()->add(
440 'flashes.entry.notice.entry_deleted'
443 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
444 $referer = $request->headers
->get('referer');
445 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
447 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
449 return $this->redirect($redirectUrl);
453 * Check if the logged user can manage the given entry.
455 * @param Entry $entry
457 private function checkUserAction(Entry
$entry)
459 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
460 throw $this->createAccessDeniedException('You can not access this entry.');
465 * Check for existing entry, if it exists, redirect to it with a message.
467 * @param Entry $entry
471 private function checkIfEntryAlreadyExists(Entry
$entry)
473 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
477 * Get public URL for entry (and generate it if necessary).
479 * @param Entry $entry
481 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
483 * @return \Symfony\Component\HttpFoundation\Response
485 public function shareAction(Entry
$entry)
487 $this->checkUserAction($entry);
489 if (null === $entry->getUuid()) {
490 $entry->generateUuid();
492 $em = $this->getDoctrine()->getManager();
493 $em->persist($entry);
497 return $this->redirect($this->generateUrl('share_entry', [
498 'uuid' => $entry->getUuid(),
503 * Disable public sharing for an entry.
505 * @param Entry $entry
507 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
509 * @return \Symfony\Component\HttpFoundation\Response
511 public function deleteShareAction(Entry
$entry)
513 $this->checkUserAction($entry);
517 $em = $this->getDoctrine()->getManager();
518 $em->persist($entry);
521 return $this->redirect($this->generateUrl('view', [
522 'id' => $entry->getId(),
527 * Ability to view a content publicly.
529 * @param Entry $entry
531 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
532 * @Cache(maxage="25200", smaxage="25200", public=true)
534 * @return \Symfony\Component\HttpFoundation\Response
536 public function shareEntryAction(Entry
$entry)
538 if (!$this->get('craue_config')->get('share_public')) {
539 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
542 return $this->render(
543 '@WallabagCore/themes/common/Entry/share.html.twig',
549 * Shows untagged articles for current user.
551 * @param Request $request
554 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
556 * @return \Symfony\Component\HttpFoundation\Response
558 public function showUntaggedEntriesAction(Request
$request, $page)
560 return $this->showEntries('untagged', $request, $page);