]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
11 | use Wallabag\CoreBundle\Entity\Entry; | |
12 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | |
13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
15 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | |
16 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | |
17 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
18 | use Wallabag\CoreBundle\Form\Type\SearchEntryType; | |
19 | ||
20 | class EntryController extends Controller | |
21 | { | |
22 | /** | |
23 | * @param Request $request | |
24 | * @param int $page | |
25 | * | |
26 | * @Route("/search/{page}", name="search", defaults={"page" = "1"}) | |
27 | * | |
28 | * @return \Symfony\Component\HttpFoundation\Response | |
29 | */ | |
30 | public function searchFormAction(Request $request, $page, $currentRoute) | |
31 | { | |
32 | $form = $this->createForm(SearchEntryType::class); | |
33 | ||
34 | $form->handleRequest($request); | |
35 | ||
36 | if ($form->isValid()) { | |
37 | return $this->showEntries('search', $request, $page); | |
38 | } | |
39 | ||
40 | return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [ | |
41 | 'form' => $form->createView(), | |
42 | 'currentRoute' => $currentRoute, | |
43 | ]); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Fetch content and update entry. | |
48 | * In case it fails, entry will return to avod loosing the data. | |
49 | * | |
50 | * @param Entry $entry | |
51 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | |
52 | * | |
53 | * @return Entry | |
54 | */ | |
55 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') | |
56 | { | |
57 | // put default title in case of fetching content failed | |
58 | $entry->setTitle('No title found'); | |
59 | ||
60 | $message = 'flashes.entry.notice.'.$prefixMessage; | |
61 | ||
62 | try { | |
63 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
64 | } catch (\Exception $e) { | |
65 | $this->get('logger')->error('Error while saving an entry', [ | |
66 | 'exception' => $e, | |
67 | 'entry' => $entry, | |
68 | ]); | |
69 | ||
70 | $message = 'flashes.entry.notice.'.$prefixMessage.'_failed'; | |
71 | } | |
72 | ||
73 | $this->get('session')->getFlashBag()->add('notice', $message); | |
74 | ||
75 | return $entry; | |
76 | } | |
77 | ||
78 | /** | |
79 | * @param Request $request | |
80 | * | |
81 | * @Route("/new-entry", name="new_entry") | |
82 | * | |
83 | * @return \Symfony\Component\HttpFoundation\Response | |
84 | */ | |
85 | public function addEntryFormAction(Request $request) | |
86 | { | |
87 | $entry = new Entry($this->getUser()); | |
88 | ||
89 | $form = $this->createForm(NewEntryType::class, $entry); | |
90 | ||
91 | $form->handleRequest($request); | |
92 | ||
93 | if ($form->isValid()) { | |
94 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); | |
95 | ||
96 | if (false !== $existingEntry) { | |
97 | $this->get('session')->getFlashBag()->add( | |
98 | 'notice', | |
99 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) | |
100 | ); | |
101 | ||
102 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); | |
103 | } | |
104 | ||
105 | $this->updateEntry($entry); | |
106 | ||
107 | $em = $this->getDoctrine()->getManager(); | |
108 | $em->persist($entry); | |
109 | $em->flush(); | |
110 | ||
111 | // entry saved, dispatch event about it! | |
112 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
113 | ||
114 | return $this->redirect($this->generateUrl('homepage')); | |
115 | } | |
116 | ||
117 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [ | |
118 | 'form' => $form->createView(), | |
119 | ]); | |
120 | } | |
121 | ||
122 | /** | |
123 | * @param Request $request | |
124 | * | |
125 | * @Route("/bookmarklet", name="bookmarklet") | |
126 | * | |
127 | * @return \Symfony\Component\HttpFoundation\Response | |
128 | */ | |
129 | public function addEntryViaBookmarkletAction(Request $request) | |
130 | { | |
131 | $entry = new Entry($this->getUser()); | |
132 | $entry->setUrl($request->get('url')); | |
133 | ||
134 | if (false === $this->checkIfEntryAlreadyExists($entry)) { | |
135 | $this->updateEntry($entry); | |
136 | ||
137 | $em = $this->getDoctrine()->getManager(); | |
138 | $em->persist($entry); | |
139 | $em->flush(); | |
140 | ||
141 | // entry saved, dispatch event about it! | |
142 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
143 | } | |
144 | ||
145 | return $this->redirect($this->generateUrl('homepage')); | |
146 | } | |
147 | ||
148 | /** | |
149 | * @Route("/new", name="new") | |
150 | * | |
151 | * @return \Symfony\Component\HttpFoundation\Response | |
152 | */ | |
153 | public function addEntryAction() | |
154 | { | |
155 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Edit an entry content. | |
160 | * | |
161 | * @param Request $request | |
162 | * @param Entry $entry | |
163 | * | |
164 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
165 | * | |
166 | * @return \Symfony\Component\HttpFoundation\Response | |
167 | */ | |
168 | public function editEntryAction(Request $request, Entry $entry) | |
169 | { | |
170 | $this->checkUserAction($entry); | |
171 | ||
172 | $form = $this->createForm(EditEntryType::class, $entry); | |
173 | ||
174 | $form->handleRequest($request); | |
175 | ||
176 | if ($form->isValid()) { | |
177 | $em = $this->getDoctrine()->getManager(); | |
178 | $em->persist($entry); | |
179 | $em->flush(); | |
180 | ||
181 | $this->get('session')->getFlashBag()->add( | |
182 | 'notice', | |
183 | 'flashes.entry.notice.entry_updated' | |
184 | ); | |
185 | ||
186 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
187 | } | |
188 | ||
189 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ | |
190 | 'form' => $form->createView(), | |
191 | ]); | |
192 | } | |
193 | ||
194 | /** | |
195 | * Shows all entries for current user. | |
196 | * | |
197 | * @param Request $request | |
198 | * @param int $page | |
199 | * | |
200 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
201 | * | |
202 | * @return \Symfony\Component\HttpFoundation\Response | |
203 | */ | |
204 | public function showAllAction(Request $request, $page) | |
205 | { | |
206 | return $this->showEntries('all', $request, $page); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Shows unread entries for current user. | |
211 | * | |
212 | * @param Request $request | |
213 | * @param int $page | |
214 | * | |
215 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
216 | * | |
217 | * @return \Symfony\Component\HttpFoundation\Response | |
218 | */ | |
219 | public function showUnreadAction(Request $request, $page) | |
220 | { | |
221 | // load the quickstart if no entry in database | |
222 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
223 | return $this->redirect($this->generateUrl('quickstart')); | |
224 | } | |
225 | ||
226 | return $this->showEntries('unread', $request, $page); | |
227 | } | |
228 | ||
229 | /** | |
230 | * Shows read entries for current user. | |
231 | * | |
232 | * @param Request $request | |
233 | * @param int $page | |
234 | * | |
235 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
236 | * | |
237 | * @return \Symfony\Component\HttpFoundation\Response | |
238 | */ | |
239 | public function showArchiveAction(Request $request, $page) | |
240 | { | |
241 | return $this->showEntries('archive', $request, $page); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Shows starred entries for current user. | |
246 | * | |
247 | * @param Request $request | |
248 | * @param int $page | |
249 | * | |
250 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
251 | * | |
252 | * @return \Symfony\Component\HttpFoundation\Response | |
253 | */ | |
254 | public function showStarredAction(Request $request, $page) | |
255 | { | |
256 | return $this->showEntries('starred', $request, $page); | |
257 | } | |
258 | ||
259 | /** | |
260 | * Global method to retrieve entries depending on the given type | |
261 | * It returns the response to be send. | |
262 | * | |
263 | * @param string $type Entries type: unread, starred or archive | |
264 | * @param Request $request | |
265 | * @param int $page | |
266 | * | |
267 | * @return \Symfony\Component\HttpFoundation\Response | |
268 | */ | |
269 | private function showEntries($type, Request $request, $page) | |
270 | { | |
271 | $repository = $this->get('wallabag_core.entry_repository'); | |
272 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); | |
273 | $currentRoute = (!is_null($request->get('currentRoute')) ? $request->get('currentRoute') : ''); | |
274 | ||
275 | switch ($type) { | |
276 | case 'search': | |
277 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); | |
278 | ||
279 | break; | |
280 | case 'untagged': | |
281 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | |
282 | ||
283 | break; | |
284 | case 'starred': | |
285 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
286 | break; | |
287 | ||
288 | case 'archive': | |
289 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
290 | break; | |
291 | ||
292 | case 'unread': | |
293 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
294 | break; | |
295 | ||
296 | case 'all': | |
297 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
298 | break; | |
299 | ||
300 | default: | |
301 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
302 | } | |
303 | ||
304 | $form = $this->createForm(EntryFilterType::class); | |
305 | ||
306 | if ($request->query->has($form->getName())) { | |
307 | // manually bind values from the request | |
308 | $form->submit($request->query->get($form->getName())); | |
309 | ||
310 | // build the query from the given form object | |
311 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
312 | } | |
313 | ||
314 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
315 | ||
316 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') | |
317 | ->prepare($pagerAdapter, $page); | |
318 | ||
319 | try { | |
320 | $entries->setCurrentPage($page); | |
321 | } catch (OutOfRangeCurrentPageException $e) { | |
322 | if ($page > 1) { | |
323 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | |
324 | } | |
325 | } | |
326 | ||
327 | return $this->render( | |
328 | 'WallabagCoreBundle:Entry:entries.html.twig', [ | |
329 | 'form' => $form->createView(), | |
330 | 'entries' => $entries, | |
331 | 'currentPage' => $page, | |
332 | 'searchTerm' => $searchTerm, | |
333 | ] | |
334 | ); | |
335 | } | |
336 | ||
337 | /** | |
338 | * Shows entry content. | |
339 | * | |
340 | * @param Entry $entry | |
341 | * | |
342 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
343 | * | |
344 | * @return \Symfony\Component\HttpFoundation\Response | |
345 | */ | |
346 | public function viewAction(Entry $entry) | |
347 | { | |
348 | $this->checkUserAction($entry); | |
349 | ||
350 | return $this->render( | |
351 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
352 | ['entry' => $entry] | |
353 | ); | |
354 | } | |
355 | ||
356 | /** | |
357 | * Reload an entry. | |
358 | * Refetch content from the website and make it readable again. | |
359 | * | |
360 | * @param Entry $entry | |
361 | * | |
362 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
363 | * | |
364 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
365 | */ | |
366 | public function reloadAction(Entry $entry) | |
367 | { | |
368 | $this->checkUserAction($entry); | |
369 | ||
370 | $this->updateEntry($entry, 'entry_reloaded'); | |
371 | ||
372 | // if refreshing entry failed, don't save it | |
373 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
374 | $bag = $this->get('session')->getFlashBag(); | |
375 | $bag->clear(); | |
376 | $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed'); | |
377 | ||
378 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
379 | } | |
380 | ||
381 | $em = $this->getDoctrine()->getManager(); | |
382 | $em->persist($entry); | |
383 | $em->flush(); | |
384 | ||
385 | // entry saved, dispatch event about it! | |
386 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
387 | ||
388 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
389 | } | |
390 | ||
391 | /** | |
392 | * Changes read status for an entry. | |
393 | * | |
394 | * @param Request $request | |
395 | * @param Entry $entry | |
396 | * | |
397 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
398 | * | |
399 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
400 | */ | |
401 | public function toggleArchiveAction(Request $request, Entry $entry) | |
402 | { | |
403 | $this->checkUserAction($entry); | |
404 | ||
405 | $entry->toggleArchive(); | |
406 | $this->getDoctrine()->getManager()->flush(); | |
407 | ||
408 | $message = 'flashes.entry.notice.entry_unarchived'; | |
409 | if ($entry->isArchived()) { | |
410 | $message = 'flashes.entry.notice.entry_archived'; | |
411 | } | |
412 | ||
413 | $this->get('session')->getFlashBag()->add( | |
414 | 'notice', | |
415 | $message | |
416 | ); | |
417 | ||
418 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
419 | ||
420 | return $this->redirect($redirectUrl); | |
421 | } | |
422 | ||
423 | /** | |
424 | * Changes starred status for an entry. | |
425 | * | |
426 | * @param Request $request | |
427 | * @param Entry $entry | |
428 | * | |
429 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
430 | * | |
431 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
432 | */ | |
433 | public function toggleStarAction(Request $request, Entry $entry) | |
434 | { | |
435 | $this->checkUserAction($entry); | |
436 | ||
437 | $entry->toggleStar(); | |
438 | $this->getDoctrine()->getManager()->flush(); | |
439 | ||
440 | $message = 'flashes.entry.notice.entry_unstarred'; | |
441 | if ($entry->isStarred()) { | |
442 | $message = 'flashes.entry.notice.entry_starred'; | |
443 | } | |
444 | ||
445 | $this->get('session')->getFlashBag()->add( | |
446 | 'notice', | |
447 | $message | |
448 | ); | |
449 | ||
450 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
451 | ||
452 | return $this->redirect($redirectUrl); | |
453 | } | |
454 | ||
455 | /** | |
456 | * Deletes entry and redirect to the homepage or the last viewed page. | |
457 | * | |
458 | * @param Entry $entry | |
459 | * | |
460 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
461 | * | |
462 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
463 | */ | |
464 | public function deleteEntryAction(Request $request, Entry $entry) | |
465 | { | |
466 | $this->checkUserAction($entry); | |
467 | ||
468 | // generates the view url for this entry to check for redirection later | |
469 | // to avoid redirecting to the deleted entry. Ugh. | |
470 | $url = $this->generateUrl( | |
471 | 'view', | |
472 | ['id' => $entry->getId()], | |
473 | UrlGeneratorInterface::ABSOLUTE_PATH | |
474 | ); | |
475 | ||
476 | // entry deleted, dispatch event about it! | |
477 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
478 | ||
479 | $em = $this->getDoctrine()->getManager(); | |
480 | $em->remove($entry); | |
481 | $em->flush(); | |
482 | ||
483 | $this->get('session')->getFlashBag()->add( | |
484 | 'notice', | |
485 | 'flashes.entry.notice.entry_deleted' | |
486 | ); | |
487 | ||
488 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) | |
489 | $referer = $request->headers->get('referer'); | |
490 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | |
491 | ||
492 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
493 | ||
494 | return $this->redirect($redirectUrl); | |
495 | } | |
496 | ||
497 | /** | |
498 | * Check if the logged user can manage the given entry. | |
499 | * | |
500 | * @param Entry $entry | |
501 | */ | |
502 | private function checkUserAction(Entry $entry) | |
503 | { | |
504 | if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) { | |
505 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
506 | } | |
507 | } | |
508 | ||
509 | /** | |
510 | * Check for existing entry, if it exists, redirect to it with a message. | |
511 | * | |
512 | * @param Entry $entry | |
513 | * | |
514 | * @return Entry|bool | |
515 | */ | |
516 | private function checkIfEntryAlreadyExists(Entry $entry) | |
517 | { | |
518 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
519 | } | |
520 | ||
521 | /** | |
522 | * Get public URL for entry (and generate it if necessary). | |
523 | * | |
524 | * @param Entry $entry | |
525 | * | |
526 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") | |
527 | * | |
528 | * @return \Symfony\Component\HttpFoundation\Response | |
529 | */ | |
530 | public function shareAction(Entry $entry) | |
531 | { | |
532 | $this->checkUserAction($entry); | |
533 | ||
534 | if (null === $entry->getUuid()) { | |
535 | $entry->generateUuid(); | |
536 | ||
537 | $em = $this->getDoctrine()->getManager(); | |
538 | $em->persist($entry); | |
539 | $em->flush(); | |
540 | } | |
541 | ||
542 | return $this->redirect($this->generateUrl('share_entry', [ | |
543 | 'uuid' => $entry->getUuid(), | |
544 | ])); | |
545 | } | |
546 | ||
547 | /** | |
548 | * Disable public sharing for an entry. | |
549 | * | |
550 | * @param Entry $entry | |
551 | * | |
552 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") | |
553 | * | |
554 | * @return \Symfony\Component\HttpFoundation\Response | |
555 | */ | |
556 | public function deleteShareAction(Entry $entry) | |
557 | { | |
558 | $this->checkUserAction($entry); | |
559 | ||
560 | $entry->cleanUuid(); | |
561 | ||
562 | $em = $this->getDoctrine()->getManager(); | |
563 | $em->persist($entry); | |
564 | $em->flush(); | |
565 | ||
566 | return $this->redirect($this->generateUrl('view', [ | |
567 | 'id' => $entry->getId(), | |
568 | ])); | |
569 | } | |
570 | ||
571 | /** | |
572 | * Ability to view a content publicly. | |
573 | * | |
574 | * @param Entry $entry | |
575 | * | |
576 | * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry") | |
577 | * @Cache(maxage="25200", smaxage="25200", public=true) | |
578 | * | |
579 | * @return \Symfony\Component\HttpFoundation\Response | |
580 | */ | |
581 | public function shareEntryAction(Entry $entry) | |
582 | { | |
583 | if (!$this->get('craue_config')->get('share_public')) { | |
584 | throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.'); | |
585 | } | |
586 | ||
587 | return $this->render( | |
588 | '@WallabagCore/themes/common/Entry/share.html.twig', | |
589 | ['entry' => $entry] | |
590 | ); | |
591 | } | |
592 | ||
593 | /** | |
594 | * Shows untagged articles for current user. | |
595 | * | |
596 | * @param Request $request | |
597 | * @param int $page | |
598 | * | |
599 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | |
600 | * | |
601 | * @return \Symfony\Component\HttpFoundation\Response | |
602 | */ | |
603 | public function showUntaggedEntriesAction(Request $request, $page) | |
604 | { | |
605 | return $this->showEntries('untagged', $request, $page); | |
606 | } | |
607 | } |