‪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 
20 
25 {
29  protected ‪$configuration = ['itemsPerPage' => 10, 'insertAbove' => false, 'insertBelow' => true, 'recordsLabel' => ''];
30 
34  protected ‪$objects;
35 
39  protected ‪$currentPage = 1;
40 
44  protected ‪$numberOfPages = 1;
45 
49  protected ‪$offset = 0;
50 
54  protected ‪$itemsPerPage = 0;
55 
59  protected ‪$numberOfObjects = 0;
60 
64  public function ‪initializeAction()
65  {
66  $this->objects = $this->widgetConfiguration['objects'];
67  ‪ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
68  $this->numberOfObjects = count($this->objects);
69  ‪$itemsPerPage = (int)$this->configuration['itemsPerPage'];
70  $this->numberOfPages = ‪$itemsPerPage > 0 ? (int)ceil($this->numberOfObjects / ‪$itemsPerPage) : 0;
71  }
72 
76  public function ‪indexAction(‪$currentPage = 1)
77  {
78  // set current page
79  $this->currentPage = max((int)‪$currentPage, 1);
80  $this->currentPage = min($this->numberOfPages, $this->currentPage);
81 
82  if ($this->currentPage > $this->numberOfPages) {
83  // set $modifiedObjects to NULL if the page does not exist
84  // (happens when numberOfPages is zero, not having any items)
85  $modifiedObjects = null;
86  } else {
87  // modify query
88  $this->itemsPerPage = (int)$this->configuration['itemsPerPage'];
89  $query = $this->objects->getQuery();
90  $query->setLimit($this->itemsPerPage);
91  $this->offset = $this->itemsPerPage * ($this->currentPage - 1);
92  if ($this->currentPage > 1) {
93  $query->setOffset($this->offset);
94  }
95  $modifiedObjects = $query->execute();
96  }
97  $this->view->assign('contentArguments', [
98  $this->widgetConfiguration['as'] => $modifiedObjects
99  ]);
100  $this->view->assign('configuration', $this->configuration);
101  $this->view->assign('pagination', $this->‪buildPagination());
102  }
103 
109  protected function ‪buildPagination()
110  {
111  $endRecord = $this->offset + ‪$this->itemsPerPage;
112  if ($endRecord > $this->numberOfObjects) {
113  $endRecord = ‪$this->numberOfObjects;
114  }
115  $pagination = [
116  'current' => ‪$this->currentPage,
117  'numberOfPages' => ‪$this->numberOfPages,
118  'hasLessPages' => $this->currentPage > 1,
119  'hasMorePages' => $this->currentPage < ‪$this->numberOfPages,
120  'startRecord' => $this->offset + 1,
121  'endRecord' => $endRecord
122  ];
123  if ($this->currentPage < $this->numberOfPages) {
124  $pagination['nextPage'] = $this->currentPage + 1;
125  }
126  if ($this->currentPage > 1) {
127  $pagination['previousPage'] = $this->currentPage - 1;
128  }
129  return $pagination;
130  }
131 }
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$objects
‪TYPO3 CMS Extbase Persistence QueryResultInterface $objects
Definition: PaginateController.php:32
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\initializeAction
‪initializeAction()
Definition: PaginateController.php:57
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$itemsPerPage
‪int $itemsPerPage
Definition: PaginateController.php:48
‪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\Be\Widget\Controller\PaginateController
Definition: PaginateController.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\indexAction
‪indexAction($currentPage=1)
Definition: PaginateController.php:69
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$offset
‪int $offset
Definition: PaginateController.php:44
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$configuration
‪array $configuration
Definition: PaginateController.php:28
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$numberOfPages
‪int $numberOfPages
Definition: PaginateController.php:40
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$currentPage
‪int $currentPage
Definition: PaginateController.php:36
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\$numberOfObjects
‪int $numberOfObjects
Definition: PaginateController.php:52
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller
Definition: PaginateController.php:16
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller\PaginateController\buildPagination
‪array buildPagination()
Definition: PaginateController.php:102
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
Definition: AbstractWidgetController.php:32