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\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
19 * @param Request $request
21 * @Route("/new-entry", name="new_entry")
23 * @return \Symfony\Component\HttpFoundation\Response
25 public function addEntryFormAction(Request
$request)
27 $entry = new Entry($this->getUser());
29 $form = $this->createForm(new NewEntryType(), $entry);
31 $form->handleRequest($request);
33 if ($form->isValid()) {
34 $content = Extractor
::extract($entry->getUrl());
36 $entry->setTitle($content->getTitle());
37 $entry->setContent($content->getBody());
39 $em = $this->getDoctrine()->getManager();
43 $this->get('session')->getFlashBag()->add(
48 return $this->redirect($this->generateUrl('homepage'));
51 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
52 'form' => $form->createView(),
57 * @param Request $request
59 * @Route("/new", name="new")
61 * @return \Symfony\Component\HttpFoundation\Response
63 public function addEntryAction(Request
$request)
65 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
69 * Edit an entry content.
71 * @param Request $request
74 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
76 * @return \Symfony\Component\HttpFoundation\Response
78 public function editEntryAction(Request
$request, Entry
$entry)
80 $this->checkUserAction($entry);
82 $form = $this->createForm(new EditEntryType(), $entry);
84 $form->handleRequest($request);
86 if ($form->isValid()) {
87 $em = $this->getDoctrine()->getManager();
91 $this->get('session')->getFlashBag()->add(
96 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
99 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
100 'form' => $form->createView(),
105 * Shows all entries for current user.
107 * @param Request $request
110 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
112 * @return \Symfony\Component\HttpFoundation\Response
114 public function showAllAction(Request
$request, $page)
116 return $this->showEntries('all', $request, $page);
120 * Shows unread entries for current user.
122 * @param Request $request
125 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
127 * @return \Symfony\Component\HttpFoundation\Response
129 public function showUnreadAction(Request
$request, $page)
131 return $this->showEntries('unread', $request, $page);
135 * Shows read entries for current user.
137 * @param Request $request
140 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
142 * @return \Symfony\Component\HttpFoundation\Response
144 public function showArchiveAction(Request
$request, $page)
146 return $this->showEntries('archive', $request, $page);
150 * Shows starred entries for current user.
152 * @param Request $request
155 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
157 * @return \Symfony\Component\HttpFoundation\Response
159 public function showStarredAction(Request
$request, $page)
161 return $this->showEntries('starred', $request, $page);
165 * Global method to retrieve entries depending on the given type
166 * It returns the response to be send.
168 * @param string $type Entries type: unread, starred or archive
169 * @param Request $request
172 * @return \Symfony\Component\HttpFoundation\Response
174 private function showEntries($type, Request
$request, $page)
176 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
180 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
184 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
188 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
192 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
196 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
199 $form = $this->get('form.factory')->create(new EntryFilterType());
201 if ($request->query
->has($form->getName())) {
202 // manually bind values from the request
203 $form->submit($request->query
->get($form->getName()));
205 // build the query from the given form object
206 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
209 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
210 $entries = new Pagerfanta($pagerAdapter);
212 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
213 $entries->setCurrentPage($page);
215 return $this->render(
216 'WallabagCoreBundle:Entry:entries.html.twig',
218 'form' => $form->createView(),
219 'entries' => $entries,
220 'currentPage' => $page,
226 * Shows entry content.
228 * @param Entry $entry
230 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
232 * @return \Symfony\Component\HttpFoundation\Response
234 public function viewAction(Entry
$entry)
236 $this->checkUserAction($entry);
238 return $this->render(
239 'WallabagCoreBundle:Entry:entry.html.twig',
240 array('entry' => $entry)
245 * Changes read status for an entry.
247 * @param Request $request
248 * @param Entry $entry
250 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
252 * @return \Symfony\Component\HttpFoundation\RedirectResponse
254 public function toggleArchiveAction(Request
$request, Entry
$entry)
256 $this->checkUserAction($entry);
258 $entry->toggleArchive();
259 $this->getDoctrine()->getManager()->flush();
261 $this->get('session')->getFlashBag()->add(
263 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
266 return $this->redirect($request->headers
->get('referer'));
270 * Changes favorite status for an entry.
272 * @param Request $request
273 * @param Entry $entry
275 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
277 * @return \Symfony\Component\HttpFoundation\RedirectResponse
279 public function toggleStarAction(Request
$request, Entry
$entry)
281 $this->checkUserAction($entry);
283 $entry->toggleStar();
284 $this->getDoctrine()->getManager()->flush();
286 $this->get('session')->getFlashBag()->add(
288 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
291 return $this->redirect($request->headers
->get('referer'));
295 * Deletes entry and redirect to the homepage.
297 * @param Entry $entry
299 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
301 * @return \Symfony\Component\HttpFoundation\RedirectResponse
303 public function deleteEntryAction(Entry
$entry)
305 $this->checkUserAction($entry);
307 $em = $this->getDoctrine()->getManager();
311 $this->get('session')->getFlashBag()->add(
316 return $this->redirect($this->generateUrl('homepage'));
320 * Check if the logged user can manage the given entry.
322 * @param Entry $entry
324 private function checkUserAction(Entry
$entry)
326 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
327 throw $this->createAccessDeniedException('You can not access this entry.');