|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, Joas Schilling <[email protected]> |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\AnnouncementCenter; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
25
|
|
|
use OCP\IDBConnection; |
|
26
|
|
|
|
|
27
|
|
|
class Manager { |
|
28
|
|
|
/** @var IDBConnection */ |
|
29
|
|
|
private $connection; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param IDBConnection $connection |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __construct(IDBConnection $connection){ |
|
35
|
7 |
|
$this->connection = $connection; |
|
36
|
7 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $subject |
|
40
|
|
|
* @param string $message |
|
41
|
|
|
* @param string $user |
|
42
|
|
|
* @param int $time |
|
43
|
|
|
* @param bool $parseStrings If the returned message should be parsed or not |
|
44
|
|
|
* @return array |
|
45
|
|
|
* @throws \InvalidArgumentException when the subject is empty or invalid |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function announce($subject, $message, $user, $time, $parseStrings = true) { |
|
48
|
3 |
|
$subject = trim($subject); |
|
49
|
3 |
|
$message = trim($message); |
|
50
|
3 |
|
if (isset($subject[512])) { |
|
51
|
1 |
|
throw new \InvalidArgumentException('Invalid subject', 1); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
if ($subject === '') { |
|
55
|
1 |
|
throw new \InvalidArgumentException('Invalid subject', 2); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
$queryBuilder = $this->connection->getQueryBuilder(); |
|
59
|
1 |
|
$queryBuilder->insert('announcements') |
|
60
|
1 |
|
->values([ |
|
61
|
1 |
|
'announcement_time' => $queryBuilder->createParameter('time'), |
|
62
|
1 |
|
'announcement_user' => $queryBuilder->createParameter('user'), |
|
63
|
1 |
|
'announcement_subject' => $queryBuilder->createParameter('subject'), |
|
64
|
1 |
|
'announcement_message' => $queryBuilder->createParameter('message'), |
|
65
|
1 |
|
]) |
|
66
|
1 |
|
->setParameter('time', $time) |
|
67
|
1 |
|
->setParameter('user', $user) |
|
68
|
1 |
|
->setParameter('subject', $subject) |
|
69
|
1 |
|
->setParameter('message', $message); |
|
70
|
1 |
|
$queryBuilder->execute(); |
|
71
|
|
|
|
|
72
|
1 |
|
$queryBuilder = $this->connection->getQueryBuilder(); |
|
73
|
1 |
|
$query = $queryBuilder->select('*') |
|
74
|
1 |
|
->from('announcements') |
|
75
|
1 |
|
->where($queryBuilder->expr()->eq('announcement_time', $queryBuilder->createParameter('time'))) |
|
76
|
1 |
|
->andWhere($queryBuilder->expr()->eq('announcement_user', $queryBuilder->createParameter('user'))) |
|
77
|
1 |
|
->orderBy('announcement_id', 'DESC') |
|
78
|
1 |
|
->setParameter('time', (int) $time) |
|
79
|
1 |
|
->setParameter('user', $user); |
|
80
|
1 |
|
$result = $query->execute(); |
|
81
|
1 |
|
$row = $result->fetch(); |
|
82
|
1 |
|
$result->closeCursor(); |
|
83
|
|
|
|
|
84
|
|
|
return [ |
|
85
|
1 |
|
'id' => (int) $row['announcement_id'], |
|
86
|
1 |
|
'author' => $row['announcement_user'], |
|
87
|
1 |
|
'time' => (int) $row['announcement_time'], |
|
88
|
1 |
|
'subject' => ($parseStrings) ? $this->parseSubject($row['announcement_subject']) : $row['announcement_subject'], |
|
89
|
1 |
|
'message' => ($parseStrings) ? $this->parseMessage($row['announcement_message']) : $row['announcement_message'], |
|
90
|
1 |
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param int $id |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function delete($id) { |
|
97
|
1 |
|
$queryBuilder = $this->connection->getQueryBuilder(); |
|
98
|
1 |
|
$queryBuilder->delete('announcements') |
|
99
|
1 |
|
->where($queryBuilder->expr()->eq('announcement_id', $queryBuilder->createParameter('id'))) |
|
100
|
1 |
|
->setParameter('id', (int) $id) |
|
101
|
1 |
|
->execute(); |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param int $id |
|
106
|
|
|
* @param bool $parseStrings |
|
107
|
|
|
* @return array |
|
108
|
|
|
* @throws \InvalidArgumentException when the id is invalid |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function getAnnouncement($id, $parseStrings = true) { |
|
111
|
2 |
|
$queryBuilder = $this->connection->getQueryBuilder(); |
|
112
|
2 |
|
$query = $queryBuilder->select('*') |
|
113
|
2 |
|
->from('announcements') |
|
114
|
2 |
|
->where($queryBuilder->expr()->eq('announcement_id', $queryBuilder->createParameter('id'))) |
|
115
|
2 |
|
->setParameter('id', (int) $id); |
|
116
|
2 |
|
$result = $query->execute(); |
|
117
|
2 |
|
$row = $result->fetch(); |
|
118
|
2 |
|
$result->closeCursor(); |
|
119
|
|
|
|
|
120
|
2 |
|
if ($row === false) { |
|
121
|
2 |
|
throw new \InvalidArgumentException('Invalid ID'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return [ |
|
125
|
1 |
|
'id' => (int) $row['announcement_id'], |
|
126
|
1 |
|
'author' => $row['announcement_user'], |
|
127
|
1 |
|
'time' => (int) $row['announcement_time'], |
|
128
|
1 |
|
'subject' => ($parseStrings) ? $this->parseSubject($row['announcement_subject']) : $row['announcement_subject'], |
|
129
|
1 |
|
'message' => ($parseStrings) ? $this->parseMessage($row['announcement_message']) : $row['announcement_message'], |
|
130
|
1 |
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param int $limit |
|
135
|
|
|
* @param int $offset |
|
136
|
|
|
* @param bool $parseStrings |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function getAnnouncements($limit = 15, $offset = 0, $parseStrings = true) { |
|
140
|
1 |
|
$queryBuilder = $this->connection->getQueryBuilder(); |
|
141
|
1 |
|
$query = $queryBuilder->select('*') |
|
142
|
1 |
|
->from('announcements') |
|
143
|
1 |
|
->orderBy('announcement_time', 'DESC') |
|
144
|
1 |
|
->setMaxResults($limit); |
|
145
|
|
|
|
|
146
|
1 |
|
if ($offset > 0) { |
|
147
|
|
|
$query->where($query->expr()->lt('announcement_id', $query->createNamedParameter($offset, IQueryBuilder::PARAM_INT))); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
$result = $query->execute(); |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
1 |
|
$announcements = []; |
|
154
|
1 |
|
while ($row = $result->fetch()) { |
|
155
|
1 |
|
$announcements[] = [ |
|
156
|
1 |
|
'id' => (int) $row['announcement_id'], |
|
157
|
1 |
|
'author' => $row['announcement_user'], |
|
158
|
1 |
|
'time' => (int) $row['announcement_time'], |
|
159
|
1 |
|
'subject' => ($parseStrings) ? $this->parseSubject($row['announcement_subject']) : $row['announcement_subject'], |
|
160
|
1 |
|
'message' => ($parseStrings) ? $this->parseMessage($row['announcement_message']) : $row['announcement_message'], |
|
161
|
|
|
]; |
|
162
|
1 |
|
} |
|
163
|
1 |
|
$result->closeCursor(); |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
1 |
|
return $announcements; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $message |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
1 |
|
protected function parseMessage($message) { |
|
174
|
1 |
|
return str_replace("\n", '<br />', str_replace(['<', '>'], ['<', '>'], $message)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $subject |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
1 |
|
protected function parseSubject($subject) { |
|
182
|
1 |
|
return str_replace("\n", ' ', str_replace(['<', '>'], ['<', '>'], $subject)); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|