22
33namespace WapplerSystems \FilecollectionGallery \Service ;
44
5- /*
6- * This file is part of the TYPO3 CMS project.
7- *
8- * It is free software; you can redistribute it and/or modify it under
9- * the terms of the GNU General Public License, either version 2
10- * of the License, or any later version.
11- *
12- * For the full copyright and license information, please read the
13- * LICENSE.txt file that was distributed with this source code.
14- *
15- * The TYPO3 project - inspiring people to share!
16- */
175
186use TYPO3 \CMS \Core \Resource \Exception \ResourceDoesNotExistException ;
7+ use TYPO3 \CMS \Core \Resource \File ;
198use TYPO3 \CMS \Core \Resource \FileCollectionRepository ;
209use TYPO3 \CMS \Core \Resource \FileReference ;
2110use TYPO3 \CMS \Extbase \Configuration \FrontendConfigurationManager ;
2817class FileCollectionService
2918{
3019
31- public function __construct (readonly FileCollectionRepository $ fileCollectionRepository , readonly FrontendConfigurationManager $ frontendConfigurationManager )
20+
21+ public function __construct (
22+ readonly FileCollectionRepository $ fileCollectionRepository ,
23+ readonly FrontendConfigurationManager $ frontendConfigurationManager )
3224 {
3325
3426 }
3527
28+
3629 /**
3730 * Returns an array of file objects for the given UIDs of fileCollections
3831 *
3932 * @param array $collectionUids The uids
40- *
33+ * @param string $sortingDirection
4134 * @return array
4235 * @throws ResourceDoesNotExistException
4336 */
44- public function getFileObjectsFromCollection (array $ collectionUids)
37+ public function getFileObjectsFromCollection (array $ collectionUids, string $ sortingDirection = ' asc ' ): array
4538 {
4639 $ imageItems = [];
4740 foreach ($ collectionUids as $ collectionUid ) {
@@ -59,7 +52,7 @@ public function getFileObjectsFromCollection(array $collectionUids)
5952 'title ' => $ collection ->getTitle (),
6053 'description ' => $ collection ->getDescription ()
6154 ];
62- if ($ item instanceof \ TYPO3 \ CMS \ Core \ Resource \ FileReference) {
55+ if ($ item instanceof FileReference) {
6356 $ file = $ this ->getFileObjectFromFileReference ($ item );
6457 $ file ->updateProperties (['collection ' => $ collectionProperties ]);
6558 $ imageItems [] = $ file ;
@@ -69,7 +62,7 @@ public function getFileObjectsFromCollection(array $collectionUids)
6962 }
7063 }
7164 }
72- return $ this ->sortFileObjects ($ imageItems );
65+ return $ this ->sortFileObjects ($ imageItems, $ sortingDirection );
7366 }
7467
7568
@@ -79,29 +72,34 @@ public function getFileObjectsFromCollection(array $collectionUids)
7972 *
8073 * @param $collectionUids
8174 * @param $galleryFolderHash
75+ * @param string $sortingDirection
8276 * @return array
77+ * @throws ResourceDoesNotExistException
8378 */
84- public function getGalleryItemsByFolderHash ($ collectionUids , $ galleryFolderHash)
79+ public function getGalleryItemsByFolderHash ($ collectionUids , $ galleryFolderHash, string $ sortingDirection = ' asc ' ): array
8580 {
8681 $ imageItems = [];
8782
8883 // Load all images from collection
8984 foreach ($ collectionUids as $ collectionUid ) {
9085 $ collection = $ this ->fileCollectionRepository ->findByUid ($ collectionUid );
86+ if ($ collection === null ) {
87+ continue ;
88+ }
9189 $ collection ->loadContents ();
9290 $ allItems = [];
9391
9492 // Load all image and sort them by folder_hash
9593 foreach ($ collection ->getItems () as $ item ) {
9694 if ($ item ->getProperty ('folder_hash ' ) === $ galleryFolderHash ) {
97- if (get_class ( $ item) === ' TYPO3\CMS\Core\Resource\ FileReference' ) {
98- array_push ( $ allItems, $ this ->getFileObjectFromFileReference ($ item) );
95+ if ($ item instanceof FileReference) {
96+ $ allItems[] = $ this ->getFileObjectFromFileReference ($ item );
9997 } else {
100- array_push ( $ allItems, $ item) ;
98+ $ allItems[] = $ item ;
10199 }
102100 }
103101 }
104- $ imageItems = $ this ->sortFileObjects ($ allItems );
102+ $ imageItems = $ this ->sortFileObjects ($ allItems, $ sortingDirection );
105103 }
106104 return $ imageItems ;
107105 }
@@ -112,7 +110,7 @@ public function getGalleryItemsByFolderHash($collectionUids, $galleryFolderHash)
112110 * @param array $settings The current settings
113111 * @return array
114112 */
115- public function buildPaginationArray ($ settings )
113+ public function buildPaginationArray ($ settings ): array
116114 {
117115 $ paginationArray = [];
118116 if (!empty ($ settings )) {
@@ -132,7 +130,7 @@ public function buildPaginationArray($settings)
132130 * @param array $settings The current settings
133131 * @return array
134132 */
135- public function buildPaginationArrayForNested ($ settings )
133+ public function buildPaginationArrayForNested (array $ settings ): array
136134 {
137135 $ paginationArray = [];
138136 if (!empty ($ settings )) {
@@ -147,7 +145,7 @@ public function buildPaginationArrayForNested($settings)
147145 }
148146
149147
150- protected function sortFileObjectsByName ($ items , int $ direction )
148+ protected function sortFileObjectsByName ($ items , int $ direction ): void
151149 {
152150 $ lowercaseNames = array_map (function ($ n ) {
153151 return strtolower ($ n ->getName ());
@@ -156,7 +154,7 @@ protected function sortFileObjectsByName($items, int $direction)
156154 array_multisort ($ lowercaseNames , $ direction , SORT_STRING , $ items );
157155 }
158156
159- protected function sortFileObjectsByDate ($ items , int $ direction )
157+ protected function sortFileObjectsByDate ($ items , int $ direction ): void
160158 {
161159 $ dates = array_map (function ($ n ) {
162160 return strtolower ($ n ->getCreationTime ());
@@ -165,7 +163,7 @@ protected function sortFileObjectsByDate($items, int $direction)
165163 array_multisort ($ dates , $ direction , SORT_NUMERIC , $ items );
166164 }
167165
168- protected function sortFileObjectsByFolderHash (&$ items , int $ direction )
166+ protected function sortFileObjectsByFolderHash (&$ items , int $ direction ): void
169167 {
170168 $ folderhashes = array_map (function ($ n ) {
171169 return strtolower ($ n ->getProperty ('folder_hash ' ));
@@ -181,10 +179,9 @@ protected function sortFileObjectsByFolderHash(&$items, int $direction)
181179 *
182180 * @return array
183181 */
184- protected function sortFileObjects ($ imageItems)
182+ protected function sortFileObjects (array $ imageItems, string $ sortingDirection = ' asc ' ): array
185183 {
186- $ configuration = $ this ->frontendConfigurationManager ->getConfiguration ();
187- switch ($ configuration ['settings ' ]['order ' ] ?? '' ) {
184+ switch ($ sortingDirection ) {
188185 case 'desc ' :
189186 $ this ->sortFileObjectsByName ($ imageItems , SORT_DESC );
190187 break ;
@@ -208,12 +205,12 @@ protected function sortFileObjects($imageItems)
208205 * Returns an FileObject from a given FileReference
209206 *
210207 */
211- protected function getFileObjectFromFileReference (FileReference $ item ): \ TYPO3 \ CMS \ Core \ Resource \ File
208+ protected function getFileObjectFromFileReference (FileReference $ item ): File
212209 {
213210 /**
214211 * The item to return
215212 *
216- * @var \TYPO3\CMS\Core\Resource\ File $returnItem
213+ * @var File $returnItem
217214 */
218215 $ returnItem = $ item ->getOriginalFile ();
219216 $ returnItem ->updateProperties ($ item ->getProperties ());
0 commit comments