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
18 * @param Request $request
20 * @Route("/new-entry", name="new_entry")
22 * @return \Symfony\Component\HttpFoundation\Response
24 public function addEntryFormAction(Request
$request)
26 $entry = new Entry($this->getUser());
28 $form = $this->createForm(new NewEntryType(), $entry);
30 $form->handleRequest($request);
32 if ($form->isValid()) {
33 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
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_form.html.twig', array(
48 'form' => $form->createView(),
53 * @param Request $request
55 * @Route("/new", name="new")
57 * @return \Symfony\Component\HttpFoundation\Response
59 public function addEntryAction(Request
$request)
61 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
65 * Edit an entry content.
67 * @param Request $request
70 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
72 * @return \Symfony\Component\HttpFoundation\Response
74 public function editEntryAction(Request
$request, Entry
$entry)
76 $this->checkUserAction($entry);
78 $form = $this->createForm(new EditEntryType(), $entry);
80 $form->handleRequest($request);
82 if ($form->isValid()) {
83 $em = $this->getDoctrine()->getManager();
87 $this->get('session')->getFlashBag()->add(
92 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
95 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
96 'form' => $form->createView(),
101 * Shows all entries for current user.
103 * @param Request $request
106 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
108 * @return \Symfony\Component\HttpFoundation\Response
110 public function showAllAction(Request
$request, $page)
112 return $this->showEntries('all', $request, $page);
116 * Shows unread entries for current user.
118 * @param Request $request
121 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
123 * @return \Symfony\Component\HttpFoundation\Response
125 public function showUnreadAction(Request
$request, $page)
127 return $this->showEntries('unread', $request, $page);
131 * Shows read entries for current user.
133 * @param Request $request
136 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
138 * @return \Symfony\Component\HttpFoundation\Response
140 public function showArchiveAction(Request
$request, $page)
142 return $this->showEntries('archive', $request, $page);
146 * Shows starred entries for current user.
148 * @param Request $request
151 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
153 * @return \Symfony\Component\HttpFoundation\Response
155 public function showStarredAction(Request
$request, $page)
157 return $this->showEntries('starred', $request, $page);
161 * Global method to retrieve entries depending on the given type
162 * It returns the response to be send.
164 * @param string $type Entries type: unread, starred or archive
165 * @param Request $request
168 * @return \Symfony\Component\HttpFoundation\Response
170 private function showEntries($type, Request
$request, $page)
172 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
176 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
180 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
184 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
188 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
192 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
195 $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser()));
197 if ($request->query
->has($form->getName())) {
198 // manually bind values from the request
199 $form->submit($request->query
->get($form->getName()));
201 // build the query from the given form object
202 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
205 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
206 $entries = new Pagerfanta($pagerAdapter);
208 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
209 $entries->setCurrentPage($page);
211 return $this->render(
212 'WallabagCoreBundle:Entry:entries.html.twig',
214 'form' => $form->createView(),
215 'entries' => $entries,
216 'currentPage' => $page,
222 * Shows entry content.
224 * @param Entry $entry
226 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
228 * @return \Symfony\Component\HttpFoundation\Response
230 public function viewAction(Entry
$entry)
232 $this->checkUserAction($entry);
234 return $this->render(
235 'WallabagCoreBundle:Entry:entry.html.twig',
236 array('entry' => $entry)
241 * Changes read status for an entry.
243 * @param Request $request
244 * @param Entry $entry
246 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
248 * @return \Symfony\Component\HttpFoundation\RedirectResponse
250 public function toggleArchiveAction(Request
$request, Entry
$entry)
252 $this->checkUserAction($entry);
254 $entry->toggleArchive();
255 $this->getDoctrine()->getManager()->flush();
257 $this->get('session')->getFlashBag()->add(
259 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
262 return $this->redirect($request->headers
->get('referer'));
266 * Changes favorite status for an entry.
268 * @param Request $request
269 * @param Entry $entry
271 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
273 * @return \Symfony\Component\HttpFoundation\RedirectResponse
275 public function toggleStarAction(Request
$request, Entry
$entry)
277 $this->checkUserAction($entry);
279 $entry->toggleStar();
280 $this->getDoctrine()->getManager()->flush();
282 $this->get('session')->getFlashBag()->add(
284 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
287 return $this->redirect($request->headers
->get('referer'));
291 * Deletes entry and redirect to the homepage.
293 * @param Entry $entry
295 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
297 * @return \Symfony\Component\HttpFoundation\RedirectResponse
299 public function deleteEntryAction(Entry
$entry)
301 $this->checkUserAction($entry);
303 $em = $this->getDoctrine()->getManager();
307 $this->get('session')->getFlashBag()->add(
312 return $this->redirect($this->generateUrl('homepage'));
316 * Check if the logged user can manage the given entry.
318 * @param Entry $entry
320 private function checkUserAction(Entry
$entry)
322 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
323 throw $this->createAccessDeniedException('You can not access this entry.');