1
|
|
|
<?php |
2
|
|
|
namespace library\storage |
3
|
|
|
{ |
4
|
|
|
|
5
|
|
|
use library\crypt\Crypt; |
6
|
|
|
use library\images\ImageResizer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class JsonStorage |
10
|
|
|
* @package library\storage |
11
|
|
|
*/ |
12
|
|
|
class JsonStorage implements Storage |
13
|
|
|
{ |
14
|
|
|
private $storagePath; |
15
|
|
|
private $repository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JsonStorage constructor. |
19
|
|
|
* |
20
|
|
|
* @param $storagePath |
21
|
|
|
*/ |
22
|
|
|
public function __construct($storagePath) |
23
|
|
|
{ |
24
|
|
|
$this->storagePath = $storagePath; |
25
|
|
|
$this->config(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retrieve the data from the storagepath |
30
|
|
|
* so it can be interacted with |
31
|
|
|
* |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
private function config() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$storagePath = __DIR__ . $this->storagePath; |
37
|
|
|
if (realpath($storagePath) !== false) { |
38
|
|
|
$jsonString = file_get_contents($storagePath); |
39
|
|
|
$this->repository = json_decode($jsonString); |
40
|
|
|
} else { |
41
|
|
|
// Here is some logic for the initialisation of a new clone of the framework |
42
|
|
|
initFramework($storagePath); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get user by username |
50
|
|
|
* |
51
|
|
|
* @param $username |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function getUserByUsername($username) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$return = array(); |
58
|
|
|
|
59
|
|
|
$users = $this->repository->users; |
60
|
|
|
foreach ($users as $user) { |
61
|
|
|
if ($user->username == $username) { |
62
|
|
|
$return = $user; |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function getUserBySlug($slug) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$return = array(); |
73
|
|
|
|
74
|
|
|
$users = $this->repository->users; |
75
|
|
|
foreach ($users as $user) { |
76
|
|
|
if ($user->slug == $slug) { |
77
|
|
|
$return = $user; |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getUsers() |
86
|
|
|
{ |
87
|
|
|
return $this->repository->users; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function saveUser($slug, $postValues) |
91
|
|
|
{ |
92
|
|
|
$userObj = $this->createUserFromPostValues($postValues); |
93
|
|
|
if ($userObj->slug != $slug) { |
94
|
|
|
// If the username changed, check for duplicates |
95
|
|
|
$doesItExist = $this->getUserBySlug($userObj->slug); |
96
|
|
|
if (!empty($doesItExist)) { |
97
|
|
|
throw new \Exception('Trying to rename user to existing username'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
$users = $this->getUsers(); |
101
|
|
|
foreach ($users as $key => $user) { |
102
|
|
|
if ($user->slug == $slug) { |
103
|
|
|
$users[$key] = $userObj; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$this->repository->users = $users; |
107
|
|
|
$this->save(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function addUser($postValues) |
111
|
|
|
{ |
112
|
|
|
$userObj = $this->createUserFromPostValues($postValues); |
113
|
|
|
|
114
|
|
|
$doesItExist = $this->getUserBySlug($userObj->slug); |
115
|
|
|
if (!empty($doesItExist)) { |
116
|
|
|
throw new \Exception('Trying to add username that already exists.'); |
117
|
|
|
} |
118
|
|
|
$this->repository->users[] = $userObj; |
119
|
|
|
$this->save(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function deleteUserBySlug($slug) |
123
|
|
|
{ |
124
|
|
|
$userToDelete = $this->getUserBySlug($slug); |
125
|
|
|
if (empty($userToDelete)) { |
126
|
|
|
throw new \Exception('Trying to delete a user that doesn\'t exist.'); |
127
|
|
|
} |
128
|
|
|
$users = $this->getUsers(); |
129
|
|
|
foreach ($users as $key => $user) { |
130
|
|
|
if ($user->slug == $userToDelete->slug) { |
131
|
|
|
unset($users[$key]); |
132
|
|
|
$this->repository->users = array_values($users); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
$this->save(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function createUserFromPostValues($postValues) |
139
|
|
|
{ |
140
|
|
|
if (isset($postValues['username'])) { |
141
|
|
|
$user = new \stdClass(); |
142
|
|
|
$user->username = $postValues['username']; |
143
|
|
|
$user->slug = slugify($postValues['username']); |
144
|
|
|
$user->rights = array(); |
145
|
|
|
if (isset($postValues['rights'])) { |
146
|
|
|
$user->rights = $postValues['rights']; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (isset($postValues['password']) && empty($postValues['password']) === false) { |
150
|
|
|
$crypt = new Crypt(); |
151
|
|
|
$user->password = $crypt->encrypt($postValues['password'], 16); |
152
|
|
|
$user->salt = $crypt->getLastSalt(); |
153
|
|
|
} else { |
154
|
|
|
$user->password = $postValues['passHash']; |
155
|
|
|
$user->salt = $postValues['salt']; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $user; |
159
|
|
|
} else { |
160
|
|
|
throw new \Exception('Trying to create user with invalid data.'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
* |
166
|
|
|
* Documents |
167
|
|
|
* |
168
|
|
|
*/ |
169
|
|
|
/** |
170
|
|
|
* Get documents |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function getDocuments() |
175
|
|
|
{ |
176
|
|
|
return $this->repository->documents; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $slug |
181
|
|
|
* @return mixed |
182
|
|
|
* @throws \Exception |
183
|
|
|
*/ |
184
|
|
|
public function getDocumentBySlug($slug) |
185
|
|
|
{ |
186
|
|
|
$documentContainer = $this->getDocumentContainerByPath('/' . $slug); |
187
|
|
|
$indices = $documentContainer['indices']; |
188
|
|
|
|
189
|
|
|
if ($indices === null) { |
190
|
|
|
$emptyReturn = new \stdClass(); |
191
|
|
|
$emptyReturn->title = 'Not found'; |
192
|
|
|
$emptyReturn->type = null; |
193
|
|
|
$emptyReturn->state = 'published'; |
194
|
|
|
return $emptyReturn; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$folder = $this->repository->documents; |
198
|
|
|
foreach ($indices as $index) { |
199
|
|
|
if ($folder === $this->repository->documents) { |
200
|
|
|
$folder = $folder[$index]; |
201
|
|
|
} else { |
202
|
|
|
$folder = $folder->content[$index]; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $folder; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
public function saveDocument($postValues) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$documentFolderObject = $this->createDocumentFromPostValues($postValues); |
212
|
|
|
|
213
|
|
|
$documentContainer = $this->getDocumentContainerByPath($_GET['slug']); |
214
|
|
|
$indices = $documentContainer['indices']; |
215
|
|
|
|
216
|
|
|
$folder = $this->repository->documents; |
217
|
|
|
$previousFolder = $this->repository->documents; |
218
|
|
|
foreach ($indices as $index) { |
219
|
|
|
if ($folder === $this->repository->documents) { |
220
|
|
|
$folder = $folder[$index]; |
221
|
|
|
} else { |
222
|
|
|
$previousFolder = $folder; |
223
|
|
|
$folder = $folder->content[$index]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ($previousFolder === $this->repository->documents) { |
228
|
|
|
// Check for duplicates |
229
|
|
|
foreach ($this->repository->documents as $index => $document) { |
230
|
|
|
if (end($indices) !== $index && $document->slug == $documentFolderObject->slug && $document->type == 'document') { |
231
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
$this->repository->documents[end($indices)] = $documentFolderObject; |
235
|
|
|
} else { |
236
|
|
|
// Check for duplicates |
237
|
|
|
foreach ($previousFolder->content as $index => $document) { |
238
|
|
|
if (end($indices) !== $index && $document->slug == $documentFolderObject->slug && $document->type == 'document') { |
239
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
$previousFolder->content[end($indices)] = $documentFolderObject ; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->save(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function addDocument($postValues) |
249
|
|
|
{ |
250
|
|
|
$documentFolderObject = $this->createDocumentFromPostValues($postValues); |
251
|
|
|
if ($postValues['path'] == '' || $postValues['path'] == '/') { |
252
|
|
|
// Check folder duplicate child |
253
|
|
|
foreach ($this->repository->documents as $document) { |
254
|
|
|
if ($document->slug == $documentFolderObject->slug && $document->type == 'document') { |
255
|
|
|
// TODO make it so it doesnt throw an exception, but instead shows a warning |
256
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
$this->repository->documents[] = $documentFolderObject; |
260
|
|
|
} else { |
261
|
|
|
// Check folder duplicate child |
262
|
|
|
$containerFolder = $this->getDocumentFolderBySlug($postValues['path']); |
263
|
|
|
if (isset($containerFolder->content)) { |
264
|
|
View Code Duplication |
foreach ($containerFolder->content as $document) { |
|
|
|
|
265
|
|
|
if ($document->slug == $documentFolderObject->slug && $document->type == 'document') { |
266
|
|
|
// TODO make it so it doesnt throw an exception, but instead shows a warning |
267
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
$containerFolder->content[] = $documentFolderObject; |
272
|
|
|
} |
273
|
|
|
$this->save(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
View Code Duplication |
public function deleteDocumentBySlug($slug) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$documentContainer = $this->getDocumentContainerByPath($slug); |
279
|
|
|
$indices = $documentContainer['indices']; |
280
|
|
|
|
281
|
|
|
$folder = $this->repository->documents; |
282
|
|
|
$previousFolder = $this->repository->documents; |
283
|
|
|
foreach ($indices as $index) { |
284
|
|
|
if ($folder === $this->repository->documents) { |
285
|
|
|
$folder = $folder[$index]; |
286
|
|
|
} else { |
287
|
|
|
$previousFolder = $folder; |
288
|
|
|
$folder = $folder->content[$index]; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if ($previousFolder === $this->repository->documents) { |
293
|
|
|
unset($this->repository->documents[end($indices)]); |
294
|
|
|
$this->repository->documents = array_values($this->repository->documents); |
295
|
|
|
} else { |
296
|
|
|
unset($previousFolder->content[end($indices)]); |
297
|
|
|
$previousFolder->content = array_values($previousFolder->content); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$this->save(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
private function createDocumentFromPostValues($postValues) |
304
|
|
|
{ |
305
|
|
|
$postValues = utf8Convert($postValues); |
306
|
|
|
$documentType = $this->getDocumentTypeBySlug($postValues['documentType']); |
307
|
|
|
|
308
|
|
|
$staticBricks = $documentType->bricks; |
309
|
|
|
|
310
|
|
|
$documentObj = new \stdClass(); |
311
|
|
|
$documentObj->title = $postValues['title']; |
312
|
|
|
$documentObj->slug = slugify($postValues['title']); |
313
|
|
|
$documentObj->type = 'document'; |
314
|
|
|
$documentObj->documentType = $documentType->title; |
315
|
|
|
$documentObj->documentTypeSlug = $documentType->slug; |
316
|
|
|
$documentObj->state = isset($postValues['state']) ? 'published' : 'unpublished'; |
317
|
|
|
$documentObj->lastModificationDate = time(); |
318
|
|
|
$documentObj->creationDate = isset($postValues['creationDate']) ? intval($postValues['creationDate']) : time(); |
319
|
|
|
$documentObj->lastModifiedBy = $_SESSION['cloudcontrol']->username; |
320
|
|
|
|
321
|
|
|
$documentObj->fields = isset($postValues['fields']) ? $postValues['fields'] : array(); |
322
|
|
|
|
323
|
|
|
$documentObj->bricks = array(); |
324
|
|
|
if (isset($postValues['bricks'])) { |
325
|
|
|
foreach ($postValues['bricks'] as $brickSlug => $brick) { |
326
|
|
|
// Check if its multiple |
327
|
|
|
$multiple = false; |
328
|
|
|
$staticBrick = null; |
329
|
|
|
foreach ($staticBricks as $staticBrick) { |
330
|
|
|
if ($staticBrick->slug === $brickSlug) { |
331
|
|
|
$multiple = $staticBrick->multiple; |
332
|
|
|
break; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
if ($multiple) { |
337
|
|
|
$brickArray = array(); |
338
|
|
|
foreach ($brick as $brickInstance) { |
339
|
|
|
$brickObj = new \stdClass(); |
340
|
|
|
$brickObj->fields = new \stdClass(); |
341
|
|
|
$brickObj->type = $staticBrick->brickSlug; |
342
|
|
|
|
343
|
|
|
foreach ($brickInstance['fields'] as $fieldName => $fieldValues) { |
344
|
|
|
$brickObj->fields->$fieldName = $fieldValues; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$brickArray[] = $brickObj; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$documentObj->bricks[$brickSlug] = $brickArray; |
351
|
|
|
} else { |
352
|
|
|
$documentObj->bricks[$brickSlug] = $brick; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
$documentObj->dynamicBricks = array(); |
357
|
|
|
if (isset($postValues['dynamicBricks'])) { |
358
|
|
|
foreach ($postValues['dynamicBricks'] as $brickTypeSlug => $brick) { |
359
|
|
|
foreach ($brick as $brickContent) { |
360
|
|
|
$brickObj = new \stdClass(); |
361
|
|
|
$brickObj->type = $brickTypeSlug; |
362
|
|
|
$brickObj->fields = $brickContent; |
363
|
|
|
$documentObj->dynamicBricks[] = $brickObj; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
return $documentObj; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Add new document in given path |
372
|
|
|
* |
373
|
|
|
* @param array $postValues |
374
|
|
|
* |
375
|
|
|
* @throws \Exception |
376
|
|
|
*/ |
377
|
|
|
public function addDocumentFolder($postValues) |
378
|
|
|
{ |
379
|
|
|
$documentFolderObject = $this->createDocumentFolderFromPostValues($postValues); |
380
|
|
|
if ($postValues['path'] == '' || $postValues['path'] == '/') { |
381
|
|
|
// Check folder duplicate child |
382
|
|
View Code Duplication |
foreach ($this->repository->documents as $document) { |
|
|
|
|
383
|
|
|
if ($document->slug == $documentFolderObject->slug && $document->type == 'folder') { |
384
|
|
|
// TODO make it so it doesnt throw an exception, but instead shows a warning |
385
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
$this->repository->documents[] = $documentFolderObject; |
389
|
|
|
} else { |
390
|
|
|
$documentContainer = $this->getDocumentContainerByPath($postValues['path']); |
391
|
|
|
$documentContainerArray = $documentContainer['indices']; |
392
|
|
|
$containerFolder = $documentContainer['containerFolder']; |
393
|
|
|
$folder = $this->repository->documents; |
394
|
|
|
foreach ($documentContainerArray as $index) { |
395
|
|
|
if ($folder === $this->repository->documents) { |
396
|
|
|
$folder = $folder[$index]; |
397
|
|
|
} else { |
398
|
|
|
$folder = $folder->content[$index]; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
} |
402
|
|
|
// Check folder duplicate child |
403
|
|
View Code Duplication |
foreach ($containerFolder->content as $document) { |
|
|
|
|
404
|
|
|
if ($document->slug == $documentFolderObject->slug && $document->type == 'folder') { |
405
|
|
|
// TODO make it so it doesnt throw an exception, but instead shows a warning |
406
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
$folder->content[] = $documentFolderObject; |
410
|
|
|
} |
411
|
|
|
$this->save(); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Delete a folder by its compound slug |
416
|
|
|
* |
417
|
|
|
* @param $slug |
418
|
|
|
* |
419
|
|
|
* @throws \Exception |
420
|
|
|
*/ |
421
|
|
View Code Duplication |
public function deleteDocumentFolderBySlug($slug) |
|
|
|
|
422
|
|
|
{ |
423
|
|
|
$documentContainer = $this->getDocumentContainerByPath($slug); |
424
|
|
|
$indices = $documentContainer['indices']; |
425
|
|
|
|
426
|
|
|
$folder = $this->repository->documents; |
427
|
|
|
$previousFolder = $this->repository->documents; |
428
|
|
|
foreach ($indices as $index) { |
429
|
|
|
if ($folder === $this->repository->documents) { |
430
|
|
|
$folder = $folder[$index]; |
431
|
|
|
} else { |
432
|
|
|
$previousFolder = $folder; |
433
|
|
|
$folder = $folder->content[$index]; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
if ($previousFolder === $this->repository->documents) { |
438
|
|
|
unset($this->repository->documents[end($indices)]); |
439
|
|
|
$this->repository->documents = array_values($this->repository->documents); |
440
|
|
|
} else { |
441
|
|
|
unset($previousFolder->content[end($indices)]); |
442
|
|
|
$previousFolder->content = array_values($previousFolder->content); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
$this->save(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Retrieve a folder by its compound slug |
450
|
|
|
* |
451
|
|
|
* @param $slug |
452
|
|
|
* |
453
|
|
|
* @return mixed |
454
|
|
|
* @throws \Exception |
455
|
|
|
*/ |
456
|
|
|
public function getDocumentFolderBySlug($slug) |
457
|
|
|
{ |
458
|
|
|
$documentContainer = $this->getDocumentContainerByPath('/' . $slug); |
459
|
|
|
$indices = $documentContainer['indices']; |
460
|
|
|
|
461
|
|
|
$folder = $this->repository->documents; |
462
|
|
|
if ($indices === null) { |
463
|
|
|
throw new \Exception('Can\'t find folder with slug `' . $slug . '`'); |
464
|
|
|
} |
465
|
|
|
foreach ($indices as $index) { |
466
|
|
|
if ($folder === $this->repository->documents) { |
467
|
|
|
$folder = $folder[$index]; |
468
|
|
|
} else { |
469
|
|
|
$folder = $folder->content[$index]; |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
return $folder; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Save changes to folder |
478
|
|
|
* |
479
|
|
|
* @param $postValues |
480
|
|
|
* |
481
|
|
|
* @throws \Exception |
482
|
|
|
*/ |
483
|
|
View Code Duplication |
public function saveDocumentFolder($postValues) |
|
|
|
|
484
|
|
|
{ |
485
|
|
|
$documentFolderObject = $this->createDocumentFolderFromPostValues($postValues); |
486
|
|
|
|
487
|
|
|
$documentContainer = $this->getDocumentContainerByPath($_GET['slug']); |
488
|
|
|
$indices = $documentContainer['indices']; |
489
|
|
|
|
490
|
|
|
$folder = $this->repository->documents; |
491
|
|
|
$previousFolder = $this->repository->documents; |
492
|
|
|
foreach ($indices as $index) { |
493
|
|
|
if ($folder === $this->repository->documents) { |
494
|
|
|
$folder = $folder[$index]; |
495
|
|
|
} else { |
496
|
|
|
$previousFolder = $folder; |
497
|
|
|
$folder = $folder->content[$index]; |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
if ($previousFolder === $this->repository->documents) { |
502
|
|
|
// Check for duplicates |
503
|
|
|
foreach ($this->repository->documents as $index => $document) { |
504
|
|
|
if (end($indices) !== $index && $document->slug == $documentFolderObject->slug && $document->type == 'folder') { |
505
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
$this->repository->documents[end($indices)] = $documentFolderObject; |
509
|
|
|
} else { |
510
|
|
|
// Check for duplicates |
511
|
|
|
foreach ($previousFolder->content as $index => $document) { |
512
|
|
|
if (end($indices) !== $index && $document->slug == $documentFolderObject->slug && $document->type == 'folder') { |
513
|
|
|
throw new \Exception('Duplicate slug: ' . $document->slug . ' in folder ' . $postValues['path']); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
$previousFolder->content[end($indices)] = $documentFolderObject ; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
$this->save(); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Convert path to indeces |
524
|
|
|
* |
525
|
|
|
* @param $path |
526
|
|
|
* |
527
|
|
|
* @return array |
528
|
|
|
* @throws \Exception |
529
|
|
|
*/ |
530
|
|
|
private function getDocumentContainerByPath($path) |
531
|
|
|
{ |
532
|
|
|
$slugs = explode('/', $path); |
533
|
|
|
$slugs = array_filter($slugs); |
534
|
|
|
end($slugs); |
535
|
|
|
$lastKey = key($slugs); |
536
|
|
|
$root = $this->repository->documents; |
537
|
|
|
$i = 0; |
538
|
|
|
$returnArray = array(); |
539
|
|
|
$noMatches = 0; |
540
|
|
|
$foundDocument = null; |
541
|
|
|
$document = null; |
542
|
|
|
$previousDocument = null; |
543
|
|
|
foreach ($slugs as $slug) { |
544
|
|
|
$matched = false; |
545
|
|
|
$previousDocument = null; |
546
|
|
|
end($root); |
547
|
|
|
$lastSubKey = key($root); |
548
|
|
|
foreach ($root as $index => $document) { |
549
|
|
|
if ($slug == $document->slug) { |
550
|
|
|
if ($i != $lastKey && $document->type == 'folder') { |
551
|
|
|
$returnArray[] = $index; |
552
|
|
|
$root = $root[$index]->content; |
553
|
|
|
$matched = true; |
554
|
|
|
} else { |
555
|
|
|
$foundDocument = $document; |
556
|
|
|
$returnArray[] = $index; |
557
|
|
|
$matched = true; |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
if ($lastSubKey != $index) { |
562
|
|
|
$previousDocument = $document; |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
if ($matched === true) { |
566
|
|
|
$noMatches += 1; |
567
|
|
|
} else { |
568
|
|
|
return array( |
569
|
|
|
'containerFolder' => new \stdClass(), |
570
|
|
|
'indices' => null, |
571
|
|
|
'document' => new \stdClass(), |
572
|
|
|
'previousDocument' => new \stdClass() |
573
|
|
|
); |
574
|
|
|
} |
575
|
|
|
$i += 1; |
576
|
|
|
} |
577
|
|
|
if ($noMatches > 0) { |
578
|
|
|
return array( |
579
|
|
|
'containerFolder' => $document, |
580
|
|
|
'indices' => $returnArray, |
581
|
|
|
'document' => $foundDocument, |
582
|
|
|
'previousDocument' => $previousDocument |
583
|
|
|
); |
584
|
|
|
} else { |
585
|
|
|
throw new \Exception('Invalid path: ' . $path); |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Create folder from post values |
591
|
|
|
* |
592
|
|
|
* @param $postValues |
593
|
|
|
* |
594
|
|
|
* @return \stdClass |
595
|
|
|
* @throws \Exception |
596
|
|
|
*/ |
597
|
|
|
private function createDocumentFolderFromPostValues($postValues) |
598
|
|
|
{ |
599
|
|
|
if (isset($postValues['title'], $postValues['path'], $postValues['content'])) { |
600
|
|
|
$documentFolderObject = new \stdClass(); |
601
|
|
|
$documentFolderObject->title = $postValues['title']; |
602
|
|
|
$documentFolderObject->slug = slugify($postValues['title']); |
603
|
|
|
$documentFolderObject->type = 'folder'; |
604
|
|
|
$documentFolderObject->content = json_decode($postValues['content']); |
605
|
|
|
|
606
|
|
|
return $documentFolderObject; |
607
|
|
|
} else { |
608
|
|
|
throw new \Exception('Trying to create document folder with invalid data.'); |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/* |
613
|
|
|
* |
614
|
|
|
* Sitemap |
615
|
|
|
* |
616
|
|
|
*/ |
617
|
|
|
/** |
618
|
|
|
* @return array |
619
|
|
|
*/ |
620
|
|
|
public function getSitemap() |
621
|
|
|
{ |
622
|
|
|
return $this->repository->sitemap; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Add a sitemap item |
627
|
|
|
* |
628
|
|
|
* @param $postValues |
629
|
|
|
* |
630
|
|
|
* @throws \Exception |
631
|
|
|
*/ |
632
|
|
|
public function addSitemapItem($postValues) |
633
|
|
|
{ |
634
|
|
|
$sitemapObject = $this->createSitemapItemFromPostValues($postValues); |
635
|
|
|
|
636
|
|
|
$this->repository->sitemap[] = $sitemapObject; |
637
|
|
|
$this->save(); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Save changes to a sitemap item |
642
|
|
|
* |
643
|
|
|
* @param $slug |
644
|
|
|
* @param $postValues |
645
|
|
|
* |
646
|
|
|
* @throws \Exception |
647
|
|
|
*/ |
648
|
|
View Code Duplication |
public function saveSitemapItem($slug, $postValues) |
|
|
|
|
649
|
|
|
{ |
650
|
|
|
$sitemapObject = $this->createSitemapItemFromPostValues($postValues); |
651
|
|
|
|
652
|
|
|
$sitemap = $this->repository->sitemap; |
653
|
|
|
foreach ($sitemap as $key => $sitemapItem) { |
654
|
|
|
if ($sitemapItem->slug == $slug) { |
655
|
|
|
$sitemap[$key] = $sitemapObject; |
656
|
|
|
} |
657
|
|
|
} |
658
|
|
|
$this->repository->sitemap = $sitemap; |
659
|
|
|
$this->save(); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Delete a sitemap item by its slug |
664
|
|
|
* |
665
|
|
|
* @param $slug |
666
|
|
|
* |
667
|
|
|
* @throws \Exception |
668
|
|
|
*/ |
669
|
|
View Code Duplication |
public function deleteSitemapItemBySlug($slug) |
|
|
|
|
670
|
|
|
{ |
671
|
|
|
$sitemap = $this->repository->sitemap; |
672
|
|
|
foreach ($sitemap as $key => $sitemapItem) { |
673
|
|
|
if ($sitemapItem->slug == $slug) { |
674
|
|
|
unset($sitemap[$key]); |
675
|
|
|
} |
676
|
|
|
} |
677
|
|
|
$sitemap = array_values($sitemap); |
678
|
|
|
$this->repository->sitemap = $sitemap; |
679
|
|
|
$this->save(); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* Create a sitemap item from post values |
684
|
|
|
* |
685
|
|
|
* @param $postValues |
686
|
|
|
* |
687
|
|
|
* @return \stdClass |
688
|
|
|
* @throws \Exception |
689
|
|
|
*/ |
690
|
|
|
private function createSitemapItemFromPostValues($postValues) |
691
|
|
|
{ |
692
|
|
|
if (isset($postValues['title'], $postValues['url'], $postValues['component'], $postValues['template'])) { |
693
|
|
|
$sitemapObject = new \stdClass(); |
694
|
|
|
$sitemapObject->title = $postValues['title']; |
695
|
|
|
$sitemapObject->slug = slugify($postValues['title']); |
696
|
|
|
$sitemapObject->url = $postValues['url']; |
697
|
|
|
$sitemapObject->component = $postValues['component']; |
698
|
|
|
$sitemapObject->template = $postValues['template']; |
699
|
|
|
$sitemapObject->regex = isset($postValues['regex']); |
700
|
|
|
$sitemapObject->parameters = new \stdClass(); |
701
|
|
View Code Duplication |
if (isset($postValues['parameterNames'], $postValues['parameterValues'])) { |
|
|
|
|
702
|
|
|
foreach ($postValues['parameterNames'] as $key => $value) { |
703
|
|
|
$sitemapObject->parameters->$value = $postValues['parameterValues'][$key]; |
704
|
|
|
} |
705
|
|
|
} |
706
|
|
|
return $sitemapObject; |
707
|
|
|
} else { |
708
|
|
|
throw new \Exception('Trying to create sitemap item with invalid data.'); |
709
|
|
|
} |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
/** |
713
|
|
|
* Save changes to a sitemap item |
714
|
|
|
* |
715
|
|
|
* @param $postValues |
716
|
|
|
* |
717
|
|
|
* @throws \Exception |
718
|
|
|
*/ |
719
|
|
|
public function saveSitemap($postValues) |
720
|
|
|
{ |
721
|
|
|
if (isset($postValues['sitemapitem']) && is_array($postValues['sitemapitem'])) { |
722
|
|
|
$sitemap = array(); |
723
|
|
|
foreach ($postValues['sitemapitem'] as $sitemapItem) { |
724
|
|
|
$sitemapItemObject = json_decode($sitemapItem); |
725
|
|
|
if (isset($sitemapItemObject->object)) { |
726
|
|
|
unset($sitemapItemObject->object); |
727
|
|
|
} |
728
|
|
|
$sitemap[] = $sitemapItemObject; |
729
|
|
|
} |
730
|
|
|
$this->repository->sitemap = $sitemap; |
731
|
|
|
$this->save(); |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Get a sitemap item by its slug |
737
|
|
|
* |
738
|
|
|
* @param $slug |
739
|
|
|
* |
740
|
|
|
* @return mixed |
741
|
|
|
*/ |
742
|
|
|
public function getSitemapItemBySlug($slug) |
743
|
|
|
{ |
744
|
|
|
$sitemap = $this->repository->sitemap; |
745
|
|
|
foreach ($sitemap as $sitemapItem) { |
746
|
|
|
if ($sitemapItem->slug == $slug) { |
747
|
|
|
return $sitemapItem; |
748
|
|
|
} |
749
|
|
|
} |
750
|
|
|
return null; |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
/* |
754
|
|
|
* |
755
|
|
|
* Images |
756
|
|
|
* |
757
|
|
|
*/ |
758
|
|
|
/** |
759
|
|
|
* Get all images |
760
|
|
|
* |
761
|
|
|
* @return array |
762
|
|
|
*/ |
763
|
|
|
public function getImages() |
764
|
|
|
{ |
765
|
|
|
return $this->repository->images; |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
public function addImage($postValues) |
769
|
|
|
{ |
770
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/images/'); |
771
|
|
|
|
772
|
|
|
$filename = $this->validateFilename($postValues['name'], $destinationPath); |
773
|
|
|
$destination = $destinationPath . '/' . $filename; |
774
|
|
|
|
775
|
|
|
if ($postValues['error'] != '0') { |
776
|
|
|
throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
if (move_uploaded_file($postValues['tmp_name'], $destination)) { |
780
|
|
|
$imageResizer = new ImageResizer($this->getImageSet()); |
781
|
|
|
$fileNames = $imageResizer->applyImageSetToImage($destination); |
782
|
|
|
$fileNames['original'] = $filename; |
783
|
|
|
$imageObject = new \stdClass(); |
784
|
|
|
$imageObject->file = $filename; |
785
|
|
|
$imageObject->type = $postValues['type']; |
786
|
|
|
$imageObject->size = $postValues['size']; |
787
|
|
|
$imageObject->set = $fileNames; |
788
|
|
|
|
789
|
|
|
$this->repository->images[] = $imageObject; |
790
|
|
|
$this->save(); |
791
|
|
|
} else { |
792
|
|
|
throw new \Exception('Error moving uploaded file'); |
793
|
|
|
} |
794
|
|
|
} |
795
|
|
|
|
796
|
|
View Code Duplication |
public function deleteImageByName($filename) |
|
|
|
|
797
|
|
|
{ |
798
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/images/'); |
799
|
|
|
|
800
|
|
|
$images = $this->getImages(); |
801
|
|
|
|
802
|
|
|
foreach ($images as $key => $image) { |
803
|
|
|
if ($image->file == $filename) { |
804
|
|
|
foreach ($image->set as $imageSetFilename) { |
805
|
|
|
$destination = $destinationPath . '/' . $imageSetFilename; |
806
|
|
|
if (file_exists($destination)) { |
807
|
|
|
unlink($destination); |
808
|
|
|
} else { |
809
|
|
|
dump($destination); |
810
|
|
|
} |
811
|
|
|
} |
812
|
|
|
unset($images[$key]); |
813
|
|
|
} |
814
|
|
|
} |
815
|
|
|
|
816
|
|
|
$this->repository->images = $images; |
817
|
|
|
$this->save(); |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* @param $filename |
822
|
|
|
* @return null |
823
|
|
|
*/ |
824
|
|
|
public function getImageByName($filename) |
825
|
|
|
{ |
826
|
|
|
$images = $this->getImages(); |
827
|
|
|
foreach ($images as $image) { |
828
|
|
|
if ($image->file == $filename) { |
829
|
|
|
return $image; |
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
return null; |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
/* |
836
|
|
|
* |
837
|
|
|
* Files |
838
|
|
|
* |
839
|
|
|
*/ |
840
|
|
|
/** |
841
|
|
|
* Get all files |
842
|
|
|
* |
843
|
|
|
* @return array |
844
|
|
|
*/ |
845
|
|
|
public function getFiles() |
846
|
|
|
{ |
847
|
|
|
$files = $this->repository->files; |
848
|
|
|
usort($files, array($this, 'compareFiles')); |
849
|
|
|
return $files; |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
private function compareFiles($a, $b) |
853
|
|
|
{ |
854
|
|
|
return strcmp($a->file, $b->file); |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
public function addFile($postValues) |
858
|
|
|
{ |
859
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/files/'); |
860
|
|
|
|
861
|
|
|
$filename = $this->validateFilename($postValues['name'], $destinationPath); |
862
|
|
|
$destination = $destinationPath . '/' . $filename; |
863
|
|
|
|
864
|
|
|
if ($postValues['error'] != '0') { |
865
|
|
|
throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
if (move_uploaded_file($postValues['tmp_name'], $destination)) { |
869
|
|
|
$file = new \stdClass(); |
870
|
|
|
$file->file = $filename; |
871
|
|
|
$file->type = $postValues['type']; |
872
|
|
|
$file->size = $postValues['size']; |
873
|
|
|
|
874
|
|
|
$this->repository->files[] = $file; |
875
|
|
|
$this->save(); |
876
|
|
|
} else { |
877
|
|
|
throw new \Exception('Error moving uploaded file'); |
878
|
|
|
} |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
private function validateFilename($filename, $path) |
882
|
|
|
{ |
883
|
|
|
$fileParts = explode('.', $filename); |
884
|
|
View Code Duplication |
if (count($fileParts) > 1) { |
|
|
|
|
885
|
|
|
$extension = end($fileParts); |
886
|
|
|
array_pop($fileParts); |
887
|
|
|
$fileNameWithoutExtension = implode('-', $fileParts); |
888
|
|
|
$fileNameWithoutExtension = slugify($fileNameWithoutExtension); |
889
|
|
|
$filename = $fileNameWithoutExtension . '.' . $extension; |
890
|
|
|
} else { |
891
|
|
|
$filename = slugify($filename); |
892
|
|
|
} |
893
|
|
|
|
894
|
|
View Code Duplication |
if (file_exists($path . '/' . $filename)) { |
|
|
|
|
895
|
|
|
$fileParts = explode('.', $filename); |
896
|
|
|
if (count($fileParts) > 1) { |
897
|
|
|
$extension = end($fileParts); |
898
|
|
|
array_pop($fileParts); |
899
|
|
|
$fileNameWithoutExtension = implode('-', $fileParts); |
900
|
|
|
$fileNameWithoutExtension .= '-copy'; |
901
|
|
|
$filename = $fileNameWithoutExtension . '.' . $extension; |
902
|
|
|
} else { |
903
|
|
|
$filename .= '-copy'; |
904
|
|
|
} |
905
|
|
|
return $this->validateFilename($filename,$path); |
906
|
|
|
} |
907
|
|
|
return $filename; |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
/** |
911
|
|
|
* @param $filename |
912
|
|
|
* @return null |
913
|
|
|
*/ |
914
|
|
|
public function getFileByName($filename) |
915
|
|
|
{ |
916
|
|
|
$files = $this->getFiles(); |
917
|
|
|
foreach ($files as $file) { |
918
|
|
|
if ($filename == $file->file) { |
919
|
|
|
return $file; |
920
|
|
|
} |
921
|
|
|
} |
922
|
|
|
return null; |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
/** |
926
|
|
|
* @param $filename |
927
|
|
|
* @throws \Exception |
928
|
|
|
*/ |
929
|
|
View Code Duplication |
public function deleteFileByName($filename) |
|
|
|
|
930
|
|
|
{ |
931
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/files/'); |
932
|
|
|
$destination = $destinationPath . '/' . $filename; |
933
|
|
|
|
934
|
|
|
if (file_exists($destination)) { |
935
|
|
|
$files = $this->getFiles(); |
936
|
|
|
foreach ($files as $key => $file) { |
937
|
|
|
if ($file->file == $filename) { |
938
|
|
|
unlink($destination); |
939
|
|
|
unset($files[$key]); |
940
|
|
|
} |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
$files = array_values($files); |
944
|
|
|
$this->repository->files = $files; |
945
|
|
|
$this->save(); |
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
/* |
950
|
|
|
* |
951
|
|
|
* Configuration |
952
|
|
|
* |
953
|
|
|
*/ |
954
|
|
|
/** |
955
|
|
|
* @return array |
956
|
|
|
*/ |
957
|
|
|
public function getDocumentTypes() |
958
|
|
|
{ |
959
|
|
|
return $this->repository->documentTypes; |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
/** |
963
|
|
|
* Add a document type from post values |
964
|
|
|
* |
965
|
|
|
* @param $postValues |
966
|
|
|
* |
967
|
|
|
* @throws \Exception |
968
|
|
|
*/ |
969
|
|
|
public function addDocumentType($postValues) |
970
|
|
|
{ |
971
|
|
|
$documentTypeObject = $this->createDocumentTypeFromPostValues($postValues); |
972
|
|
|
|
973
|
|
|
$this->repository->documentTypes[] = $documentTypeObject; |
974
|
|
|
$this->save(); |
975
|
|
|
} |
976
|
|
|
|
977
|
|
|
/** |
978
|
|
|
* Create a document type from post values |
979
|
|
|
* |
980
|
|
|
* @param $postValues |
981
|
|
|
* |
982
|
|
|
* @return \stdClass |
983
|
|
|
* @throws \Exception |
984
|
|
|
*/ |
985
|
|
|
public function createDocumentTypeFromPostValues($postValues) |
986
|
|
|
{ |
987
|
|
|
if (isset($postValues['title'])) { |
988
|
|
|
$documentTypeObject = new \stdClass(); |
989
|
|
|
$documentTypeObject->title = $postValues['title']; |
990
|
|
|
$documentTypeObject->slug = slugify($postValues['title']); |
991
|
|
|
$documentTypeObject->fields = array(); |
992
|
|
|
$documentTypeObject->bricks = array(); |
993
|
|
|
$documentTypeObject->dynamicBricks = isset($postValues['dynamicBricks']) ? $postValues['dynamicBricks'] : array(); |
994
|
|
View Code Duplication |
if (isset($postValues['fieldTitles'], $postValues['fieldTypes'], $postValues['fieldRequired'], $postValues['fieldMultiple'])) { |
|
|
|
|
995
|
|
|
foreach ($postValues['fieldTitles'] as $key => $value) { |
996
|
|
|
$fieldObject = new \stdClass(); |
997
|
|
|
$fieldObject->title = $value; |
998
|
|
|
$fieldObject->slug = slugify($value); |
999
|
|
|
$fieldObject->type = $postValues['fieldTypes'][$key]; |
1000
|
|
|
$fieldObject->required = ($postValues['fieldRequired'][$key] === 'true'); |
1001
|
|
|
$fieldObject->multiple = ($postValues['fieldMultiple'][$key] === 'true'); |
1002
|
|
|
|
1003
|
|
|
$documentTypeObject->fields[] = $fieldObject; |
1004
|
|
|
} |
1005
|
|
|
} |
1006
|
|
|
if (isset($postValues['brickTitles'], $postValues['brickBricks'])) { |
1007
|
|
|
foreach ($postValues['brickTitles'] as $key => $value) { |
1008
|
|
|
$brickObject = new \stdClass(); |
1009
|
|
|
$brickObject->title = $value; |
1010
|
|
|
$brickObject->slug = slugify($value); |
1011
|
|
|
$brickObject->brickSlug = $postValues['brickBricks'][$key]; |
1012
|
|
|
$brickObject->multiple = ($postValues['brickMultiples'][$key] === 'true'); |
1013
|
|
|
|
1014
|
|
|
$documentTypeObject->bricks[] = $brickObject; |
1015
|
|
|
} |
1016
|
|
|
} |
1017
|
|
|
return $documentTypeObject; |
1018
|
|
|
} else { |
1019
|
|
|
throw new \Exception('Trying to create document type with invalid data.'); |
1020
|
|
|
} |
1021
|
|
|
} |
1022
|
|
|
|
1023
|
|
|
/** |
1024
|
|
|
* Delete document type |
1025
|
|
|
* |
1026
|
|
|
* @param $slug |
1027
|
|
|
* |
1028
|
|
|
* @throws \Exception |
1029
|
|
|
*/ |
1030
|
|
View Code Duplication |
public function deleteDocumentTypeBySlug($slug) |
|
|
|
|
1031
|
|
|
{ |
1032
|
|
|
$documentTypes = $this->repository->documentTypes; |
1033
|
|
|
foreach ($documentTypes as $key => $documentTypeObject) { |
1034
|
|
|
if ($documentTypeObject->slug == $slug) { |
1035
|
|
|
unset($documentTypes[$key]); |
1036
|
|
|
} |
1037
|
|
|
} |
1038
|
|
|
$documentTypes = array_values($documentTypes); |
1039
|
|
|
$this->repository->documentTypes = $documentTypes; |
1040
|
|
|
$this->save(); |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
|
|
/** |
1044
|
|
|
* Get document type by its slug |
1045
|
|
|
* |
1046
|
|
|
* @param $slug |
1047
|
|
|
* @param bool $getBricks |
1048
|
|
|
* |
1049
|
|
|
* @return mixed |
1050
|
|
|
*/ |
1051
|
|
|
public function getDocumentTypeBySlug($slug, $getBricks = false) |
1052
|
|
|
{ |
1053
|
|
|
$documentTypes = $this->repository->documentTypes; |
1054
|
|
|
foreach ($documentTypes as $documentType) { |
1055
|
|
|
if ($documentType->slug == $slug) { |
1056
|
|
|
if ($getBricks === true) { |
1057
|
|
|
foreach ($documentType->bricks as $key => $brick) { |
1058
|
|
|
$brickStructure = $this->getBrickBySlug($brick->brickSlug); |
1059
|
|
|
$documentType->bricks[$key]->structure = $brickStructure; |
1060
|
|
|
} |
1061
|
|
|
foreach ($documentType->dynamicBricks as $key => $brickSlug) { |
1062
|
|
|
$brickStructure = $this->getBrickBySlug($brickSlug); |
1063
|
|
|
$documentType->dynamicBricks[$key] = $brickStructure; |
1064
|
|
|
} |
1065
|
|
|
} |
1066
|
|
|
return $documentType; |
1067
|
|
|
} |
1068
|
|
|
} |
1069
|
|
|
return null; |
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
/** |
1073
|
|
|
* Save changes to a document type |
1074
|
|
|
* |
1075
|
|
|
* @param $slug |
1076
|
|
|
* @param $postValues |
1077
|
|
|
* |
1078
|
|
|
* @throws \Exception |
1079
|
|
|
*/ |
1080
|
|
View Code Duplication |
public function saveDocumentType($slug, $postValues) |
|
|
|
|
1081
|
|
|
{ |
1082
|
|
|
$documentTypeObject = $this->createDocumentTypeFromPostValues($postValues); |
1083
|
|
|
|
1084
|
|
|
$documentTypes = $this->repository->documentTypes; |
1085
|
|
|
foreach ($documentTypes as $key => $documentType) { |
1086
|
|
|
if ($documentType->slug == $slug) { |
1087
|
|
|
$documentTypes[$key] = $documentTypeObject; |
1088
|
|
|
} |
1089
|
|
|
} |
1090
|
|
|
$this->repository->documentTypes = $documentTypes; |
1091
|
|
|
$this->save(); |
1092
|
|
|
} |
1093
|
|
|
|
1094
|
|
|
/* |
1095
|
|
|
* |
1096
|
|
|
* Bricks |
1097
|
|
|
* |
1098
|
|
|
*/ |
1099
|
|
|
/** |
1100
|
|
|
* @return array |
1101
|
|
|
*/ |
1102
|
|
|
public function getBricks() |
1103
|
|
|
{ |
1104
|
|
|
return $this->repository->bricks; |
1105
|
|
|
} |
1106
|
|
|
|
1107
|
|
|
/** |
1108
|
|
|
* Add a brick |
1109
|
|
|
* |
1110
|
|
|
* @param $postValues |
1111
|
|
|
* |
1112
|
|
|
* @throws \Exception |
1113
|
|
|
*/ |
1114
|
|
|
public function addBrick($postValues) |
1115
|
|
|
{ |
1116
|
|
|
$brickObject = $this->createBrickFromPostValues($postValues); |
1117
|
|
|
|
1118
|
|
|
$this->repository->bricks[] = $brickObject; |
1119
|
|
|
$this->save(); |
1120
|
|
|
} |
1121
|
|
|
|
1122
|
|
|
/** |
1123
|
|
|
* Create a brick from post values |
1124
|
|
|
* |
1125
|
|
|
* @param $postValues |
1126
|
|
|
* |
1127
|
|
|
* @return \stdClass |
1128
|
|
|
* @throws \Exception |
1129
|
|
|
*/ |
1130
|
|
|
public function createBrickFromPostValues($postValues) |
1131
|
|
|
{ |
1132
|
|
|
if (isset($postValues['title'])) { |
1133
|
|
|
$brickObject = new \stdClass(); |
1134
|
|
|
$brickObject->title = $postValues['title']; |
1135
|
|
|
$brickObject->slug = slugify($postValues['title']); |
1136
|
|
|
$brickObject->fields = array(); |
1137
|
|
View Code Duplication |
if (isset($postValues['fieldTitles'], $postValues['fieldTypes'], $postValues['fieldRequired'], $postValues['fieldMultiple'])) { |
|
|
|
|
1138
|
|
|
foreach ($postValues['fieldTitles'] as $key => $value) { |
1139
|
|
|
$fieldObject = new \stdClass(); |
1140
|
|
|
$fieldObject->title = $value; |
1141
|
|
|
$fieldObject->slug = slugify($value); |
1142
|
|
|
$fieldObject->type = $postValues['fieldTypes'][$key]; |
1143
|
|
|
$fieldObject->required = ($postValues['fieldRequired'][$key] === 'true'); |
1144
|
|
|
$fieldObject->multiple = ($postValues['fieldMultiple'][$key] === 'true'); |
1145
|
|
|
|
1146
|
|
|
$brickObject->fields[] = $fieldObject; |
1147
|
|
|
} |
1148
|
|
|
} |
1149
|
|
|
return $brickObject; |
1150
|
|
|
} else { |
1151
|
|
|
throw new \Exception('Trying to create document type with invalid data.'); |
1152
|
|
|
} |
1153
|
|
|
} |
1154
|
|
|
|
1155
|
|
|
/** |
1156
|
|
|
* Get a brick by its slug |
1157
|
|
|
* |
1158
|
|
|
* @param $slug |
1159
|
|
|
* |
1160
|
|
|
* @return \stdClass |
1161
|
|
|
*/ |
1162
|
|
|
public function getBrickBySlug($slug) |
1163
|
|
|
{ |
1164
|
|
|
$bricks = $this->repository->bricks; |
1165
|
|
|
foreach ($bricks as $brick) { |
1166
|
|
|
if ($brick->slug == $slug) { |
1167
|
|
|
return $brick; |
1168
|
|
|
} |
1169
|
|
|
} |
1170
|
|
|
return null; |
1171
|
|
|
} |
1172
|
|
|
|
1173
|
|
|
/** |
1174
|
|
|
* Save changes to a brick |
1175
|
|
|
* |
1176
|
|
|
* @param $slug |
1177
|
|
|
* @param $postValues |
1178
|
|
|
* |
1179
|
|
|
* @throws \Exception |
1180
|
|
|
*/ |
1181
|
|
View Code Duplication |
public function saveBrick($slug, $postValues) |
|
|
|
|
1182
|
|
|
{ |
1183
|
|
|
$brickObject = $this->createBrickFromPostValues($postValues); |
1184
|
|
|
|
1185
|
|
|
$bricks = $this->repository->bricks; |
1186
|
|
|
foreach ($bricks as $key => $brick) { |
1187
|
|
|
if ($brick->slug == $slug) { |
1188
|
|
|
$bricks[$key] = $brickObject; |
1189
|
|
|
} |
1190
|
|
|
} |
1191
|
|
|
$this->repository->bricks = $bricks; |
1192
|
|
|
$this->save(); |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
|
|
/** |
1196
|
|
|
* Delete a brick by its slug |
1197
|
|
|
* |
1198
|
|
|
* @param $slug |
1199
|
|
|
* |
1200
|
|
|
* @throws \Exception |
1201
|
|
|
*/ |
1202
|
|
View Code Duplication |
public function deleteBrickBySlug($slug) |
|
|
|
|
1203
|
|
|
{ |
1204
|
|
|
$bricks = $this->repository->bricks; |
1205
|
|
|
foreach ($bricks as $key => $brickObject) { |
1206
|
|
|
if ($brickObject->slug == $slug) { |
1207
|
|
|
unset($bricks[$key]); |
1208
|
|
|
} |
1209
|
|
|
} |
1210
|
|
|
|
1211
|
|
|
$bricks = array_values($bricks); |
1212
|
|
|
$this->repository->bricks = $bricks; |
1213
|
|
|
$this->save(); |
1214
|
|
|
} |
1215
|
|
|
|
1216
|
|
|
/* |
1217
|
|
|
* |
1218
|
|
|
* Misc |
1219
|
|
|
* |
1220
|
|
|
*/ |
1221
|
|
|
/** |
1222
|
|
|
* Save changes made to the repository |
1223
|
|
|
* in the storagepath |
1224
|
|
|
* |
1225
|
|
|
* @throws \Exception |
1226
|
|
|
*/ |
1227
|
|
|
private function save() { |
1228
|
|
|
$storagePath = __DIR__ . $this->storagePath; |
1229
|
|
|
if (realpath($storagePath) !== false) { |
1230
|
|
|
$json = $this->getEncodedRepository(); |
1231
|
|
|
copy($storagePath, $storagePath . '.bak'); |
1232
|
|
|
file_put_contents($storagePath, $json); |
1233
|
|
|
} else { |
1234
|
|
|
throw new \Exception('Couldnt find storagePath ' . $storagePath); |
1235
|
|
|
} |
1236
|
|
|
} |
1237
|
|
|
|
1238
|
|
|
/* |
1239
|
|
|
* |
1240
|
|
|
* Image Set |
1241
|
|
|
* |
1242
|
|
|
*/ |
1243
|
|
|
|
1244
|
|
|
/** |
1245
|
|
|
* Get the image set |
1246
|
|
|
* |
1247
|
|
|
* @return array |
1248
|
|
|
*/ |
1249
|
|
|
public function getImageSet() |
1250
|
|
|
{ |
1251
|
|
|
return $this->repository->imageSet; |
1252
|
|
|
} |
1253
|
|
|
|
1254
|
|
|
/** |
1255
|
|
|
* Get Image by slug |
1256
|
|
|
* |
1257
|
|
|
* @param $slug |
1258
|
|
|
* |
1259
|
|
|
* @return \stdClass |
1260
|
|
|
*/ |
1261
|
|
|
public function getImageSetBySlug($slug) |
1262
|
|
|
{ |
1263
|
|
|
$imageSet = $this->getImageSet(); |
1264
|
|
|
foreach ($imageSet as $set) { |
1265
|
|
|
if ($set->slug == $slug) { |
1266
|
|
|
return $set; |
1267
|
|
|
} |
1268
|
|
|
} |
1269
|
|
|
return null; |
1270
|
|
|
} |
1271
|
|
|
|
1272
|
|
|
/** |
1273
|
|
|
* Save Image Set by it's slug |
1274
|
|
|
* |
1275
|
|
|
* @param $slug |
1276
|
|
|
* @param $postValues |
1277
|
|
|
* |
1278
|
|
|
* @throws \Exception |
1279
|
|
|
*/ |
1280
|
|
|
public function saveImageSet($slug, $postValues) |
1281
|
|
|
{ |
1282
|
|
|
$imageSetObject = $this->createImageSetFromPostValues($postValues); |
1283
|
|
|
|
1284
|
|
|
$imageSet = $this->repository->imageSet; |
1285
|
|
|
foreach ($imageSet as $key => $set) { |
1286
|
|
|
if ($set->slug == $slug) { |
1287
|
|
|
$imageSet[$key] = $imageSetObject; |
1288
|
|
|
} |
1289
|
|
|
} |
1290
|
|
|
$this->repository->imageSet = $imageSet; |
1291
|
|
|
$this->save(); |
1292
|
|
|
} |
1293
|
|
|
|
1294
|
|
|
/** |
1295
|
|
|
* Ceate image set from post values |
1296
|
|
|
* |
1297
|
|
|
* @param $postValues |
1298
|
|
|
* |
1299
|
|
|
* @return \stdClass |
1300
|
|
|
* @throws \Exception |
1301
|
|
|
*/ |
1302
|
|
|
private function createImageSetFromPostValues($postValues) |
1303
|
|
|
{ |
1304
|
|
|
if (isset($postValues['title'], $postValues['width'], $postValues['height'], $postValues['method'])) { |
1305
|
|
|
$imageSetObject = new \stdClass(); |
1306
|
|
|
|
1307
|
|
|
$imageSetObject->title = $postValues['title']; |
1308
|
|
|
$imageSetObject->slug = slugify($postValues['title']); |
1309
|
|
|
$imageSetObject->width = $postValues['width']; |
1310
|
|
|
$imageSetObject->height = $postValues['height']; |
1311
|
|
|
$imageSetObject->method = $postValues['method']; |
1312
|
|
|
|
1313
|
|
|
return $imageSetObject; |
1314
|
|
|
} else { |
1315
|
|
|
throw new \Exception('Trying to create image set with invalid data.'); |
1316
|
|
|
} |
1317
|
|
|
} |
1318
|
|
|
|
1319
|
|
|
/** |
1320
|
|
|
* Add image set |
1321
|
|
|
* |
1322
|
|
|
* @param $postValues |
1323
|
|
|
* |
1324
|
|
|
* @throws \Exception |
1325
|
|
|
*/ |
1326
|
|
|
public function addImageSet($postValues) |
1327
|
|
|
{ |
1328
|
|
|
$imageSetObject = $this->createImageSetFromPostValues($postValues); |
1329
|
|
|
|
1330
|
|
|
$this->repository->imageSet[] = $imageSetObject; |
1331
|
|
|
|
1332
|
|
|
$this->save(); |
1333
|
|
|
} |
1334
|
|
|
|
1335
|
|
|
/** |
1336
|
|
|
* Delete Image Set by its slug |
1337
|
|
|
* |
1338
|
|
|
* @param $slug |
1339
|
|
|
* |
1340
|
|
|
* @throws \Exception |
1341
|
|
|
*/ |
1342
|
|
|
public function deleteImageSetBySlug($slug) |
1343
|
|
|
{ |
1344
|
|
|
$imageSet = $this->getImageSet(); |
1345
|
|
|
|
1346
|
|
|
foreach ($imageSet as $key => $set) { |
1347
|
|
|
if ($set->slug == $slug) { |
1348
|
|
|
unset($imageSet[$key]); |
1349
|
|
|
} |
1350
|
|
|
} |
1351
|
|
|
$imageSet = array_values($imageSet); |
1352
|
|
|
$this->repository->imageSet = $imageSet; |
1353
|
|
|
$this->save(); |
1354
|
|
|
} |
1355
|
|
|
|
1356
|
|
|
/** |
1357
|
|
|
* Get the image set with the smallest size |
1358
|
|
|
* |
1359
|
|
|
* @return \stdClass |
1360
|
|
|
*/ |
1361
|
|
|
public function getSmallestImageSet() |
1362
|
|
|
{ |
1363
|
|
|
$imageSet = $this->getImageSet(); |
1364
|
|
|
|
1365
|
|
|
$returnSize = PHP_INT_MAX; |
1366
|
|
|
$returnSet = null; |
1367
|
|
|
|
1368
|
|
|
foreach ($imageSet as $set) { |
1369
|
|
|
$size = $set->width * $set->height; |
1370
|
|
|
if ($size < $returnSize) { |
1371
|
|
|
$returnSize = $size; |
1372
|
|
|
$returnSet = $set; |
1373
|
|
|
} |
1374
|
|
|
} |
1375
|
|
|
|
1376
|
|
|
if ($returnSet === null) { |
1377
|
|
|
$returnSet = new \stdClass(); |
1378
|
|
|
$returnSet->slug = 'original'; |
1379
|
|
|
} |
1380
|
|
|
|
1381
|
|
|
return $returnSet; |
1382
|
|
|
} |
1383
|
|
|
|
1384
|
|
|
/** |
1385
|
|
|
* @return array |
1386
|
|
|
*/ |
1387
|
|
|
public function getApplicationComponents() |
1388
|
|
|
{ |
1389
|
|
|
return $this->repository->applicationComponents; |
1390
|
|
|
} |
1391
|
|
|
|
1392
|
|
|
public function addApplicationComponent($postValues) |
1393
|
|
|
{ |
1394
|
|
|
$applicationComponent = $this->createApplicationComponentFromPostValues($postValues); |
1395
|
|
|
|
1396
|
|
|
$this->repository->applicationComponents[] = $applicationComponent; |
1397
|
|
|
$this->save(); |
1398
|
|
|
} |
1399
|
|
|
|
1400
|
|
|
private function createApplicationComponentFromPostValues($postValues) |
1401
|
|
|
{ |
1402
|
|
|
if (isset($postValues['title'], $postValues['component'])) { |
1403
|
|
|
$applicationComponent = new \stdClass(); |
1404
|
|
|
$applicationComponent->title = $postValues['title']; |
1405
|
|
|
$applicationComponent->slug = slugify($postValues['title']); |
1406
|
|
|
$applicationComponent->component = $postValues['component']; |
1407
|
|
|
$applicationComponent->parameters = new \stdClass(); |
1408
|
|
View Code Duplication |
if (isset($postValues['parameterNames'], $postValues['parameterValues'])) { |
|
|
|
|
1409
|
|
|
foreach ($postValues['parameterNames'] as $key => $value) { |
1410
|
|
|
$applicationComponent->parameters->$value = $postValues['parameterValues'][$key]; |
1411
|
|
|
} |
1412
|
|
|
} |
1413
|
|
|
return $applicationComponent; |
1414
|
|
|
} else { |
1415
|
|
|
throw new \Exception('Trying to create application component with invalid data.'); |
1416
|
|
|
} |
1417
|
|
|
} |
1418
|
|
|
|
1419
|
|
|
public function getApplicationComponentBySlug($slug) |
1420
|
|
|
{ |
1421
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
1422
|
|
|
foreach ($applicationComponents as $applicationComponent) { |
1423
|
|
|
if ($applicationComponent->slug == $slug) { |
1424
|
|
|
return $applicationComponent; |
1425
|
|
|
} |
1426
|
|
|
} |
1427
|
|
|
return null; |
1428
|
|
|
} |
1429
|
|
|
|
1430
|
|
View Code Duplication |
public function saveApplicationComponent($slug, $postValues) |
|
|
|
|
1431
|
|
|
{ |
1432
|
|
|
$newApplicationComponent = $this->createApplicationComponentFromPostValues($postValues); |
1433
|
|
|
|
1434
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
1435
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
1436
|
|
|
if ($applicationComponent->slug == $slug) { |
1437
|
|
|
$applicationComponents[$key] = $newApplicationComponent; |
1438
|
|
|
} |
1439
|
|
|
} |
1440
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
1441
|
|
|
$this->save(); |
1442
|
|
|
} |
1443
|
|
|
|
1444
|
|
View Code Duplication |
public function deleteApplicationComponentBySlug($slug) |
|
|
|
|
1445
|
|
|
{ |
1446
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
1447
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
1448
|
|
|
if ($applicationComponent->slug == $slug) { |
1449
|
|
|
unset($applicationComponents[$key]); |
1450
|
|
|
} |
1451
|
|
|
} |
1452
|
|
|
$applicationComponents = array_values($applicationComponents); |
1453
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
1454
|
|
|
$this->save(); |
1455
|
|
|
} |
1456
|
|
|
|
1457
|
|
|
private function getEncodedRepository() |
1458
|
|
|
{ |
1459
|
|
|
$json = json_encode($this->repository); |
1460
|
|
|
if ($json === false) { |
1461
|
|
|
$this->throwJsonException(); |
1462
|
|
|
} |
1463
|
|
|
return $json; |
1464
|
|
|
} |
1465
|
|
|
|
1466
|
|
|
private function throwJsonException() |
1467
|
|
|
{ |
1468
|
|
|
$error = 'JSON Encoding failed'; |
1469
|
|
|
switch (json_last_error()) { |
1470
|
|
|
case JSON_ERROR_NONE: |
1471
|
|
|
$error .= ' - No errors'; |
1472
|
|
|
break; |
1473
|
|
|
case JSON_ERROR_DEPTH: |
1474
|
|
|
$error .= ' - Maximum stack depth exceeded'; |
1475
|
|
|
break; |
1476
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
1477
|
|
|
$error .= ' - Underflow or the modes mismatch'; |
1478
|
|
|
break; |
1479
|
|
|
case JSON_ERROR_CTRL_CHAR: |
1480
|
|
|
$error .= ' - Unexpected control character found'; |
1481
|
|
|
break; |
1482
|
|
|
case JSON_ERROR_SYNTAX: |
1483
|
|
|
$error .= ' - Syntax error, malformed JSON'; |
1484
|
|
|
break; |
1485
|
|
|
case JSON_ERROR_UTF8: |
1486
|
|
|
$error .= ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
1487
|
|
|
break; |
1488
|
|
|
default: |
1489
|
|
|
$error .= ' - Unknown error'; |
1490
|
|
|
break; |
1491
|
|
|
} |
1492
|
|
|
|
1493
|
|
|
throw new \Exception($error); |
1494
|
|
|
} |
1495
|
|
|
} |
1496
|
|
|
} |
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.