summaryrefslogtreecommitdiff
path: root/app/Model/UserUnreadNotification.php
blob: cc0f326a28851292e4d3981d287feaff65674d04 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

namespace Kanboard\Model;

/**
 * User Unread Notification
 *
 * @package  model
 * @author   Frederic Guillot
 */
class UserUnreadNotification extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const TABLE = 'user_has_unread_notifications';

    /**
     * Add unread notification to someone
     *
     * @access public
     * @param  integer   $user_id
     * @param  string    $event_name
     * @param  array     $event_data
     */
    public function create($user_id, $event_name, array $event_data)
    {
        $this->db->table(self::TABLE)->insert(array(
            'user_id' => $user_id,
            'date_creation' => time(),
            'event_name' => $event_name,
            'event_data' => json_encode($event_data),
        ));
    }

    /**
     * Get all notifications for a user
     *
     * @access public
     * @param  integer $user_id
     * @return array
     */
    public function getAll($user_id)
    {
        $events = $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('date_creation')->findAll();

        foreach ($events as &$event) {
            $event['event_data'] = json_decode($event['event_data'], true);
            $event['title'] = $this->notification->getTitleWithoutAuthor($event['event_name'], $event['event_data']);
        }

        return $events;
    }

    /**
     * Mark a notification as read
     *
     * @access public
     * @param  integer $user_id
     * @param  integer $notification_id
     * @return boolean
     */
    public function markAsRead($user_id, $notification_id)
    {
        return $this->db->table(self::TABLE)->eq('id', $notification_id)->eq('user_id', $user_id)->remove();
    }

    /**
     * Mark all notifications as read for a user
     *
     * @access public
     * @param  integer $user_id
     * @return boolean
     */
    public function markAllAsRead($user_id)
    {
        return $this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
    }

    /**
     * Return true if the user as unread notifications
     *
     * @access public
     * @param  integer $user_id
     * @return boolean
     */
    public function hasNotifications($user_id)
    {
        return $this->db->table(self::TABLE)->eq('user_id', $user_id)->exists();
    }
}