1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Circles - Bring cloud-users closer together. |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Maxence Lange <[email protected]> |
9
|
|
|
* @copyright 2017 |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OCA\Circles\Db; |
28
|
|
|
|
29
|
|
|
use OCA\Circles\Exceptions\CircleAlreadyExistsException; |
30
|
|
|
use OCA\Circles\Exceptions\CircleCreationException; |
31
|
|
|
use OCA\Circles\Exceptions\CircleDoesNotExistException; |
32
|
|
|
use OCA\Circles\Model\Circle; |
33
|
|
|
use OCA\Circles\Model\Member; |
34
|
|
|
|
35
|
|
|
use OCP\IDBConnection; |
36
|
|
|
use OCP\AppFramework\Db\Mapper; |
37
|
|
|
|
38
|
|
|
class CirclesMapper extends Mapper { |
39
|
|
|
|
40
|
|
|
const TABLENAME = 'circles_circles'; |
41
|
|
|
|
42
|
|
|
private $miscService; |
43
|
|
|
|
44
|
|
|
public function __construct(IDBConnection $db, $miscService) { |
45
|
|
|
parent::__construct($db, self::TABLENAME, 'OCA\Circles\Db\Circles'); |
46
|
|
|
$this->miscService = $miscService; |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// public function find($id) { |
|
|
|
|
51
|
|
|
// try { |
52
|
|
|
// $sql = sprintf('SELECT * FROM *PREFIX*%s WHERE id = ?', self::TABLENAME); |
53
|
|
|
// |
54
|
|
|
// return $this->findEntity($sql, [$id]); |
55
|
|
|
// } catch (DoesNotExistException $dnee) { |
56
|
|
|
// return null; |
57
|
|
|
// } |
58
|
|
|
// } |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns all circle from a user point-of-view |
63
|
|
|
* |
64
|
|
|
* @param $userId |
65
|
|
|
* @param $type |
66
|
|
|
* @param string $name |
67
|
|
|
* @param int $level |
68
|
|
|
* @param int $circleId |
69
|
|
|
* |
70
|
|
|
* @return Circle[]|null |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function findCirclesByUser($userId, $type, $name = '', $level = 0, $circleId = -1) { |
73
|
|
|
|
74
|
|
|
$type = (int)$type; |
75
|
|
|
$level = (int)$level; |
76
|
|
|
$circleId = (int)$circleId; |
77
|
|
|
|
78
|
|
|
$qb = $this->db->getQueryBuilder(); |
79
|
|
|
$qb->select( |
80
|
|
|
'c.id', 'c.name', 'c.description', 'c.type', 'c.creation', |
81
|
|
|
'u.joined', 'u.level', 'u.status' |
82
|
|
|
) |
83
|
|
|
->selectAlias('o.user_id', 'owner') |
84
|
|
|
->from(self::TABLENAME, 'c') |
85
|
|
|
->from(MembersMapper::TABLENAME, 'o') |
86
|
|
|
->where( |
87
|
|
|
$qb->expr() |
88
|
|
|
->eq('c.id', 'o.circle_id'), |
89
|
|
|
$qb->expr() |
90
|
|
|
->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($level > 0) { |
95
|
|
|
$qb->andWhere( |
96
|
|
|
$qb->expr() |
97
|
|
|
->gte('u.level', $qb->createNamedParameter($level)) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
if ($circleId > 0) { |
101
|
|
|
$qb->andWhere( |
102
|
|
|
$qb->expr() |
103
|
|
|
->eq('c.id', $qb->createNamedParameter($circleId)) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
$qb->leftJoin( |
109
|
|
|
'c', MembersMapper::TABLENAME, 'u', |
110
|
|
|
$qb->expr() |
111
|
|
|
->andX( |
112
|
|
|
$qb->expr() |
113
|
|
|
->eq('c.id', 'u.circle_id'), |
114
|
|
|
$qb->expr() |
115
|
|
|
->eq('u.user_id', $qb->createNamedParameter($userId)) |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$orTypesArray = []; |
120
|
|
|
if (Circle::CIRCLES_PERSONAL & (int)$type) { |
121
|
|
|
array_push( |
122
|
|
|
$orTypesArray, |
123
|
|
|
$qb->expr() |
124
|
|
|
->andX( |
125
|
|
|
$qb->expr() |
126
|
|
|
->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL)), |
127
|
|
|
$qb->expr() |
128
|
|
|
->eq('o.user_id', $qb->createNamedParameter($userId)) |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (Circle::CIRCLES_HIDDEN & (int)$type) { |
134
|
|
|
array_push( |
135
|
|
|
$orTypesArray, $qb->expr() |
136
|
|
|
->andX( |
137
|
|
|
$qb->expr() |
138
|
|
|
->eq( |
139
|
|
|
'c.type', |
140
|
|
|
$qb->createNamedParameter(Circle::CIRCLES_HIDDEN) |
141
|
|
|
), |
142
|
|
|
$qb->expr() |
143
|
|
|
->orX( |
144
|
|
|
$qb->expr() |
145
|
|
|
->gte( |
146
|
|
|
'u.level', |
147
|
|
|
$qb->createNamedParameter(Member::LEVEL_MEMBER) |
148
|
|
|
), |
149
|
|
|
$qb->expr() |
150
|
|
|
->eq( |
151
|
|
|
'c.id', |
152
|
|
|
$qb->createNamedParameter($circleId) |
153
|
|
|
), |
154
|
|
|
$qb->expr() |
155
|
|
|
->eq( |
156
|
|
|
'c.name', |
157
|
|
|
$qb->createNamedParameter($name) |
158
|
|
|
) |
159
|
|
|
) |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
View Code Duplication |
if (Circle::CIRCLES_PRIVATE & (int)$type) { |
|
|
|
|
164
|
|
|
array_push( |
165
|
|
|
$orTypesArray, $qb->expr() |
166
|
|
|
->eq( |
167
|
|
|
'c.type', |
168
|
|
|
$qb->createNamedParameter(Circle::CIRCLES_PRIVATE) |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
View Code Duplication |
if (Circle::CIRCLES_PUBLIC & (int)$type) { |
|
|
|
|
173
|
|
|
array_push( |
174
|
|
|
$orTypesArray, $qb->expr() |
175
|
|
|
->eq( |
176
|
|
|
'c.type', |
177
|
|
|
$qb->createNamedParameter(Circle::CIRCLES_PUBLIC) |
178
|
|
|
) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (sizeof($orTypesArray) === 0) { |
183
|
|
|
return null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$orXTypes = $qb->expr() |
187
|
|
|
->orX(); |
188
|
|
|
|
189
|
|
|
foreach ($orTypesArray as $orTypes) { |
190
|
|
|
$orXTypes->add($orTypes); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$qb->andWhere($orXTypes); |
194
|
|
|
|
195
|
|
|
$qb->groupBy('c.id'); |
196
|
|
|
$qb->orderBy('c.name', 'ASC'); |
197
|
|
|
|
198
|
|
|
$cursor = $qb->execute(); |
199
|
|
|
|
200
|
|
|
$result = []; |
201
|
|
|
while ($data = $cursor->fetch()) { |
202
|
|
|
if ($name === '' || stripos($data['name'], $name) !== false) { |
203
|
|
|
$result[] = Circle::fromArray($data); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
$cursor->closeCursor(); |
207
|
|
|
|
208
|
|
|
return $result; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns details about a circle. |
214
|
|
|
* |
215
|
|
|
* @param string $userId |
216
|
|
|
* @param int $circleId |
217
|
|
|
* |
218
|
|
|
* @return Circle |
219
|
|
|
* @throws CircleDoesNotExistException |
220
|
|
|
*/ |
221
|
|
|
public function getDetailsFromCircle($userId, $circleId) { |
222
|
|
|
|
223
|
|
|
$result = $this->findCirclesByUser($userId, Circle::CIRCLES_ALL, '', 0, $circleId); |
224
|
|
|
if (sizeof($result) !== 1) { |
225
|
|
|
throw new CircleDoesNotExistException( |
226
|
|
|
"The circle does not exist or is hidden to the user" |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $result[0]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param Circle $circle |
236
|
|
|
* @param Member $owner |
237
|
|
|
* |
238
|
|
|
* @return bool |
239
|
|
|
* @throws CircleAlreadyExistsException |
240
|
|
|
* @throws CircleCreationException |
241
|
|
|
*/ |
242
|
|
|
public function create(Circle &$circle, Member &$owner) { |
243
|
|
|
|
244
|
|
|
if ($circle->getType() === Circle::CIRCLES_PERSONAL) { |
245
|
|
|
|
246
|
|
|
$list = $this->findCirclesByUser( |
247
|
|
|
$owner->getUserId(), Circle::CIRCLES_PERSONAL, $circle->getName(), |
248
|
|
|
Member::LEVEL_OWNER |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
foreach ($list as $test) { |
|
|
|
|
252
|
|
|
if ($test->getName() === $circle->getName()) { |
253
|
|
|
throw new CircleAlreadyExistsException(); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
} else { |
258
|
|
|
|
259
|
|
|
$qb = $this->db->getQueryBuilder(); |
260
|
|
|
$qb->select( |
261
|
|
|
'c.id', 'c.name', 'c.type' |
262
|
|
|
) |
263
|
|
|
->from(self::TABLENAME, 'c') |
264
|
|
|
->where( |
265
|
|
|
$qb->expr() |
266
|
|
|
->neq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL)) |
267
|
|
|
); |
268
|
|
|
|
269
|
|
|
$cursor = $qb->execute(); |
270
|
|
|
|
271
|
|
|
while ($data = $cursor->fetch()) { |
272
|
|
|
if (strtolower($data['name']) === strtolower($circle->getName())) { |
273
|
|
|
throw new CircleAlreadyExistsException(); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
$cursor->closeCursor(); |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
$qb = $this->db->getQueryBuilder(); |
282
|
|
|
$qb->insert(self::TABLENAME) |
283
|
|
|
->setValue('name', $qb->createNamedParameter($circle->getName())) |
284
|
|
|
->setValue('description', $qb->createNamedParameter($circle->getDescription())) |
285
|
|
|
->setValue('type', $qb->createNamedParameter($circle->getType())) |
286
|
|
|
->setValue('creation', 'CURRENT_TIMESTAMP()'); |
287
|
|
|
$qb->execute(); |
288
|
|
|
$circleid = $qb->getLastInsertId(); |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
if ($circleid < 1) { |
292
|
|
|
throw new CircleCreationException(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$circle->setId($circleid); |
296
|
|
|
$owner->setLevel(Member::LEVEL_OWNER) |
297
|
|
|
->setStatus(Member::STATUS_MEMBER) |
298
|
|
|
->setCircleId($circleid); |
299
|
|
|
|
300
|
|
|
return true; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* remove a circle |
306
|
|
|
* |
307
|
|
|
* @param Circle $circle |
308
|
|
|
*/ |
309
|
|
View Code Duplication |
public function destroy(Circle $circle) { |
|
|
|
|
310
|
|
|
$qb = $this->db->getQueryBuilder(); |
311
|
|
|
$qb->delete(self::TABLENAME) |
312
|
|
|
->where( |
313
|
|
|
$qb->expr() |
314
|
|
|
->eq( |
315
|
|
|
'id', $qb->createNamedParameter($circle->getId()) |
316
|
|
|
) |
317
|
|
|
); |
318
|
|
|
|
319
|
|
|
$qb->execute(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.