summaryrefslogtreecommitdiff
path: root/app/Model/Notification.php
blob: 0c2a5d244d518a328bfcfd0cc34292c002087cf6 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

namespace Model;

use Core\Session;
use Core\Translator;

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

    /**
     * Get a list of people with notifications enabled
     *
     * @access public
     * @param  integer   $project_id      Project id
     * @param  array     $exclude_users   List of user_id to exclude
     * @return array
     */
    public function getUsersWithNotification($project_id, array $exclude_users = array())
    {
        if ($this->projectPermission->isEverybodyAllowed($project_id)) {

            return $this->db
                        ->table(User::TABLE)
                        ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language')
                        ->eq('notifications_enabled', '1')
                        ->neq('email', '')
                        ->notin(User::TABLE.'.id', $exclude_users)
                        ->findAll();
        }

        return $this->db
            ->table(ProjectPermission::TABLE)
            ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language')
            ->join(User::TABLE, 'id', 'user_id')
            ->eq('project_id', $project_id)
            ->eq('notifications_enabled', '1')
            ->neq('email', '')
            ->notin(User::TABLE.'.id', $exclude_users)
            ->findAll();
    }

    /**
     * Get the list of users to send the notification for a given project
     *
     * @access public
     * @param  integer   $project_id      Project id
     * @param  array     $exclude_users   List of user_id to exclude
     * @return array
     */
    public function getUsersList($project_id, array $exclude_users = array())
    {
        // Exclude the connected user
        if (Session::isOpen() && $this->userSession->isLogged()) {
            $exclude_users[] = $this->userSession->getId();
        }

        $users = $this->getUsersWithNotification($project_id, $exclude_users);

        foreach ($users as $index => $user) {

            $projects = $this->db->table(self::TABLE)
                                 ->eq('user_id', $user['id'])
                                 ->findAllByColumn('project_id');

            // The user have selected only some projects
            if (! empty($projects)) {

                // If the user didn't select this project we remove that guy from the list
                if (! in_array($project_id, $projects)) {
                    unset($users[$index]);
                }
            }
        }

        return $users;
    }

    /**
     * Send the email notifications
     *
     * @access public
     * @param  string    $template    Template name
     * @param  array     $users       List of users
     * @param  array     $data        Template data
     */
    public function sendEmails($template, array $users, array $data)
    {
        foreach ($users as $user) {

            // 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'));
            }

            $this->emailClient->send(
                $user['email'],
                $user['name'] ?: $user['username'],
                $this->getMailSubject($template, $data),
                $this->getMailContent($template, $data)
            );
        }

        // Restore locales
        $this->config->setupTranslations();
    }

    /**
     * Get the mail subject for a given label
     *
     * @access private
     * @param  string    $label       Label
     * @param  array     $data        Template data
     */
    private function getStandardMailSubject($label, array $data)
    {
        return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
    }

    /**
     * Get the mail subject for a given template name
     *
     * @access public
     * @param  string    $template    Template name
     * @param  array     $data        Template data
     */
    public function getMailSubject($template, array $data)
    {
        switch ($template) {
            case 'file_creation':
                $subject = $this->getStandardMailSubject(e('New attachment'), $data);
                break;
            case 'comment_creation':
                $subject = $this->getStandardMailSubject(e('New comment'), $data);
                break;
            case 'comment_update':
                $subject = $this->getStandardMailSubject(e('Comment updated'), $data);
                break;
            case 'subtask_creation':
                $subject = $this->getStandardMailSubject(e('New subtask'), $data);
                break;
            case 'subtask_update':
                $subject = $this->getStandardMailSubject(e('Subtask updated'), $data);
                break;
            case 'task_creation':
                $subject = $this->getStandardMailSubject(e('New task'), $data);
                break;
            case 'task_update':
                $subject = $this->getStandardMailSubject(e('Task updated'), $data);
                break;
            case 'task_close':
                $subject = $this->getStandardMailSubject(e('Task closed'), $data);
                break;
            case 'task_open':
                $subject = $this->getStandardMailSubject(e('Task opened'), $data);
                break;
            case 'task_move_column':
                $subject = $this->getStandardMailSubject(e('Column Change'), $data);
                break;
            case 'task_move_position':
                $subject = $this->getStandardMailSubject(e('Position Change'), $data);
                break;
            case 'task_assignee_change':
                $subject = $this->getStandardMailSubject(e('Assignee Change'), $data);
                break;
            case 'task_due':
                $subject = e('[%s][Due tasks]', $data['project']);
                break;
            default:
                $subject = e('[Kanboard] Notification');
        }

        return $subject;
    }

    /**
     * Get the mail content for a given template name
     *
     * @access public
     * @param  string    $template    Template name
     * @param  array     $data        Template data
     */
    public function getMailContent($template, array $data)
    {
        return $this->template->render(
            'notification/'.$template,
            $data + array('application_url' => $this->config->get('application_url'))
        );
    }

    /**
     * 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)
    {
        // Delete all selected projects
        $this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();

        if (isset($values['notifications_enabled']) && $values['notifications_enabled'] == 1) {

            // Activate notifications
            $this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
                'notifications_enabled' => '1'
            ));

            // Save selected projects
            if (! empty($values['projects'])) {

                foreach ($values['projects'] as $project_id => $checkbox_value) {
                    $this->db->table(self::TABLE)->insert(array(
                        'user_id' => $user_id,
                        'project_id' => $project_id,
                    ));
                }
            }
        }
        else {

            // Disable notifications
            $this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
                'notifications_enabled' => '0'
            ));
        }
    }

    /**
     * Read user settings to display the form
     *
     * @access public
     * @param  integer   $user_id   User id
     * @return array
     */
    public function readSettings($user_id)
    {
        $values = array();
        $values['notifications_enabled'] = $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_enabled');

        $projects = $this->db->table(self::TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');

        foreach ($projects as $project_id) {
            $values['project_'.$project_id] = true;
        }

        return $values;
    }
}