1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
use Jitamin\Foundation\Translator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* User Notification. |
19
|
|
|
*/ |
20
|
|
|
class UserNotificationModel extends Model |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Send notifications to people. |
24
|
|
|
* |
25
|
|
|
* @param string $event_name |
26
|
|
|
* @param array $event_data |
27
|
|
|
*/ |
28
|
|
|
public function sendNotifications($event_name, array $event_data) |
29
|
|
|
{ |
30
|
|
|
$users = $this->getUsersWithNotificationEnabled($event_data['task']['project_id'], $this->userSession->getId()); |
|
|
|
|
31
|
|
|
|
32
|
|
|
foreach ($users as $user) { |
33
|
|
|
if ($this->userNotificationFilterModel->shouldReceiveNotification($user, $event_data)) { |
|
|
|
|
34
|
|
|
$this->sendUserNotification($user, $event_name, $event_data); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Send notification to someone. |
41
|
|
|
* |
42
|
|
|
* @param array $user User |
43
|
|
|
* @param string $event_name |
44
|
|
|
* @param array $event_data |
45
|
|
|
*/ |
46
|
|
|
public function sendUserNotification(array $user, $event_name, array $event_data) |
47
|
|
|
{ |
48
|
|
|
Translator::unload(); |
49
|
|
|
|
50
|
|
|
// Use the user language otherwise use the application language (do not use the session language) |
51
|
|
|
if (!empty($user['language'])) { |
52
|
|
|
Translator::load($user['language']); |
53
|
|
|
} else { |
54
|
|
|
Translator::load($this->settingModel->get('application_language', 'en_US')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($this->userNotificationTypeModel->getSelectedTypes($user['id']) as $type) { |
|
|
|
|
58
|
|
|
$this->userNotificationTypeModel->getType($type)->notifyUser($user, $event_name, $event_data); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Restore locales |
62
|
|
|
$this->languageModel->loadCurrentLanguage(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a list of people with notifications enabled. |
67
|
|
|
* |
68
|
|
|
* @param int $project_id Project id |
69
|
|
|
* @param int $exclude_user_id User id to exclude |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getUsersWithNotificationEnabled($project_id, $exclude_user_id = 0) |
74
|
|
|
{ |
75
|
|
|
if ($this->projectPermissionModel->isEverybodyAllowed($project_id)) { |
|
|
|
|
76
|
|
|
return $this->getEverybodyWithNotificationEnabled($exclude_user_id); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$users = []; |
80
|
|
|
$members = $this->getProjectUserMembersWithNotificationEnabled($project_id, $exclude_user_id); |
81
|
|
|
$groups = $this->getProjectGroupMembersWithNotificationEnabled($project_id, $exclude_user_id); |
82
|
|
|
|
83
|
|
|
foreach (array_merge($members, $groups) as $user) { |
84
|
|
|
if (!isset($users[$user['id']])) { |
85
|
|
|
$users[$user['id']] = $user; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return array_values($users); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Enable notification for someone. |
94
|
|
|
* |
95
|
|
|
* @param int $user_id |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function enableNotification($user_id) |
100
|
|
|
{ |
101
|
|
|
return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(['notifications_enabled' => 1]); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Disable notification for someone. |
106
|
|
|
* |
107
|
|
|
* @param int $user_id |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function disableNotification($user_id) |
112
|
|
|
{ |
113
|
|
|
return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(['notifications_enabled' => 0]); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Save settings for the given user. |
118
|
|
|
* |
119
|
|
|
* @param int $user_id User id |
120
|
|
|
* @param array $values Form values |
121
|
|
|
*/ |
122
|
|
|
public function saveSettings($user_id, array $values) |
123
|
|
|
{ |
124
|
|
|
$types = empty($values['notification_types']) ? [] : array_keys($values['notification_types']); |
125
|
|
|
|
126
|
|
|
if (!empty($types)) { |
127
|
|
|
$this->enableNotification($user_id); |
128
|
|
|
} else { |
129
|
|
|
$this->disableNotification($user_id); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$filter = empty($values['notifications_filter']) ? UserNotificationFilterModel::FILTER_BOTH : $values['notifications_filter']; |
133
|
|
|
$project_ids = empty($values['notification_projects']) ? [] : array_keys($values['notification_projects']); |
134
|
|
|
|
135
|
|
|
$this->userNotificationFilterModel->saveFilter($user_id, $filter); |
|
|
|
|
136
|
|
|
$this->userNotificationFilterModel->saveSelectedProjects($user_id, $project_ids); |
|
|
|
|
137
|
|
|
$this->userNotificationTypeModel->saveSelectedTypes($user_id, $types); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Read user settings to display the form. |
142
|
|
|
* |
143
|
|
|
* @param int $user_id User id |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function readSettings($user_id) |
148
|
|
|
{ |
149
|
|
|
$values = $this->db->table(UserModel::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne(); |
|
|
|
|
150
|
|
|
$values['notification_types'] = $this->userNotificationTypeModel->getSelectedTypes($user_id); |
|
|
|
|
151
|
|
|
$values['notification_projects'] = $this->userNotificationFilterModel->getSelectedProjects($user_id); |
|
|
|
|
152
|
|
|
|
153
|
|
|
return $values; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get a list of project members with notification enabled. |
158
|
|
|
* |
159
|
|
|
* @param int $project_id Project id |
160
|
|
|
* @param int $exclude_user_id User id to exclude |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
private function getProjectUserMembersWithNotificationEnabled($project_id, $exclude_user_id) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
return $this->db |
|
|
|
|
167
|
|
|
->table(ProjectUserRoleModel::TABLE) |
168
|
|
|
->columns(UserModel::TABLE.'.id', UserModel::TABLE.'.username', UserModel::TABLE.'.name', UserModel::TABLE.'.email', UserModel::TABLE.'.language', UserModel::TABLE.'.notifications_filter') |
169
|
|
|
->join(UserModel::TABLE, 'id', 'user_id') |
170
|
|
|
->eq(ProjectUserRoleModel::TABLE.'.project_id', $project_id) |
171
|
|
|
->eq(UserModel::TABLE.'.notifications_enabled', '1') |
172
|
|
|
->eq(UserModel::TABLE.'.is_active', 1) |
173
|
|
|
->neq(UserModel::TABLE.'.id', $exclude_user_id) |
174
|
|
|
->findAll(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get a list of group members with notification enabled. |
179
|
|
|
* |
180
|
|
|
* @param int $project_id Project id |
181
|
|
|
* @param int $exclude_user_id User id to exclude |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
private function getProjectGroupMembersWithNotificationEnabled($project_id, $exclude_user_id) |
186
|
|
|
{ |
187
|
|
|
return $this->db |
|
|
|
|
188
|
|
|
->table(ProjectGroupRoleModel::TABLE) |
189
|
|
|
->columns(UserModel::TABLE.'.id', UserModel::TABLE.'.username', UserModel::TABLE.'.name', UserModel::TABLE.'.email', UserModel::TABLE.'.language', UserModel::TABLE.'.notifications_filter') |
190
|
|
|
->join(GroupMemberModel::TABLE, 'group_id', 'group_id', ProjectGroupRoleModel::TABLE) |
191
|
|
|
->join(UserModel::TABLE, 'id', 'user_id', GroupMemberModel::TABLE) |
192
|
|
|
->eq(ProjectGroupRoleModel::TABLE.'.project_id', $project_id) |
193
|
|
|
->eq(UserModel::TABLE.'.notifications_enabled', '1') |
194
|
|
|
->neq(UserModel::TABLE.'.id', $exclude_user_id) |
195
|
|
|
->eq(UserModel::TABLE.'.is_active', 1) |
196
|
|
|
->findAll(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get a list of project members with notification enabled. |
201
|
|
|
* |
202
|
|
|
* @param int $exclude_user_id User id to exclude |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
private function getEverybodyWithNotificationEnabled($exclude_user_id) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
return $this->db |
|
|
|
|
209
|
|
|
->table(UserModel::TABLE) |
210
|
|
|
->columns(UserModel::TABLE.'.id', UserModel::TABLE.'.username', UserModel::TABLE.'.name', UserModel::TABLE.'.email', UserModel::TABLE.'.language', UserModel::TABLE.'.notifications_filter') |
211
|
|
|
->eq('notifications_enabled', '1') |
212
|
|
|
->neq(UserModel::TABLE.'.id', $exclude_user_id) |
213
|
|
|
->eq(UserModel::TABLE.'.is_active', 1) |
214
|
|
|
->findAll(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.