summaryrefslogtreecommitdiff
path: root/app/Model/UserNotificationFilter.php
blob: 780ddfc7fe493defb4245e2cc713c9ae0a7f19c5 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php

namespace Kanboard\Model;

/**
 * User Notification Filter
 *
 * @package  model
 * @author   Frederic Guillot
 */
class UserNotificationFilter extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const PROJECT_TABLE = 'user_has_notifications';

    /**
     * User filters
     *
     * @var integer
     */
    const FILTER_NONE      = 1;
    const FILTER_ASSIGNEE  = 2;
    const FILTER_CREATOR   = 3;
    const FILTER_BOTH      = 4;

    /**
     * Get the list of filters
     *
     * @access public
     * @return array
     */
    public function getFilters()
    {
        return array(
            self::FILTER_NONE => t('All tasks'),
            self::FILTER_ASSIGNEE => t('Only for tasks assigned to me'),
            self::FILTER_CREATOR => t('Only for tasks created by me'),
            self::FILTER_BOTH => t('Only for tasks created by me and assigned to me'),
        );
    }

    /**
     * Get user selected filter
     *
     * @access public
     * @param  integer  $user_id
     * @return integer
     */
    public function getSelectedFilter($user_id)
    {
        return $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_filter');
    }

    /**
     * Save selected filter for a user
     *
     * @access public
     * @param  integer  $user_id
     * @param  string   $filter
     * @return boolean
     */
    public function saveFilter($user_id, $filter)
    {
        return $this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
            'notifications_filter' => $filter,
        ));
    }

    /**
     * Get user selected projects
     *
     * @access public
     * @param  integer  $user_id
     * @return array
     */
    public function getSelectedProjects($user_id)
    {
        return $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
    }

    /**
     * Save selected projects for a user
     *
     * @access public
     * @param  integer    $user_id
     * @param  integer[]  $project_ids
     * @return boolean
     */
    public function saveSelectedProjects($user_id, array $project_ids)
    {
        $results = array();
        $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->remove();

        foreach ($project_ids as $project_id) {
            $results[] = $this->db->table(self::PROJECT_TABLE)->insert(array(
                'user_id' => $user_id,
                'project_id' => $project_id,
            ));
        }

        return !in_array(false, $results, true);
    }

    /**
     * Return true if the user should receive notification
     *
     * @access public
     * @param  array  $user
     * @param  array  $event_data
     * @return boolean
     */
    public function shouldReceiveNotification(array $user, array $event_data)
    {
        $filters = array(
            'filterNone',
            'filterAssignee',
            'filterCreator',
            'filterBoth',
        );

        foreach ($filters as $filter) {
            if ($this->$filter($user, $event_data)) {
                return $this->filterProject($user, $event_data);
            }
        }

        return false;
    }

    /**
     * Return true if the user will receive all notifications
     *
     * @access public
     * @param  array  $user
     * @return boolean
     */
    public function filterNone(array $user)
    {
        return $user['notifications_filter'] == self::FILTER_NONE;
    }

    /**
     * Return true if the user is the assignee and selected the filter "assignee"
     *
     * @access public
     * @param  array  $user
     * @param  array  $event_data
     * @return boolean
     */
    public function filterAssignee(array $user, array $event_data)
    {
        return $user['notifications_filter'] == self::FILTER_ASSIGNEE && $event_data['task']['owner_id'] == $user['id'];
    }

    /**
     * Return true if the user is the creator and enabled the filter "creator"
     *
     * @access public
     * @param  array  $user
     * @param  array  $event_data
     * @return boolean
     */
    public function filterCreator(array $user, array $event_data)
    {
        return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id'];
    }

    /**
     * Return true if the user is the assignee or the creator and selected the filter "both"
     *
     * @access public
     * @param  array  $user
     * @param  array  $event_data
     * @return boolean
     */
    public function filterBoth(array $user, array $event_data)
    {
        return $user['notifications_filter'] == self::FILTER_BOTH &&
               ($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id']);
    }

    /**
     * Return true if the user want to receive notification for the selected project
     *
     * @access public
     * @param  array  $user
     * @param  array  $event_data
     * @return boolean
     */
    public function filterProject(array $user, array $event_data)
    {
        $projects = $this->getSelectedProjects($user['id']);

        if (! empty($projects)) {
            return in_array($event_data['task']['project_id'], $projects);
        }

        return true;
    }
}