|
1
|
|
|
<?php |
|
2
|
|
|
namespace library\storage { |
|
3
|
|
|
|
|
4
|
|
|
use library\images\ImageResizer; |
|
5
|
|
|
use library\storage\factories\ApplicationComponentFactory; |
|
6
|
|
|
use library\storage\factories\BrickFactory; |
|
7
|
|
|
use library\storage\factories\DocumentFolderFactory; |
|
8
|
|
|
use library\storage\factories\DocumentTypeFactory; |
|
9
|
|
|
use library\storage\factories\ImageSetFactory; |
|
10
|
|
|
use library\storage\factories\SitemapItemFactory; |
|
11
|
|
|
use library\storage\factories\UserFactory; |
|
12
|
|
|
use library\storage\storage\SitemapStorage; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class JsonStorage |
|
16
|
|
|
* @package library\storage |
|
17
|
|
|
*/ |
|
18
|
|
|
class Storage |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var SitemapStorage |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $sitemap; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var String |
|
26
|
|
|
*/ |
|
27
|
|
|
private $storageDir; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Repository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $repository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* JsonStorage constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $storageDir |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($storageDir) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->storageDir = $storageDir; |
|
41
|
|
|
$this->config(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve the data from the storagepath |
|
46
|
|
|
* so it can be interacted with |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
private function config() |
|
51
|
|
|
{ |
|
52
|
|
|
$storagePath = __DIR__ . '/../../' . $this->storageDir; |
|
53
|
|
|
if (realpath($storagePath) === false) { |
|
54
|
|
|
initFramework(); |
|
55
|
|
|
if (Repository::create($storagePath)) { |
|
56
|
|
|
$repository = new Repository($storagePath); |
|
57
|
|
|
$repository->init(); |
|
58
|
|
|
$this->repository = $repository; |
|
59
|
|
|
} else { |
|
60
|
|
|
throw new \Exception('Could not create repository directory: ' . $storagePath); |
|
61
|
|
|
} |
|
62
|
|
|
} else { |
|
63
|
|
|
$this->repository = new Repository($storagePath); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get user by username |
|
71
|
|
|
* |
|
72
|
|
|
* @param $username |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function getUserByUsername($username) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$return = array(); |
|
79
|
|
|
|
|
80
|
|
|
$users = $this->repository->users; |
|
81
|
|
|
foreach ($users as $user) { |
|
82
|
|
|
if ($user->username == $username) { |
|
83
|
|
|
$return = $user; |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get user by slug |
|
93
|
|
|
* |
|
94
|
|
|
* @param $slug |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function getUserBySlug($slug) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
$return = array(); |
|
101
|
|
|
|
|
102
|
|
|
$users = $this->repository->users; |
|
103
|
|
|
foreach ($users as $user) { |
|
104
|
|
|
if ($user->slug == $slug) { |
|
105
|
|
|
$return = $user; |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get all users |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getUsers() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->repository->users; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Save user |
|
125
|
|
|
* |
|
126
|
|
|
* @param $slug |
|
127
|
|
|
* @param $postValues |
|
128
|
|
|
* |
|
129
|
|
|
* @throws \Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
public function saveUser($slug, $postValues) |
|
132
|
|
|
{ |
|
133
|
|
|
$userObj = UserFactory::createUserFromPostValues($postValues); |
|
134
|
|
|
if ($userObj->slug != $slug) { |
|
135
|
|
|
// If the username changed, check for duplicates |
|
136
|
|
|
$doesItExist = $this->getUserBySlug($userObj->slug); |
|
137
|
|
|
if (!empty($doesItExist)) { |
|
138
|
|
|
throw new \Exception('Trying to rename user to existing username'); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
$users = $this->getUsers(); |
|
142
|
|
|
foreach ($users as $key => $user) { |
|
143
|
|
|
if ($user->slug == $slug) { |
|
144
|
|
|
$users[$key] = $userObj; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
$this->repository->users = $users; |
|
148
|
|
|
$this->save(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Add user |
|
153
|
|
|
* |
|
154
|
|
|
* @param $postValues |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \Exception |
|
157
|
|
|
*/ |
|
158
|
|
|
public function addUser($postValues) |
|
159
|
|
|
{ |
|
160
|
|
|
$userObj = UserFactory::createUserFromPostValues($postValues); |
|
161
|
|
|
|
|
162
|
|
|
$doesItExist = $this->getUserBySlug($userObj->slug); |
|
163
|
|
|
if (!empty($doesItExist)) { |
|
164
|
|
|
throw new \Exception('Trying to add username that already exists.'); |
|
165
|
|
|
} |
|
166
|
|
|
$users = $this->repository->users; |
|
167
|
|
|
$users[] = $userObj; |
|
168
|
|
|
$this->repository->users = $users; |
|
169
|
|
|
$this->save(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Delete user by slug |
|
174
|
|
|
* |
|
175
|
|
|
* @param $slug |
|
176
|
|
|
* |
|
177
|
|
|
* @throws \Exception |
|
178
|
|
|
*/ |
|
179
|
|
|
public function deleteUserBySlug($slug) |
|
180
|
|
|
{ |
|
181
|
|
|
$userToDelete = $this->getUserBySlug($slug); |
|
182
|
|
|
if (empty($userToDelete)) { |
|
183
|
|
|
throw new \Exception('Trying to delete a user that doesn\'t exist.'); |
|
184
|
|
|
} |
|
185
|
|
|
$users = $this->getUsers(); |
|
186
|
|
|
foreach ($users as $key => $user) { |
|
187
|
|
|
if ($user->slug == $userToDelete->slug) { |
|
188
|
|
|
unset($users[$key]); |
|
189
|
|
|
$this->repository->users = array_values($users); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
$this->save(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/* |
|
196
|
|
|
* |
|
197
|
|
|
* Documents |
|
198
|
|
|
* |
|
199
|
|
|
*/ |
|
200
|
|
|
/** |
|
201
|
|
|
* Get documents |
|
202
|
|
|
* |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getDocuments() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->repository->getDocuments(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function getTotalDocumentCount() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->repository->getTotalDocumentCount(); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param string $slug |
|
217
|
|
|
* |
|
218
|
|
|
* @return mixed |
|
219
|
|
|
* @throws \Exception |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getDocumentBySlug($slug) |
|
222
|
|
|
{ |
|
223
|
|
|
$path = '/' . $slug; |
|
224
|
|
|
|
|
225
|
|
|
return $this->repository->getDocumentByPath($path); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param $postValues |
|
230
|
|
|
*/ |
|
231
|
|
|
public function saveDocument($postValues) |
|
232
|
|
|
{ |
|
233
|
|
|
$oldPath = '/' . $postValues['path']; |
|
234
|
|
|
|
|
235
|
|
|
$container = $this->getDocumentContainerByPath($oldPath); |
|
236
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this); |
|
237
|
|
|
if ($container->path === '/') { |
|
238
|
|
|
$newPath = $container->path . $documentObject->slug; |
|
239
|
|
|
} else { |
|
240
|
|
|
$newPath = $container->path . '/' . $documentObject->slug; |
|
241
|
|
|
} |
|
242
|
|
|
$documentObject->path = $newPath; |
|
243
|
|
|
$this->repository->saveDocument($documentObject); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
View Code Duplication |
public function addDocument($postValues) |
|
|
|
|
|
|
247
|
|
|
{ |
|
248
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this); |
|
249
|
|
|
if ($postValues['path'] === '/') { |
|
250
|
|
|
$documentObject->path = $postValues['path'] . $documentObject->slug; |
|
251
|
|
|
} else { |
|
252
|
|
|
$documentObject->path = $postValues['path'] . '/' . $documentObject->slug; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
$this->repository->saveDocument($documentObject); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function deleteDocumentBySlug($slug) |
|
259
|
|
|
{ |
|
260
|
|
|
$path = '/' . $slug; |
|
261
|
|
|
$this->repository->deleteDocumentByPath($path); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Add new document in given path |
|
266
|
|
|
* |
|
267
|
|
|
* @param array $postValues |
|
268
|
|
|
* |
|
269
|
|
|
* @throws \Exception |
|
270
|
|
|
*/ |
|
271
|
|
View Code Duplication |
public function addDocumentFolder($postValues) |
|
|
|
|
|
|
272
|
|
|
{ |
|
273
|
|
|
$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues); |
|
274
|
|
|
if ($postValues['path'] === '/') { |
|
275
|
|
|
$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug; |
|
276
|
|
|
} else { |
|
277
|
|
|
$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug; |
|
278
|
|
|
} |
|
279
|
|
|
$this->repository->saveDocument($documentFolderObject); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Delete a folder by its compound slug |
|
284
|
|
|
* |
|
285
|
|
|
* @param $slug |
|
286
|
|
|
* |
|
287
|
|
|
* @throws \Exception |
|
288
|
|
|
*/ |
|
289
|
|
|
public function deleteDocumentFolderBySlug($slug) |
|
290
|
|
|
{ |
|
291
|
|
|
$path = '/' . $slug; |
|
292
|
|
|
$this->repository->deleteDocumentByPath($path); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Retrieve a folder by its compound slug |
|
297
|
|
|
* |
|
298
|
|
|
* @param $slug |
|
299
|
|
|
* |
|
300
|
|
|
* @return mixed |
|
301
|
|
|
* @throws \Exception |
|
302
|
|
|
*/ |
|
303
|
|
|
public function getDocumentFolderBySlug($slug) |
|
304
|
|
|
{ |
|
305
|
|
|
$path = '/' . $slug; |
|
306
|
|
|
|
|
307
|
|
|
return $this->repository->getDocumentByPath($path); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Save changes to folder |
|
312
|
|
|
* |
|
313
|
|
|
* @param $postValues |
|
314
|
|
|
* |
|
315
|
|
|
* @throws \Exception |
|
316
|
|
|
*/ |
|
317
|
|
|
public function saveDocumentFolder($postValues) |
|
318
|
|
|
{ |
|
319
|
|
|
$this->addDocumentFolder($postValues); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Convert path to indeces |
|
324
|
|
|
* |
|
325
|
|
|
* @param $path |
|
326
|
|
|
* |
|
327
|
|
|
* @return array |
|
328
|
|
|
* @throws \Exception |
|
329
|
|
|
*/ |
|
330
|
|
|
private function getDocumentContainerByPath($path) |
|
331
|
|
|
{ |
|
332
|
|
|
return $this->repository->getDocumentContainerByPath($path); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @return SitemapStorage |
|
337
|
|
|
*/ |
|
338
|
|
|
public function getSitemap() |
|
339
|
|
|
{ |
|
340
|
|
|
if (!$this->sitemap instanceof SitemapStorage) { |
|
341
|
|
|
$this->sitemap = new SitemapStorage($this->repository); |
|
342
|
|
|
} |
|
343
|
|
|
return $this->sitemap; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/* |
|
347
|
|
|
* |
|
348
|
|
|
* Images |
|
349
|
|
|
* |
|
350
|
|
|
*/ |
|
351
|
|
|
/** |
|
352
|
|
|
* Get all images |
|
353
|
|
|
* |
|
354
|
|
|
* @return array |
|
355
|
|
|
*/ |
|
356
|
|
|
public function getImages() |
|
357
|
|
|
{ |
|
358
|
|
|
return $this->repository->images; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
public function addImage($postValues) |
|
362
|
|
|
{ |
|
363
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/images/'); |
|
364
|
|
|
|
|
365
|
|
|
$filename = $this->validateFilename($postValues['name'], $destinationPath); |
|
366
|
|
|
$destination = $destinationPath . '/' . $filename; |
|
367
|
|
|
|
|
368
|
|
|
if ($postValues['error'] != '0') { |
|
369
|
|
|
throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
if (move_uploaded_file($postValues['tmp_name'], $destination)) { |
|
373
|
|
|
$imageResizer = new ImageResizer($this->getImageSet()); |
|
374
|
|
|
$fileNames = $imageResizer->applyImageSetToImage($destination); |
|
375
|
|
|
$fileNames['original'] = $filename; |
|
376
|
|
|
$imageObject = new \stdClass(); |
|
377
|
|
|
$imageObject->file = $filename; |
|
378
|
|
|
$imageObject->type = $postValues['type']; |
|
379
|
|
|
$imageObject->size = $postValues['size']; |
|
380
|
|
|
$imageObject->set = $fileNames; |
|
381
|
|
|
|
|
382
|
|
|
$images = $this->repository->images; |
|
383
|
|
|
$images[] = $imageObject; |
|
384
|
|
|
$this->repository->images = $images; |
|
385
|
|
|
|
|
386
|
|
|
$this->save(); |
|
387
|
|
|
} else { |
|
388
|
|
|
throw new \Exception('Error moving uploaded file'); |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
View Code Duplication |
public function deleteImageByName($filename) |
|
|
|
|
|
|
393
|
|
|
{ |
|
394
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/images/'); |
|
395
|
|
|
|
|
396
|
|
|
$images = $this->getImages(); |
|
397
|
|
|
|
|
398
|
|
|
foreach ($images as $key => $image) { |
|
399
|
|
|
if ($image->file == $filename) { |
|
400
|
|
|
foreach ($image->set as $imageSetFilename) { |
|
401
|
|
|
$destination = $destinationPath . '/' . $imageSetFilename; |
|
402
|
|
|
if (file_exists($destination)) { |
|
403
|
|
|
unlink($destination); |
|
404
|
|
|
} else { |
|
405
|
|
|
dump($destination); |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
unset($images[$key]); |
|
409
|
|
|
} |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
$this->repository->images = $images; |
|
413
|
|
|
$this->save(); |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* @param $filename |
|
418
|
|
|
* |
|
419
|
|
|
* @return null |
|
420
|
|
|
*/ |
|
421
|
|
|
public function getImageByName($filename) |
|
422
|
|
|
{ |
|
423
|
|
|
$images = $this->getImages(); |
|
424
|
|
|
foreach ($images as $image) { |
|
425
|
|
|
if ($image->file == $filename) { |
|
426
|
|
|
return $image; |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
return null; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/* |
|
434
|
|
|
* |
|
435
|
|
|
* Files |
|
436
|
|
|
* |
|
437
|
|
|
*/ |
|
438
|
|
|
/** |
|
439
|
|
|
* Get all files |
|
440
|
|
|
* |
|
441
|
|
|
* @return array |
|
442
|
|
|
*/ |
|
443
|
|
|
public function getFiles() |
|
444
|
|
|
{ |
|
445
|
|
|
$files = $this->repository->files; |
|
446
|
|
|
usort($files, array($this, 'compareFiles')); |
|
447
|
|
|
|
|
448
|
|
|
return $files; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** |
|
452
|
|
|
* @return string |
|
453
|
|
|
*/ |
|
454
|
|
|
public function getStorageDir() |
|
455
|
|
|
{ |
|
456
|
|
|
return $this->storageDir; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
public function getContentDbHandle() |
|
460
|
|
|
{ |
|
461
|
|
|
return $this->repository->getContentDbHandle(); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
private function compareFiles($a, $b) |
|
465
|
|
|
{ |
|
466
|
|
|
return strcmp($a->file, $b->file); |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
public function addFile($postValues) |
|
470
|
|
|
{ |
|
471
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/files/'); |
|
472
|
|
|
|
|
473
|
|
|
$filename = $this->validateFilename($postValues['name'], $destinationPath); |
|
474
|
|
|
$destination = $destinationPath . '/' . $filename; |
|
475
|
|
|
|
|
476
|
|
|
if ($postValues['error'] != '0') { |
|
477
|
|
|
throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
if (move_uploaded_file($postValues['tmp_name'], $destination)) { |
|
481
|
|
|
$file = new \stdClass(); |
|
482
|
|
|
$file->file = $filename; |
|
483
|
|
|
$file->type = $postValues['type']; |
|
484
|
|
|
$file->size = $postValues['size']; |
|
485
|
|
|
|
|
486
|
|
|
$files = $this->repository->files; |
|
487
|
|
|
$files[] = $file; |
|
488
|
|
|
$this->repository->files = $files; |
|
489
|
|
|
$this->save(); |
|
490
|
|
|
} else { |
|
491
|
|
|
throw new \Exception('Error moving uploaded file'); |
|
492
|
|
|
} |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
private function validateFilename($filename, $path) |
|
496
|
|
|
{ |
|
497
|
|
|
$fileParts = explode('.', $filename); |
|
498
|
|
View Code Duplication |
if (count($fileParts) > 1) { |
|
|
|
|
|
|
499
|
|
|
$extension = end($fileParts); |
|
500
|
|
|
array_pop($fileParts); |
|
501
|
|
|
$fileNameWithoutExtension = implode('-', $fileParts); |
|
502
|
|
|
$fileNameWithoutExtension = slugify($fileNameWithoutExtension); |
|
503
|
|
|
$filename = $fileNameWithoutExtension . '.' . $extension; |
|
504
|
|
|
} else { |
|
505
|
|
|
$filename = slugify($filename); |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
View Code Duplication |
if (file_exists($path . '/' . $filename)) { |
|
|
|
|
|
|
509
|
|
|
$fileParts = explode('.', $filename); |
|
510
|
|
|
if (count($fileParts) > 1) { |
|
511
|
|
|
$extension = end($fileParts); |
|
512
|
|
|
array_pop($fileParts); |
|
513
|
|
|
$fileNameWithoutExtension = implode('-', $fileParts); |
|
514
|
|
|
$fileNameWithoutExtension .= '-copy'; |
|
515
|
|
|
$filename = $fileNameWithoutExtension . '.' . $extension; |
|
516
|
|
|
} else { |
|
517
|
|
|
$filename .= '-copy'; |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
return $this->validateFilename($filename, $path); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
return $filename; |
|
524
|
|
|
} |
|
525
|
|
|
|
|
526
|
|
|
/** |
|
527
|
|
|
* @param $filename |
|
528
|
|
|
* |
|
529
|
|
|
* @return null |
|
530
|
|
|
*/ |
|
531
|
|
|
public function getFileByName($filename) |
|
532
|
|
|
{ |
|
533
|
|
|
$files = $this->getFiles(); |
|
534
|
|
|
foreach ($files as $file) { |
|
535
|
|
|
if ($filename == $file->file) { |
|
536
|
|
|
return $file; |
|
537
|
|
|
} |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
return null; |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
|
|
/** |
|
544
|
|
|
* @param $filename |
|
545
|
|
|
* |
|
546
|
|
|
* @throws \Exception |
|
547
|
|
|
*/ |
|
548
|
|
View Code Duplication |
public function deleteFileByName($filename) |
|
|
|
|
|
|
549
|
|
|
{ |
|
550
|
|
|
$destinationPath = realpath(__DIR__ . '/../../www/files/'); |
|
551
|
|
|
$destination = $destinationPath . '/' . $filename; |
|
552
|
|
|
|
|
553
|
|
|
if (file_exists($destination)) { |
|
554
|
|
|
$files = $this->getFiles(); |
|
555
|
|
|
foreach ($files as $key => $file) { |
|
556
|
|
|
if ($file->file == $filename) { |
|
557
|
|
|
unlink($destination); |
|
558
|
|
|
unset($files[$key]); |
|
559
|
|
|
} |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
$files = array_values($files); |
|
563
|
|
|
$this->repository->files = $files; |
|
564
|
|
|
$this->save(); |
|
565
|
|
|
} |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/* |
|
569
|
|
|
* |
|
570
|
|
|
* Configuration |
|
571
|
|
|
* |
|
572
|
|
|
*/ |
|
573
|
|
|
/** |
|
574
|
|
|
* @return array |
|
575
|
|
|
*/ |
|
576
|
|
|
public function getDocumentTypes() |
|
577
|
|
|
{ |
|
578
|
|
|
return $this->repository->documentTypes; |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
|
|
/** |
|
582
|
|
|
* Add a document type from post values |
|
583
|
|
|
* |
|
584
|
|
|
* @param $postValues |
|
585
|
|
|
* |
|
586
|
|
|
* @throws \Exception |
|
587
|
|
|
*/ |
|
588
|
|
|
public function addDocumentType($postValues) |
|
589
|
|
|
{ |
|
590
|
|
|
$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues); |
|
591
|
|
|
|
|
592
|
|
|
$documentTypes = $this->repository->documentTypes; |
|
593
|
|
|
$documentTypes[] = $documentTypeObject; |
|
594
|
|
|
$this->repository->documentTypes = $documentTypes; |
|
595
|
|
|
|
|
596
|
|
|
$this->save(); |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/** |
|
600
|
|
|
* Delete document type |
|
601
|
|
|
* |
|
602
|
|
|
* @param $slug |
|
603
|
|
|
* |
|
604
|
|
|
* @throws \Exception |
|
605
|
|
|
*/ |
|
606
|
|
View Code Duplication |
public function deleteDocumentTypeBySlug($slug) |
|
|
|
|
|
|
607
|
|
|
{ |
|
608
|
|
|
$documentTypes = $this->repository->documentTypes; |
|
609
|
|
|
foreach ($documentTypes as $key => $documentTypeObject) { |
|
610
|
|
|
if ($documentTypeObject->slug == $slug) { |
|
611
|
|
|
unset($documentTypes[$key]); |
|
612
|
|
|
} |
|
613
|
|
|
} |
|
614
|
|
|
$documentTypes = array_values($documentTypes); |
|
615
|
|
|
$this->repository->documentTypes = $documentTypes; |
|
616
|
|
|
$this->save(); |
|
617
|
|
|
} |
|
618
|
|
|
|
|
619
|
|
|
/** |
|
620
|
|
|
* Get document type by its slug |
|
621
|
|
|
* |
|
622
|
|
|
* @param $slug |
|
623
|
|
|
* @param bool $getBricks |
|
624
|
|
|
* |
|
625
|
|
|
* @return mixed |
|
626
|
|
|
*/ |
|
627
|
|
|
public function getDocumentTypeBySlug($slug, $getBricks = false) |
|
628
|
|
|
{ |
|
629
|
|
|
$documentTypes = $this->repository->documentTypes; |
|
630
|
|
|
foreach ($documentTypes as $documentType) { |
|
631
|
|
|
if ($documentType->slug == $slug) { |
|
632
|
|
|
if ($getBricks === true) { |
|
633
|
|
|
foreach ($documentType->bricks as $key => $brick) { |
|
634
|
|
|
$brickStructure = $this->getBrickBySlug($brick->brickSlug); |
|
635
|
|
|
$documentType->bricks[$key]->structure = $brickStructure; |
|
636
|
|
|
} |
|
637
|
|
|
foreach ($documentType->dynamicBricks as $key => $brickSlug) { |
|
638
|
|
|
$brickStructure = $this->getBrickBySlug($brickSlug); |
|
639
|
|
|
$documentType->dynamicBricks[$key] = $brickStructure; |
|
640
|
|
|
} |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
return $documentType; |
|
644
|
|
|
} |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
return null; |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
/** |
|
651
|
|
|
* Save changes to a document type |
|
652
|
|
|
* |
|
653
|
|
|
* @param $slug |
|
654
|
|
|
* @param $postValues |
|
655
|
|
|
* |
|
656
|
|
|
* @throws \Exception |
|
657
|
|
|
*/ |
|
658
|
|
View Code Duplication |
public function saveDocumentType($slug, $postValues) |
|
|
|
|
|
|
659
|
|
|
{ |
|
660
|
|
|
$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues); |
|
661
|
|
|
|
|
662
|
|
|
$documentTypes = $this->repository->documentTypes; |
|
663
|
|
|
foreach ($documentTypes as $key => $documentType) { |
|
664
|
|
|
if ($documentType->slug == $slug) { |
|
665
|
|
|
$documentTypes[$key] = $documentTypeObject; |
|
666
|
|
|
} |
|
667
|
|
|
} |
|
668
|
|
|
$this->repository->documentTypes = $documentTypes; |
|
669
|
|
|
$this->save(); |
|
670
|
|
|
} |
|
671
|
|
|
|
|
672
|
|
|
/* |
|
673
|
|
|
* |
|
674
|
|
|
* Bricks |
|
675
|
|
|
* |
|
676
|
|
|
*/ |
|
677
|
|
|
/** |
|
678
|
|
|
* @return array |
|
679
|
|
|
*/ |
|
680
|
|
|
public function getBricks() |
|
681
|
|
|
{ |
|
682
|
|
|
return $this->repository->bricks; |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
/** |
|
686
|
|
|
* Add a brick |
|
687
|
|
|
* |
|
688
|
|
|
* @param $postValues |
|
689
|
|
|
* |
|
690
|
|
|
* @throws \Exception |
|
691
|
|
|
*/ |
|
692
|
|
|
public function addBrick($postValues) |
|
693
|
|
|
{ |
|
694
|
|
|
$brickObject = BrickFactory::createBrickFromPostValues($postValues); |
|
695
|
|
|
|
|
696
|
|
|
$bricks = $this->repository->bricks; |
|
697
|
|
|
$bricks[] = $brickObject; |
|
698
|
|
|
$this->repository->bricks = $bricks; |
|
699
|
|
|
|
|
700
|
|
|
$this->save(); |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
/** |
|
704
|
|
|
* Get a brick by its slug |
|
705
|
|
|
* |
|
706
|
|
|
* @param $slug |
|
707
|
|
|
* |
|
708
|
|
|
* @return \stdClass |
|
709
|
|
|
*/ |
|
710
|
|
|
public function getBrickBySlug($slug) |
|
711
|
|
|
{ |
|
712
|
|
|
$bricks = $this->repository->bricks; |
|
713
|
|
|
foreach ($bricks as $brick) { |
|
714
|
|
|
if ($brick->slug == $slug) { |
|
715
|
|
|
return $brick; |
|
716
|
|
|
} |
|
717
|
|
|
} |
|
718
|
|
|
|
|
719
|
|
|
return null; |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
|
|
/** |
|
723
|
|
|
* Save changes to a brick |
|
724
|
|
|
* |
|
725
|
|
|
* @param $slug |
|
726
|
|
|
* @param $postValues |
|
727
|
|
|
* |
|
728
|
|
|
* @throws \Exception |
|
729
|
|
|
*/ |
|
730
|
|
|
public function saveBrick($slug, $postValues) |
|
731
|
|
|
{ |
|
732
|
|
|
$brickObject = BrickFactory::createBrickFromPostValues($postValues); |
|
733
|
|
|
|
|
734
|
|
|
$bricks = $this->repository->bricks; |
|
735
|
|
|
foreach ($bricks as $key => $brick) { |
|
736
|
|
|
if ($brick->slug == $slug) { |
|
737
|
|
|
$bricks[$key] = $brickObject; |
|
738
|
|
|
} |
|
739
|
|
|
} |
|
740
|
|
|
$this->repository->bricks = $bricks; |
|
741
|
|
|
$this->save(); |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
|
|
/** |
|
745
|
|
|
* Delete a brick by its slug |
|
746
|
|
|
* |
|
747
|
|
|
* @param $slug |
|
748
|
|
|
* |
|
749
|
|
|
* @throws \Exception |
|
750
|
|
|
*/ |
|
751
|
|
|
public function deleteBrickBySlug($slug) |
|
752
|
|
|
{ |
|
753
|
|
|
$bricks = $this->repository->bricks; |
|
754
|
|
|
foreach ($bricks as $key => $brickObject) { |
|
755
|
|
|
if ($brickObject->slug == $slug) { |
|
756
|
|
|
unset($bricks[$key]); |
|
757
|
|
|
} |
|
758
|
|
|
} |
|
759
|
|
|
|
|
760
|
|
|
$bricks = array_values($bricks); |
|
761
|
|
|
$this->repository->bricks = $bricks; |
|
762
|
|
|
$this->save(); |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
/* |
|
766
|
|
|
* |
|
767
|
|
|
* Misc |
|
768
|
|
|
* |
|
769
|
|
|
*/ |
|
770
|
|
|
/** |
|
771
|
|
|
* Save changes made to the repository |
|
772
|
|
|
* |
|
773
|
|
|
* @throws \Exception |
|
774
|
|
|
*/ |
|
775
|
|
|
private function save() |
|
776
|
|
|
{ |
|
777
|
|
|
$this->repository->save(); |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
/* |
|
781
|
|
|
* |
|
782
|
|
|
* Image Set |
|
783
|
|
|
* |
|
784
|
|
|
*/ |
|
785
|
|
|
|
|
786
|
|
|
/** |
|
787
|
|
|
* Get the image set |
|
788
|
|
|
* |
|
789
|
|
|
* @return array |
|
790
|
|
|
*/ |
|
791
|
|
|
public function getImageSet() |
|
792
|
|
|
{ |
|
793
|
|
|
return $this->repository->imageSet; |
|
794
|
|
|
} |
|
795
|
|
|
|
|
796
|
|
|
/** |
|
797
|
|
|
* Get Image by slug |
|
798
|
|
|
* |
|
799
|
|
|
* @param $slug |
|
800
|
|
|
* |
|
801
|
|
|
* @return \stdClass |
|
802
|
|
|
*/ |
|
803
|
|
|
public function getImageSetBySlug($slug) |
|
804
|
|
|
{ |
|
805
|
|
|
$imageSet = $this->getImageSet(); |
|
806
|
|
|
foreach ($imageSet as $set) { |
|
807
|
|
|
if ($set->slug == $slug) { |
|
808
|
|
|
return $set; |
|
809
|
|
|
} |
|
810
|
|
|
} |
|
811
|
|
|
|
|
812
|
|
|
return null; |
|
813
|
|
|
} |
|
814
|
|
|
|
|
815
|
|
|
/** |
|
816
|
|
|
* Save Image Set by it's slug |
|
817
|
|
|
* |
|
818
|
|
|
* @param $slug |
|
819
|
|
|
* @param $postValues |
|
820
|
|
|
* |
|
821
|
|
|
* @throws \Exception |
|
822
|
|
|
*/ |
|
823
|
|
|
public function saveImageSet($slug, $postValues) |
|
824
|
|
|
{ |
|
825
|
|
|
$imageSetObject = ImageSetFactory::createImageSetFromPostValues($postValues); |
|
826
|
|
|
|
|
827
|
|
|
$imageSet = $this->repository->imageSet; |
|
828
|
|
|
foreach ($imageSet as $key => $set) { |
|
829
|
|
|
if ($set->slug == $slug) { |
|
830
|
|
|
$imageSet[$key] = $imageSetObject; |
|
831
|
|
|
} |
|
832
|
|
|
} |
|
833
|
|
|
$this->repository->imageSet = $imageSet; |
|
834
|
|
|
$this->save(); |
|
835
|
|
|
} |
|
836
|
|
|
|
|
837
|
|
|
/** |
|
838
|
|
|
* Add image set |
|
839
|
|
|
* |
|
840
|
|
|
* @param $postValues |
|
841
|
|
|
* |
|
842
|
|
|
* @throws \Exception |
|
843
|
|
|
*/ |
|
844
|
|
|
public function addImageSet($postValues) |
|
845
|
|
|
{ |
|
846
|
|
|
$imageSetObject = ImageSetFactory::createImageSetFromPostValues($postValues); |
|
847
|
|
|
|
|
848
|
|
|
$imageSet = $this->repository->imageSet; |
|
849
|
|
|
$imageSet[] = $imageSetObject; |
|
850
|
|
|
$this->repository->imageSet = $imageSet; |
|
851
|
|
|
|
|
852
|
|
|
$this->save(); |
|
853
|
|
|
} |
|
854
|
|
|
|
|
855
|
|
|
/** |
|
856
|
|
|
* Delete Image Set by its slug |
|
857
|
|
|
* |
|
858
|
|
|
* @param $slug |
|
859
|
|
|
* |
|
860
|
|
|
* @throws \Exception |
|
861
|
|
|
*/ |
|
862
|
|
|
public function deleteImageSetBySlug($slug) |
|
863
|
|
|
{ |
|
864
|
|
|
$imageSet = $this->getImageSet(); |
|
865
|
|
|
|
|
866
|
|
|
foreach ($imageSet as $key => $set) { |
|
867
|
|
|
if ($set->slug == $slug) { |
|
868
|
|
|
unset($imageSet[$key]); |
|
869
|
|
|
} |
|
870
|
|
|
} |
|
871
|
|
|
$imageSet = array_values($imageSet); |
|
872
|
|
|
$this->repository->imageSet = $imageSet; |
|
873
|
|
|
$this->save(); |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
|
|
/** |
|
877
|
|
|
* Get the image set with the smallest size |
|
878
|
|
|
* |
|
879
|
|
|
* @return \stdClass |
|
880
|
|
|
*/ |
|
881
|
|
|
public function getSmallestImageSet() |
|
882
|
|
|
{ |
|
883
|
|
|
$imageSet = $this->getImageSet(); |
|
884
|
|
|
|
|
885
|
|
|
$returnSize = PHP_INT_MAX; |
|
886
|
|
|
$returnSet = null; |
|
887
|
|
|
|
|
888
|
|
|
foreach ($imageSet as $set) { |
|
889
|
|
|
$size = $set->width * $set->height; |
|
890
|
|
|
if ($size < $returnSize) { |
|
891
|
|
|
$returnSize = $size; |
|
892
|
|
|
$returnSet = $set; |
|
893
|
|
|
} |
|
894
|
|
|
} |
|
895
|
|
|
|
|
896
|
|
|
if ($returnSet === null) { |
|
897
|
|
|
$returnSet = new \stdClass(); |
|
898
|
|
|
$returnSet->slug = 'original'; |
|
899
|
|
|
} |
|
900
|
|
|
|
|
901
|
|
|
return $returnSet; |
|
902
|
|
|
} |
|
903
|
|
|
|
|
904
|
|
|
/** |
|
905
|
|
|
* @return array |
|
906
|
|
|
*/ |
|
907
|
|
|
public function getApplicationComponents() |
|
908
|
|
|
{ |
|
909
|
|
|
return $this->repository->applicationComponents; |
|
910
|
|
|
} |
|
911
|
|
|
|
|
912
|
|
|
public function addApplicationComponent($postValues) |
|
913
|
|
|
{ |
|
914
|
|
|
$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues); |
|
915
|
|
|
$applicationComponents = $this->repository->applicationComponents; |
|
916
|
|
|
$applicationComponents[] = $applicationComponent; |
|
917
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
|
918
|
|
|
|
|
919
|
|
|
$this->save(); |
|
920
|
|
|
} |
|
921
|
|
|
|
|
922
|
|
|
public function getApplicationComponentBySlug($slug) |
|
923
|
|
|
{ |
|
924
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
|
925
|
|
|
foreach ($applicationComponents as $applicationComponent) { |
|
926
|
|
|
if ($applicationComponent->slug == $slug) { |
|
927
|
|
|
return $applicationComponent; |
|
928
|
|
|
} |
|
929
|
|
|
} |
|
930
|
|
|
|
|
931
|
|
|
return null; |
|
932
|
|
|
} |
|
933
|
|
|
|
|
934
|
|
|
public function saveApplicationComponent($slug, $postValues) |
|
935
|
|
|
{ |
|
936
|
|
|
$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues); |
|
937
|
|
|
|
|
938
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
|
939
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
|
940
|
|
|
if ($applicationComponent->slug == $slug) { |
|
941
|
|
|
$applicationComponents[$key] = $newApplicationComponent; |
|
942
|
|
|
} |
|
943
|
|
|
} |
|
944
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
|
945
|
|
|
$this->save(); |
|
946
|
|
|
} |
|
947
|
|
|
|
|
948
|
|
|
public function deleteApplicationComponentBySlug($slug) |
|
949
|
|
|
{ |
|
950
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
|
951
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
|
952
|
|
|
if ($applicationComponent->slug == $slug) { |
|
953
|
|
|
unset($applicationComponents[$key]); |
|
954
|
|
|
} |
|
955
|
|
|
} |
|
956
|
|
|
$applicationComponents = array_values($applicationComponents); |
|
957
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
|
958
|
|
|
$this->save(); |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
} |
|
962
|
|
|
} |
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.