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
;
18 use Wallabag\CoreBundle\Form\Type\SearchEntryType
;
20 class EntryController
extends Controller
23 * @param Request $request
26 * @Route("/search/{page}", name="search", defaults={"page" = "1"})
28 * @return \Symfony\Component\HttpFoundation\Response
30 public function searchFormAction(Request
$request, $page)
32 $form = $this->createForm(SearchEntryType
::class);
34 $form->handleRequest($request);
36 if ($form->isValid()) {
37 return $this->showEntries('search', $request, $page);
40 return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [
41 'form' => $form->createView(),
46 * Fetch content and update entry.
47 * In case it fails, entry will return to avod loosing the data.
50 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
54 private function updateEntry(Entry
$entry, $prefixMessage = 'entry_saved')
56 // put default title in case of fetching content failed
57 $entry->setTitle('No title found');
59 $message = 'flashes.entry.notice.'.$prefixMessage;
62 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
63 } catch (\Exception
$e) {
64 $this->get('logger')->error('Error while saving an entry', [
69 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
72 $this->get('session')->getFlashBag()->add('notice', $message);
78 * @param Request $request
80 * @Route("/new-entry", name="new_entry")
82 * @return \Symfony\Component\HttpFoundation\Response
84 public function addEntryFormAction(Request
$request)
86 $entry = new Entry($this->getUser());
88 $form = $this->createForm(NewEntryType
::class, $entry);
90 $form->handleRequest($request);
92 if ($form->isValid()) {
93 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
95 if (false !== $existingEntry) {
96 $this->get('session')->getFlashBag()->add(
98 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
101 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
104 $this->updateEntry($entry);
106 $em = $this->getDoctrine()->getManager();
107 $em->persist($entry);
110 // entry saved, dispatch event about it!
111 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
113 return $this->redirect($this->generateUrl('homepage'));
116 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
117 'form' => $form->createView(),
122 * @param Request $request
124 * @Route("/bookmarklet", name="bookmarklet")
126 * @return \Symfony\Component\HttpFoundation\Response
128 public function addEntryViaBookmarkletAction(Request
$request)
130 $entry = new Entry($this->getUser());
131 $entry->setUrl($request->get('url'));
133 if (false === $this->checkIfEntryAlreadyExists($entry)) {
134 $this->updateEntry($entry);
136 $em = $this->getDoctrine()->getManager();
137 $em->persist($entry);
140 // entry saved, dispatch event about it!
141 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
144 return $this->redirect($this->generateUrl('homepage'));
148 * @Route("/new", name="new")
150 * @return \Symfony\Component\HttpFoundation\Response
152 public function addEntryAction()
154 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
158 * Edit an entry content.
160 * @param Request $request
161 * @param Entry $entry
163 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
165 * @return \Symfony\Component\HttpFoundation\Response
167 public function editEntryAction(Request
$request, Entry
$entry)
169 $this->checkUserAction($entry);
171 $form = $this->createForm(EditEntryType
::class, $entry);
173 $form->handleRequest($request);
175 if ($form->isValid()) {
176 $em = $this->getDoctrine()->getManager();
177 $em->persist($entry);
180 $this->get('session')->getFlashBag()->add(
182 'flashes.entry.notice.entry_updated'
185 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
188 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
189 'form' => $form->createView(),
194 * Shows all entries for current user.
196 * @param Request $request
199 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
201 * @return \Symfony\Component\HttpFoundation\Response
203 public function showAllAction(Request
$request, $page)
205 return $this->showEntries('all', $request, $page);
209 * Shows unread entries for current user.
211 * @param Request $request
214 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
216 * @return \Symfony\Component\HttpFoundation\Response
218 public function showUnreadAction(Request
$request, $page)
220 // load the quickstart if no entry in database
221 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
222 return $this->redirect($this->generateUrl('quickstart'));
225 return $this->showEntries('unread', $request, $page);
229 * Shows read entries for current user.
231 * @param Request $request
234 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
236 * @return \Symfony\Component\HttpFoundation\Response
238 public function showArchiveAction(Request
$request, $page)
240 return $this->showEntries('archive', $request, $page);
244 * Shows starred entries for current user.
246 * @param Request $request
249 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
251 * @return \Symfony\Component\HttpFoundation\Response
253 public function showStarredAction(Request
$request, $page)
255 return $this->showEntries('starred', $request, $page);
259 * Global method to retrieve entries depending on the given type
260 * It returns the response to be send.
262 * @param string $type Entries type: unread, starred or archive
263 * @param Request $request
266 * @return \Symfony\Component\HttpFoundation\Response
268 private function showEntries($type, Request
$request, $page)
270 $repository = $this->get('wallabag_core.entry_repository');
271 $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
275 $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm);
279 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
283 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
287 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
291 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
295 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
299 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
302 $form = $this->createForm(EntryFilterType
::class);
304 if ($request->query
->has($form->getName())) {
305 // manually bind values from the request
306 $form->submit($request->query
->get($form->getName()));
308 // build the query from the given form object
309 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
312 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
314 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
315 ->prepare($pagerAdapter, $page);
318 $entries->setCurrentPage($page);
319 } catch (OutOfRangeCurrentPageException
$e) {
321 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
325 return $this->render(
326 'WallabagCoreBundle:Entry:entries.html.twig', [
327 'form' => $form->createView(),
328 'entries' => $entries,
329 'currentPage' => $page,
330 'searchTerm' => $searchTerm,
336 * Shows entry content.
338 * @param Entry $entry
340 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
342 * @return \Symfony\Component\HttpFoundation\Response
344 public function viewAction(Entry
$entry)
346 $this->checkUserAction($entry);
348 return $this->render(
349 'WallabagCoreBundle:Entry:entry.html.twig',
356 * Refetch content from the website and make it readable again.
358 * @param Entry $entry
360 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
362 * @return \Symfony\Component\HttpFoundation\RedirectResponse
364 public function reloadAction(Entry
$entry)
366 $this->checkUserAction($entry);
368 $this->updateEntry($entry, 'entry_reloaded');
370 // if refreshing entry failed, don't save it
371 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
372 $bag = $this->get('session')->getFlashBag();
374 $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
376 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
379 $em = $this->getDoctrine()->getManager();
380 $em->persist($entry);
383 // entry saved, dispatch event about it!
384 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
386 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
390 * Changes read status for an entry.
392 * @param Request $request
393 * @param Entry $entry
395 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
397 * @return \Symfony\Component\HttpFoundation\RedirectResponse
399 public function toggleArchiveAction(Request
$request, Entry
$entry)
401 $this->checkUserAction($entry);
403 $entry->toggleArchive();
404 $this->getDoctrine()->getManager()->flush();
406 $message = 'flashes.entry.notice.entry_unarchived';
407 if ($entry->isArchived()) {
408 $message = 'flashes.entry.notice.entry_archived';
411 $this->get('session')->getFlashBag()->add(
416 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
418 return $this->redirect($redirectUrl);
422 * Changes starred status for an entry.
424 * @param Request $request
425 * @param Entry $entry
427 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
429 * @return \Symfony\Component\HttpFoundation\RedirectResponse
431 public function toggleStarAction(Request
$request, Entry
$entry)
433 $this->checkUserAction($entry);
435 $entry->toggleStar();
436 $this->getDoctrine()->getManager()->flush();
438 $message = 'flashes.entry.notice.entry_unstarred';
439 if ($entry->isStarred()) {
440 $message = 'flashes.entry.notice.entry_starred';
443 $this->get('session')->getFlashBag()->add(
448 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
450 return $this->redirect($redirectUrl);
454 * Deletes entry and redirect to the homepage or the last viewed page.
456 * @param Entry $entry
458 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
460 * @return \Symfony\Component\HttpFoundation\RedirectResponse
462 public function deleteEntryAction(Request
$request, Entry
$entry)
464 $this->checkUserAction($entry);
466 // generates the view url for this entry to check for redirection later
467 // to avoid redirecting to the deleted entry. Ugh.
468 $url = $this->generateUrl(
470 ['id' => $entry->getId()],
471 UrlGeneratorInterface
::ABSOLUTE_PATH
474 // entry deleted, dispatch event about it!
475 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
477 $em = $this->getDoctrine()->getManager();
481 $this->get('session')->getFlashBag()->add(
483 'flashes.entry.notice.entry_deleted'
486 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
487 $referer = $request->headers
->get('referer');
488 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
490 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
492 return $this->redirect($redirectUrl);
496 * Check if the logged user can manage the given entry.
498 * @param Entry $entry
500 private function checkUserAction(Entry
$entry)
502 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
503 throw $this->createAccessDeniedException('You can not access this entry.');
508 * Check for existing entry, if it exists, redirect to it with a message.
510 * @param Entry $entry
514 private function checkIfEntryAlreadyExists(Entry
$entry)
516 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
520 * Get public URL for entry (and generate it if necessary).
522 * @param Entry $entry
524 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
526 * @return \Symfony\Component\HttpFoundation\Response
528 public function shareAction(Entry
$entry)
530 $this->checkUserAction($entry);
532 if (null === $entry->getUuid()) {
533 $entry->generateUuid();
535 $em = $this->getDoctrine()->getManager();
536 $em->persist($entry);
540 return $this->redirect($this->generateUrl('share_entry', [
541 'uuid' => $entry->getUuid(),
546 * Disable public sharing for an entry.
548 * @param Entry $entry
550 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
552 * @return \Symfony\Component\HttpFoundation\Response
554 public function deleteShareAction(Entry
$entry)
556 $this->checkUserAction($entry);
560 $em = $this->getDoctrine()->getManager();
561 $em->persist($entry);
564 return $this->redirect($this->generateUrl('view', [
565 'id' => $entry->getId(),
570 * Ability to view a content publicly.
572 * @param Entry $entry
574 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
575 * @Cache(maxage="25200", smaxage="25200", public=true)
577 * @return \Symfony\Component\HttpFoundation\Response
579 public function shareEntryAction(Entry
$entry)
581 if (!$this->get('craue_config')->get('share_public')) {
582 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
585 return $this->render(
586 '@WallabagCore/themes/common/Entry/share.html.twig',
592 * Shows untagged articles for current user.
594 * @param Request $request
597 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
599 * @return \Symfony\Component\HttpFoundation\Response
601 public function showUntaggedEntriesAction(Request
$request, $page)
603 return $this->showEntries('untagged', $request, $page);