]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c NL |
4 | |
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
163eae0b | 7 | use Symfony\Component\HttpFoundation\Request; |
be463487 | 8 | use Wallabag\CoreBundle\Entity\Entry; |
6b767d1c | 9 | use Wallabag\CoreBundle\Service\Extractor; |
82d6d9cb JB |
10 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
11 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
9d50517c NL |
12 | |
13 | class EntryController extends Controller | |
14 | { | |
b84a8055 | 15 | /** |
3d2b2d62 J |
16 | * @param Request $request |
17 | * | |
b84a8055 | 18 | * @Route("/new", name="new_entry") |
3d2b2d62 | 19 | * |
b84a8055 NL |
20 | * @return \Symfony\Component\HttpFoundation\Response |
21 | */ | |
22 | public function addEntryAction(Request $request) | |
23 | { | |
3b815d2d | 24 | $entry = new Entry($this->getUser()); |
b84a8055 | 25 | |
82d6d9cb | 26 | $form = $this->createForm(new NewEntryType(), $entry); |
b84a8055 NL |
27 | |
28 | $form->handleRequest($request); | |
29 | ||
30 | if ($form->isValid()) { | |
6b767d1c | 31 | $content = Extractor::extract($entry->getUrl()); |
b84a8055 | 32 | |
6b767d1c NL |
33 | $entry->setTitle($content->getTitle()); |
34 | $entry->setContent($content->getBody()); | |
35 | ||
36 | $em = $this->getDoctrine()->getManager(); | |
b84a8055 NL |
37 | $em->persist($entry); |
38 | $em->flush(); | |
39 | ||
40 | $this->get('session')->getFlashBag()->add( | |
41 | 'notice', | |
42 | 'Entry saved' | |
43 | ); | |
44 | ||
45 | return $this->redirect($this->generateUrl('homepage')); | |
46 | } | |
47 | ||
ad4d1caa | 48 | return $this->render('WallabagCoreBundle:Entry:new.html.twig', array( |
b84a8055 NL |
49 | 'form' => $form->createView(), |
50 | )); | |
51 | } | |
52 | ||
82d6d9cb JB |
53 | /** |
54 | * Edit an entry content. | |
55 | * | |
56 | * @param Request $request | |
57 | * @param Entry $entry | |
58 | * | |
59 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
60 | * | |
61 | * @return \Symfony\Component\HttpFoundation\Response | |
62 | */ | |
63 | public function editEntryAction(Request $request, Entry $entry) | |
64 | { | |
65 | $this->checkUserAction($entry); | |
66 | ||
67 | $form = $this->createForm(new EditEntryType(), $entry); | |
68 | ||
69 | $form->handleRequest($request); | |
70 | ||
71 | if ($form->isValid()) { | |
72 | $em = $this->getDoctrine()->getManager(); | |
73 | $em->persist($entry); | |
74 | $em->flush(); | |
75 | ||
76 | $this->get('session')->getFlashBag()->add( | |
77 | 'notice', | |
78 | 'Entry updated' | |
79 | ); | |
80 | ||
81 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
82 | } | |
83 | ||
84 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
85 | 'form' => $form->createView(), | |
86 | )); | |
87 | } | |
88 | ||
9d50517c | 89 | /** |
4346a860 | 90 | * Shows unread entries for current user. |
163eae0b | 91 | * |
9fb6ac83 | 92 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 93 | * |
163eae0b | 94 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 95 | */ |
9fb6ac83 | 96 | public function showUnreadAction($page) |
9d50517c | 97 | { |
3b815d2d J |
98 | $entries = $this->getDoctrine() |
99 | ->getRepository('WallabagCoreBundle:Entry') | |
9fb6ac83 FG |
100 | ->findUnreadByUser($this->getUser()->getId()); |
101 | ||
102 | $entries->setCurrentPage($page); | |
9d50517c NL |
103 | |
104 | return $this->render( | |
ad4d1caa | 105 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 FG |
106 | array( |
107 | 'entries' => $entries, | |
108 | 'currentPage' => $page | |
109 | ) | |
9d50517c | 110 | ); |
9d50517c | 111 | } |
bd9f0815 NL |
112 | |
113 | /** | |
4346a860 | 114 | * Shows read entries for current user. |
163eae0b | 115 | * |
9fb6ac83 | 116 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
3d2b2d62 | 117 | * |
163eae0b | 118 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 119 | */ |
9fb6ac83 | 120 | public function showArchiveAction($page) |
bd9f0815 | 121 | { |
3b815d2d J |
122 | $entries = $this->getDoctrine() |
123 | ->getRepository('WallabagCoreBundle:Entry') | |
9fb6ac83 FG |
124 | ->findArchiveByUser($this->getUser()->getId()); |
125 | ||
126 | $entries->setCurrentPage($page); | |
bd9f0815 NL |
127 | |
128 | return $this->render( | |
ad4d1caa | 129 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 FG |
130 | array( |
131 | 'entries' => $entries, | |
132 | 'currentPage' => $page | |
133 | ) | |
bd9f0815 | 134 | ); |
bd9f0815 NL |
135 | } |
136 | ||
137 | /** | |
4346a860 | 138 | * Shows starred entries for current user. |
163eae0b | 139 | * |
9fb6ac83 | 140 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) |
3d2b2d62 | 141 | * |
163eae0b | 142 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 143 | */ |
9fb6ac83 | 144 | public function showStarredAction($page) |
bd9f0815 | 145 | { |
3b815d2d J |
146 | $entries = $this->getDoctrine() |
147 | ->getRepository('WallabagCoreBundle:Entry') | |
9fb6ac83 FG |
148 | ->findStarredByUser($this->getUser()->getId()); |
149 | ||
150 | $entries->setCurrentPage($page); | |
bd9f0815 NL |
151 | |
152 | return $this->render( | |
ad4d1caa | 153 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 FG |
154 | array( |
155 | 'entries' => $entries, | |
156 | 'currentPage' => $page | |
157 | ) | |
bd9f0815 | 158 | ); |
bd9f0815 NL |
159 | } |
160 | ||
161 | /** | |
4346a860 | 162 | * Shows entry content. |
163eae0b | 163 | * |
3d2b2d62 J |
164 | * @param Entry $entry |
165 | * | |
bd9f0815 | 166 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 167 | * |
163eae0b | 168 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 169 | */ |
be463487 | 170 | public function viewAction(Entry $entry) |
bd9f0815 | 171 | { |
3d2b2d62 J |
172 | $this->checkUserAction($entry); |
173 | ||
bd9f0815 | 174 | return $this->render( |
ad4d1caa | 175 | 'WallabagCoreBundle:Entry:entry.html.twig', |
bd9f0815 NL |
176 | array('entry' => $entry) |
177 | ); | |
163eae0b NL |
178 | } |
179 | ||
180 | /** | |
4346a860 | 181 | * Changes read status for an entry. |
163eae0b | 182 | * |
3d2b2d62 J |
183 | * @param Request $request |
184 | * @param Entry $entry | |
185 | * | |
163eae0b | 186 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 187 | * |
163eae0b NL |
188 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
189 | */ | |
be463487 | 190 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 191 | { |
3d2b2d62 J |
192 | $this->checkUserAction($entry); |
193 | ||
163eae0b NL |
194 | $entry->toggleArchive(); |
195 | $this->getDoctrine()->getManager()->flush(); | |
196 | ||
197 | $this->get('session')->getFlashBag()->add( | |
198 | 'notice', | |
199 | 'Entry archived' | |
200 | ); | |
201 | ||
202 | return $this->redirect($request->headers->get('referer')); | |
203 | } | |
204 | ||
205 | /** | |
4346a860 | 206 | * Changes favorite status for an entry. |
163eae0b | 207 | * |
3d2b2d62 J |
208 | * @param Request $request |
209 | * @param Entry $entry | |
210 | * | |
163eae0b | 211 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 212 | * |
163eae0b NL |
213 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
214 | */ | |
be463487 | 215 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 216 | { |
3d2b2d62 J |
217 | $this->checkUserAction($entry); |
218 | ||
163eae0b NL |
219 | $entry->toggleStar(); |
220 | $this->getDoctrine()->getManager()->flush(); | |
221 | ||
222 | $this->get('session')->getFlashBag()->add( | |
223 | 'notice', | |
224 | 'Entry starred' | |
225 | ); | |
226 | ||
227 | return $this->redirect($request->headers->get('referer')); | |
228 | } | |
229 | ||
230 | /** | |
4346a860 | 231 | * Deletes entry. |
163eae0b | 232 | * |
3d2b2d62 J |
233 | * @param Request $request |
234 | * @param Entry $entry | |
235 | * | |
163eae0b | 236 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 237 | * |
163eae0b NL |
238 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
239 | */ | |
be463487 | 240 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 241 | { |
3d2b2d62 J |
242 | $this->checkUserAction($entry); |
243 | ||
1d147791 NL |
244 | $em = $this->getDoctrine()->getManager(); |
245 | $em->remove($entry); | |
246 | $em->flush(); | |
163eae0b NL |
247 | |
248 | $this->get('session')->getFlashBag()->add( | |
249 | 'notice', | |
250 | 'Entry deleted' | |
251 | ); | |
bd9f0815 | 252 | |
163eae0b | 253 | return $this->redirect($request->headers->get('referer')); |
bd9f0815 | 254 | } |
3d2b2d62 J |
255 | |
256 | /** | |
4346a860 | 257 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
258 | * |
259 | * @param Entry $entry | |
260 | */ | |
261 | private function checkUserAction(Entry $entry) | |
262 | { | |
263 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 264 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
265 | } |
266 | } | |
9d50517c | 267 | } |