3 namespace Wallabag\CoreBundle\Controller
;
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Form\Type\NewEntryType
;
11 use Wallabag\CoreBundle\Form\Type\EditEntryType
;
12 use Wallabag\CoreBundle\Filter\EntryFilterType
;
13 use Pagerfanta\Adapter\DoctrineORMAdapter
;
14 use Pagerfanta\Pagerfanta
;
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(new NewEntryType(), $entry);
48 $form->handleRequest($request);
50 if ($form->isValid()) {
51 $this->updateEntry($entry);
52 $this->get('session')->getFlashBag()->add(
57 return $this->redirect($this->generateUrl('homepage'));
60 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
61 'form' => $form->createView(),
66 * @param Request $request
68 * @Route("/bookmarklet", name="bookmarklet")
70 * @return \Symfony\Component\HttpFoundation\Response
72 public function addEntryViaBookmarklet(Request
$request)
74 $entry = new Entry($this->getUser());
75 $entry->setUrl($request->get('url'));
76 $this->updateEntry($entry);
78 return $this->redirect($this->generateUrl('homepage'));
82 * @param Request $request
84 * @Route("/new", name="new")
86 * @return \Symfony\Component\HttpFoundation\Response
88 public function addEntryAction(Request
$request)
90 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
94 * Edit an entry content.
96 * @param Request $request
99 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
101 * @return \Symfony\Component\HttpFoundation\Response
103 public function editEntryAction(Request
$request, Entry
$entry)
105 $this->checkUserAction($entry);
107 $form = $this->createForm(new EditEntryType(), $entry);
109 $form->handleRequest($request);
111 if ($form->isValid()) {
112 $em = $this->getDoctrine()->getManager();
113 $em->persist($entry);
116 $this->get('session')->getFlashBag()->add(
121 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
124 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
125 'form' => $form->createView(),
130 * Shows all entries for current user.
132 * @param Request $request
135 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
137 * @return \Symfony\Component\HttpFoundation\Response
139 public function showAllAction(Request
$request, $page)
141 return $this->showEntries('all', $request, $page);
145 * Shows unread entries for current user.
147 * @param Request $request
150 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
152 * @return \Symfony\Component\HttpFoundation\Response
154 public function showUnreadAction(Request
$request, $page)
156 return $this->showEntries('unread', $request, $page);
160 * Shows read entries for current user.
162 * @param Request $request
165 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
167 * @return \Symfony\Component\HttpFoundation\Response
169 public function showArchiveAction(Request
$request, $page)
171 return $this->showEntries('archive', $request, $page);
175 * Shows starred entries for current user.
177 * @param Request $request
180 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
182 * @return \Symfony\Component\HttpFoundation\Response
184 public function showStarredAction(Request
$request, $page)
186 return $this->showEntries('starred', $request, $page);
190 * Global method to retrieve entries depending on the given type
191 * It returns the response to be send.
193 * @param string $type Entries type: unread, starred or archive
194 * @param Request $request
197 * @return \Symfony\Component\HttpFoundation\Response
199 private function showEntries($type, Request
$request, $page)
201 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
205 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
209 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
213 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
217 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
221 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
224 $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser()));
226 if ($request->query
->has($form->getName())) {
227 // manually bind values from the request
228 $form->submit($request->query
->get($form->getName()));
230 // build the query from the given form object
231 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
234 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
235 $entries = new Pagerfanta($pagerAdapter);
237 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
238 $entries->setCurrentPage($page);
240 return $this->render(
241 'WallabagCoreBundle:Entry:entries.html.twig',
243 'form' => $form->createView(),
244 'entries' => $entries,
245 'currentPage' => $page,
251 * Shows entry content.
253 * @param Entry $entry
255 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
257 * @return \Symfony\Component\HttpFoundation\Response
259 public function viewAction(Entry
$entry)
261 $this->checkUserAction($entry);
263 return $this->render(
264 'WallabagCoreBundle:Entry:entry.html.twig',
265 array('entry' => $entry)
271 * Refetch content from the website and make it readable again.
273 * @param Entry $entry
275 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
277 * @return \Symfony\Component\HttpFoundation\RedirectResponse
279 public function reloadAction(Entry
$entry)
281 $this->checkUserAction($entry);
283 $message = 'Entry reloaded';
284 if (false === $this->updateEntry($entry)) {
285 $message = 'Failed to reload entry';
288 $this->get('session')->getFlashBag()->add(
293 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
297 * Changes read status for an entry.
299 * @param Request $request
300 * @param Entry $entry
302 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
304 * @return \Symfony\Component\HttpFoundation\RedirectResponse
306 public function toggleArchiveAction(Request
$request, Entry
$entry)
308 $this->checkUserAction($entry);
310 $entry->toggleArchive();
311 $this->getDoctrine()->getManager()->flush();
313 $this->get('session')->getFlashBag()->add(
315 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
318 return $this->redirect($request->headers
->get('referer'));
322 * Changes favorite status for an entry.
324 * @param Request $request
325 * @param Entry $entry
327 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
329 * @return \Symfony\Component\HttpFoundation\RedirectResponse
331 public function toggleStarAction(Request
$request, Entry
$entry)
333 $this->checkUserAction($entry);
335 $entry->toggleStar();
336 $this->getDoctrine()->getManager()->flush();
338 $this->get('session')->getFlashBag()->add(
340 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
343 return $this->redirect($request->headers
->get('referer'));
347 * Deletes entry and redirect to the homepage or the last viewed page.
349 * @param Entry $entry
351 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
353 * @return \Symfony\Component\HttpFoundation\RedirectResponse
355 public function deleteEntryAction(Request
$request, Entry
$entry)
357 $this->checkUserAction($entry);
359 // generates the view url for this entry to check for redirection later
360 // to avoid redirecting to the deleted entry. Ugh.
361 $url = $this->generateUrl(
363 array('id' => $entry->getId()),
364 UrlGeneratorInterface
::ABSOLUTE_URL
367 $em = $this->getDoctrine()->getManager();
371 $this->get('session')->getFlashBag()->add(
376 // don't redirect user to the deleted entry
377 return $this->redirect($url !== $request->headers
->get('referer') ?: $this->generateUrl('homepage'));
381 * Check if the logged user can manage the given entry.
383 * @param Entry $entry
385 private function checkUserAction(Entry
$entry)
387 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
388 throw $this->createAccessDeniedException('You can not access this entry.');