|
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 OCA\Circles\Exceptions\CircleTypeNotValid; |
|
30
|
|
|
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException; |
|
31
|
|
|
|
|
32
|
|
|
class Circle extends BaseCircle implements \JsonSerializable { |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
private $fullJson = false; |
|
36
|
|
|
|
|
37
|
|
|
/** @var bool */ |
|
38
|
|
|
private $lightJson = false; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public function getTypeString() { |
|
42
|
|
View Code Duplication |
switch ($this->getType()) { |
|
|
|
|
|
|
43
|
|
|
case self::CIRCLES_PERSONAL: |
|
44
|
|
|
return 'Personal'; |
|
45
|
|
|
case self::CIRCLES_HIDDEN: |
|
46
|
|
|
return 'Hidden'; |
|
47
|
|
|
case self::CIRCLES_PRIVATE: |
|
48
|
|
|
return 'Private'; |
|
49
|
|
|
case self::CIRCLES_PUBLIC: |
|
50
|
|
|
return 'Public'; |
|
51
|
|
|
case self::CIRCLES_ALL: |
|
52
|
|
|
return 'All'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return 'none'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getTypeLongString() { |
|
59
|
|
|
return self::typeLongString($this->getType()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public function getInfo() { |
|
64
|
|
|
return $this->getTypeLongString(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public function jsonSerialize() { |
|
69
|
|
|
$json = array( |
|
70
|
|
|
'id' => $this->getId(), |
|
71
|
|
|
'name' => $this->getName(), |
|
72
|
|
|
'owner' => $this->getOwner(), |
|
73
|
|
|
'user' => $this->getUser(), |
|
74
|
|
|
'description' => $this->getDescription(), |
|
75
|
|
|
'settings' => $this->getSettings(), |
|
76
|
|
|
'type' => $this->getTypeString(), |
|
77
|
|
|
'creation' => $this->getCreation(), |
|
78
|
|
|
'typeString' => $this->getTypeString(), |
|
79
|
|
|
'typeLongString' => $this->getTypeLongString(), |
|
80
|
|
|
'unique_id' => $this->getUniqueId($this->fullJson), |
|
81
|
|
|
'members' => $this->getMembers(), |
|
82
|
|
|
'links' => $this->getLinks() |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->lightJson) |
|
86
|
|
|
{ |
|
87
|
|
|
$json['members'] = []; |
|
88
|
|
|
$json['links'] = []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $json; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
public function getJson($full = false, $light = false) { |
|
96
|
|
|
$this->fullJson = $full; |
|
97
|
|
|
$this->lightJson = $light; |
|
98
|
|
|
$json = json_encode($this); |
|
99
|
|
|
$this->fullJson = false; |
|
100
|
|
|
$this->lightJson = false; |
|
101
|
|
|
|
|
102
|
|
|
return $json; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
// /** |
|
|
|
|
|
|
108
|
|
|
// * set all infos from an Array. |
|
109
|
|
|
// * |
|
110
|
|
|
// * @param $arr |
|
111
|
|
|
// * |
|
112
|
|
|
// * @return $this |
|
113
|
|
|
// */ |
|
114
|
|
|
// public function fromArray($arr) { |
|
115
|
|
|
// $this->setId($arr['id']); |
|
116
|
|
|
// $this->setName($arr['name']); |
|
117
|
|
|
// $this->setUniqueId($arr['unique_id']); |
|
118
|
|
|
// $this->setDescription($arr['description']); |
|
119
|
|
|
// $this->setType($arr['type']); |
|
120
|
|
|
// $this->setCreation($arr['creation']); |
|
121
|
|
|
//// $this->setOwnerMemberFromArray($arr); |
|
122
|
|
|
//// $this->setUserMemberFromArray($arr); |
|
123
|
|
|
// |
|
124
|
|
|
// return $this; |
|
125
|
|
|
// } |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* set all infos from an Array. |
|
129
|
|
|
* |
|
130
|
|
|
* @param $l10n |
|
131
|
|
|
* @param $arr |
|
132
|
|
|
* |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function fromArray2($l10n, $arr) { |
|
136
|
|
|
$circle = new Circle($l10n); |
|
137
|
|
|
|
|
138
|
|
|
$circle->setId($arr['id']); |
|
139
|
|
|
$circle->setName($arr['name']); |
|
140
|
|
|
$circle->setUniqueId($arr['unique_id']); |
|
141
|
|
|
$circle->setDescription($arr['description']); |
|
142
|
|
|
if (key_exists('links', $arr)) { |
|
143
|
|
|
$circle->setLinks($arr['links']); |
|
144
|
|
|
} |
|
145
|
|
|
if (key_exists('settings', $arr)) { |
|
146
|
|
|
$circle->setSettings($arr['settings']); |
|
147
|
|
|
} |
|
148
|
|
|
$circle->setType($arr['type']); |
|
149
|
|
|
$circle->setCreation($arr['creation']); |
|
150
|
|
|
|
|
151
|
|
|
if (key_exists('user', $arr)) { |
|
152
|
|
|
$circle->setUser(Member::fromArray2($l10n, $arr['user'])); |
|
153
|
|
|
} |
|
154
|
|
|
if (key_exists('owner', $arr)) { |
|
155
|
|
|
$circle->setOwner(Member::fromArray2($l10n, $arr['owner'])); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $circle; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
public static function fromJSON($l10n, $json) { |
|
163
|
|
|
return self::fromArray2($l10n, json_decode($json, true)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
// |
|
|
|
|
|
|
168
|
|
|
// |
|
169
|
|
|
// /** |
|
170
|
|
|
// * set User Infos from Array |
|
171
|
|
|
// * |
|
172
|
|
|
// * @param $array |
|
173
|
|
|
// */ |
|
174
|
|
|
// // TODO rewrite the function based of setOwnerMemberFromArray() |
|
175
|
|
|
// private function setUserMemberFromArray($array) { |
|
176
|
|
|
// if (key_exists('status', $array) |
|
177
|
|
|
// && key_exists('level', $array) |
|
178
|
|
|
// && key_exists('joined', $array) |
|
179
|
|
|
// ) { |
|
180
|
|
|
// $user = new Member($this->l10n); |
|
181
|
|
|
// $user->setStatus($array['status']); |
|
182
|
|
|
// $user->setLevel($array['level']); |
|
183
|
|
|
// $user->setJoined($array['joined']); |
|
184
|
|
|
// $this->setUser($user); |
|
185
|
|
|
// } |
|
186
|
|
|
// } |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @throws CircleTypeNotValid |
|
191
|
|
|
*/ |
|
192
|
|
|
public function cantBePersonal() { |
|
193
|
|
|
if ($this->getType() === self::CIRCLES_PERSONAL) { |
|
194
|
|
|
throw new CircleTypeNotValid( |
|
195
|
|
|
$this->l10n->t("This option is not available for personal circles") |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @throws FederatedCircleNotAllowedException |
|
203
|
|
|
*/ |
|
204
|
|
|
public function hasToBeFederated() { |
|
205
|
|
|
if ($this->getSetting('allow_links') !== 'true') { |
|
206
|
|
|
throw new FederatedCircleNotAllowedException( |
|
207
|
|
|
$this->l10n->t('The circle is not Federated') |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param $type |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
public static function typeLongString($type) { |
|
218
|
|
|
switch ($type) { |
|
219
|
|
|
case self::CIRCLES_PERSONAL: |
|
220
|
|
|
return 'Personal circle'; |
|
221
|
|
|
case self::CIRCLES_HIDDEN: |
|
222
|
|
|
return 'Hidden circle'; |
|
223
|
|
|
case self::CIRCLES_PRIVATE: |
|
224
|
|
|
return 'Private circle'; |
|
225
|
|
|
case self::CIRCLES_PUBLIC: |
|
226
|
|
|
return 'Public circle'; |
|
227
|
|
|
case self::CIRCLES_ALL: |
|
228
|
|
|
return 'All circles'; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return 'none'; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.