Completed
Push — master ( 131901...40b8d9 )
by Jöran
03:01
created

FileCollectionService::buildArrayForAssignToView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 7
crap 2
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
	 * Sorts the Result Array according to the Flexform Settings
166
	 *
167
	 * @param array $imageItems The image items
168
	 *
169
	 * @return array
170
	 */
171
	protected function sortFileObjects($imageItems) {
172 9
		$lowercase = array_map(function($n) {
173 9
			return strtolower($n->getName());
174 9
		}, $imageItems);
175 9
		$configuration = $this->frontendConfigurationManager->getConfiguration();
176 9
		if ($configuration['settings']['order'] === 'desc') {
177 3
			array_multisort($lowercase, SORT_DESC, SORT_STRING, $imageItems);
178 3
		} else {
179 6
			array_multisort($lowercase, SORT_ASC, SORT_STRING, $imageItems);
180
		}
181 9
		return $imageItems;
182
	}
183
184
	/**
185
	 * Returns an FileObject from a given FileReference
186
	 *
187
	 * @param \TYPO3\CMS\Core\Resource\FileReference $item The item
188
	 *
189
	 * @return \TYPO3\CMS\Core\Resource\File
190
	 */
191
	protected function getFileObjectFromFileReference(FileReference $item) {
192
		/**
193
		 * The item to return
194
		 *
195
		 * @var \TYPO3\CMS\Core\Resource\File $returnItem
196
		 */
197
		$returnItem = $item->getOriginalFile();
198
		$returnItem->updateProperties($item->getProperties());
199
		return $returnItem;
200
	}
201
}
202