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

Circle::getAltName()   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 0
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
 * @package OCA\Circles\Model
47
 */
48
class Circle extends ManagedModel implements INC21Convert, INC21QueryRow, JsonSerializable {
49
50
51
	use TArrayTools;
52
	use TNC21Convert;
53
54
55
	// specific value
56
	const CFG_CIRCLE = 0;        // only for code readability. Circle is locked by default.
57
	const CFG_SINGLE = 1;        // Circle with only one single member.
58
	const CFG_PERSONAL = 2;      // Personal circle, only the owner can see it.
59
60
	// bitwise
61
	const CFG_VISIBLE = 8;        // Visible to everyone, if not visible, people have to know its name to be able to find it
62
	const CFG_OPEN = 16;          // Circle is open, people can join
63
	const CFG_INVITE = 32;        // Adding a member generate an invitation that needs to be accepted
64
	const CFG_REQUEST = 64;       // Request to join Circles needs to be confirmed by a moderator
65
	const CFG_FRIEND = 128;       // Request to join Circles needs to be confirmed by a moderator
66
	const CFG_PROTECTED = 256;    // Password protected to join/request
67
	const CFG_NO_OWNER = 512;     // no owner, only members
68
	const CFG_HIDDEN = 1024;      // Fully hidden, only backend Circles
69
	const CFG_ROOT = 2048;        // Circle cannot be inside another Circle
70
	const CFG_FEDERATED = 4096;   // Federated
71
72
// examples:
73
// CFG_OPEN: everyone can enter. moderator can add members.
74
// CFG_OPEN + CFG_REQUEST: anyone can initiate a request to join the circle, moderator can add members
75
// CFG_OPEN + CFG_INVITE: every one can enter, moderator must send invitation.
76
// CFG_OPEN + CFG_INVITE + CFG_REQUEST: every one send a request, moderator must send invitation.
77
// CFG_CIRCLE: no one can enter, moderator can add members.
78
// CFG_INVITE: no one can enter, moderator must send invitation.
79
// CFG_FRIEND: no one can enter, but all members can add new member.
80
// CFG_FRIEND + CFG_REQUEST: no one can join the circle, but all members can request a moderator to accept new member
81
// CFG_FRIEND + CFG_INVITE: no one can join the circle, but all members can add new member. An invitation will be generated
82
// CFG_FRIEND + CFG_INVITE + CFG_REQUEST: no one can join the circle, but all members can request a moderator to accept new member. An invitation will be generated
83
84
// CFG_REQUEST: useless
85
// CFG_OPEN + CFG_FRIEND: useless
86
// CFG_OPEN + CFG_FRIEND + CFG_REQUEST: useless
87
88
89
	static $DEF = [
90
		1    => 'S|Single',
91
		2    => 'P|Personal',
92
		8    => 'V|Visible',
93
		16   => 'O|Open',
94
		32   => 'I|Invite',
95
		64   => 'JR|Join Request',
96
		128  => 'F|Friends',
97
		256  => 'PP|Password Protected',
98
		512  => 'NO|No Owner',
99
		1024 => 'H|Hidden',
100
		2048 => 'T|Root',
101
		4096 => 'F|Federated'
102
	];
103
104
105
	/** @var string */
106
	private $id = '';
107
108
	/** @var int */
109
	private $config = 0;
110
111
	/** @var string */
112
	private $name = '';
113
114
	/** @var string */
115
	private $altName = '';
116
117
	/** @var Member */
118
	private $owner;
119
120
	/** @var array */
121
	private $members = [];
122
123
	/** @var array */
124
	private $settings = [];
125
126
	/** @var string */
127
	private $description = '';
128
129
	/** @var string */
130
	private $contactAddressBook = '';
131
132
	/** @var string */
133
	private $contactGroupName = '';
134
135
//	/** @var bool */
136
//	private $hidden = false;
137
138
	/** @var int */
139
	private $creation = 0;
140
141
142
	public function __construct() {
143
	}
144
145
	/**
146
	 * @param string $id
147
	 *
148
	 * @return self
149
	 */
150
	public function setId(string $id): self {
151
		$this->id = $id;
152
153
		return $this;
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	public function getId(): string {
160
		return $this->id;
161
	}
162
163
164
	/**
165
	 * @param int $config
166
	 *
167
	 * @return self
168
	 */
169
	public function setConfig(int $config): self {
170
		$this->config = $config;
171
172
//		$this->hidden = false;
173
//		foreach (array_keys(self::$DEF) as $def) {
174
//			if ($this->isType($def) && substr(self::$DEF[$def], 0, 1) === '*') {
175
//				$this->setHidden(true);
176
//				break;
177
//			}
178
//		}
179
180
		return $this;
181
	}
182
183
	/**
184
	 * @return int
185
	 */
186
	public function getConfig(): int {
187
		return $this->config;
188
	}
189
190
	/**
191
	 * @param int $flag
192
	 *
193
	 * @return bool
194
	 */
195
	public function isConfig(int $flag): bool {
196
		return (($this->getConfig() & $flag) !== 0);
197
	}
198
199
200
	/**
201
	 * @param string $name
202
	 *
203
	 * @return self
204
	 */
205
	public function setName(string $name): self {
206
		$this->name = $name;
207
208
		return $this;
209
	}
210
211
	/**
212
	 * @return string
213
	 */
214
	public function getName(): string {
215
		return $this->name;
216
	}
217
218
219
	/**
220
	 * @param string $altName
221
	 *
222
	 * @return self
223
	 */
224
	public function setAltName(string $altName): self {
225
		$this->altName = $altName;
226
227
		return $this;
228
	}
229
230
	/**
231
	 * @return string
232
	 */
233
	public function getAltName(): string {
234
		return $this->altName;
235
	}
236
237
238
	/**
239
	 * @param Member $owner
240
	 */
241
	public function setOwner(Member $owner): void {
242
		$this->owner = $owner;
243
	}
244
245
	/**
246
	 * @return Member
247
	 */
248
	public function getOwner(): Member {
249
		return $this->owner;
250
	}
251
252
	/**
253
	 * @return bool
254
	 */
255
	public function hasOwner(): bool {
256
		return ($this->owner !== null);
257
	}
258
259
260
	/**
261
	 * @param array $members
262
	 *
263
	 * @return self
264
	 */
265
	public function setMembers(array $members): self {
266
		$this->members = $members;
267
268
		return $this;
269
	}
270
271
	/**
272
	 * @return array
273
	 */
274
	public function getMembers(): array {
275
		$this->getManager()->getMembers($this);
276
277
		return $this->members;
278
	}
279
280
281
	/**
282
	 * @param array $settings
283
	 *
284
	 * @return self
285
	 */
286
	public function setSettings(array $settings): self {
287
		$this->settings = $settings;
288
289
		return $this;
290
	}
291
292
	/**
293
	 * @return array
294
	 */
295
	public function getSettings(): array {
296
		return $this->settings;
297
	}
298
299
300
	/**
301
	 * @param string $description
302
	 *
303
	 * @return self
304
	 */
305
	public function setDescription(string $description): self {
306
		$this->description = $description;
307
308
		return $this;
309
	}
310
311
	/**
312
	 * @return string
313
	 */
314
	public function getDescription(): string {
315
		return $this->description;
316
	}
317
318
319
	/**
320
	 * @param string $contactAddressBook
321
	 *
322
	 * @return self
323
	 */
324
	public function setContactAddressBook(string $contactAddressBook): self {
325
		$this->contactAddressBook = $contactAddressBook;
326
327
		return $this;
328
	}
329
330
	/**
331
	 * @return string
332
	 */
333
	public function getContactAddressBook(): string {
334
		return $this->contactAddressBook;
335
	}
336
337
338
	/**
339
	 * @param string $contactGroupName
340
	 *
341
	 * @return self
342
	 */
343
	public function setContactGroupName(string $contactGroupName): self {
344
		$this->contactGroupName = $contactGroupName;
345
346
		return $this;
347
	}
348
349
	/**
350
	 * @return string
351
	 */
352
	public function getContactGroupName(): string {
353
		return $this->contactGroupName;
354
	}
355
356
357
//	/**
358
//	 * @param bool $hidden
359
//	 *
360
//	 * @return Circle
361
//	 */
362
//	public function setHidden(bool $hidden): self {
363
//		$this->hidden = $hidden;
364
//
365
//		return $this;
366
//	}
367
//
368
//	/**
369
//	 * @return bool
370
//	 */
371
//	public function isHidden(): bool {
372
//		return $this->hidden;
373
//	}
374
375
376
	/**
377
	 * @param int $creation
378
	 *
379
	 * @return self
380
	 */
381
	public function setCreation(int $creation): self {
382
		$this->creation = $creation;
383
384
		return $this;
385
	}
386
387
	/**
388
	 * @return int
389
	 */
390
	public function getCreation(): int {
391
		return $this->creation;
392
	}
393
394
395
	/**
396
	 * @param array $data
397
	 *
398
	 * @return $this
399
	 */
400
	public function import(array $data): INC21Convert {
401
		$this->setId($this->get('id', $data))
402
			 ->setName($this->get('name', $data))
403
			 ->setAltName($this->get('alt_name', $data))
404
			 ->setConfig($this->getInt('config', $data))
405
			 ->setSettings($this->getArray('settings', $data))
406
//			 ->setContactAddressBook($this->get('contact_addressbook', $data))
407
//			 ->setContactGroupName($this->get('contact_groupname', $data))
408
			 ->setDescription($this->get('description', $data))
409
			 ->setCreation($this->getInt('creation', $data));
410
411
		try {
412
			/** @var Member $owner */
413
			$owner = $this->convert($this->getArray('owner', $data), Member::class);
414
			$this->setOwner($owner);
415
		} catch (InvalidItemException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
416
		}
417
418
		return $this;
419
	}
420
421
422
	/**
423
	 * @return array
424
	 */
425
	public function jsonSerialize(): array {
426
		$arr = [
427
			'id'          => $this->getId(),
428
			'name'        => $this->getName(),
429
			'alt_name'    => $this->getAltName(),
430
			'config'      => $this->getConfig(),
431
			'description' => $this->getDescription(),
432
			'settings'    => $this->getSettings(),
433
			//			'hidden'      => $this->isHidden(),
434
			'creation'    => $this->getCreation()
435
		];
436
437
		if ($this->hasOwner()) {
438
			$arr['owner'] = $this->getOwner();
439
		}
440
441
		return $arr;
442
	}
443
444
445
	/**
446
	 * @param array $data
447
	 *
448
	 * @return INC21QueryRow
449
	 */
450
	public function importFromDatabase(array $data): INC21QueryRow {
451
		$this->setId($this->get('unique_id', $data))
452
			 ->setName($this->get('name', $data))
453
			 ->setAltName($this->get('alt_name', $data))
454
			 ->setConfig($this->getInt('config', $data))
455
			 ->setSettings($this->getArray('settings', $data))
456
			 ->setContactAddressBook($this->get('contact_addressbook', $data))
457
			 ->setContactGroupName($this->get('contact_groupname', $data))
458
			 ->setDescription($this->get('description', $data));
459
460
		$creation = $this->get('creation', $data);
461
		$this->setCreation(DateTime::createFromFormat('Y-m-d H:i:s', $creation)->getTimestamp());
462
463
		$this->getManager()->importOwnerFromDatabase($this, $data);
464
465
		return $this;
466
	}
467
468
}
469