aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Controller/NotificationsController.php
blob: 17e576cdaa12a2f22b0572bd7ef51bdf16e7abf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

namespace Wallabag\CoreBundle\Controller;

use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Pagerfanta;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Wallabag\CoreBundle\Entity\Notification;

class NotificationsController extends Controller
{
    /**
     * @Route("/notifications/{page}", name="notifications-all", defaults={"page" = "1"})
     *
     * @param int $page
     *
     * @return Response
     */
    public function getAllNotificationsAction($page = 1)
    {
        $qb = $this->getDoctrine()->getRepository('WallabagCoreBundle:Notification')->getBuilderForNotificationsByUser($this->getUser()->getId());
        $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);

        $notifications = new Pagerfanta($pagerAdapter);
        $notifications->setMaxPerPage($this->getParameter('wallabag_core.notifications_nb'));

        try {
            $notifications->setCurrentPage($page);
        } catch (OutOfRangeCurrentPageException $e) {
            if ($page > 1) {
                return $this->redirect($this->generateUrl('notifications-all', ['page' => $notifications->getNbPages()]), 302);
            }
        }

        return $this->render('WallabagCoreBundle:Notification:notifications.html.twig', [
            'notifications' => $notifications,
            'currentPage' => $page,
        ]);
    }

    /**
     * @Route("/notifications/readall", name="notification-archive-all")
     *
     * @param Request $request
     *
     * @return Response
     */
    public function markAllNotificationsAsReadAction(Request $request)
    {
        $this->getDoctrine()->getRepository('WallabagCoreBundle:Notification')->markAllAsReadForUser($this->getUser()->getId());

        return $this->redirectToRoute('notifications-all');
    }

    /**
     * @Route("/notifications/read/{notification}", name="notification-archive")
     *
     * @param Notification $notification
     *
     * @return Response
     */
    public function markNotificationsAsReadAction(Notification $notification)
    {
        $em = $this->getDoctrine()->getManager();

        $notification->setRead(true);

        $em->persist($notification);
        $em->flush();

        return $this->redirectToRoute('notifications-all');
    }

    /**
     * @Route("/notifications/read/{notification}/redirect", name="notification-archive-redirect", requirements={"notification" = "\d+"})
     *
     * @param Request      $request
     * @param Notification $notification
     */
    public function markNotificationAsReadAndRedirectAction(Request $request, Notification $notification)
    {
        $em = $this->getDoctrine()->getManager();

        $notification->setRead(true);

        $em->persist($notification);
        $em->flush();

        $redirection = $request->get('redirection');
        $this->redirect($redirection);
    }
}