3 namespace Wallabag\CoreBundle\Controller
;
5 use Pagerfanta\Adapter\DoctrineORMAdapter
;
6 use Pagerfanta\Exception\OutOfRangeCurrentPageException
;
7 use Pagerfanta\Pagerfanta
;
8 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
9 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
12 use Wallabag\CoreBundle\Entity\Entry
;
13 use Wallabag\CoreBundle\Form\Type\EntryFilterType
;
14 use Wallabag\CoreBundle\Form\Type\EditEntryType
;
15 use Wallabag\CoreBundle\Form\Type\NewEntryType
;
17 class EntryController
extends Controller
22 private function updateEntry(Entry
$entry)
25 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
26 $em = $this->getDoctrine()->getManager();
29 } catch (\Exception
$e) {
37 * @param Request $request
39 * @Route("/new-entry", name="new_entry")
41 * @return \Symfony\Component\HttpFoundation\Response
43 public function addEntryFormAction(Request
$request)
45 $entry = new Entry($this->getUser());
47 $form = $this->createForm(NewEntryType
::class, $entry);
49 $form->handleRequest($request);
51 if ($form->isValid()) {
52 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
54 if (false !== $existingEntry) {
55 $this->get('session')->getFlashBag()->add(
57 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
60 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
63 $this->updateEntry($entry);
64 $this->get('session')->getFlashBag()->add(
66 'flashes.entry.notice.entry_saved'
69 return $this->redirect($this->generateUrl('homepage'));
72 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
73 'form' => $form->createView(),
78 * @param Request $request
80 * @Route("/bookmarklet", name="bookmarklet")
82 * @return \Symfony\Component\HttpFoundation\Response
84 public function addEntryViaBookmarkletAction(Request
$request)
86 $entry = new Entry($this->getUser());
87 $entry->setUrl($request->get('url'));
89 if (false === $this->checkIfEntryAlreadyExists($entry)) {
90 $this->updateEntry($entry);
93 return $this->redirect($this->generateUrl('homepage'));
97 * @Route("/new", name="new")
99 * @return \Symfony\Component\HttpFoundation\Response
101 public function addEntryAction()
103 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
107 * Edit an entry content.
109 * @param Request $request
110 * @param Entry $entry
112 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
114 * @return \Symfony\Component\HttpFoundation\Response
116 public function editEntryAction(Request
$request, Entry
$entry)
118 $this->checkUserAction($entry);
120 $form = $this->createForm(EditEntryType
::class, $entry);
122 $form->handleRequest($request);
124 if ($form->isValid()) {
125 $em = $this->getDoctrine()->getManager();
126 $em->persist($entry);
129 $this->get('session')->getFlashBag()->add(
131 'flashes.entry.notice.entry_updated'
134 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
137 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
138 'form' => $form->createView(),
143 * Shows all entries for current user.
145 * @param Request $request
148 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
150 * @return \Symfony\Component\HttpFoundation\Response
152 public function showAllAction(Request
$request, $page)
154 return $this->showEntries('all', $request, $page);
158 * Shows unread entries for current user.
160 * @param Request $request
163 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
165 * @return \Symfony\Component\HttpFoundation\Response
167 public function showUnreadAction(Request
$request, $page)
169 // load the quickstart if no entry in database
170 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
171 return $this->redirect($this->generateUrl('quickstart'));
174 return $this->showEntries('unread', $request, $page);
178 * Shows read entries for current user.
180 * @param Request $request
183 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
185 * @return \Symfony\Component\HttpFoundation\Response
187 public function showArchiveAction(Request
$request, $page)
189 return $this->showEntries('archive', $request, $page);
193 * Shows starred entries for current user.
195 * @param Request $request
198 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
200 * @return \Symfony\Component\HttpFoundation\Response
202 public function showStarredAction(Request
$request, $page)
204 return $this->showEntries('starred', $request, $page);
208 * Global method to retrieve entries depending on the given type
209 * It returns the response to be send.
211 * @param string $type Entries type: unread, starred or archive
212 * @param Request $request
215 * @return \Symfony\Component\HttpFoundation\Response
217 private function showEntries($type, Request
$request, $page)
219 $repository = $this->get('wallabag_core.entry_repository');
223 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
227 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
231 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
235 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
239 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
242 $form = $this->createForm(EntryFilterType
::class);
244 if ($request->query
->has($form->getName())) {
245 // manually bind values from the request
246 $form->submit($request->query
->get($form->getName()));
248 // build the query from the given form object
249 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
252 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
253 $entries = new Pagerfanta($pagerAdapter);
255 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
257 $entries->setCurrentPage($page);
258 } catch (OutOfRangeCurrentPageException
$e) {
260 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
264 return $this->render(
265 'WallabagCoreBundle:Entry:entries.html.twig',
267 'form' => $form->createView(),
268 'entries' => $entries,
269 'currentPage' => $page,
275 * Shows entry content.
277 * @param Entry $entry
279 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
281 * @return \Symfony\Component\HttpFoundation\Response
283 public function viewAction(Entry
$entry)
285 $this->checkUserAction($entry);
287 return $this->render(
288 'WallabagCoreBundle:Entry:entry.html.twig',
295 * Refetch content from the website and make it readable again.
297 * @param Entry $entry
299 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
301 * @return \Symfony\Component\HttpFoundation\RedirectResponse
303 public function reloadAction(Entry
$entry)
305 $this->checkUserAction($entry);
307 $message = 'flashes.entry.notice.entry_reloaded';
308 if (false === $this->updateEntry($entry)) {
309 $message = 'flashes.entry.notice.entry_reload_failed';
312 $this->get('session')->getFlashBag()->add(
317 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
321 * Changes read status for an entry.
323 * @param Request $request
324 * @param Entry $entry
326 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
328 * @return \Symfony\Component\HttpFoundation\RedirectResponse
330 public function toggleArchiveAction(Request
$request, Entry
$entry)
332 $this->checkUserAction($entry);
334 $entry->toggleArchive();
335 $this->getDoctrine()->getManager()->flush();
337 $message = 'flashes.entry.notice.entry_unarchived';
338 if ($entry->isArchived()) {
339 $message = 'flashes.entry.notice.entry_archived';
342 $this->get('session')->getFlashBag()->add(
347 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
349 return $this->redirect($redirectUrl);
353 * Changes favorite status for an entry.
355 * @param Request $request
356 * @param Entry $entry
358 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
360 * @return \Symfony\Component\HttpFoundation\RedirectResponse
362 public function toggleStarAction(Request
$request, Entry
$entry)
364 $this->checkUserAction($entry);
366 $entry->toggleStar();
367 $this->getDoctrine()->getManager()->flush();
369 $message = 'flashes.entry.notice.entry_unstarred';
370 if ($entry->isStarred()) {
371 $message = 'flashes.entry.notice.entry_starred';
374 $this->get('session')->getFlashBag()->add(
379 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers
->get('referer'));
381 return $this->redirect($redirectUrl);
385 * Deletes entry and redirect to the homepage or the last viewed page.
387 * @param Entry $entry
389 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
391 * @return \Symfony\Component\HttpFoundation\RedirectResponse
393 public function deleteEntryAction(Request
$request, Entry
$entry)
395 $this->checkUserAction($entry);
397 // generates the view url for this entry to check for redirection later
398 // to avoid redirecting to the deleted entry. Ugh.
399 $url = $this->generateUrl(
401 ['id' => $entry->getId()],
402 UrlGeneratorInterface
::ABSOLUTE_URL
405 $em = $this->getDoctrine()->getManager();
409 $this->get('session')->getFlashBag()->add(
411 'flashes.entry.notice.entry_deleted'
414 // don't redirect user to the deleted entry
415 $to = ($url !== $request->headers
->get('referer') ? $request->headers
->get('referer') : null);
417 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
419 return $this->redirect($redirectUrl);
423 * Check if the logged user can manage the given entry.
425 * @param Entry $entry
427 private function checkUserAction(Entry
$entry)
429 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
430 throw $this->createAccessDeniedException('You can not access this entry.');
435 * Check for existing entry, if it exists, redirect to it with a message.
437 * @param Entry $entry
441 private function checkIfEntryAlreadyExists(Entry
$entry)
443 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());