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 $em = $this->getDoctrine()->getManager();
45 $entry = new Entry($this->getUser());
47 $form = $this->createForm(new NewEntryType(), $entry);
49 $form->handleRequest($request);
51 if ($form->isValid()) {
53 ->getRepository('WallabagCoreBundle:Entry')
54 ->findOneByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
56 if (count($existingEntry) > 0) {
57 $this->get('session')->getFlashBag()->add(
59 'Entry already saved on '.$existingEntry[0]->getCreatedAt()->format('d-m-Y')
62 return $this->redirect($this->generateUrl('view', array('id' => $existingEntry[0]->getId())));
65 $this->updateEntry($entry);
66 $this->get('session')->getFlashBag()->add(
71 return $this->redirect($this->generateUrl('homepage'));
74 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
75 'form' => $form->createView(),
80 * @param Request $request
82 * @Route("/bookmarklet", name="bookmarklet")
84 * @return \Symfony\Component\HttpFoundation\Response
86 public function addEntryViaBookmarklet(Request
$request)
88 $entry = new Entry($this->getUser());
89 $entry->setUrl($request->get('url'));
90 $this->updateEntry($entry);
92 return $this->redirect($this->generateUrl('homepage'));
96 * @param Request $request
98 * @Route("/new", name="new")
100 * @return \Symfony\Component\HttpFoundation\Response
102 public function addEntryAction(Request
$request)
104 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
108 * Edit an entry content.
110 * @param Request $request
111 * @param Entry $entry
113 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
115 * @return \Symfony\Component\HttpFoundation\Response
117 public function editEntryAction(Request
$request, Entry
$entry)
119 $this->checkUserAction($entry);
121 $form = $this->createForm(new EditEntryType(), $entry);
123 $form->handleRequest($request);
125 if ($form->isValid()) {
126 $em = $this->getDoctrine()->getManager();
127 $em->persist($entry);
130 $this->get('session')->getFlashBag()->add(
135 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
138 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
139 'form' => $form->createView(),
144 * Shows all entries for current user.
146 * @param Request $request
149 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
151 * @return \Symfony\Component\HttpFoundation\Response
153 public function showAllAction(Request
$request, $page)
155 return $this->showEntries('all', $request, $page);
159 * Shows unread entries for current user.
161 * @param Request $request
164 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
166 * @return \Symfony\Component\HttpFoundation\Response
168 public function showUnreadAction(Request
$request, $page)
170 return $this->showEntries('unread', $request, $page);
174 * Shows read entries for current user.
176 * @param Request $request
179 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
181 * @return \Symfony\Component\HttpFoundation\Response
183 public function showArchiveAction(Request
$request, $page)
185 return $this->showEntries('archive', $request, $page);
189 * Shows starred entries for current user.
191 * @param Request $request
194 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
196 * @return \Symfony\Component\HttpFoundation\Response
198 public function showStarredAction(Request
$request, $page)
200 return $this->showEntries('starred', $request, $page);
204 * Global method to retrieve entries depending on the given type
205 * It returns the response to be send.
207 * @param string $type Entries type: unread, starred or archive
208 * @param Request $request
211 * @return \Symfony\Component\HttpFoundation\Response
213 private function showEntries($type, Request
$request, $page)
215 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
219 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
223 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
227 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
231 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
235 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
238 $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser()));
240 if ($request->query
->has($form->getName())) {
241 // manually bind values from the request
242 $form->submit($request->query
->get($form->getName()));
244 // build the query from the given form object
245 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
248 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
249 $entries = new Pagerfanta($pagerAdapter);
251 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
252 $entries->setCurrentPage($page);
254 return $this->render(
255 'WallabagCoreBundle:Entry:entries.html.twig',
257 'form' => $form->createView(),
258 'entries' => $entries,
259 'currentPage' => $page,
265 * Shows entry content.
267 * @param Entry $entry
269 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
271 * @return \Symfony\Component\HttpFoundation\Response
273 public function viewAction(Entry
$entry)
275 $this->checkUserAction($entry);
277 return $this->render(
278 'WallabagCoreBundle:Entry:entry.html.twig',
279 array('entry' => $entry)
285 * Refetch content from the website and make it readable again.
287 * @param Entry $entry
289 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
291 * @return \Symfony\Component\HttpFoundation\RedirectResponse
293 public function reloadAction(Entry
$entry)
295 $this->checkUserAction($entry);
297 $message = 'Entry reloaded';
298 if (false === $this->updateEntry($entry)) {
299 $message = 'Failed to reload entry';
302 $this->get('session')->getFlashBag()->add(
307 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
311 * Changes read status for an entry.
313 * @param Request $request
314 * @param Entry $entry
316 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
318 * @return \Symfony\Component\HttpFoundation\RedirectResponse
320 public function toggleArchiveAction(Request
$request, Entry
$entry)
322 $this->checkUserAction($entry);
324 $entry->toggleArchive();
325 $this->getDoctrine()->getManager()->flush();
327 $this->get('session')->getFlashBag()->add(
329 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
332 return $this->redirect($request->headers
->get('referer'));
336 * Changes favorite status for an entry.
338 * @param Request $request
339 * @param Entry $entry
341 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
343 * @return \Symfony\Component\HttpFoundation\RedirectResponse
345 public function toggleStarAction(Request
$request, Entry
$entry)
347 $this->checkUserAction($entry);
349 $entry->toggleStar();
350 $this->getDoctrine()->getManager()->flush();
352 $this->get('session')->getFlashBag()->add(
354 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
357 return $this->redirect($request->headers
->get('referer'));
361 * Deletes entry and redirect to the homepage or the last viewed page.
363 * @param Entry $entry
365 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
367 * @return \Symfony\Component\HttpFoundation\RedirectResponse
369 public function deleteEntryAction(Request
$request, Entry
$entry)
371 $this->checkUserAction($entry);
373 // generates the view url for this entry to check for redirection later
374 // to avoid redirecting to the deleted entry. Ugh.
375 $url = $this->generateUrl(
377 array('id' => $entry->getId()),
378 UrlGeneratorInterface
::ABSOLUTE_URL
381 $em = $this->getDoctrine()->getManager();
385 $this->get('session')->getFlashBag()->add(
390 // don't redirect user to the deleted entry
391 return $this->redirect($url !== $request->headers
->get('referer') ? $request->headers
->get('referer') : $this->generateUrl('homepage'));
395 * Check if the logged user can manage the given entry.
397 * @param Entry $entry
399 private function checkUserAction(Entry
$entry)
401 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
402 throw $this->createAccessDeniedException('You can not access this entry.');