summaryrefslogtreecommitdiff
path: root/app/Model/UserNotification.php
blob: ef90bb637ea4c61a3da55ff710b4cd9ac2ed258f (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;

use Kanboard\Core\Base;
use Kanboard\Core\Translator;

/**
 * User Notification
 *
 * @package  model
 * @author   Frederic Guillot
 */
class UserNotification extends Base
{
    /**
     * Send notifications to people
     *
     * @access public
     * @param  string $event_name
     * @param  array  $event_data
     */
    public function sendNotifications($event_name, array $event_data)
    {
        $users = $this->getUsersWithNotificationEnabled($event_data['task']['project_id'], $this->userSession->getId());

        foreach ($users as $user) {
            if ($this->userNotificationFilter->shouldReceiveNotification($user, $event_data)) {
                $this->sendUserNotification($user, $event_name, $event_data);
            }
        }
    }

    /**
     * Send notification to someone
     *
     * @access public
     * @param  array     $user        User
     * @param  string    $event_name
     * @param  array     $event_data
     */
    public function sendUserNotification(array $user, $event_name, array $event_data)
    {
        Translator::unload();

        // Use the user language otherwise use the application language (do not use the session language)
        if (! empty($user['language'])) {
            Translator::load($user['language']);
        } else {
            Translator::load($this->config->get('application_language', 'en_US'));
        }

        foreach ($this->userNotificationType->getSelectedTypes($user['id']) as $type) {
            $this->userNotificationType->getType($type)->notifyUser($user, $event_name, $event_data);
        }

        // Restore locales
        $this->language->loadCurrentLanguage();
    }

    /**
     * Get a list of people with notifications enabled
     *
     * @access public
     * @param  integer   $project_id        Project id
     * @param  integer   $exclude_user_id   User id to exclude
     * @return array
     */
    public function getUsersWithNotificationEnabled($project_id, $exclude_user_id = 0)
    {
        if ($this->projectPermission->isEverybodyAllowed($project_id)) {
            return $this->getEverybodyWithNotificationEnabled($exclude_user_id);
        }

        $users = array();
        $members = $this->getProjectUserMembersWithNotificationEnabled($project_id, $exclude_user_id);
        $groups = $this->getProjectGroupMembersWithNotificationEnabled($project_id, $exclude_user_id);

        foreach (array_merge($members, $groups) as $user) {
            if (! isset($users[$user['id']])) {
                $users[$user['id']] = $user;
            }
        }

        return array_values($users);
    }

    /**
     * Enable notification for someone
     *
     * @access public
     * @param  integer $user_id
     * @return boolean
     */
    public function enableNotification($user_id)
    {
        return $this->db->table(User::TABLE)->eq('id', $user_id)->update(array('notifications_enabled' => 1));
    }

    /**
     * Disable notification for someone
     *
     * @access public
     * @param  integer $user_id
     * @return boolean
     */
    public function disableNotification($user_id)
    {
        return $this->db->table(User::TABLE)->eq('id', $user_id)->update(array('notifications_enabled' => 0));
    }

    /**
     * Save settings for the given user
     *
     * @access public
     * @param  integer   $user_id   User id
     * @param  array     $values    Form values
     */
    public function saveSettings($user_id, array $values)
    {
        $types = empty($values['notification_types']) ? array() : array_keys($values['notification_types']);

        if (! empty($types)) {
            $this->enableNotification($user_id);
        } else {
            $this->disableNotification($user_id);
        }

        $filter = empty($values['notifications_filter']) ? UserNotificationFilter::FILTER_BOTH : $values['notifications_filter'];
        $project_ids = empty($values['notification_projects']) ? array() : array_keys($values['notification_projects']);

        $this->userNotificationFilter->saveFilter($user_id, $filter);
        $this->userNotificationFilter->saveSelectedProjects($user_id, $project_ids);
        $this->userNotificationType->saveSelectedTypes($user_id, $types);
    }

    /**
     * Read user settings to display the form
     *
     * @access public
     * @param  integer   $user_id   User id
     * @return array
     */
    public function readSettings($user_id)
    {
        $values = $this->db->table(User::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne();
        $values['notification_types'] = $this->userNotificationType->getSelectedTypes($user_id);
        $values['notification_projects'] = $this->userNotificationFilter->getSelectedProjects($user_id);
        return $values;
    }

    /**
     * Get a list of group members with notification enabled
     *
     * @access private
     * @param  integer   $project_id        Project id
     * @param  integer   $exclude_user_id   User id to exclude
     * @return array
     */
    private function getProjectUserMembersWithNotificationEnabled($project_id, $exclude_user_id)
    {
        return $this->db
            ->table(ProjectUserRole::TABLE)
            ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
            ->join(User::TABLE, 'id', 'user_id')
            ->eq(ProjectUserRole::TABLE.'.project_id', $project_id)
            ->eq(User::TABLE.'.notifications_enabled', '1')
            ->eq(User::TABLE.'.is_active', 1)
            ->neq(User::TABLE.'.id', $exclude_user_id)
            ->findAll();
    }

    private function getProjectGroupMembersWithNotificationEnabled($project_id, $exclude_user_id)
    {
        return $this->db
            ->table(ProjectGroupRole::TABLE)
            ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
            ->join(GroupMember::TABLE, 'group_id', 'group_id', ProjectGroupRole::TABLE)
            ->join(User::TABLE, 'id', 'user_id', GroupMember::TABLE)
            ->eq(ProjectGroupRole::TABLE.'.project_id', $project_id)
            ->eq(User::TABLE.'.notifications_enabled', '1')
            ->neq(User::TABLE.'.id', $exclude_user_id)
            ->eq(User::TABLE.'.is_active', 1)
            ->findAll();
    }

    /**
     * Get a list of project members with notification enabled
     *
     * @access private
     * @param  integer   $exclude_user_id   User id to exclude
     * @return array
     */
    private function getEverybodyWithNotificationEnabled($exclude_user_id)
    {
        return $this->db
            ->table(User::TABLE)
            ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
            ->eq('notifications_enabled', '1')
            ->neq(User::TABLE.'.id', $exclude_user_id)
            ->eq(User::TABLE.'.is_active', 1)
            ->findAll();
    }
}