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