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 Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Service\Extractor
;
10 use Wallabag\CoreBundle\Form\Type\EntryType
;
12 class EntryController
extends Controller
15 * @param Request $request
17 * @Route("/new", name="new_entry")
19 * @return \Symfony\Component\HttpFoundation\Response
21 public function addEntryAction(Request
$request)
23 $entry = new Entry($this->getUser());
25 $form = $this->createForm(new EntryType(), $entry);
27 $form->handleRequest($request);
29 if ($form->isValid()) {
30 $content = Extractor
::extract($entry->getUrl());
32 $entry->setTitle($content->getTitle());
33 $entry->setContent($content->getBody());
35 $em = $this->getDoctrine()->getManager();
39 $this->get('session')->getFlashBag()->add(
44 return $this->redirect($this->generateUrl('homepage'));
47 return $this->render('WallabagCoreBundle:Entry:new.html.twig', array(
48 'form' => $form->createView(),
53 * Shows unread entries for current user.
55 * @Route("/unread", name="unread")
57 * @return \Symfony\Component\HttpFoundation\Response
59 public function showUnreadAction()
61 // TODO change pagination
62 $entries = $this->getDoctrine()
63 ->getRepository('WallabagCoreBundle:Entry')
64 ->findUnreadByUser($this->getUser()->getId(), 0);
67 'WallabagCoreBundle:Entry:entries.html.twig',
68 array('entries' => $entries)
73 * Shows read entries for current user.
75 * @Route("/archive", name="archive")
77 * @return \Symfony\Component\HttpFoundation\Response
79 public function showArchiveAction()
81 // TODO change pagination
82 $entries = $this->getDoctrine()
83 ->getRepository('WallabagCoreBundle:Entry')
84 ->findArchiveByUser($this->getUser()->getId(), 0);
87 'WallabagCoreBundle:Entry:entries.html.twig',
88 array('entries' => $entries)
93 * Shows starred entries for current user.
95 * @Route("/starred", name="starred")
97 * @return \Symfony\Component\HttpFoundation\Response
99 public function showStarredAction()
101 // TODO change pagination
102 $entries = $this->getDoctrine()
103 ->getRepository('WallabagCoreBundle:Entry')
104 ->findStarredByUser($this->getUser()->getId(), 0);
106 return $this->render(
107 'WallabagCoreBundle:Entry:entries.html.twig',
108 array('entries' => $entries)
113 * Shows entry content.
115 * @param Entry $entry
117 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
119 * @return \Symfony\Component\HttpFoundation\Response
121 public function viewAction(Entry
$entry)
123 $this->checkUserAction($entry);
125 return $this->render(
126 'WallabagCoreBundle:Entry:entry.html.twig',
127 array('entry' => $entry)
132 * Changes read status for an entry.
134 * @param Request $request
135 * @param Entry $entry
137 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
139 * @return \Symfony\Component\HttpFoundation\RedirectResponse
141 public function toggleArchiveAction(Request
$request, Entry
$entry)
143 $this->checkUserAction($entry);
145 $entry->toggleArchive();
146 $this->getDoctrine()->getManager()->flush();
148 $this->get('session')->getFlashBag()->add(
153 return $this->redirect($request->headers
->get('referer'));
157 * Changes favorite status for an entry.
159 * @param Request $request
160 * @param Entry $entry
162 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
164 * @return \Symfony\Component\HttpFoundation\RedirectResponse
166 public function toggleStarAction(Request
$request, Entry
$entry)
168 $this->checkUserAction($entry);
170 $entry->toggleStar();
171 $this->getDoctrine()->getManager()->flush();
173 $this->get('session')->getFlashBag()->add(
178 return $this->redirect($request->headers
->get('referer'));
184 * @param Request $request
185 * @param Entry $entry
187 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
189 * @return \Symfony\Component\HttpFoundation\RedirectResponse
191 public function deleteEntryAction(Request
$request, Entry
$entry)
193 $this->checkUserAction($entry);
195 $em = $this->getDoctrine()->getManager();
199 $this->get('session')->getFlashBag()->add(
204 return $this->redirect($request->headers
->get('referer'));
208 * Check if the logged user can manage the given entry.
210 * @param Entry $entry
212 private function checkUserAction(Entry
$entry)
214 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
215 throw $this->createAccessDeniedException('You can not use this entry.');