|
1
|
|
|
<?php |
|
2
|
|
|
namespace SKYFILLERS\SfFilecollectionGallery\Service; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager; |
|
18
|
|
|
use TYPO3\CMS\Core\Resource\FileCollectionRepository; |
|
19
|
|
|
use TYPO3\CMS\Core\Resource\FileReference; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* FileCollectionService |
|
23
|
|
|
* |
|
24
|
|
|
* @author Jöran Kurschatke <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class FileCollectionService |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Collection Repository |
|
31
|
|
|
* |
|
32
|
|
|
* @var \TYPO3\CMS\Core\Resource\FileCollectionRepository |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $fileCollectionRepository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The Frontend Configuration |
|
38
|
|
|
* |
|
39
|
|
|
* @var \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $frontendConfigurationManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Inject the fileCollection repository |
|
45
|
|
|
* |
|
46
|
|
|
* @param \TYPO3\CMS\Core\Resource\FileCollectionRepository $fileCollectionRepository |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
9 |
|
*/ |
|
50
|
9 |
|
public function injectFileCollectionRepository(FileCollectionRepository $fileCollectionRepository) |
|
51
|
9 |
|
{ |
|
52
|
|
|
$this->fileCollectionRepository = $fileCollectionRepository; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Inject the Frontend Configuration Manager. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager $frontendConfigurationManager |
|
59
|
|
|
* |
|
60
|
9 |
|
* @return void |
|
61
|
9 |
|
*/ |
|
62
|
9 |
|
public function injectFrontendConfigurationManager(FrontendConfigurationManager $frontendConfigurationManager) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->frontendConfigurationManager = $frontendConfigurationManager; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns an array of file objects for the given UIDs of fileCollections |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $collectionUids The uids |
|
71
|
9 |
|
* |
|
72
|
9 |
|
* @return array |
|
73
|
9 |
|
*/ |
|
74
|
9 |
|
public function getFileObjectsFromCollection(array $collectionUids) |
|
75
|
9 |
|
{ |
|
76
|
9 |
|
$imageItems = array(); |
|
77
|
9 |
|
foreach ($collectionUids as $collectionUid) { |
|
78
|
|
|
$collection = $this->fileCollectionRepository->findByUid($collectionUid); |
|
79
|
|
|
$collection->loadContents(); |
|
80
|
9 |
|
foreach ($collection->getItems() as $item) { |
|
81
|
|
|
if (get_class($item) === 'TYPO3\CMS\Core\Resource\FileReference') { |
|
82
|
9 |
|
array_push($imageItems, $this->getFileObjectFromFileReference($item)); |
|
83
|
9 |
|
} else { |
|
84
|
9 |
|
array_push($imageItems, $item); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $this->sortFileObjects($imageItems); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns an array of gallery covers for the given UIDs of fileCollections |
|
93
|
|
|
* |
|
94
|
|
|
* @param $collectionUids |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getGalleryCoversFromCollections($collectionUids) |
|
98
|
|
|
{ |
|
99
|
|
|
$imageItems = array(); |
|
100
|
|
|
foreach ($collectionUids as $collectionUid) { |
|
101
|
|
|
$collection = $this->fileCollectionRepository->findByUid($collectionUid); |
|
102
|
|
|
$collection->loadContents(); |
|
103
|
|
|
$galleryCover = array(); |
|
104
|
|
|
foreach ($collection->getItems() as $item) { |
|
105
|
|
|
if (get_class($item) === 'TYPO3\CMS\Core\Resource\FileReference') { |
|
106
|
|
|
array_push($galleryCover, $this->getFileObjectFromFileReference($item)); |
|
107
|
|
|
} else { |
|
108
|
|
|
array_push($galleryCover, $item); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
$galleryCover = $this->sortFileObjects($galleryCover); |
|
112
|
|
|
|
|
113
|
|
|
$galleryCover[0]->galleryUID = $collectionUid; |
|
114
|
|
|
$galleryCover[0]->galleryTitle = $collection->getTitle(); |
|
115
|
|
|
$galleryCover[0]->galleryDescription = $collection->getDescription(); |
|
116
|
|
|
$galleryCover[0]->gallerySize = sizeof($galleryCover); |
|
117
|
|
|
|
|
118
|
|
|
array_push($imageItems, $galleryCover[0]); |
|
119
|
|
|
} |
|
120
|
|
|
return $this->sortFileObjects($imageItems); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns an array of gallery covers for the given UIDs of fileCollections |
|
125
|
|
|
* Use if you have recursive folder collection. |
|
126
|
|
|
* |
|
127
|
|
|
* @param $collectionUids |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getGalleryCoversFromNestedFoldersCollection($collectionUids) |
|
131
|
|
|
{ |
|
132
|
|
|
$configuration = $this->frontendConfigurationManager->getConfiguration(); |
|
133
|
|
|
$imageItems = array(); |
|
134
|
|
|
|
|
135
|
|
|
// Load all images from collection |
|
136
|
|
|
foreach ($collectionUids as $collectionUid) { |
|
137
|
|
|
$collection = $this->fileCollectionRepository->findByUid($collectionUid); |
|
138
|
|
|
$collection->loadContents(); |
|
139
|
|
|
$galleryCover = array(); |
|
140
|
|
|
|
|
141
|
|
|
// Load all image and sort them by folder_hash |
|
142
|
|
|
foreach ($collection->getItems() as $item) { |
|
143
|
|
|
if (get_class($item) === 'TYPO3\CMS\Core\Resource\FileReference') { |
|
144
|
|
|
array_push($galleryCover, $this->getFileObjectFromFileReference($item)); |
|
145
|
|
|
} else { |
|
146
|
|
|
array_push($galleryCover, $item); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
$this->sortFileObjectsByFolderHash($galleryCover, SORT_ASC); |
|
150
|
|
|
|
|
151
|
|
|
// Split all images in separate arrays |
|
152
|
|
|
$folderHashedGalleryCovers = array(); |
|
153
|
|
|
$oldFolderHash = ''; |
|
154
|
|
|
foreach ($galleryCover as $item) { |
|
155
|
|
|
$getFolderPath = explode(strrchr($item->getIdentifier(), "/"), $item->getIdentifier()); |
|
156
|
|
|
$itemIdentificatorWithoutFilename = $getFolderPath[0]; |
|
157
|
|
|
if ($item->getProperty('folder_hash') !== $oldFolderHash) { |
|
158
|
|
|
$oldFolderHash = $item->getProperty('folder_hash'); |
|
159
|
|
|
$folderHashedGalleryCovers[$itemIdentificatorWithoutFilename] = array(); |
|
160
|
|
|
array_push($folderHashedGalleryCovers[$itemIdentificatorWithoutFilename], $item); |
|
161
|
|
|
} else { |
|
162
|
|
|
array_push($folderHashedGalleryCovers[$itemIdentificatorWithoutFilename], $item); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Sort the array by key => $itemIdentificatorWithoutFilename |
|
167
|
|
|
if ($configuration['settings']['orderNestedFolder'] == "asc") { |
|
168
|
|
|
ksort($folderHashedGalleryCovers); |
|
169
|
|
|
} else { |
|
170
|
|
|
krsort($folderHashedGalleryCovers); |
|
171
|
|
|
} |
|
172
|
9 |
|
|
|
173
|
9 |
|
// Sort all subarrays depending on the current settings |
|
174
|
9 |
|
foreach ($folderHashedGalleryCovers as $itemIdentificatorWithoutFilename => $folderHashedGalleryCoversArray) { |
|
175
|
9 |
|
$folderHashedGalleryCovers[$itemIdentificatorWithoutFilename] = $this->sortFileObjects($folderHashedGalleryCoversArray); |
|
176
|
3 |
|
$folderHashedGalleryCovers[$itemIdentificatorWithoutFilename][0]->galleryFolder = $folderHashedGalleryCovers[$itemIdentificatorWithoutFilename][0]->getProperty('folder_hash'); |
|
177
|
3 |
|
$folderHashedGalleryCovers[$itemIdentificatorWithoutFilename][0]->galleryUID = $collectionUid; |
|
178
|
6 |
|
$folderHashedGalleryCovers[$itemIdentificatorWithoutFilename][0]->gallerySize = sizeof($folderHashedGalleryCovers[$itemIdentificatorWithoutFilename]); |
|
179
|
|
|
array_push($imageItems, $folderHashedGalleryCovers[$itemIdentificatorWithoutFilename][0]); |
|
180
|
9 |
|
} |
|
181
|
|
|
} |
|
182
|
|
|
return $imageItems; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns an array of gallery covers for the given UIDs of fileCollections |
|
187
|
|
|
* Use if you have recursive folder collection. |
|
188
|
|
|
* |
|
189
|
|
|
* @param $collectionUids |
|
190
|
|
|
* @param $galleryFolder |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getGalleryItemsByFolderHash($collectionUids, $galleryFolderHash) |
|
194
|
|
|
{ |
|
195
|
|
|
$imageItems = array(); |
|
196
|
|
|
|
|
197
|
|
|
// Load all images from collection |
|
198
|
|
|
foreach ($collectionUids as $collectionUid) { |
|
199
|
|
|
$collection = $this->fileCollectionRepository->findByUid($collectionUid); |
|
200
|
|
|
$collection->loadContents(); |
|
201
|
|
|
$allItems = array(); |
|
202
|
|
|
|
|
203
|
|
|
// Load all image and sort them by folder_hash |
|
204
|
|
|
foreach ($collection->getItems() as $item) { |
|
205
|
|
|
if($item->getProperty('folder_hash') === $galleryFolderHash) { |
|
206
|
|
|
if (get_class($item) === 'TYPO3\CMS\Core\Resource\FileReference') { |
|
207
|
|
|
array_push($allItems, $this->getFileObjectFromFileReference($item)); |
|
208
|
|
|
} else { |
|
209
|
|
|
array_push($allItems, $item); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
$imageItems = $this->sortFileObjects($allItems); |
|
214
|
|
|
} |
|
215
|
|
|
return $imageItems; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Returns the array including pagination settings |
|
220
|
|
|
* |
|
221
|
|
|
* @param array $settings The current settings |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
public function buildPaginationArray($settings) |
|
225
|
|
|
{ |
|
226
|
|
|
$paginationArray = array(); |
|
227
|
|
|
if (!empty($settings)) { |
|
228
|
|
|
$paginationArray = array( |
|
229
|
|
|
'itemsPerPage' => $settings['imagesPerPage'], |
|
230
|
|
|
'maximumVisiblePages' => $settings['numberOfPages'], |
|
231
|
|
|
'insertAbove' => $settings['insertAbove'], |
|
232
|
|
|
'insertBelow' => $settings['insertBelow'] |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
return $paginationArray; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Returns the array for assign to view in controller |
|
240
|
|
|
* |
|
241
|
|
|
* @param array $imageItems The imageItems to show |
|
242
|
|
|
* @param int $offset The offset in gallery |
|
243
|
|
|
* @param array $paginationConfiguration The pagination config |
|
244
|
|
|
* @param array $settings The settings array |
|
245
|
|
|
* @param int $currentUid The current uid |
|
246
|
|
|
* @param int $columnPosition The column position |
|
247
|
|
|
* @param bool $showBackToGallerySelectionLink If back link should be shown |
|
248
|
|
|
* |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
public function buildArrayForAssignToView($imageItems, $offset, $paginationConfiguration, $settings, |
|
252
|
|
|
$currentUid, $columnPosition, $showBackToGallerySelectionLink) |
|
253
|
|
|
{ |
|
254
|
|
|
$assign = array( |
|
255
|
|
|
'imageItems' => $imageItems, |
|
256
|
|
|
'offset' => $offset, |
|
257
|
|
|
'paginationConfiguration' => $paginationConfiguration, |
|
258
|
|
|
'settings' => $settings, |
|
259
|
|
|
'currentUid' => $currentUid, |
|
260
|
|
|
'columnPosition' => $columnPosition, |
|
261
|
|
|
'showBackToGallerySelectionLink' => $showBackToGallerySelectionLink |
|
262
|
|
|
); |
|
263
|
|
|
return $assign; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param array $items The items to sort |
|
268
|
|
|
* @param $direction the direction. SORT_DESC or SORT_ASC |
|
269
|
|
|
* @return bool |
|
270
|
|
|
*/ |
|
271
|
|
|
protected function sortFileObjectsByName(&$items, $direction) |
|
272
|
|
|
{ |
|
273
|
|
|
$lowercaseNames = array_map(function ($n) { |
|
274
|
|
|
return strtolower($n->getName()); |
|
275
|
|
|
}, $items); |
|
276
|
|
|
|
|
277
|
|
|
array_multisort($lowercaseNames, $direction, SORT_STRING, $items); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param array $items The items to sort |
|
282
|
|
|
* @param $direction the direction. SORT_DESC or SORT_ASC |
|
283
|
|
|
* @return bool |
|
284
|
|
|
*/ |
|
285
|
|
|
protected function sortFileObjectsByDate(&$items, $direction) |
|
286
|
|
|
{ |
|
287
|
|
|
$dates = array_map(function ($n) { |
|
288
|
|
|
return strtolower($n->getCreationTime()); |
|
289
|
|
|
}, $items); |
|
290
|
|
|
|
|
291
|
|
|
array_multisort($dates, $direction, SORT_NUMERIC, $items); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param array $items The items to sort |
|
296
|
|
|
* @param $direction the direction. SORT_DESC or SORT_ASC |
|
297
|
|
|
* @return bool |
|
298
|
|
|
*/ |
|
299
|
|
|
protected function sortFileObjectsByFolderHash(&$items, $direction) |
|
300
|
|
|
{ |
|
301
|
|
|
$folderhashes = array_map(function ($n) { |
|
302
|
|
|
return strtolower($n->getProperty('folder_hash')); |
|
303
|
|
|
}, $items); |
|
304
|
|
|
|
|
305
|
|
|
array_multisort($folderhashes, $direction, SORT_NUMERIC, $items); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Sorts the Result Array according to the Flexform Settings |
|
310
|
|
|
* |
|
311
|
|
|
* @param array $imageItems The image items |
|
312
|
|
|
* |
|
313
|
|
|
* @return array |
|
314
|
|
|
*/ |
|
315
|
|
|
protected function sortFileObjects($imageItems) |
|
316
|
|
|
{ |
|
317
|
|
|
$configuration = $this->frontendConfigurationManager->getConfiguration(); |
|
318
|
|
|
switch ($configuration['settings']['order']) { |
|
319
|
|
|
case 'desc': |
|
320
|
|
|
$this->sortFileObjectsByName($imageItems, SORT_DESC); |
|
321
|
|
|
break; |
|
322
|
|
|
case 'date-desc': |
|
323
|
|
|
$this->sortFileObjectsByDate($imageItems, SORT_DESC); |
|
324
|
|
|
break; |
|
325
|
|
|
case 'date-asc': |
|
326
|
|
|
$this->sortFileObjectsByDate($imageItems, SORT_ASC); |
|
327
|
|
|
break; |
|
328
|
|
|
case 'manual': |
|
329
|
|
|
// Do not sort. This could be default, but could be breaking, since default was ASC before. |
|
330
|
|
|
break; |
|
331
|
|
|
default: |
|
332
|
|
|
$this->sortFileObjectsByName($imageItems, SORT_ASC); |
|
333
|
|
|
break; |
|
334
|
|
|
} |
|
335
|
|
|
return $imageItems; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Returns an FileObject from a given FileReference |
|
340
|
|
|
* |
|
341
|
|
|
* @param \TYPO3\CMS\Core\Resource\FileReference $item The item |
|
342
|
|
|
* |
|
343
|
|
|
* @return \TYPO3\CMS\Core\Resource\File |
|
344
|
|
|
*/ |
|
345
|
|
|
protected function getFileObjectFromFileReference(FileReference $item) |
|
346
|
|
|
{ |
|
347
|
|
|
/** |
|
348
|
|
|
* The item to return |
|
349
|
|
|
* |
|
350
|
|
|
* @var \TYPO3\CMS\Core\Resource\File $returnItem |
|
351
|
|
|
*/ |
|
352
|
|
|
$returnItem = $item->getOriginalFile(); |
|
353
|
|
|
$returnItem->updateProperties($item->getProperties()); |
|
354
|
|
|
return $returnItem; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|