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