‪TYPO3CMS  10.4
PaginateController.php
Go to the documentation of this file.
1 <?php
2 
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 
17 
22 
27 {
31  protected ‪$configuration = [
32  'itemsPerPage' => 10,
33  'insertAbove' => false,
34  'insertBelow' => true,
35  'maximumNumberOfLinks' => 99,
36  'addQueryStringMethod' => '',
37  'section' => ''
38  ];
39 
43  protected ‪$objects;
44 
48  protected ‪$currentPage = 1;
49 
53  protected ‪$maximumNumberOfLinks = 99;
54 
58  protected ‪$numberOfPages = 1;
59 
63  protected ‪$displayRangeStart;
64 
68  protected ‪$displayRangeEnd;
69 
73  public function ‪initializeAction()
74  {
75  $this->objects = $this->widgetConfiguration['objects'];
76  ‪ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
77  $itemsPerPage = (int)$this->configuration['itemsPerPage'];
78  $this->numberOfPages = $itemsPerPage > 0 ? ceil(count($this->objects) / $itemsPerPage) : 0;
79  $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
80  }
81 
85  public function ‪indexAction(‪$currentPage = 1)
86  {
87  // set current page
88  $this->currentPage = (int)‪$currentPage;
89  if ($this->currentPage < 1) {
90  $this->currentPage = 1;
91  }
92  if ($this->currentPage > $this->numberOfPages) {
93  // set $modifiedObjects to NULL if the page does not exist
94  $modifiedObjects = null;
95  } else {
96  // modify query
97  $itemsPerPage = (int)$this->configuration['itemsPerPage'];
98  $offset = 0;
99  if ($this->objects instanceof QueryResultInterface) {
100  $offset = (int)$this->objects->getQuery()->getOffset();
101  }
102  if ($this->currentPage > 1) {
103  $offset = $offset + ((int)($itemsPerPage * ($this->currentPage - 1)));
104  }
105  $modifiedObjects = $this->‪prepareObjectsSlice($itemsPerPage, $offset);
106  }
107  $this->view->assign('contentArguments', [
108  $this->widgetConfiguration['as'] => $modifiedObjects
109  ]);
110  $this->view->assign('configuration', $this->configuration);
111  $this->view->assign('pagination', $this->‪buildPagination());
112  }
113 
118  protected function ‪calculateDisplayRange()
119  {
121  if (‪$maximumNumberOfLinks > $this->numberOfPages) {
123  }
124  $delta = floor(‪$maximumNumberOfLinks / 2);
125  $this->displayRangeStart = $this->currentPage - $delta;
126  $this->displayRangeEnd = $this->currentPage + $delta - (‪$maximumNumberOfLinks % 2 === 0 ? 1 : 0);
127  if ($this->displayRangeStart < 1) {
128  $this->displayRangeEnd -= $this->displayRangeStart - 1;
129  }
130  if ($this->displayRangeEnd > $this->numberOfPages) {
131  $this->displayRangeStart -= $this->displayRangeEnd - ‪$this->numberOfPages;
132  }
133  $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
134  $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
135  }
136 
143  protected function ‪buildPagination()
144  {
145  $this->‪calculateDisplayRange();
146  $pages = [];
147  for ($i = $this->displayRangeStart; $i <= ‪$this->displayRangeEnd; $i++) {
148  $pages[] = ['number' => $i, 'isCurrent' => $i === ‪$this->currentPage];
149  }
150  $pagination = [
151  'pages' => $pages,
152  'current' => ‪$this->currentPage,
153  'numberOfPages' => ‪$this->numberOfPages,
154  'displayRangeStart' => ‪$this->displayRangeStart,
155  'displayRangeEnd' => ‪$this->displayRangeEnd,
156  'hasLessPages' => $this->displayRangeStart > 2,
157  'hasMorePages' => $this->displayRangeEnd + 1 < ‪$this->numberOfPages
158  ];
159  if ($this->currentPage < $this->numberOfPages) {
160  $pagination['nextPage'] = $this->currentPage + 1;
161  }
162  if ($this->currentPage > 1) {
163  $pagination['previousPage'] = $this->currentPage - 1;
164  }
165  return $pagination;
166  }
167 
175  protected function ‪prepareObjectsSlice($itemsPerPage, $offset)
176  {
177  if ($this->objects instanceof ‪QueryResultInterface) {
178  $currentRange = $offset + $itemsPerPage;
179  $endOfRange = min($currentRange, count($this->objects));
180  $query = $this->objects->getQuery();
181  $query->setLimit($itemsPerPage);
182  if ($offset > 0) {
183  $query->setOffset($offset);
184  if ($currentRange > $endOfRange) {
185  $newLimit = $endOfRange - $offset;
186  $query->setLimit($newLimit);
187  }
188  }
189  $modifiedObjects = $query->execute();
190  return $modifiedObjects;
191  }
192  if ($this->objects instanceof ObjectStorage) {
193  $modifiedObjects = [];
194  $objectArray = $this->objects->toArray();
195  $endOfRange = min($offset + $itemsPerPage, count($objectArray));
196  for ($i = $offset; $i < $endOfRange; $i++) {
197  $modifiedObjects[] = $objectArray[$i];
198  }
199  return $modifiedObjects;
200  }
201  if (is_array($this->objects)) {
202  $modifiedObjects = array_slice($this->objects, $offset, $itemsPerPage);
203  return $modifiedObjects;
204  }
205  throw new \InvalidArgumentException(
206  'The ViewHelper "' . static::class
207  . '" accepts as argument "QueryResultInterface", "\SplObjectStorage", "ObjectStorage" or an array. '
208  . 'given: ' . get_class($this->objects),
209  1385547291
210  );
211  }
212 }
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\initializeAction
‪initializeAction()
Definition: PaginateController.php:66
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\indexAction
‪indexAction($currentPage=1)
Definition: PaginateController.php:78
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller
Definition: AutocompleteController.php:16
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$displayRangeStart
‪int $displayRangeStart
Definition: PaginateController.php:57
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$currentPage
‪int $currentPage
Definition: PaginateController.php:45
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$displayRangeEnd
‪int $displayRangeEnd
Definition: PaginateController.php:61
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$maximumNumberOfLinks
‪int $maximumNumberOfLinks
Definition: PaginateController.php:49
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$configuration
‪array $configuration
Definition: PaginateController.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$numberOfPages
‪int $numberOfPages
Definition: PaginateController.php:53
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController
Definition: PaginateController.php:27
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\calculateDisplayRange
‪calculateDisplayRange()
Definition: PaginateController.php:111
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\$objects
‪QueryResultInterface ObjectStorage array $objects
Definition: PaginateController.php:41
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\buildPagination
‪array buildPagination()
Definition: PaginateController.php:136
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController\prepareObjectsSlice
‪array QueryResultInterface prepareObjectsSlice($itemsPerPage, $offset)
Definition: PaginateController.php:168
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
Definition: AbstractWidgetController.php:32