1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMS Pico - Integration of Pico within your files to create websites. |
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\CMSPico\Model; |
28
|
|
|
|
29
|
|
|
use OCA\CMSPico\Service\MiscService; |
30
|
|
|
|
31
|
|
|
class WebsiteCore implements \JsonSerializable { |
32
|
|
|
|
33
|
|
|
const TYPE_PUBLIC = 1; |
34
|
|
|
const TYPE_PRIVATE = 2; |
35
|
|
|
|
36
|
|
|
const SITE_LENGTH_MIN = 3; |
37
|
|
|
const NAME_LENGTH_MIN = 5; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** @var int */ |
41
|
|
|
private $id; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $userId; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
private $name; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
private $theme = 'default'; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
private $site; |
54
|
|
|
|
55
|
|
|
/** @var int */ |
56
|
|
|
private $type = self::TYPE_PUBLIC; |
57
|
|
|
|
58
|
|
|
/** @var array */ |
59
|
|
|
private $options = []; |
60
|
|
|
|
61
|
|
|
/** @var string */ |
62
|
|
|
private $path; |
63
|
|
|
|
64
|
|
|
/** @var string */ |
65
|
|
|
private $page; |
66
|
|
|
|
67
|
|
|
/** @var int */ |
68
|
|
|
private $creation; |
69
|
|
|
|
70
|
|
|
/** @var string */ |
71
|
|
|
private $viewer; |
72
|
|
|
|
73
|
|
|
/** @var string */ |
74
|
|
|
private $templateSource; |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
public function __construct($data = '') { |
78
|
|
|
|
79
|
|
|
if (is_array($data)) { |
80
|
|
|
$this->fromArray($data); |
81
|
|
|
|
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->fromJSON($data); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $id |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setId($id) { |
95
|
|
|
$this->id = (int)$id; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getId() { |
104
|
|
|
return $this->id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setName($name) { |
114
|
|
|
$this->name = $name; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getName() { |
123
|
|
|
return $this->name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $theme |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function setTheme($theme) { |
133
|
|
|
$this->theme = $theme; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getTheme() { |
142
|
|
|
return $this->theme; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $userId |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setUserId($userId) { |
151
|
|
|
$this->userId = $userId; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getUserId() { |
160
|
|
|
return $this->userId; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $site |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function setSite($site) { |
170
|
|
|
$this->site = $site; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getSite() { |
179
|
|
|
return $this->site; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param int $type |
185
|
|
|
* |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setType($type) { |
189
|
|
|
$this->type = $type; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return int |
196
|
|
|
*/ |
197
|
|
|
public function getType() { |
198
|
|
|
return $this->type; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $key |
204
|
|
|
* @param string $value |
205
|
|
|
* |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
public function setOption($key, $value) { |
209
|
|
|
$this->options[$key] = $value; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $key |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getOption($key) { |
220
|
|
|
if (!key_exists($key, $this->options)) { |
221
|
|
|
return ''; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return (string)$this->options[$key]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param array|string $options |
230
|
|
|
* |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
|
|
public function setOptions($options) { |
234
|
|
|
if (!is_array($options)) { |
235
|
|
|
$options = json_decode($options, true); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($options === null) { |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->options = $options; |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param bool $json |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
public function getOptions($json = false) { |
254
|
|
|
if ($json === true) { |
255
|
|
|
return json_encode($this->options); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->options; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $path |
264
|
|
|
* |
265
|
|
|
* @return $this |
266
|
|
|
*/ |
267
|
|
|
public function setPath($path) { |
268
|
|
|
$this->path = MiscService::endSlash($path); |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
|
|
public function getPath() { |
277
|
|
|
return $this->path; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param string $page |
283
|
|
|
* |
284
|
|
|
* @return $this |
285
|
|
|
*/ |
286
|
|
|
public function setPage($page) { |
287
|
|
|
$this->page = $page; |
288
|
|
|
|
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public function getPage() { |
296
|
|
|
return $this->page; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param int $creation |
302
|
|
|
* |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function setCreation($creation) { |
306
|
|
|
$this->creation = $creation; |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return int |
313
|
|
|
*/ |
314
|
|
|
public function getCreation() { |
315
|
|
|
return $this->creation; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param string $viewer |
321
|
|
|
* |
322
|
|
|
* @return $this |
323
|
|
|
*/ |
324
|
|
|
public function setViewer($viewer) { |
325
|
|
|
$this->viewer = $viewer; |
326
|
|
|
|
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
|
|
public function getViewer() { |
334
|
|
|
return $this->viewer; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param string $source |
340
|
|
|
* |
341
|
|
|
* @return $this |
342
|
|
|
*/ |
343
|
|
|
public function setTemplateSource($source) { |
344
|
|
|
$this->templateSource = $source; |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return string |
351
|
|
|
*/ |
352
|
|
|
public function getTemplateSource() { |
353
|
|
|
return $this->templateSource; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
|
|
public function jsonSerialize() { |
361
|
|
|
return array( |
362
|
|
|
'id' => $this->getId(), |
363
|
|
|
'name' => $this->getName(), |
364
|
|
|
'user_id' => $this->getUserId(), |
365
|
|
|
'site' => $this->getSite(), |
366
|
|
|
'page' => $this->getPage(), |
367
|
|
|
'theme' => $this->getTheme(), |
368
|
|
|
'type' => $this->getType(), |
369
|
|
|
'options' => $this->getOptions(), |
370
|
|
|
'path' => $this->getPath(), |
371
|
|
|
'creation' => $this->getCreation() |
372
|
|
|
); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param array $arr |
378
|
|
|
* |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
|
|
public function fromArray($arr) { |
382
|
|
|
if (!is_array($arr)) { |
383
|
|
|
return false; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
MiscService::mustContains($arr, ['name', 'user_id', 'site', 'type', 'path']); |
387
|
|
|
|
388
|
|
|
$this->setId((int)MiscService::get($arr, 'id')) |
389
|
|
|
->setName($arr['name']) |
390
|
|
|
->setUserId($arr['user_id']) |
391
|
|
|
->setSite($arr['site']) |
392
|
|
|
->setPage($arr['page']) |
393
|
|
|
->setTheme(MiscService::get($arr, 'theme', 'default')) |
394
|
|
|
->setType($arr['type']) |
395
|
|
|
->setOptions(MiscService::get($arr, 'options')) |
396
|
|
|
->setPath($arr['path']) |
397
|
|
|
->setCreation((int)MiscService::get($arr, 'creation')); |
398
|
|
|
|
399
|
|
|
return true; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @param string $json |
405
|
|
|
*/ |
406
|
|
|
public function fromJSON($json) { |
407
|
|
|
$this->fromArray(json_decode($json, true)); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
} |