3 namespace Wallabag\CoreBundle\Controller
;
5 use Pagerfanta\Adapter\DoctrineORMAdapter
;
6 use Pagerfanta\Pagerfanta
;
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\Filter\EntryFilterType
;
13 use Wallabag\CoreBundle\Form\Type\EditEntryType
;
14 use Wallabag\CoreBundle\Form\Type\NewEntryType
;
16 class EntryController
extends Controller
21 private function updateEntry(Entry
$entry)
24 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
25 $em = $this->getDoctrine()->getManager();
28 } catch (\Exception
$e) {
36 * @param Request $request
38 * @Route("/new-entry", name="new_entry")
40 * @return \Symfony\Component\HttpFoundation\Response
42 public function addEntryFormAction(Request
$request)
44 $entry = new Entry($this->getUser());
46 $form = $this->createForm(NewEntryType
::class, $entry);
48 $form->handleRequest($request);
50 if ($form->isValid()) {
51 // check for existing entry, if it exists, redirect to it with a message
52 $existingEntry = $this->get('wallabag_core.entry_repository')
53 ->existByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
55 if (false !== $existingEntry) {
56 $this->get('session')->getFlashBag()->add(
58 'Entry already saved on '.$existingEntry['createdAt']->format('d-m-Y')
61 return $this->redirect($this->generateUrl('view', array('id' => $existingEntry['id'])));
64 $this->updateEntry($entry);
65 $this->get('session')->getFlashBag()->add(
70 return $this->redirect($this->generateUrl('homepage'));
73 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
74 'form' => $form->createView(),
79 * @param Request $request
81 * @Route("/bookmarklet", name="bookmarklet")
83 * @return \Symfony\Component\HttpFoundation\Response
85 public function addEntryViaBookmarklet(Request
$request)
87 $entry = new Entry($this->getUser());
88 $entry->setUrl($request->get('url'));
89 $this->updateEntry($entry);
91 return $this->redirect($this->generateUrl('homepage'));
95 * @param Request $request
97 * @Route("/new", name="new")
99 * @return \Symfony\Component\HttpFoundation\Response
101 public function addEntryAction(Request
$request)
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(
134 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
137 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
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 return $this->showEntries('unread', $request, $page);
173 * Shows read entries for current user.
175 * @param Request $request
178 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
180 * @return \Symfony\Component\HttpFoundation\Response
182 public function showArchiveAction(Request
$request, $page)
184 return $this->showEntries('archive', $request, $page);
188 * Shows starred entries for current user.
190 * @param Request $request
193 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
195 * @return \Symfony\Component\HttpFoundation\Response
197 public function showStarredAction(Request
$request, $page)
199 return $this->showEntries('starred', $request, $page);
203 * Global method to retrieve entries depending on the given type
204 * It returns the response to be send.
206 * @param string $type Entries type: unread, starred or archive
207 * @param Request $request
210 * @return \Symfony\Component\HttpFoundation\Response
212 private function showEntries($type, Request
$request, $page)
214 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
218 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
222 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
226 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
230 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
234 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
237 $form = $this->createForm(EntryFilterType
::class);
239 if ($request->query
->has($form->getName())) {
240 // manually bind values from the request
241 $form->submit($request->query
->get($form->getName()));
243 // build the query from the given form object
244 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
247 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
248 $entries = new Pagerfanta($pagerAdapter);
250 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
251 $entries->setCurrentPage($page);
253 return $this->render(
254 'WallabagCoreBundle:Entry:entries.html.twig',
256 'form' => $form->createView(),
257 'entries' => $entries,
258 'currentPage' => $page,
264 * Shows entry content.
266 * @param Entry $entry
268 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
270 * @return \Symfony\Component\HttpFoundation\Response
272 public function viewAction(Entry
$entry)
274 $this->checkUserAction($entry);
276 return $this->render(
277 'WallabagCoreBundle:Entry:entry.html.twig',
278 array('entry' => $entry)
284 * Refetch content from the website and make it readable again.
286 * @param Entry $entry
288 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
290 * @return \Symfony\Component\HttpFoundation\RedirectResponse
292 public function reloadAction(Entry
$entry)
294 $this->checkUserAction($entry);
296 $message = 'Entry reloaded';
297 if (false === $this->updateEntry($entry)) {
298 $message = 'Failed to reload entry';
301 $this->get('session')->getFlashBag()->add(
306 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
310 * Changes read status for an entry.
312 * @param Request $request
313 * @param Entry $entry
315 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
317 * @return \Symfony\Component\HttpFoundation\RedirectResponse
319 public function toggleArchiveAction(Request
$request, Entry
$entry)
321 $this->checkUserAction($entry);
323 $entry->toggleArchive();
324 $this->getDoctrine()->getManager()->flush();
326 $this->get('session')->getFlashBag()->add(
328 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
331 return $this->redirect($request->headers
->get('referer'));
335 * Changes favorite status for an entry.
337 * @param Request $request
338 * @param Entry $entry
340 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
342 * @return \Symfony\Component\HttpFoundation\RedirectResponse
344 public function toggleStarAction(Request
$request, Entry
$entry)
346 $this->checkUserAction($entry);
348 $entry->toggleStar();
349 $this->getDoctrine()->getManager()->flush();
351 $this->get('session')->getFlashBag()->add(
353 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
356 return $this->redirect($request->headers
->get('referer'));
360 * Deletes entry and redirect to the homepage or the last viewed page.
362 * @param Entry $entry
364 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
366 * @return \Symfony\Component\HttpFoundation\RedirectResponse
368 public function deleteEntryAction(Request
$request, Entry
$entry)
370 $this->checkUserAction($entry);
372 // generates the view url for this entry to check for redirection later
373 // to avoid redirecting to the deleted entry. Ugh.
374 $url = $this->generateUrl(
376 array('id' => $entry->getId()),
377 UrlGeneratorInterface
::ABSOLUTE_URL
380 $em = $this->getDoctrine()->getManager();
384 $this->get('session')->getFlashBag()->add(
389 // don't redirect user to the deleted entry
390 return $this->redirect($url !== $request->headers
->get('referer') ? $request->headers
->get('referer') : $this->generateUrl('homepage'));
394 * Check if the logged user can manage the given entry.
396 * @param Entry $entry
398 private function checkUserAction(Entry
$entry)
400 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
401 throw $this->createAccessDeniedException('You can not access this entry.');