]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
improve hateoas implementation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Controller;
9d50517c
NL
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
163eae0b 7use Symfony\Component\HttpFoundation\Request;
be463487 8use Wallabag\CoreBundle\Entity\Entry;
6b767d1c 9use Wallabag\CoreBundle\Service\Extractor;
7781faa0 10use Wallabag\CoreBundle\Form\Type\EntryType;
9d50517c
NL
11
12class EntryController extends Controller
13{
b84a8055 14 /**
3d2b2d62
J
15 * @param Request $request
16 *
b84a8055 17 * @Route("/new", name="new_entry")
3d2b2d62 18 *
b84a8055
NL
19 * @return \Symfony\Component\HttpFoundation\Response
20 */
21 public function addEntryAction(Request $request)
22 {
3b815d2d 23 $entry = new Entry($this->getUser());
b84a8055 24
7781faa0 25 $form = $this->createForm(new EntryType(), $entry);
b84a8055
NL
26
27 $form->handleRequest($request);
28
29 if ($form->isValid()) {
6b767d1c 30 $content = Extractor::extract($entry->getUrl());
b84a8055 31
6b767d1c
NL
32 $entry->setTitle($content->getTitle());
33 $entry->setContent($content->getBody());
34
35 $em = $this->getDoctrine()->getManager();
b84a8055
NL
36 $em->persist($entry);
37 $em->flush();
38
39 $this->get('session')->getFlashBag()->add(
40 'notice',
41 'Entry saved'
42 );
43
44 return $this->redirect($this->generateUrl('homepage'));
45 }
46
ad4d1caa 47 return $this->render('WallabagCoreBundle:Entry:new.html.twig', array(
b84a8055
NL
48 'form' => $form->createView(),
49 ));
50 }
51
9d50517c 52 /**
163eae0b
NL
53 * Shows unread entries for current user
54 *
9d50517c 55 * @Route("/unread", name="unread")
3d2b2d62 56 *
163eae0b 57 * @return \Symfony\Component\HttpFoundation\Response
9d50517c 58 */
bd9f0815 59 public function showUnreadAction()
9d50517c 60 {
a8c90c5c 61 // TODO change pagination
3b815d2d
J
62 $entries = $this->getDoctrine()
63 ->getRepository('WallabagCoreBundle:Entry')
64 ->findUnreadByUser($this->getUser()->getId(), 0);
9d50517c
NL
65
66 return $this->render(
ad4d1caa 67 'WallabagCoreBundle:Entry:entries.html.twig',
9d50517c
NL
68 array('entries' => $entries)
69 );
9d50517c 70 }
bd9f0815
NL
71
72 /**
163eae0b
NL
73 * Shows read entries for current user
74 *
bd9f0815 75 * @Route("/archive", name="archive")
3d2b2d62 76 *
163eae0b 77 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815
NL
78 */
79 public function showArchiveAction()
80 {
a8c90c5c 81 // TODO change pagination
3b815d2d
J
82 $entries = $this->getDoctrine()
83 ->getRepository('WallabagCoreBundle:Entry')
84 ->findArchiveByUser($this->getUser()->getId(), 0);
bd9f0815
NL
85
86 return $this->render(
ad4d1caa 87 'WallabagCoreBundle:Entry:entries.html.twig',
bd9f0815
NL
88 array('entries' => $entries)
89 );
bd9f0815
NL
90 }
91
92 /**
163eae0b
NL
93 * Shows starred entries for current user
94 *
bd9f0815 95 * @Route("/starred", name="starred")
3d2b2d62 96 *
163eae0b 97 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815
NL
98 */
99 public function showStarredAction()
100 {
a8c90c5c 101 // TODO change pagination
3b815d2d
J
102 $entries = $this->getDoctrine()
103 ->getRepository('WallabagCoreBundle:Entry')
104 ->findStarredByUser($this->getUser()->getId(), 0);
bd9f0815
NL
105
106 return $this->render(
ad4d1caa 107 'WallabagCoreBundle:Entry:entries.html.twig',
bd9f0815
NL
108 array('entries' => $entries)
109 );
bd9f0815
NL
110 }
111
112 /**
163eae0b
NL
113 * Shows entry content
114 *
3d2b2d62
J
115 * @param Entry $entry
116 *
bd9f0815 117 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 118 *
163eae0b 119 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 120 */
be463487 121 public function viewAction(Entry $entry)
bd9f0815 122 {
3d2b2d62
J
123 $this->checkUserAction($entry);
124
bd9f0815 125 return $this->render(
ad4d1caa 126 'WallabagCoreBundle:Entry:entry.html.twig',
bd9f0815
NL
127 array('entry' => $entry)
128 );
163eae0b
NL
129 }
130
131 /**
132 * Changes read status for an entry
133 *
3d2b2d62
J
134 * @param Request $request
135 * @param Entry $entry
136 *
163eae0b 137 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 138 *
163eae0b
NL
139 * @return \Symfony\Component\HttpFoundation\RedirectResponse
140 */
be463487 141 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 142 {
3d2b2d62
J
143 $this->checkUserAction($entry);
144
163eae0b
NL
145 $entry->toggleArchive();
146 $this->getDoctrine()->getManager()->flush();
147
148 $this->get('session')->getFlashBag()->add(
149 'notice',
150 'Entry archived'
151 );
152
153 return $this->redirect($request->headers->get('referer'));
154 }
155
156 /**
157 * Changes favorite status for an entry
158 *
3d2b2d62
J
159 * @param Request $request
160 * @param Entry $entry
161 *
163eae0b 162 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 163 *
163eae0b
NL
164 * @return \Symfony\Component\HttpFoundation\RedirectResponse
165 */
be463487 166 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 167 {
3d2b2d62
J
168 $this->checkUserAction($entry);
169
163eae0b
NL
170 $entry->toggleStar();
171 $this->getDoctrine()->getManager()->flush();
172
173 $this->get('session')->getFlashBag()->add(
174 'notice',
175 'Entry starred'
176 );
177
178 return $this->redirect($request->headers->get('referer'));
179 }
180
181 /**
182 * Deletes entry
183 *
3d2b2d62
J
184 * @param Request $request
185 * @param Entry $entry
186 *
163eae0b 187 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 188 *
163eae0b
NL
189 * @return \Symfony\Component\HttpFoundation\RedirectResponse
190 */
be463487 191 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 192 {
3d2b2d62
J
193 $this->checkUserAction($entry);
194
42a90646 195 $entry->setDeleted(1);
3d2b2d62 196 $this->getDoctrine()->getManager()->flush();
163eae0b
NL
197
198 $this->get('session')->getFlashBag()->add(
199 'notice',
200 'Entry deleted'
201 );
bd9f0815 202
163eae0b 203 return $this->redirect($request->headers->get('referer'));
bd9f0815 204 }
3d2b2d62
J
205
206 /**
207 * Check if the logged user can manage the given entry
208 *
209 * @param Entry $entry
210 */
211 private function checkUserAction(Entry $entry)
212 {
213 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
214 throw $this->createAccessDeniedException('You can not use this entry.');
215 }
216 }
9d50517c 217}