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\Form\Type\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 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
53 if (false !== $existingEntry) {
54 $this->get('session')->getFlashBag()->add(
56 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', array('%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')))
59 return $this->redirect($this->generateUrl('view', array('id' => $existingEntry->getId())));
62 $this->updateEntry($entry);
63 $this->get('session')->getFlashBag()->add(
65 'flashes.entry.notice.entry_saved'
68 return $this->redirect($this->generateUrl('homepage'));
71 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
72 'form' => $form->createView(),
77 * @param Request $request
79 * @Route("/bookmarklet", name="bookmarklet")
81 * @return \Symfony\Component\HttpFoundation\Response
83 public function addEntryViaBookmarkletAction(Request
$request)
85 $entry = new Entry($this->getUser());
86 $entry->setUrl($request->get('url'));
88 if (false === $this->checkIfEntryAlreadyExists($entry)) {
89 $this->updateEntry($entry);
92 return $this->redirect($this->generateUrl('homepage'));
96 * @Route("/new", name="new")
98 * @return \Symfony\Component\HttpFoundation\Response
100 public function addEntryAction()
102 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
106 * Edit an entry content.
108 * @param Request $request
109 * @param Entry $entry
111 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
113 * @return \Symfony\Component\HttpFoundation\Response
115 public function editEntryAction(Request
$request, Entry
$entry)
117 $this->checkUserAction($entry);
119 $form = $this->createForm(EditEntryType
::class, $entry);
121 $form->handleRequest($request);
123 if ($form->isValid()) {
124 $em = $this->getDoctrine()->getManager();
125 $em->persist($entry);
128 $this->get('session')->getFlashBag()->add(
130 'flashes.entry.notice.entry_updated'
133 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
136 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
137 'form' => $form->createView(),
142 * Shows all entries for current user.
144 * @param Request $request
147 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
149 * @return \Symfony\Component\HttpFoundation\Response
151 public function showAllAction(Request
$request, $page)
153 return $this->showEntries('all', $request, $page);
157 * Shows unread entries for current user.
159 * @param Request $request
162 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
164 * @return \Symfony\Component\HttpFoundation\Response
166 public function showUnreadAction(Request
$request, $page)
168 // load the quickstart if no entry in database
169 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
170 return $this->redirect($this->generateUrl('quickstart'));
173 return $this->showEntries('unread', $request, $page);
177 * Shows read entries for current user.
179 * @param Request $request
182 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
184 * @return \Symfony\Component\HttpFoundation\Response
186 public function showArchiveAction(Request
$request, $page)
188 return $this->showEntries('archive', $request, $page);
192 * Shows starred entries for current user.
194 * @param Request $request
197 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
199 * @return \Symfony\Component\HttpFoundation\Response
201 public function showStarredAction(Request
$request, $page)
203 return $this->showEntries('starred', $request, $page);
207 * Global method to retrieve entries depending on the given type
208 * It returns the response to be send.
210 * @param string $type Entries type: unread, starred or archive
211 * @param Request $request
214 * @return \Symfony\Component\HttpFoundation\Response
216 private function showEntries($type, Request
$request, $page)
218 $repository = $this->get('wallabag_core.entry_repository');
222 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
226 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
230 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
234 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
238 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
241 $form = $this->createForm(EntryFilterType
::class);
243 if ($request->query
->has($form->getName())) {
244 // manually bind values from the request
245 $form->submit($request->query
->get($form->getName()));
247 // build the query from the given form object
248 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
251 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
252 $entries = new Pagerfanta($pagerAdapter);
254 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
255 $entries->setCurrentPage($page);
257 return $this->render(
258 'WallabagCoreBundle:Entry:entries.html.twig',
260 'form' => $form->createView(),
261 'entries' => $entries,
262 'currentPage' => $page,
268 * Shows entry content.
270 * @param Entry $entry
272 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
274 * @return \Symfony\Component\HttpFoundation\Response
276 public function viewAction(Entry
$entry)
278 $this->checkUserAction($entry);
280 return $this->render(
281 'WallabagCoreBundle:Entry:entry.html.twig',
282 array('entry' => $entry)
288 * Refetch content from the website and make it readable again.
290 * @param Entry $entry
292 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
294 * @return \Symfony\Component\HttpFoundation\RedirectResponse
296 public function reloadAction(Entry
$entry)
298 $this->checkUserAction($entry);
300 $message = 'flashes.entry.notice.entry_reloaded';
301 if (false === $this->updateEntry($entry)) {
302 $message = 'flashes.entry.notice.entry_reload_failed';
305 $this->get('session')->getFlashBag()->add(
310 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
314 * Changes read status for an entry.
316 * @param Request $request
317 * @param Entry $entry
319 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
321 * @return \Symfony\Component\HttpFoundation\RedirectResponse
323 public function toggleArchiveAction(Request
$request, Entry
$entry)
325 $this->checkUserAction($entry);
327 $entry->toggleArchive();
328 $this->getDoctrine()->getManager()->flush();
330 $message = 'flashes.entry.notice.entry_unarchived';
331 if ($entry->isArchived()) {
332 $message = 'flashes.entry.notice.entry_archived';
335 $this->get('session')->getFlashBag()->add(
340 return $this->redirect($request->headers
->get('referer'));
344 * Changes favorite status for an entry.
346 * @param Request $request
347 * @param Entry $entry
349 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
351 * @return \Symfony\Component\HttpFoundation\RedirectResponse
353 public function toggleStarAction(Request
$request, Entry
$entry)
355 $this->checkUserAction($entry);
357 $entry->toggleStar();
358 $this->getDoctrine()->getManager()->flush();
360 $message = 'flashes.entry.notice.entry_unstarred';
361 if ($entry->isStarred()) {
362 $message = 'flashes.entry.notice.entry_starred';
365 $this->get('session')->getFlashBag()->add(
370 return $this->redirect($request->headers
->get('referer'));
374 * Deletes entry and redirect to the homepage or the last viewed page.
376 * @param Entry $entry
378 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
380 * @return \Symfony\Component\HttpFoundation\RedirectResponse
382 public function deleteEntryAction(Request
$request, Entry
$entry)
384 $this->checkUserAction($entry);
386 // generates the view url for this entry to check for redirection later
387 // to avoid redirecting to the deleted entry. Ugh.
388 $url = $this->generateUrl(
390 array('id' => $entry->getId()),
391 UrlGeneratorInterface
::ABSOLUTE_URL
394 $em = $this->getDoctrine()->getManager();
398 $this->get('session')->getFlashBag()->add(
400 'flashes.entry.notice.entry_deleted'
403 // don't redirect user to the deleted entry
404 return $this->redirect($url !== $request->headers
->get('referer') ? $request->headers
->get('referer') : $this->generateUrl('homepage'));
408 * Check if the logged user can manage the given entry.
410 * @param Entry $entry
412 private function checkUserAction(Entry
$entry)
414 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
415 throw $this->createAccessDeniedException('You can not access this entry.');
420 * Check for existing entry, if it exists, redirect to it with a message.
426 private function checkIfEntryAlreadyExists($entry)
428 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());