1
|
|
|
<?php |
2
|
|
|
namespace SKYFILLERS\SfFilecollectionGallery\Controller; |
3
|
|
|
/* |
4
|
|
|
* This file is part of the TYPO3 CMS project. |
5
|
|
|
* |
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
8
|
|
|
* of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please read the |
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* The TYPO3 project - inspiring people to share! |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This view helper uses the technology of paginate widget but works with arrays |
18
|
|
|
* and the assigned objects don't need the QueryResultInterface. |
19
|
|
|
* |
20
|
|
|
* @author Paul Beck <[email protected]> |
21
|
|
|
* @author Armin Ruediger Vieweg <[email protected]> |
22
|
|
|
* @author Benjamin Schulte <[email protected]> |
23
|
|
|
* @author Jöran Kurschatke <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController { |
26
|
|
|
/** |
27
|
|
|
* Configuration Array |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $configuration = array('itemsPerPage' => 5, 'insertAbove' => FALSE, 'insertBelow' => TRUE, |
32
|
|
|
'maximumVisiblePages' => 7); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* All objects |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $objects; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Current Page |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $currentPage = 1; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Number of pages |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $numberOfPages = 1; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Items per pages |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
protected $itemsPerPage = 0; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initialize Action of the widget controller |
64
|
|
|
* |
65
|
|
|
* @todo Replace deprecated method |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function initializeAction() { |
69
|
|
|
$this->objects = $this->widgetConfiguration['objects']; |
70
|
|
|
\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule( |
71
|
|
|
$this->configuration, $this->widgetConfiguration['configuration'], TRUE); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the items per page |
76
|
|
|
* |
77
|
|
|
* @return int the items per page |
78
|
|
|
*/ |
79
|
|
|
public function getItemsPerPage() { |
80
|
|
|
return $this->itemsPerPage; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the items per page |
85
|
|
|
* |
86
|
|
|
* @param int $itemsPerPage The items per page |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function setItemsPerPage($itemsPerPage) { |
91
|
|
|
if (empty($itemsPerPage)) { |
92
|
|
|
$this->itemsPerPage = (integer)$this->configuration['itemsPerPage']; |
93
|
|
|
} else { |
94
|
|
|
$this->itemsPerPage = $itemsPerPage; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the number of pages |
100
|
|
|
* |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getNumberOfPages() { |
104
|
|
|
return $this->numberOfPages; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the number of pages |
109
|
|
|
* |
110
|
|
|
* @param int $numberOfPages The number of pages |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function setNumberOfPages($numberOfPages) { |
115
|
|
|
$this->numberOfPages = $numberOfPages; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the current page |
120
|
|
|
* |
121
|
|
|
* @return int the current page |
122
|
|
|
*/ |
123
|
|
|
public function getCurrentPage() { |
124
|
|
|
return $this->currentPage; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets the current page and limits it between 1 and $this->numberOfPages. |
129
|
|
|
* |
130
|
|
|
* @param int $currentPage The current page |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function setCurrentPage($currentPage) { |
135
|
|
|
$this->currentPage = $currentPage; |
136
|
|
|
if ($this->currentPage < 1) { |
137
|
|
|
$this->currentPage = 1; |
138
|
|
|
} elseif ($this->currentPage > $this->numberOfPages) { |
139
|
|
|
$this->currentPage = $this->numberOfPages; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns all visible objects within a range, |
145
|
|
|
* depending on itemsPerPage and the currentPage. |
146
|
|
|
* |
147
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryResult|array $objects The list of objects |
148
|
|
|
* |
149
|
|
|
* @return array<mixed> the list of visible objects |
150
|
|
|
*/ |
151
|
|
|
public function getVisibleObjects($objects) { |
152
|
|
|
$i = 0; |
153
|
|
|
$modifiedObjects = array(); |
154
|
|
|
$indexMin = $this->itemsPerPage * ($this->currentPage - 1); |
155
|
|
|
$indexMax = $this->itemsPerPage * $this->currentPage - 1; |
156
|
|
|
|
157
|
|
|
foreach ($objects as $object) { |
158
|
|
|
if ($i >= $indexMin && $i <= $indexMax) { |
159
|
|
|
$modifiedObjects[] = $object; |
160
|
|
|
} |
161
|
|
|
$i++; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $modifiedObjects; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Index action of the widget controller |
169
|
|
|
* |
170
|
|
|
* @param int $currentPage The current page |
171
|
|
|
* @param int $itemsPerPage The items per page |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function indexAction($currentPage = 1, $itemsPerPage = 0) { |
176
|
|
|
$this->setItemsPerPage($itemsPerPage); |
177
|
|
|
$this->setNumberOfPages(intval(ceil(count($this->objects) / (integer)$this->itemsPerPage))); |
178
|
|
|
$this->setCurrentPage((integer)$currentPage); |
179
|
|
|
|
180
|
|
|
$this->view->assign('contentArguments', array( |
181
|
|
|
$this->widgetConfiguration['as'] => $this->getVisibleObjects($this->objects) |
182
|
|
|
)); |
183
|
|
|
$this->view->assign('configuration', $this->configuration); |
184
|
|
|
if ($this->numberOfPages >= 2) { |
185
|
|
|
$this->view->assign('pagination', $this->buildPagination()); |
186
|
|
|
} |
187
|
|
|
$this->view->assign('itemsPerPage', $this->itemsPerPage); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns an array with the keys "pages", "current", |
192
|
|
|
* "numberOfPages", "nextPage" & "previousPage" |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
protected function buildPagination() { |
197
|
|
|
$sidePageCount = intval($this->configuration['maximumVisiblePages']) >> 1; |
198
|
|
|
|
199
|
|
|
$firstPage = max($this->currentPage - $sidePageCount, 1); |
200
|
|
|
$lastPage = min($firstPage + $sidePageCount * 2, $this->numberOfPages); |
201
|
|
|
$firstPage = max($lastPage - $sidePageCount * 2, 1); |
202
|
|
|
|
203
|
|
|
$pages = array(); |
204
|
|
|
foreach (range($firstPage, $lastPage) as $index) { |
205
|
|
|
$pages[] = array( |
206
|
|
|
'number' => $index, |
207
|
|
|
'isCurrent' => ($index === $this->currentPage) |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$pagination = array( |
212
|
|
|
'pages' => $pages, |
213
|
|
|
'current' => $this->currentPage, |
214
|
|
|
'numberOfPages' => $this->numberOfPages, |
215
|
|
|
'itemsPerPage' => $this->itemsPerPage, |
216
|
|
|
'firstPage' => 1, |
217
|
|
|
'lastPage' => $this->numberOfPages, |
218
|
|
|
'isFirstPage' => ($this->currentPage == 1), |
219
|
|
|
'isLastPage' => ($this->currentPage == $this->numberOfPages) |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
return $this->addPreviousAndNextPage($pagination); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Adds the nextPage and the previousPage to the pagination array |
227
|
|
|
* |
228
|
|
|
* @param array $pagination The pagination array which get previous and |
229
|
|
|
* next page |
230
|
|
|
* |
231
|
|
|
* @return array the pagination array which contains some meta data and |
232
|
|
|
* another array which are the pages |
233
|
|
|
*/ |
234
|
|
|
protected function addPreviousAndNextPage($pagination) { |
235
|
|
|
if ($this->currentPage < $this->numberOfPages) { |
236
|
|
|
$pagination['nextPage'] = $this->currentPage + 1; |
237
|
|
|
} |
238
|
|
|
if ($this->currentPage > 1) { |
239
|
|
|
$pagination['previousPage'] = $this->currentPage - 1; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $pagination; |
243
|
|
|
} |
244
|
|
|
} |