Completed
Pull Request — master (#551)
by Maxence
02:16
created

Circle::setViewer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Model;
33
34
use daita\MySmallPhpTools\Db\Nextcloud\nc21\INC21QueryRow;
35
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\Model\Nextcloud\nc21\INC21Convert;
37
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Convert;
38
use daita\MySmallPhpTools\Traits\TArrayTools;
39
use DateTime;
40
use JsonSerializable;
41
42
43
/**
44
 * Class Circle
45
 *
46
 * ** examples of use of bitwise flags for members management:
47
 *      CFG_OPEN, CFG_REQUEST, CFG_INVITE, CFG_FRIEND
48
 *
49
 * - CFG_OPEN                             => everyone can enter. moderator can add members.
50
 * - CFG_OPEN | CFG_REQUEST               => anyone can initiate a request to join the circle, moderator can
51
 *                                           add members
52
 * - CFG_OPEN | CFG_INVITE                => every one can enter, moderator must send invitation.
53
 * - CFG_OPEN | CFG_INVITE | CFG_REQUEST  => every one send a request, moderator must send invitation.
54
 * - CFG_OPEN | CFG_FRIEND                => useless
55
 * - CFG_OPEN | CFG_FRIEND | *            => useless
56
 *
57
 * - CFG_CIRCLE                           => no one can enter, moderator can add members.
58
 *                                           default config, this is only for code readability.
59
 * - CFG_INVITE                           => no one can enter, moderator must send invitation.
60
 * - CFG_FRIEND                           => no one can enter, but all members can add new member.
61
 * - CFG_REQUEST                          => useless (use CFG_OPEN | CFG_REQUEST)
62
 * - CFG_FRIEND | CFG_REQUEST             => no one can join the circle, but all members can request a
63
 *                                           moderator to accept new member
64
 * - CFG_FRIEND | CFG_INVITE              => no one can join the circle, but all members can add new member.
65
 *                                           An invitation will be generated
66
 * - CFG_FRIEND | CFG_INVITE | CFG_REQUEST  => no one can join the circle, but all members can request a
67
 *                                             moderator to accept new member. An invitation will be generated
68
 *
69
 * @package OCA\Circles\Model
70
 */
71
class Circle extends ManagedModel implements INC21Convert, INC21QueryRow, JsonSerializable {
72
73
74
	use TArrayTools;
75
	use TNC21Convert;
76
77
78
	// specific value
79
	const CFG_CIRCLE = 0;        // only for code readability. Circle is locked by default.
80
	const CFG_SINGLE = 1;        // Circle with only one single member.
81
	const CFG_PERSONAL = 2;      // Personal circle, only the owner can see it.
82
83
	// bitwise
84
	const CFG_VISIBLE = 8;        // Visible to everyone, if not visible, people have to know its name to be able to find it
85
	const CFG_OPEN = 16;          // Circle is open, people can join
86
	const CFG_INVITE = 32;        // Adding a member generate an invitation that needs to be accepted
87
	const CFG_REQUEST = 64;       // Request to join Circles needs to be confirmed by a moderator
88
	const CFG_FRIEND = 128;       // Members of the circle can invite their friends
89
	const CFG_PROTECTED = 256;    // Password protected to join/request
90
	const CFG_NO_OWNER = 512;     // no owner, only members
91
	const CFG_HIDDEN = 1024;      // Fully hidden, only backend Circles
92
	const CFG_ROOT = 2048;        // Circle cannot be inside another Circle
93
	const CFG_FEDERATED = 4096;   // Federated
94
95
	const ID_LENGTH = 14;
96
97
	static $DEF = [
98
		1    => 'S|Single',
99
		2    => 'P|Personal',
100
		8    => 'V|Visible',
101
		16   => 'O|Open',
102
		32   => 'I|Invite',
103
		64   => 'JR|Join Request',
104
		128  => 'F|Friends',
105
		256  => 'PP|Password Protected',
106
		512  => 'NO|No Owner',
107
		1024 => 'H|Hidden',
108
		2048 => 'T|Root',
109
		4096 => 'F|Federated'
110
	];
111
112
113
	/** @var string */
114
	private $id = '';
115
116
	/** @var int */
117
	private $config = 0;
118
119
	/** @var int */
120
	private $type = 0;
121
122
	/** @var string */
123
	private $name = '';
124
125
	/** @var string */
126
	private $altName = '';
127
128
	/** @var Member */
129
	private $owner;
130
131
	/** @var array */
132
	private $members = [];
133
134
	/** @var Member */
135
	private $viewer;
136
137
	/** @var array */
138
	private $settings = [];
139
140
	/** @var string */
141
	private $description = '';
142
143
	/** @var int */
144
	private $contactAddressBook = 0;
145
146
	/** @var string */
147
	private $contactGroupName = '';
148
149
//	/** @var bool */
150
//	private $hidden = false;
151
152
	/** @var int */
153
	private $creation = 0;
154
155
156
	/** @var Circle[] */
157
	private $memberOf = null;
158
159
	private $completeJson = false;
160
161
162
	/**
163
	 * Circle constructor.
164
	 */
165
	public function __construct() {
166
	}
167
168
	/**
169
	 * @param string $id
170
	 *
171
	 * @return self
172
	 */
173
	public function setId(string $id): self {
174
		$this->id = $id;
175
176
		return $this;
177
	}
178
179
	/**
180
	 * @return string
181
	 */
182
	public function getId(): string {
183
		return $this->id;
184
	}
185
186
187
	/**
188
	 * @param int $config
189
	 *
190
	 * @return self
191
	 */
192
	public function setConfig(int $config): self {
193
		$this->config = $config;
194
195
//		$this->hidden = false;
196
//		foreach (array_keys(self::$DEF) as $def) {
197
//			if ($this->isType($def) && substr(self::$DEF[$def], 0, 1) === '*') {
198
//				$this->setHidden(true);
199
//				break;
200
//			}
201
//		}
202
203
		return $this;
204
	}
205
206
	/**
207
	 * @return int
208
	 */
209
	public function getConfig(): int {
210
		return $this->config;
211
	}
212
213
	/**
214
	 * @param int $flag
215
	 *
216
	 * @return bool
217
	 */
218
	public function isConfig(int $flag): bool {
219
		return (($this->getConfig() & $flag) !== 0);
220
	}
221
222
223
	/**
224
	 * @param int $type
225
	 *
226
	 * @deprecated
227
	 */
228
	public function setType(int $type): void {
229
		$this->type = $type;
230
	}
231
232
	/**
233
	 * @return int
234
	 * @deprecated
235
	 */
236
	public function getType(): int {
237
		return $this->type;
238
	}
239
240
241
	/**
242
	 * @param string $name
243
	 *
244
	 * @return self
245
	 */
246
	public function setName(string $name): self {
247
		$this->name = $name;
248
249
		return $this;
250
	}
251
252
	/**
253
	 * @return string
254
	 */
255
	public function getName(): string {
256
		return $this->name;
257
	}
258
259
260
	/**
261
	 * @param string $altName
262
	 *
263
	 * @return self
264
	 */
265
	public function setAltName(string $altName): self {
266
		$this->altName = $altName;
267
268
		return $this;
269
	}
270
271
	/**
272
	 * @return string
273
	 */
274
	public function getAltName(): string {
275
		return $this->altName;
276
	}
277
278
279
	/**
280
	 * @param Member $owner
281
	 */
282
	public function setOwner(Member $owner): void {
283
		$this->owner = $owner;
284
	}
285
286
	/**
287
	 * @return Member
288
	 */
289
	public function getOwner(): Member {
290
		return $this->owner;
291
	}
292
293
	/**
294
	 * @return bool
295
	 */
296
	public function hasOwner(): bool {
297
		return ($this->owner !== null);
298
	}
299
300
301
	/**
302
	 * @param array $members
303
	 *
304
	 * @return self
305
	 */
306
	public function setMembers(array $members): self {
307
		$this->members = $members;
308
309
		return $this;
310
	}
311
312
	/**
313
	 * @return array
314
	 */
315
	public function getMembers(): array {
316
		if (empty($this->members)) {
317
			$this->getManager()->getMembers($this);
318
		}
319
320
		return $this->members;
321
	}
322
323
324
	/**
325
	 * @param Member $viewer
326
	 */
327
	public function setViewer(Member $viewer): void {
328
		$this->viewer = $viewer;
329
	}
330
331
	/**
332
	 * @return Member
333
	 */
334
	public function getViewer(): Member {
335
		return $this->viewer;
336
	}
337
338
	/**
339
	 * @return bool
340
	 */
341
	public function hasViewer(): bool {
342
		return ($this->viewer !== null);
343
	}
344
345
346
	/**
347
	 * @param array $settings
348
	 *
349
	 * @return self
350
	 */
351
	public function setSettings(array $settings): self {
352
		$this->settings = $settings;
353
354
		return $this;
355
	}
356
357
	/**
358
	 * @return array
359
	 */
360
	public function getSettings(): array {
361
		return $this->settings;
362
	}
363
364
365
	/**
366
	 * @param string $description
367
	 *
368
	 * @return self
369
	 */
370
	public function setDescription(string $description): self {
371
		$this->description = $description;
372
373
		return $this;
374
	}
375
376
	/**
377
	 * @return string
378
	 */
379
	public function getDescription(): string {
380
		return $this->description;
381
	}
382
383
384
	/**
385
	 * @param int $contactAddressBook
386
	 *
387
	 * @return self
388
	 */
389
	public function setContactAddressBook(int $contactAddressBook): self {
390
		$this->contactAddressBook = $contactAddressBook;
391
392
		return $this;
393
	}
394
395
	/**
396
	 * @return int
397
	 */
398
	public function getContactAddressBook(): int {
399
		return $this->contactAddressBook;
400
	}
401
402
403
	/**
404
	 * @param string $contactGroupName
405
	 *
406
	 * @return self
407
	 */
408
	public function setContactGroupName(string $contactGroupName): self {
409
		$this->contactGroupName = $contactGroupName;
410
411
		return $this;
412
	}
413
414
	/**
415
	 * @return string
416
	 */
417
	public function getContactGroupName(): string {
418
		return $this->contactGroupName;
419
	}
420
421
422
//	/**
423
//	 * @param bool $hidden
424
//	 *
425
//	 * @return Circle
426
//	 */
427
//	public function setHidden(bool $hidden): self {
428
//		$this->hidden = $hidden;
429
//
430
//		return $this;
431
//	}
432
//
433
//	/**
434
//	 * @return bool
435
//	 */
436
//	public function isHidden(): bool {
437
//		return $this->hidden;
438
//	}
439
440
441
	/**
442
	 * @param array $memberOf
443
	 *
444
	 * @return $this
445
	 */
446
	public function setMemberOf(array $memberOf): self {
447
		$this->memberOf = $memberOf;
448
449
		return $this;
450
	}
451
452
	/**
453
	 * @return Circle[]
454
	 */
455
	public function memberOf(): array {
456
		if ($this->memberOf === null) {
457
			$this->getManager()->memberOf($this);
458
		}
459
460
		return $this->memberOf;
461
	}
462
463
464
	/**
465
	 * @param int $creation
466
	 *
467
	 * @return self
468
	 */
469
	public function setCreation(int $creation): self {
470
		$this->creation = $creation;
471
472
		return $this;
473
	}
474
475
	/**
476
	 * @return int
477
	 */
478
	public function getCreation(): int {
479
		return $this->creation;
480
	}
481
482
483
	/**
484
	 * @param array $data
485
	 *
486
	 * @return $this
487
	 */
488
	public function import(array $data): INC21Convert {
489
		$this->setId($this->get('id', $data))
490
			 ->setName($this->get('name', $data))
491
			 ->setAltName($this->get('alt_name', $data))
492
			 ->setConfig($this->getInt('config', $data))
493
			 ->setSettings($this->getArray('settings', $data))
494
//			 ->setContactAddressBook($this->get('contact_addressbook', $data))
495
//			 ->setContactGroupName($this->get('contact_groupname', $data))
496
			 ->setDescription($this->get('description', $data))
497
			 ->setCreation($this->getInt('creation', $data));
498
499
		try {
500
			/** @var Member $owner */
501
			$owner = $this->convert($this->getArray('owner', $data), Member::class);
502
			$this->setOwner($owner);
503
		} catch (InvalidItemException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
504
		}
505
506
		return $this;
507
	}
508
509
510
	/**
511
	 * @return array
512
	 */
513
	public function jsonSerialize(): array {
514
		$arr = [
515
			'id'          => $this->getId(),
516
			'name'        => $this->getName(),
517
			'alt_name'    => $this->getAltName(),
518
			'config'      => $this->getConfig(),
519
			'description' => $this->getDescription(),
520
			'settings'    => $this->getSettings(),
521
			//			'hidden'      => $this->isHidden(),
522
			'creation'    => $this->getCreation()
523
		];
524
525
		if ($this->hasOwner()) {
526
			$arr['owner'] = $this->getOwner();
527
		}
528
529
		if ($this->getManager()->isFullDetails()) {
530
			$arr['memberOf'] = $this->memberOf();
531
		}
532
533
		return array_filter($arr);
534
	}
535
536
537
	/**
538
	 * @param array $data
539
	 *
540
	 * @return INC21QueryRow
541
	 */
542
	public function importFromDatabase(array $data): INC21QueryRow {
543
		$this->setId($this->get('unique_id', $data))
544
			 ->setName($this->get('name', $data))
545
			 ->setAltName($this->get('alt_name', $data))
546
			 ->setConfig($this->getInt('config', $data))
547
			 ->setSettings($this->getArray('settings', $data))
548
			 ->setContactAddressBook($this->getInt('contact_addressbook', $data))
549
			 ->setContactGroupName($this->get('contact_groupname', $data))
550
			 ->setDescription($this->get('description', $data));
551
552
		$creation = $this->get('creation', $data);
553
		$this->setCreation(DateTime::createFromFormat('Y-m-d H:i:s', $creation)->getTimestamp());
554
555
		$this->getManager()->importOwnerFromDatabase($this, $data);
556
		$this->getManager()->importViewerFromDatabase($this, $data);
557
558
		return $this;
559
	}
560
561
}
562