Completed
Push — master ( b190e4...58ad6c )
by Maxence
02:50
created

BaseMember::getDisplayNAme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Model;
28
29
use OC\L10N\L10N;
30
use OCA\Circles\Service\MiscService;
31
32
class BaseMember implements \JsonSerializable {
33
34
	const LEVEL_NONE = 0;
35
	const LEVEL_MEMBER = 1;
36
	const LEVEL_MODERATOR = 4;
37
	const LEVEL_ADMIN = 8;
38
	const LEVEL_OWNER = 9;
39
40
	const STATUS_NONMEMBER = 'Unknown';
41
	const STATUS_INVITED = 'Invited';
42
	const STATUS_REQUEST = 'Requesting';
43
	const STATUS_MEMBER = 'Member';
44
	const STATUS_BLOCKED = 'Blocked';
45
	const STATUS_KICKED = 'Kicked';
46
47
	const TYPE_USER = 1;
48
	const TYPE_MAIL = 2;
49
50
	/** @var string */
51
	private $circleUniqueId;
52
53
	/** @var L10N */
54
	protected $l10n;
55
56
	/** @var string */
57
	private $userId = '';
58
59
	/** @var string */
60
	private $groupId = '';
61
62
	/** @var int */
63
	private $type = self::TYPE_USER;
64
65
	/** @var string */
66
	private $displayName;
67
68
	/** @var int */
69
	private $level;
70
71
	/** @var string */
72
	private $status;
73
74
	/** @var string */
75
	private $note;
76
77
	/** @var string */
78
	private $joined;
79
80
	/**
81
	 * BaseMember constructor.
82
	 *
83
	 * @param $l10n
84
	 * @param string $userId
85
	 * @param string $circleUniqueId
86
	 */
87
	public function __construct($l10n, $userId = '', $circleUniqueId = '') {
88
		$this->l10n = $l10n;
89
90
		if ($userId !== '') {
91
			$this->setUserId($userId);
92
		}
93
		if ($circleUniqueId > -1) {
94
			$this->setCircleId($circleUniqueId);
95
		}
96
		$this->setLevel(Member::LEVEL_NONE);
97
		$this->setStatus(Member::STATUS_NONMEMBER);
98
	}
99
100
101
	/**
102
	 * @param string $circleUniqueId
103
	 *
104
	 * @return $this
105
	 */
106
	public function setCircleId($circleUniqueId) {
107
		$this->circleUniqueId = $circleUniqueId;
108
109
		return $this;
110
	}
111
112
	/**
113
	 * @return string
114
	 */
115
	public function getCircleId() {
116
		return $this->circleUniqueId;
117
	}
118
119
120
	/**
121
	 * @param string $groupId
122
	 */
123
	public function setGroupId($groupId) {
124
		$this->groupId = $groupId;
125
	}
126
127
	/**
128
	 * @return string
129
	 */
130
	public function getGroupId() {
131
		return $this->groupId;
132
	}
133
134
135
	/**
136
	 * @return int
137
	 */
138
	public function getType() {
139
		return $this->type;
140
	}
141
142
	public function setType($type) {
143
		$this->type = $type;
144
	}
145
146
147
	public function getViewerType() {
148
		if ($this->getUserId() === '') {
149
			return 'group';
150
		} else {
151
			return 'user';
152
		}
153
	}
154
155
	public function getViewerId() {
156
		if ($this->getViewerType() === 'user') {
157
			return $this->getUserId();
158
		} else {
159
			return $this->getGroupId();
160
		}
161
	}
162
163
	public function setUserId($userId) {
164
		$this->userId = $userId;
165
166
		if ($userId !== null && $userId !== '') {
167
			$this->setDisplayName(
168
				MiscService::staticGetDisplayName($userId)
169
			);
170
		}
171
172
		return $this;
173
	}
174
175
	public function getUserId() {
176
		return $this->userId;
177
	}
178
179
180
	public function setDisplayName($display) {
181
		$this->displayName = $display;
182
183
		return $this;
184
	}
185
186
	public function getDisplayNAme() {
187
		return $this->displayName;
188
	}
189
190
191
	public function setLevel($level) {
192
		$this->level = (int)$level;
193
194
		return $this;
195
	}
196
197
	public function getLevel() {
198
		return $this->level;
199
	}
200
201
202
	public function setNote($note) {
203
		$this->note = $note;
204
205
		return $this;
206
	}
207
208
	public function getNote() {
209
		return $this->note;
210
	}
211
212
213
	public function setStatus($status) {
214
		if (is_null($status)) {
215
			$this->status = self::STATUS_NONMEMBER;
216
		} else {
217
			$this->status = $status;
218
		}
219
220
		return $this;
221
	}
222
223
	public function getStatus() {
224
		return $this->status;
225
	}
226
227
228
	public function setJoined($joined) {
229
		$this->joined = $joined;
230
231
		return $this;
232
	}
233
234
	public function getJoined() {
235
		return $this->joined;
236
	}
237
238
239
	public function isLevel($level) {
240
		return ($this->getLevel() >= $level);
241
	}
242
243
244
	public function isAlmostMember() {
245
		return ($this->getStatus() === Member::STATUS_INVITED
246
				|| $this->getStatus() === Member::STATUS_REQUEST);
247
	}
248
249
250
	protected function setAsAMember($level = 1) {
251
		$this->setStatus(Member::STATUS_MEMBER);
252
		$this->setLevel($level);
253
	}
254
255
256
	public static function fromArray($l10n, $arr) {
257
		if ($arr === null) {
258
			return null;
259
		}
260
261
		$member = new Member($l10n);
262
263
		$member->setCircleId($arr['circle_id']);
264
		$member->setLevel($arr['level']);
265
266
		if (key_exists('user_id', $arr)) {
267
			$member->setUserId($arr['user_id']);
268
		}
269
270
		if (key_exists('group_id', $arr)) {
271
			$member->setGroupId($arr['group_id']);
272
		}
273
274
		if (key_exists('status', $arr)) {
275
			$member->setStatus($arr['status']);
276
		}
277
278
		if (key_exists('note', $arr)) {
279
			$member->setNote($arr['note']);
280
		}
281
282
		if (key_exists('joined', $arr)) {
283
			$member->setJoined($arr['joined']);
284
		}
285
286
		return $member;
287
	}
288
289
290
	public static function fromJSON($l10n, $json) {
291
		return self::fromArray($l10n, json_decode($json, true));
292
	}
293
294
	public function jsonSerialize() {
295
		return array(
296
			'circle_id'    => $this->getCircleId(),
297
			'user_id'      => $this->getUserId(),
298
			'group_id'     => $this->getGroupId(),
299
			'type'         => $this->getViewerType(),
300
			'display_name' => $this->getDisplayName(),
301
			'level'        => $this->getLevel(),
302
			'level_string' => $this->getLevelString(),
303
			'status'       => $this->getStatus(),
304
			'note'         => $this->getNote(),
305
			'joined'       => $this->getJoined()
306
		);
307
	}
308
309
	public function getLevelString() {
310
		switch ($this->getLevel()) {
311
			case self::LEVEL_NONE:
312
				return 'Not a member';
313
			case self::LEVEL_MEMBER:
314
				return 'Member';
315
			case self::LEVEL_MODERATOR:
316
				return 'Moderator';
317
			case self::LEVEL_ADMIN:
318
				return 'Admin';
319
			case self::LEVEL_OWNER:
320
				return 'Owner';
321
		}
322
323
		return 'none';
324
	}
325
}
326