summaryrefslogtreecommitdiff
path: root/app/Controller/WebNotificationController.php
blob: 263d6d9e457d666cb491bad9a70499c5764674b7 (plain)
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
<?php

namespace Kanboard\Controller;

/**
 * Web notification controller
 *
 * @package  Kanboard\Controller
 * @author   Frederic Guillot
 */
class WebNotificationController extends BaseController
{
    /**
     * Mark all notifications as read
     *
     * @access public
     */
    public function flush()
    {
        $user_id = $this->getUserId();

        $this->userUnreadNotification->markAllAsRead($user_id);
        $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id)));
    }

    /**
     * Mark a notification as read
     *
     * @access public
     */
    public function remove()
    {
        $user_id = $this->getUserId();
        $notification_id = $this->request->getIntegerParam('notification_id');

        $this->userUnreadNotification->markAsRead($user_id, $notification_id);
        $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id)));
    }

    /**
     * Redirect to the task and mark notification as read
     */
    public function redirect()
    {
        $user_id = $this->getUserId();
        $notification_id = $this->request->getIntegerParam('notification_id');

        $notification = $this->userUnreadNotification->getById($notification_id);
        $this->userUnreadNotification->markAsRead($user_id, $notification_id);

        if (empty($notification)) {
            $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id)));
        } elseif ($this->helper->text->contains($notification['event_name'], 'comment')) {
            $this->response->redirect($this->helper->url->to(
                'TaskViewController',
                'show',
                array('task_id' => $notification['event_data']['task']['id'], 'project_id' => $notification['event_data']['task']['project_id']),
                'comment-'.$notification['event_data']['comment']['id']
            ));
        } else {
            $this->response->redirect($this->helper->url->to(
                'TaskViewController',
                'show',
                array('task_id' => $notification['event_data']['task']['id'], 'project_id' => $notification['event_data']['task']['project_id'])
            ));
        }
    }

    private function getUserId()
    {
        $user_id = $this->request->getIntegerParam('user_id');

        if (! $this->userSession->isAdmin() && $user_id != $this->userSession->getId()) {
            $user_id = $this->userSession->getId();
        }

        return $user_id;
    }
}