]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/NotificationsController.php
17e576cdaa12a2f22b0572bd7ef51bdf16e7abf1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / NotificationsController.php
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\HttpFoundation\Response;
12 use Wallabag\CoreBundle\Entity\Notification;
13
14 class NotificationsController extends Controller
15 {
16 /**
17 * @Route("/notifications/{page}", name="notifications-all", defaults={"page" = "1"})
18 *
19 * @param int $page
20 *
21 * @return Response
22 */
23 public function getAllNotificationsAction($page = 1)
24 {
25 $qb = $this->getDoctrine()->getRepository('WallabagCoreBundle:Notification')->getBuilderForNotificationsByUser($this->getUser()->getId());
26 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
27
28 $notifications = new Pagerfanta($pagerAdapter);
29 $notifications->setMaxPerPage($this->getParameter('wallabag_core.notifications_nb'));
30
31 try {
32 $notifications->setCurrentPage($page);
33 } catch (OutOfRangeCurrentPageException $e) {
34 if ($page > 1) {
35 return $this->redirect($this->generateUrl('notifications-all', ['page' => $notifications->getNbPages()]), 302);
36 }
37 }
38
39 return $this->render('WallabagCoreBundle:Notification:notifications.html.twig', [
40 'notifications' => $notifications,
41 'currentPage' => $page,
42 ]);
43 }
44
45 /**
46 * @Route("/notifications/readall", name="notification-archive-all")
47 *
48 * @param Request $request
49 *
50 * @return Response
51 */
52 public function markAllNotificationsAsReadAction(Request $request)
53 {
54 $this->getDoctrine()->getRepository('WallabagCoreBundle:Notification')->markAllAsReadForUser($this->getUser()->getId());
55
56 return $this->redirectToRoute('notifications-all');
57 }
58
59 /**
60 * @Route("/notifications/read/{notification}", name="notification-archive")
61 *
62 * @param Notification $notification
63 *
64 * @return Response
65 */
66 public function markNotificationsAsReadAction(Notification $notification)
67 {
68 $em = $this->getDoctrine()->getManager();
69
70 $notification->setRead(true);
71
72 $em->persist($notification);
73 $em->flush();
74
75 return $this->redirectToRoute('notifications-all');
76 }
77
78 /**
79 * @Route("/notifications/read/{notification}/redirect", name="notification-archive-redirect", requirements={"notification" = "\d+"})
80 *
81 * @param Request $request
82 * @param Notification $notification
83 */
84 public function markNotificationAsReadAndRedirectAction(Request $request, Notification $notification)
85 {
86 $em = $this->getDoctrine()->getManager();
87
88 $notification->setRead(true);
89
90 $em->persist($notification);
91 $em->flush();
92
93 $redirection = $request->get('redirection');
94 $this->redirect($redirection);
95 }
96 }