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\Helper\Url
;
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->createFormBuilder($entry)
27 ->add('save', 'submit')
30 $form->handleRequest($request);
32 if ($form->isValid()) {
33 $content = Extractor
::extract($entry->getUrl());
35 $entry->setTitle($content->getTitle());
36 $entry->setContent($content->getBody());
38 $em = $this->getDoctrine()->getManager();
42 $this->get('session')->getFlashBag()->add(
47 return $this->redirect($this->generateUrl('homepage'));
50 return $this->render('WallabagCoreBundle:Entry:new.html.twig', array(
51 'form' => $form->createView(),
56 * Shows unread entries for current user
58 * @Route("/unread", name="unread")
60 * @return \Symfony\Component\HttpFoundation\Response
62 public function showUnreadAction()
64 // TODO change pagination
65 $entries = $this->getDoctrine()
66 ->getRepository('WallabagCoreBundle:Entry')
67 ->findUnreadByUser($this->getUser()->getId(), 0);
70 'WallabagCoreBundle:Entry:entries.html.twig',
71 array('entries' => $entries)
76 * Shows read entries for current user
78 * @Route("/archive", name="archive")
80 * @return \Symfony\Component\HttpFoundation\Response
82 public function showArchiveAction()
84 // TODO change pagination
85 $entries = $this->getDoctrine()
86 ->getRepository('WallabagCoreBundle:Entry')
87 ->findArchiveByUser($this->getUser()->getId(), 0);
90 'WallabagCoreBundle:Entry:entries.html.twig',
91 array('entries' => $entries)
96 * Shows starred entries for current user
98 * @Route("/starred", name="starred")
100 * @return \Symfony\Component\HttpFoundation\Response
102 public function showStarredAction()
104 // TODO change pagination
105 $entries = $this->getDoctrine()
106 ->getRepository('WallabagCoreBundle:Entry')
107 ->findStarredByUser($this->getUser()->getId(), 0);
109 return $this->render(
110 'WallabagCoreBundle:Entry:entries.html.twig',
111 array('entries' => $entries)
116 * Shows entry content
118 * @param Entry $entry
120 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
122 * @return \Symfony\Component\HttpFoundation\Response
124 public function viewAction(Entry
$entry)
126 $this->checkUserAction($entry);
128 return $this->render(
129 'WallabagCoreBundle:Entry:entry.html.twig',
130 array('entry' => $entry)
135 * Changes read status for an entry
137 * @param Request $request
138 * @param Entry $entry
140 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
142 * @return \Symfony\Component\HttpFoundation\RedirectResponse
144 public function toggleArchiveAction(Request
$request, Entry
$entry)
146 $this->checkUserAction($entry);
148 $entry->toggleArchive();
149 $this->getDoctrine()->getManager()->flush();
151 $this->get('session')->getFlashBag()->add(
156 return $this->redirect($request->headers
->get('referer'));
160 * Changes favorite status for an entry
162 * @param Request $request
163 * @param Entry $entry
165 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
167 * @return \Symfony\Component\HttpFoundation\RedirectResponse
169 public function toggleStarAction(Request
$request, Entry
$entry)
171 $this->checkUserAction($entry);
173 $entry->toggleStar();
174 $this->getDoctrine()->getManager()->flush();
176 $this->get('session')->getFlashBag()->add(
181 return $this->redirect($request->headers
->get('referer'));
187 * @param Request $request
188 * @param Entry $entry
190 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
192 * @return \Symfony\Component\HttpFoundation\RedirectResponse
194 public function deleteEntryAction(Request
$request, Entry
$entry)
196 $this->checkUserAction($entry);
198 $entry->setDeleted(1);
199 $this->getDoctrine()->getManager()->flush();
201 $this->get('session')->getFlashBag()->add(
206 return $this->redirect($request->headers
->get('referer'));
210 * Check if the logged user can manage the given entry
212 * @param Entry $entry
214 private function checkUserAction(Entry
$entry)
216 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
217 throw $this->createAccessDeniedException('You can not use this entry.');