]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Pagerfanta\Pagerfanta; | |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
10 | use Symfony\Component\HttpFoundation\Request; | |
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
12 | use Wallabag\CoreBundle\Entity\Entry; | |
13 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | |
14 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
15 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
16 | ||
17 | class EntryController extends Controller | |
18 | { | |
19 | /** | |
20 | * @param Entry $entry | |
21 | */ | |
22 | private function updateEntry(Entry $entry) | |
23 | { | |
24 | try { | |
25 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
26 | $em = $this->getDoctrine()->getManager(); | |
27 | $em->persist($entry); | |
28 | $em->flush(); | |
29 | } catch (\Exception $e) { | |
30 | return false; | |
31 | } | |
32 | ||
33 | return true; | |
34 | } | |
35 | ||
36 | /** | |
37 | * @param Request $request | |
38 | * | |
39 | * @Route("/new-entry", name="new_entry") | |
40 | * | |
41 | * @return \Symfony\Component\HttpFoundation\Response | |
42 | */ | |
43 | public function addEntryFormAction(Request $request) | |
44 | { | |
45 | $entry = new Entry($this->getUser()); | |
46 | ||
47 | $form = $this->createForm(NewEntryType::class, $entry); | |
48 | ||
49 | $form->handleRequest($request); | |
50 | ||
51 | if ($form->isValid()) { | |
52 | // check for existing entry, if it exists, redirect to it with a message | |
53 | $existingEntry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
54 | ||
55 | if (false !== $existingEntry) { | |
56 | $this->get('session')->getFlashBag()->add( | |
57 | 'notice', | |
58 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', array('%date%' => $existingEntry->getCreatedAt()->format('d-m-Y'))) | |
59 | ); | |
60 | ||
61 | return $this->redirect($this->generateUrl('view', array('id' => $existingEntry->getId()))); | |
62 | } | |
63 | ||
64 | $this->updateEntry($entry); | |
65 | $this->get('session')->getFlashBag()->add( | |
66 | 'notice', | |
67 | 'flashes.entry.notice.entry_saved' | |
68 | ); | |
69 | ||
70 | return $this->redirect($this->generateUrl('homepage')); | |
71 | } | |
72 | ||
73 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( | |
74 | 'form' => $form->createView(), | |
75 | )); | |
76 | } | |
77 | ||
78 | /** | |
79 | * @param Request $request | |
80 | * | |
81 | * @Route("/bookmarklet", name="bookmarklet") | |
82 | * | |
83 | * @return \Symfony\Component\HttpFoundation\Response | |
84 | */ | |
85 | public function addEntryViaBookmarkletAction(Request $request) | |
86 | { | |
87 | $entry = new Entry($this->getUser()); | |
88 | $entry->setUrl($request->get('url')); | |
89 | $this->updateEntry($entry); | |
90 | ||
91 | return $this->redirect($this->generateUrl('homepage')); | |
92 | } | |
93 | ||
94 | /** | |
95 | * @Route("/new", name="new") | |
96 | * | |
97 | * @return \Symfony\Component\HttpFoundation\Response | |
98 | */ | |
99 | public function addEntryAction() | |
100 | { | |
101 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Edit an entry content. | |
106 | * | |
107 | * @param Request $request | |
108 | * @param Entry $entry | |
109 | * | |
110 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
111 | * | |
112 | * @return \Symfony\Component\HttpFoundation\Response | |
113 | */ | |
114 | public function editEntryAction(Request $request, Entry $entry) | |
115 | { | |
116 | $this->checkUserAction($entry); | |
117 | ||
118 | $form = $this->createForm(EditEntryType::class, $entry); | |
119 | ||
120 | $form->handleRequest($request); | |
121 | ||
122 | if ($form->isValid()) { | |
123 | $em = $this->getDoctrine()->getManager(); | |
124 | $em->persist($entry); | |
125 | $em->flush(); | |
126 | ||
127 | $this->get('session')->getFlashBag()->add( | |
128 | 'notice', | |
129 | 'flashes.entry.notice.entry_updated' | |
130 | ); | |
131 | ||
132 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
133 | } | |
134 | ||
135 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
136 | 'form' => $form->createView(), | |
137 | )); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Shows all entries for current user. | |
142 | * | |
143 | * @param Request $request | |
144 | * @param int $page | |
145 | * | |
146 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
147 | * | |
148 | * @return \Symfony\Component\HttpFoundation\Response | |
149 | */ | |
150 | public function showAllAction(Request $request, $page) | |
151 | { | |
152 | return $this->showEntries('all', $request, $page); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Shows unread entries for current user. | |
157 | * | |
158 | * @param Request $request | |
159 | * @param int $page | |
160 | * | |
161 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
162 | * | |
163 | * @return \Symfony\Component\HttpFoundation\Response | |
164 | */ | |
165 | public function showUnreadAction(Request $request, $page) | |
166 | { | |
167 | // load the quickstart if no entry in database | |
168 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
169 | return $this->redirect($this->generateUrl('quickstart')); | |
170 | } | |
171 | ||
172 | return $this->showEntries('unread', $request, $page); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Shows read entries for current user. | |
177 | * | |
178 | * @param Request $request | |
179 | * @param int $page | |
180 | * | |
181 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
182 | * | |
183 | * @return \Symfony\Component\HttpFoundation\Response | |
184 | */ | |
185 | public function showArchiveAction(Request $request, $page) | |
186 | { | |
187 | return $this->showEntries('archive', $request, $page); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Shows starred entries for current user. | |
192 | * | |
193 | * @param Request $request | |
194 | * @param int $page | |
195 | * | |
196 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
197 | * | |
198 | * @return \Symfony\Component\HttpFoundation\Response | |
199 | */ | |
200 | public function showStarredAction(Request $request, $page) | |
201 | { | |
202 | return $this->showEntries('starred', $request, $page); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Global method to retrieve entries depending on the given type | |
207 | * It returns the response to be send. | |
208 | * | |
209 | * @param string $type Entries type: unread, starred or archive | |
210 | * @param Request $request | |
211 | * @param int $page | |
212 | * | |
213 | * @return \Symfony\Component\HttpFoundation\Response | |
214 | */ | |
215 | private function showEntries($type, Request $request, $page) | |
216 | { | |
217 | $repository = $this->get('wallabag_core.entry_repository'); | |
218 | ||
219 | switch ($type) { | |
220 | case 'starred': | |
221 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
222 | break; | |
223 | ||
224 | case 'archive': | |
225 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
226 | break; | |
227 | ||
228 | case 'unread': | |
229 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
230 | break; | |
231 | ||
232 | case 'all': | |
233 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
234 | break; | |
235 | ||
236 | default: | |
237 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
238 | } | |
239 | ||
240 | $form = $this->createForm(EntryFilterType::class); | |
241 | ||
242 | if ($request->query->has($form->getName())) { | |
243 | // manually bind values from the request | |
244 | $form->submit($request->query->get($form->getName())); | |
245 | ||
246 | // build the query from the given form object | |
247 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
248 | } | |
249 | ||
250 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
251 | $entries = new Pagerfanta($pagerAdapter); | |
252 | ||
253 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
254 | try { | |
255 | $entries->setCurrentPage($page); | |
256 | } catch (OutOfRangeCurrentPageException $e) { | |
257 | if ($page > 1) { | |
258 | return $this->redirect($this->generateUrl($type, array('page' => $entries->getNbPages())), 302); | |
259 | } | |
260 | } | |
261 | ||
262 | return $this->render( | |
263 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
264 | array( | |
265 | 'form' => $form->createView(), | |
266 | 'entries' => $entries, | |
267 | 'currentPage' => $page, | |
268 | ) | |
269 | ); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Shows entry content. | |
274 | * | |
275 | * @param Entry $entry | |
276 | * | |
277 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
278 | * | |
279 | * @return \Symfony\Component\HttpFoundation\Response | |
280 | */ | |
281 | public function viewAction(Entry $entry) | |
282 | { | |
283 | $this->checkUserAction($entry); | |
284 | ||
285 | return $this->render( | |
286 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
287 | array('entry' => $entry) | |
288 | ); | |
289 | } | |
290 | ||
291 | /** | |
292 | * Reload an entry. | |
293 | * Refetch content from the website and make it readable again. | |
294 | * | |
295 | * @param Entry $entry | |
296 | * | |
297 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
298 | * | |
299 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
300 | */ | |
301 | public function reloadAction(Entry $entry) | |
302 | { | |
303 | $this->checkUserAction($entry); | |
304 | ||
305 | $message = 'flashes.entry.notice.entry_reloaded'; | |
306 | if (false === $this->updateEntry($entry)) { | |
307 | $message = 'flashes.entry.notice.entry_reload_failed'; | |
308 | } | |
309 | ||
310 | $this->get('session')->getFlashBag()->add( | |
311 | 'notice', | |
312 | $message | |
313 | ); | |
314 | ||
315 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Changes read status for an entry. | |
320 | * | |
321 | * @param Request $request | |
322 | * @param Entry $entry | |
323 | * | |
324 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
325 | * | |
326 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
327 | */ | |
328 | public function toggleArchiveAction(Request $request, Entry $entry) | |
329 | { | |
330 | $this->checkUserAction($entry); | |
331 | ||
332 | $entry->toggleArchive(); | |
333 | $this->getDoctrine()->getManager()->flush(); | |
334 | ||
335 | $message = 'flashes.entry.notice.entry_unarchived'; | |
336 | if ($entry->isArchived()) { | |
337 | $message = 'flashes.entry.notice.entry_archived'; | |
338 | } | |
339 | ||
340 | $this->get('session')->getFlashBag()->add( | |
341 | 'notice', | |
342 | $message | |
343 | ); | |
344 | ||
345 | return $this->redirect($request->headers->get('referer')); | |
346 | } | |
347 | ||
348 | /** | |
349 | * Changes favorite status for an entry. | |
350 | * | |
351 | * @param Request $request | |
352 | * @param Entry $entry | |
353 | * | |
354 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
355 | * | |
356 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
357 | */ | |
358 | public function toggleStarAction(Request $request, Entry $entry) | |
359 | { | |
360 | $this->checkUserAction($entry); | |
361 | ||
362 | $entry->toggleStar(); | |
363 | $this->getDoctrine()->getManager()->flush(); | |
364 | ||
365 | $message = 'flashes.entry.notice.entry_unstarred'; | |
366 | if ($entry->isStarred()) { | |
367 | $message = 'flashes.entry.notice.entry_starred'; | |
368 | } | |
369 | ||
370 | $this->get('session')->getFlashBag()->add( | |
371 | 'notice', | |
372 | $message | |
373 | ); | |
374 | ||
375 | return $this->redirect($request->headers->get('referer')); | |
376 | } | |
377 | ||
378 | /** | |
379 | * Deletes entry and redirect to the homepage or the last viewed page. | |
380 | * | |
381 | * @param Entry $entry | |
382 | * | |
383 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
384 | * | |
385 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
386 | */ | |
387 | public function deleteEntryAction(Request $request, Entry $entry) | |
388 | { | |
389 | $this->checkUserAction($entry); | |
390 | ||
391 | // generates the view url for this entry to check for redirection later | |
392 | // to avoid redirecting to the deleted entry. Ugh. | |
393 | $url = $this->generateUrl( | |
394 | 'view', | |
395 | array('id' => $entry->getId()), | |
396 | UrlGeneratorInterface::ABSOLUTE_URL | |
397 | ); | |
398 | ||
399 | $em = $this->getDoctrine()->getManager(); | |
400 | $em->remove($entry); | |
401 | $em->flush(); | |
402 | ||
403 | $this->get('session')->getFlashBag()->add( | |
404 | 'notice', | |
405 | 'flashes.entry.notice.entry_deleted' | |
406 | ); | |
407 | ||
408 | // don't redirect user to the deleted entry | |
409 | return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); | |
410 | } | |
411 | ||
412 | /** | |
413 | * Check if the logged user can manage the given entry. | |
414 | * | |
415 | * @param Entry $entry | |
416 | */ | |
417 | private function checkUserAction(Entry $entry) | |
418 | { | |
419 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
420 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
421 | } | |
422 | } | |
423 | } |