Completed
Push — master ( 3b52f6...fdc605 )
by Jöran
16:56
created

FileCollectionService::sortFileObjects()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 6
Bugs 2 Features 1
Metric Value
c 6
b 2
f 1
dl 0
loc 21
ccs 0
cts 3
cp 0
rs 8.7624
cc 5
eloc 18
nc 5
nop 1
crap 30
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
	 * Collection Repository
30
	 *
31
	 * @var \TYPO3\CMS\Core\Resource\FileCollectionRepository
32
	 */
33
	protected $fileCollectionRepository;
34
35
	/**
36
	 * The Frontend Configuration
37
	 *
38
	 * @var \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager
39
	 */
40
	protected $frontendConfigurationManager;
41
42
	/**
43
	 * Inject the fileCollection repository
44
	 *
45
	 * @param \TYPO3\CMS\Core\Resource\FileCollectionRepository $fileCollectionRepository
46
	 *
47
	 * @return void
48
	 */
49 9
	public function injectFileCollectionRepository(FileCollectionRepository $fileCollectionRepository) {
50 9
		$this->fileCollectionRepository = $fileCollectionRepository;
51 9
	}
52
53
	/**
54
	 * Inject the Frontend Configuration Manager.
55
	 *
56
	 * @param \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager $frontendConfigurationManager
57
	 *
58
	 * @return void
59
	 */
60 9
	public function injectFrontendConfigurationManager(FrontendConfigurationManager $frontendConfigurationManager) {
61 9
		$this->frontendConfigurationManager = $frontendConfigurationManager;
62 9
	}
63
64
	/**
65
	 * Returns an array of file objects for the given UIDs of fileCollections
66
	 *
67
	 * @param array $collectionUids The uids
68
	 *
69
	 * @return array
70
	 */
71 9
	public function getFileObjectsFromCollection(array $collectionUids) {
72 9
		$imageItems = array();
73 9
		foreach ($collectionUids as $collectionUid) {
74 9
			$collection = $this->fileCollectionRepository->findByUid($collectionUid);
75 9
			$collection->loadContents();
76 9
			foreach ($collection->getItems() as $item) {
77 9
				if (get_class($item) === 'TYPO3\CMS\Core\Resource\FileReference') {
78
					array_push($imageItems, $this->getFileObjectFromFileReference($item));
79
				} else {
80 9
					array_push($imageItems, $item);
81
				}
82 9
			}
83 9
		}
84 9
		return $this->sortFileObjects($imageItems);
85
	}
86
87
	/**
88
	 * Returns an array of gallery covers for the given UIDs of fileCollections
89
	 *
90
	 * @param $collectionUids
91
	 * @return array
92
	 */
93
	public function getGalleryCoversFromCollections($collectionUids) {
94
		$imageItems = array();
95
		foreach ($collectionUids as $collectionUid) {
96
			$collection = $this->fileCollectionRepository->findByUid($collectionUid);
97
			$collection->loadContents();
98
			$galleryCover = array();
99
			foreach ($collection->getItems() as $item) {
100
				if (get_class($item) === 'TYPO3\CMS\Core\Resource\FileReference') {
101
					array_push($galleryCover, $this->getFileObjectFromFileReference($item));
102
				} else {
103
					array_push($galleryCover, $item);
104
				}
105
			}
106
			$galleryCover = $this->sortFileObjects($galleryCover);
107
108
			$galleryCover[0]->galleryUID = $collectionUid;
109
			$galleryCover[0]->galleryTitle = $collection->getTitle();
110
			$galleryCover[0]->galleryDescription = $collection->getDescription();
111
			$galleryCover[0]->gallerySize = sizeof($galleryCover);
112
113
			array_push($imageItems, $galleryCover[0]);
114
		}
115
		return $this->sortFileObjects($imageItems);
116
	}
117
118
	/**
119
	 * Returns the array including pagination settings
120
	 *
121
	 * @param array $settings The current settings
122
	 * @return array
123
	 */
124
	public function buildPaginationArray($settings) {
125
		$paginationArray = array();
126
		if (!empty($settings)) {
127
			$paginationArray = array(
128
				'itemsPerPage' => $settings['imagesPerPage'],
129
				'maximumVisiblePages' => $settings['numberOfPages'],
130
				'insertAbove' => $settings['insertAbove'],
131
				'insertBelow' => $settings['insertBelow']
132
			);
133
		}
134
		return $paginationArray;
135
	}
136
137
	/**
138
	 * Returns the array for assign to view in controller
139
	 *
140
	 * @param array $imageItems The imageItems to show
141
	 * @param int $offset The offset in gallery
142
	 * @param array $paginationConfiguration The pagination config
143
	 * @param array $settings The settings array
144
	 * @param int $currentUid The current uid
145
	 * @param int $columnPosition The column position
146
	 * @param bool $showBackToGallerySelectionLink If back link should be shown
147
	 *
148
	 * @return array
149
	 */
150
	public function buildArrayForAssignToView($imageItems, $offset, $paginationConfiguration, $settings,
151
											  $currentUid, $columnPosition, $showBackToGallerySelectionLink) {
152
		$assign = array(
153
			'imageItems' => $imageItems,
154
			'offset' => $offset,
155
			'paginationConfiguration' => $paginationConfiguration,
156
			'settings' => $settings,
157
			'currentUid' => $currentUid,
158
			'columnPosition' => $columnPosition,
159
			'showBackToGallerySelectionLink' => $showBackToGallerySelectionLink
160
		);
161
		return $assign;
162
	}
163
164
	/**
165
	 * @param array $items The items to sort
166
	 * @param $direction the direction. SORT_DESC or SORT_ASC
167
	 * @return bool
168
	 */
169
	protected function sortFileObjectsByName(&$items, $direction) {
170
		$lowercaseNames = array_map(function($n) {
171
			return strtolower($n->getName());
172 9
		}, $items);
173 9
174 9
		array_multisort($lowercaseNames, $direction, SORT_STRING, $items);
175 9
	}
176 9
177 3
	/**
178 3
	 * @param array $items The items to sort
179 6
	 * @param $direction the direction. SORT_DESC or SORT_ASC
180
	 * @return bool
181 9
	 */
182
	protected function sortFileObjectsByDate(&$items, $direction) {
183
		$dates = array_map(function($n) {
184
			return strtolower($n->getCreationTime());
185
		}, $items);
186
187
		array_multisort($dates, $direction, SORT_NUMERIC, $items);
188
	}
189
190
	/**
191
	 * Sorts the Result Array according to the Flexform Settings
192
	 *
193
	 * @param array $imageItems The image items
194
	 *
195
	 * @return array
196
	 */
197
	protected function sortFileObjects($imageItems) {
198
		$configuration = $this->frontendConfigurationManager->getConfiguration();
199
		switch ($configuration['settings']['order']) {
200
			case 'desc':
201
				$this->sortFileObjectsByName($imageItems, SORT_DESC);
202
				break;
203
			case 'date-desc':
204
				$this->sortFileObjectsByDate($imageItems, SORT_DESC);
205
				break;
206
			case 'date-asc':
207
				$this->sortFileObjectsByDate($imageItems, SORT_ASC);
208
				break;
209
			case 'manual':
210
				// Do not sort. This could be default, but could be breaking, since default was ASC before.
211
				break;
212
			default:
213
				$this->sortFileObjectsByName($imageItems, SORT_ASC);
214
				break;
215
		}
216
		return $imageItems;
217
	}
218
219
	/**
220
	 * Returns an FileObject from a given FileReference
221
	 *
222
	 * @param \TYPO3\CMS\Core\Resource\FileReference $item The item
223
	 *
224
	 * @return \TYPO3\CMS\Core\Resource\File
225
	 */
226
	protected function getFileObjectFromFileReference(FileReference $item) {
227
		/**
228
		 * The item to return
229
		 *
230
		 * @var \TYPO3\CMS\Core\Resource\File $returnItem
231
		 */
232
		$returnItem = $item->getOriginalFile();
233
		$returnItem->updateProperties($item->getProperties());
234
		return $returnItem;
235
	}
236
}
237