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
22 private function updateEntry(Entry
$entry)
25 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
27 $em = $this->getDoctrine()->getManager();
30 } catch (\Exception
$e) {
31 $this->get('logger')->error('Error while saving an entry', [
43 * @param Request $request
45 * @Route("/new-entry", name="new_entry")
47 * @return \Symfony\Component\HttpFoundation\Response
49 public function addEntryFormAction(Request
$request)
51 $entry = new Entry($this->getUser());
53 $form = $this->createForm(NewEntryType
::class, $entry);
55 $form->handleRequest($request);
57 if ($form->isValid()) {
58 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
60 if (false !== $existingEntry) {
61 $this->get('session')->getFlashBag()->add(
63 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
66 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
69 $message = 'flashes.entry.notice.entry_saved';
70 if (false === $this->updateEntry($entry)) {
71 $message = 'flashes.entry.notice.entry_saved_failed';
74 $this->get('session')->getFlashBag()->add('notice', $message);
76 return $this->redirect($this->generateUrl('homepage'));
79 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
80 'form' => $form->createView(),
85 * @param Request $request
87 * @Route("/bookmarklet", name="bookmarklet")
89 * @return \Symfony\Component\HttpFoundation\Response
91 public function addEntryViaBookmarkletAction(Request
$request)
93 $entry = new Entry($this->getUser());
94 $entry->setUrl($request->get('url'));
96 if (false === $this->checkIfEntryAlreadyExists($entry)) {
97 $this->updateEntry($entry);
100 return $this->redirect($this->generateUrl('homepage'));
104 * @Route("/new", name="new")
106 * @return \Symfony\Component\HttpFoundation\Response
108 public function addEntryAction()
110 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
114 * Edit an entry content.
116 * @param Request $request
117 * @param Entry $entry
119 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
121 * @return \Symfony\Component\HttpFoundation\Response
123 public function editEntryAction(Request
$request, Entry
$entry)
125 $this->checkUserAction($entry);
127 $form = $this->createForm(EditEntryType
::class, $entry);
129 $form->handleRequest($request);
131 if ($form->isValid()) {
132 $em = $this->getDoctrine()->getManager();
133 $em->persist($entry);
136 $this->get('session')->getFlashBag()->add(
138 'flashes.entry.notice.entry_updated'
141 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
144 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
145 'form' => $form->createView(),
150 * Shows all entries for current user.
152 * @param Request $request
155 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
157 * @return \Symfony\Component\HttpFoundation\Response
159 public function showAllAction(Request
$request, $page)
161 return $this->showEntries('all', $request, $page);
165 * Shows unread entries for current user.
167 * @param Request $request
170 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
172 * @return \Symfony\Component\HttpFoundation\Response
174 public function showUnreadAction(Request
$request, $page)
176 // load the quickstart if no entry in database
177 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
178 return $this->redirect($this->generateUrl('quickstart'));
181 return $this->showEntries('unread', $request, $page);
185 * Shows read entries for current user.
187 * @param Request $request
190 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
192 * @return \Symfony\Component\HttpFoundation\Response
194 public function showArchiveAction(Request
$request, $page)
196 return $this->showEntries('archive', $request, $page);
200 * Shows starred entries for current user.
202 * @param Request $request
205 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
207 * @return \Symfony\Component\HttpFoundation\Response
209 public function showStarredAction(Request
$request, $page)
211 return $this->showEntries('starred', $request, $page);
215 * Global method to retrieve entries depending on the given type
216 * It returns the response to be send.
218 * @param string $type Entries type: unread, starred or archive
219 * @param Request $request
222 * @return \Symfony\Component\HttpFoundation\Response
224 private function showEntries($type, Request
$request, $page)
226 $repository = $this->get('wallabag_core.entry_repository');
230 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
234 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
238 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
242 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
246 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
250 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
253 $form = $this->createForm(EntryFilterType
::class);
255 if ($request->query
->has($form->getName())) {
256 // manually bind values from the request
257 $form->submit($request->query
->get($form->getName()));
259 // build the query from the given form object
260 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
263 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
265 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
266 ->prepare($pagerAdapter, $page);
269 $entries->setCurrentPage($page);
270 } catch (OutOfRangeCurrentPageException
$e) {
272 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
276 return $this->render(
277 'WallabagCoreBundle:Entry:entries.html.twig',
279 'form' => $form->createView(),
280 'entries' => $entries,
281 'currentPage' => $page,
287 * Shows entry content.
289 * @param Entry $entry
291 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
293 * @return \Symfony\Component\HttpFoundation\Response
295 public function viewAction(Entry
$entry)
297 $this->checkUserAction($entry);
299 return $this->render(
300 'WallabagCoreBundle:Entry:entry.html.twig',
307 * Refetch content from the website and make it readable again.
309 * @param Entry $entry
311 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
313 * @return \Symfony\Component\HttpFoundation\RedirectResponse
315 public function reloadAction(Entry
$entry)
317 $this->checkUserAction($entry);
319 $message = 'flashes.entry.notice.entry_reloaded';
320 if (false === $this->updateEntry($entry)) {
321 $message = 'flashes.entry.notice.entry_reload_failed';
324 $this->get('session')->getFlashBag()->add(
329 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
333 * Changes read status for an entry.
335 * @param Request $request
336 * @param Entry $entry
338 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
340 * @return \Symfony\Component\HttpFoundation\RedirectResponse
342 public function toggleArchiveAction(Request
$request, Entry
$entry)
344 $this->checkUserAction($entry);
346 $entry->toggleArchive();
347 $this->getDoctrine()->getManager()->flush();
349 $message = 'flashes.entry.notice.entry_unarchived';
350 if ($entry->isArchived()) {
351 $message = 'flashes.entry.notice.entry_archived';
354 $this->get('session')->getFlashBag()->add(
359 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
361 return $this->redirect($redirectUrl);
365 * Changes starred status for an entry.
367 * @param Request $request
368 * @param Entry $entry
370 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
372 * @return \Symfony\Component\HttpFoundation\RedirectResponse
374 public function toggleStarAction(Request
$request, Entry
$entry)
376 $this->checkUserAction($entry);
378 $entry->toggleStar();
379 $this->getDoctrine()->getManager()->flush();
381 $message = 'flashes.entry.notice.entry_unstarred';
382 if ($entry->isStarred()) {
383 $message = 'flashes.entry.notice.entry_starred';
386 $this->get('session')->getFlashBag()->add(
391 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
393 return $this->redirect($redirectUrl);
397 * Deletes entry and redirect to the homepage or the last viewed page.
399 * @param Entry $entry
401 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
403 * @return \Symfony\Component\HttpFoundation\RedirectResponse
405 public function deleteEntryAction(Request
$request, Entry
$entry)
407 $this->checkUserAction($entry);
409 // generates the view url for this entry to check for redirection later
410 // to avoid redirecting to the deleted entry. Ugh.
411 $url = $this->generateUrl(
413 ['id' => $entry->getId()],
414 UrlGeneratorInterface
::ABSOLUTE_PATH
417 $em = $this->getDoctrine()->getManager();
421 $this->get('session')->getFlashBag()->add(
423 'flashes.entry.notice.entry_deleted'
426 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
427 $referer = $request->headers
->get('referer');
428 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
430 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
432 return $this->redirect($redirectUrl);
436 * Check if the logged user can manage the given entry.
438 * @param Entry $entry
440 private function checkUserAction(Entry
$entry)
442 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
443 throw $this->createAccessDeniedException('You can not access this entry.');
448 * Check for existing entry, if it exists, redirect to it with a message.
450 * @param Entry $entry
454 private function checkIfEntryAlreadyExists(Entry
$entry)
456 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
460 * Get public URL for entry (and generate it if necessary).
462 * @param Entry $entry
464 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
466 * @return \Symfony\Component\HttpFoundation\Response
468 public function shareAction(Entry
$entry)
470 $this->checkUserAction($entry);
472 if (null === $entry->getUuid()) {
473 $entry->generateUuid();
475 $em = $this->getDoctrine()->getManager();
476 $em->persist($entry);
480 return $this->redirect($this->generateUrl('share_entry', [
481 'uuid' => $entry->getUuid(),
486 * Disable public sharing for an entry.
488 * @param Entry $entry
490 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
492 * @return \Symfony\Component\HttpFoundation\Response
494 public function deleteShareAction(Entry
$entry)
496 $this->checkUserAction($entry);
500 $em = $this->getDoctrine()->getManager();
501 $em->persist($entry);
504 return $this->redirect($this->generateUrl('view', [
505 'id' => $entry->getId(),
510 * Ability to view a content publicly.
512 * @param Entry $entry
514 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
515 * @Cache(maxage="25200", smaxage="25200", public=true)
517 * @return \Symfony\Component\HttpFoundation\Response
519 public function shareEntryAction(Entry
$entry)
521 if (!$this->get('craue_config')->get('share_public')) {
522 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
525 return $this->render(
526 '@WallabagCore/themes/share.html.twig',
532 * Shows untagged articles for current user.
534 * @param Request $request
537 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
539 * @return \Symfony\Component\HttpFoundation\Response
541 public function showUntaggedEntriesAction(Request
$request, $page)
543 return $this->showEntries('untagged', $request, $page);