-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
PaginateController.php
164 lines (147 loc) · 5.2 KB
/
PaginateController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Neos\Media\Browser\ViewHelpers\Controller;
/*
* This file is part of the Neos.Media.Browser package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface;
use Neos\FluidAdaptor\Core\Widget\AbstractWidgetController;
use Neos\Media\Domain\Model\AssetSource\AssetSourceConnectionExceptionInterface;
use Neos\Utility\Arrays;
/**
* Controller for the paginate view helper
*/
class PaginateController extends AbstractWidgetController
{
/**
* @var AssetProxyQueryResultInterface
*/
protected $assetProxyQueryResult;
/**
* @var array
*/
protected $configuration = ['itemsPerPage' => 10, 'insertAbove' => false, 'insertBelow' => true, 'maximumNumberOfLinks' => 99];
/**
* @var int
*/
protected $currentPage = 1;
/**
* @var int
*/
protected $pagesBefore = 0;
/**
* @var int
*/
protected $pagesAfter = 0;
/**
* @var int
*/
protected $maximumNumberOfLinks = 99;
/**
* @var int
*/
protected $numberOfPages = 1;
/**
* @var int
*/
protected $displayRangeStart;
/**
* @var int
*/
protected $displayRangeEnd;
/**
* @return void
*/
protected function initializeAction()
{
$this->assetProxyQueryResult = $this->widgetConfiguration['queryResult'];
$this->configuration = Arrays::arrayMergeRecursiveOverrule($this->configuration, $this->widgetConfiguration['configuration'], true);
$this->numberOfPages = (int)ceil(count($this->assetProxyQueryResult) / (int)$this->configuration['itemsPerPage']);
$this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
}
/**
* @param int $currentPage
* @return void
*/
public function indexAction($currentPage = 1)
{
$this->currentPage = (int)$currentPage;
if ($this->currentPage < 1) {
$this->currentPage = 1;
} elseif ($this->currentPage > $this->numberOfPages) {
$this->currentPage = $this->numberOfPages;
}
try {
$itemsPerPage = (int)$this->configuration['itemsPerPage'];
$query = $this->assetProxyQueryResult->getQuery();
$query->setLimit($itemsPerPage);
if ($this->currentPage > 1) {
$query->setOffset((int)($itemsPerPage * ($this->currentPage - 1)));
}
$modifiedObjects = $query->execute();
$this->view->assign('contentArguments', [$this->widgetConfiguration['as'] => $modifiedObjects]);
$this->view->assign('configuration', $this->configuration);
$this->view->assign('pagination', $this->buildPagination());
} catch (AssetSourceConnectionExceptionInterface $exception) {
$this->view->assign('connectionError', $exception);
}
}
/**
* If a certain number of links should be displayed, adjust before and after
* amounts accordingly.
*
* @return void
*/
protected function calculateDisplayRange()
{
$maximumNumberOfLinks = $this->maximumNumberOfLinks;
if ($maximumNumberOfLinks > $this->numberOfPages) {
$maximumNumberOfLinks = $this->numberOfPages;
}
$delta = floor($maximumNumberOfLinks / 2);
$this->displayRangeStart = $this->currentPage - $delta;
$this->displayRangeEnd = $this->currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
if ($this->displayRangeStart < 1) {
$this->displayRangeEnd -= $this->displayRangeStart - 1;
}
if ($this->displayRangeEnd > $this->numberOfPages) {
$this->displayRangeStart -= ($this->displayRangeEnd - $this->numberOfPages);
}
$this->displayRangeStart = (int)max($this->displayRangeStart, 1);
$this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
}
/**
* Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
*
* @return array
*/
protected function buildPagination()
{
$this->calculateDisplayRange();
$pages = [];
for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
$pages[] = ['number' => $i, 'isCurrent' => ($i === $this->currentPage)];
}
$pagination = [
'pages' => $pages,
'current' => $this->currentPage,
'numberOfPages' => $this->numberOfPages,
'displayRangeStart' => $this->displayRangeStart,
'displayRangeEnd' => $this->displayRangeEnd,
'hasLessPages' => $this->displayRangeStart > 2,
'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages
];
if ($this->currentPage < $this->numberOfPages) {
$pagination['nextPage'] = $this->currentPage + 1;
}
if ($this->currentPage > 1) {
$pagination['previousPage'] = $this->currentPage - 1;
}
return $pagination;
}
}