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\Form\Type\NewEntryType
;
10 use Wallabag\CoreBundle\Form\Type\EditEntryType
;
11 use Wallabag\CoreBundle\Filter\EntryFilterType
;
12 use Pagerfanta\Adapter\DoctrineORMAdapter
;
13 use Pagerfanta\Pagerfanta
;
15 class EntryController
extends Controller
20 private function updateEntry(Entry
$entry)
23 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
24 $em = $this->getDoctrine()->getManager();
27 } catch (\Exception
$e) {
35 * @param Request $request
37 * @Route("/new-entry", name="new_entry")
39 * @return \Symfony\Component\HttpFoundation\Response
41 public function addEntryFormAction(Request
$request)
43 $entry = new Entry($this->getUser());
45 $form = $this->createForm(new NewEntryType(), $entry);
47 $form->handleRequest($request);
49 if ($form->isValid()) {
50 $this->updateEntry($entry);
51 $this->get('session')->getFlashBag()->add(
56 return $this->redirect($this->generateUrl('homepage'));
59 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
60 'form' => $form->createView(),
65 * @param Request $request
67 * @Route("/bookmarklet", name="bookmarklet")
69 * @return \Symfony\Component\HttpFoundation\Response
71 public function addEntryViaBookmarklet(Request
$request)
73 $entry = new Entry($this->getUser());
74 $entry->setUrl($request->get('url'));
75 $this->updateEntry($entry);
77 return $this->redirect($this->generateUrl('homepage'));
81 * @param Request $request
83 * @Route("/new", name="new")
85 * @return \Symfony\Component\HttpFoundation\Response
87 public function addEntryAction(Request
$request)
89 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
93 * Edit an entry content.
95 * @param Request $request
98 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
100 * @return \Symfony\Component\HttpFoundation\Response
102 public function editEntryAction(Request
$request, Entry
$entry)
104 $this->checkUserAction($entry);
106 $form = $this->createForm(new EditEntryType(), $entry);
108 $form->handleRequest($request);
110 if ($form->isValid()) {
111 $em = $this->getDoctrine()->getManager();
112 $em->persist($entry);
115 $this->get('session')->getFlashBag()->add(
120 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
123 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
124 'form' => $form->createView(),
129 * Shows all entries for current user.
131 * @param Request $request
134 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
136 * @return \Symfony\Component\HttpFoundation\Response
138 public function showAllAction(Request
$request, $page)
140 return $this->showEntries('all', $request, $page);
144 * Shows unread entries for current user.
146 * @param Request $request
149 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
151 * @return \Symfony\Component\HttpFoundation\Response
153 public function showUnreadAction(Request
$request, $page)
155 return $this->showEntries('unread', $request, $page);
159 * Shows read entries for current user.
161 * @param Request $request
164 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
166 * @return \Symfony\Component\HttpFoundation\Response
168 public function showArchiveAction(Request
$request, $page)
170 return $this->showEntries('archive', $request, $page);
174 * Shows starred entries for current user.
176 * @param Request $request
179 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
181 * @return \Symfony\Component\HttpFoundation\Response
183 public function showStarredAction(Request
$request, $page)
185 return $this->showEntries('starred', $request, $page);
189 * Global method to retrieve entries depending on the given type
190 * It returns the response to be send.
192 * @param string $type Entries type: unread, starred or archive
193 * @param Request $request
196 * @return \Symfony\Component\HttpFoundation\Response
198 private function showEntries($type, Request
$request, $page)
200 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
204 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
208 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
212 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
216 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
220 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
223 $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser()));
225 if ($request->query
->has($form->getName())) {
226 // manually bind values from the request
227 $form->submit($request->query
->get($form->getName()));
229 // build the query from the given form object
230 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
233 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
234 $entries = new Pagerfanta($pagerAdapter);
236 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
237 $entries->setCurrentPage($page);
239 return $this->render(
240 'WallabagCoreBundle:Entry:entries.html.twig',
242 'form' => $form->createView(),
243 'entries' => $entries,
244 'currentPage' => $page,
250 * Shows entry content.
252 * @param Entry $entry
254 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
256 * @return \Symfony\Component\HttpFoundation\Response
258 public function viewAction(Entry
$entry)
260 $this->checkUserAction($entry);
262 return $this->render(
263 'WallabagCoreBundle:Entry:entry.html.twig',
264 array('entry' => $entry)
269 * Changes read status for an entry.
271 * @param Request $request
272 * @param Entry $entry
274 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
276 * @return \Symfony\Component\HttpFoundation\RedirectResponse
278 public function toggleArchiveAction(Request
$request, Entry
$entry)
280 $this->checkUserAction($entry);
282 $entry->toggleArchive();
283 $this->getDoctrine()->getManager()->flush();
285 $this->get('session')->getFlashBag()->add(
287 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
290 return $this->redirect($request->headers
->get('referer'));
294 * Changes favorite status for an entry.
296 * @param Request $request
297 * @param Entry $entry
299 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
301 * @return \Symfony\Component\HttpFoundation\RedirectResponse
303 public function toggleStarAction(Request
$request, Entry
$entry)
305 $this->checkUserAction($entry);
307 $entry->toggleStar();
308 $this->getDoctrine()->getManager()->flush();
310 $this->get('session')->getFlashBag()->add(
312 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
315 return $this->redirect($request->headers
->get('referer'));
319 * Deletes entry and redirect to the homepage.
321 * @param Entry $entry
323 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
325 * @return \Symfony\Component\HttpFoundation\RedirectResponse
327 public function deleteEntryAction(Entry
$entry)
329 $this->checkUserAction($entry);
331 $em = $this->getDoctrine()->getManager();
335 $this->get('session')->getFlashBag()->add(
340 return $this->redirect($this->generateUrl('homepage'));
344 * Check if the logged user can manage the given entry.
346 * @param Entry $entry
348 private function checkUserAction(Entry
$entry)
350 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
351 throw $this->createAccessDeniedException('You can not access this entry.');