/
Alert.php
158 lines (136 loc) · 5.55 KB
/
Alert.php
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
<?php
/**
* Provides access to create & get alerts for a user, as well as their PMs.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0-pr8
* @license MIT
*/
class Alert
{
/**
* Creates an alert for the specified user.
*
* @deprecated Use Alert::send instead
*
* @param int $user_id Contains the ID of the user who we are creating the alert for.
* @param string $type Contains the alert type, eg 'tag' for user tagging.
* @param array $text_short Contains the alert text in short form for the dropdown.
* @param array $text Contains full information about the alert.
* @param ?string $link Contains link to view the alert, defaults to #.
* @param ?string $content Optional alert content.
*/
public static function create(int $user_id, string $type, array $text_short, array $text, ?string $link = '#', string $content = null): void
{
$db = DB::getInstance();
$language = $db->query('SELECT nl2_languages.short_code AS `short_code` FROM nl2_users LEFT JOIN nl2_languages ON nl2_languages.id = nl2_users.language_id WHERE nl2_users.id = ?', [$user_id]);
if (!$language->count()) {
return;
}
$language = new Language($text_short['path'], $language->first()->short_code);
$text_short = $text_short['content'] ?? str_replace($text_short['replace'] ?? '', $text_short['replace_with'] ?? '', $language->get($text_short['file'], $text_short['term']));
$text = $text['content'] ?? str_replace($text['replace'] ?? '', $text['replace_with'] ?? '', $language->get($text['file'], $text['term']));
$db->insert('alerts', [
'user_id' => $user_id,
'type' => $type,
'url' => $link,
'content_short' => $text_short,
'content' => $text,
'content_rich' => $content,
'created' => date('U'),
]);
}
/**
* Post a new alert to a user.
*
* @param int $userId
* @param string $title
* @param string $content
* @param string|null $link Optional link to redirect the user to on click
* @param bool $skipPurify If true the content will not be purified before displaying to user - use with care
* @return void
*/
public static function send(int $userId, string $title, string $content, ?string $link = '', bool $skipPurify = false)
{
DB::getInstance()->insert('alerts', [
'user_id' => $userId,
'type' => 'alert',
'url' => $link ?? '',
'content_short' => $title, // Column maintained for legacy reasons
'content' => $title,
'content_rich' => $content,
'created' => date('U'),
'bypass_purify' => $skipPurify,
]);
}
/**
* Get user alerts.
*
* @param int $user_id Contains the ID of the user who we are getting alerts for.
* @param bool $all Do we want to get all alerts (including read), or not; defaults to false).
*
* @return array All their alerts.
*/
public static function getAlerts(int $user_id, bool $all = false): array
{
$db = DB::getInstance();
if ($all == true) {
return $db->get('alerts', ['user_id', $user_id])->results();
}
return $db->query('SELECT * FROM nl2_alerts WHERE user_id = ? AND `read` = 0', [$user_id])->results();
}
/**
* Get a users unread messages.
*
* @param int $user_id The ID of the user who we are getting messages for.
* @param bool $all Get all alerts (including read), or not. Defaults to false.
*
* @return array All their messages matching the $all filter.
*/
public static function getPMs(int $user_id, bool $all = false): array
{
$db = DB::getInstance();
if ($all == true) {
$pms_access = $db->get('private_messages_users', ['user_id', $user_id])->results();
$pms = [];
foreach ($pms_access as $pm) {
// Get actual PM information
$pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results();
if (!count($pm_full)) {
continue;
}
$pm_full = $pm_full[0];
$pms[] = [
'id' => $pm_full->id,
'title' => Output::getClean($pm_full->title),
'created' => $pm_full->created,
'author_id' => $pm_full->author_id,
'last_reply_user' => $pm_full->last_reply_user,
'last_reply_date' => $pm_full->last_reply_date,
];
}
return $pms;
}
$pms = $db->get('private_messages_users', ['user_id', $user_id])->results();
$unread = [];
foreach ($pms as $pm) {
if ($pm->read == 0) {
$pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results();
if (!count($pm_full)) {
continue;
}
$pm_full = $pm_full[0];
$unread[] = [
'id' => $pm_full->id,
'title' => Output::getClean($pm_full->title),
'created' => $pm_full->created,
'author_id' => $pm_full->author_id,
'last_reply_user' => $pm_full->last_reply_user,
'last_reply_date' => $pm_full->last_reply_date,
];
}
}
return $unread;
}
}