TYPO3 CMS  TYPO3_8-7
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  */
16 
21 {
25  protected $configuration = ['itemsPerPage' => 10, 'insertAbove' => false, 'insertBelow' => true, 'recordsLabel' => ''];
26 
30  protected $objects;
31 
35  protected $currentPage = 1;
36 
40  protected $numberOfPages = 1;
41 
45  protected $offset = 0;
46 
50  protected $itemsPerPage = 0;
51 
55  protected $numberOfObjects = 0;
56 
60  public function initializeAction()
61  {
62  $this->objects = $this->widgetConfiguration['objects'];
63  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
64  $this->numberOfObjects = count($this->objects);
65  $itemsPerPage = (int)$this->configuration['itemsPerPage'];
66  $this->numberOfPages = $itemsPerPage > 0 ? ceil($this->numberOfObjects / $itemsPerPage) : 0;
67  }
68 
72  public function indexAction($currentPage = 1)
73  {
74  // set current page
75  $this->currentPage = (int)$currentPage;
76  if ($this->currentPage < 1) {
77  $this->currentPage = 1;
78  }
79  if ($this->currentPage > $this->numberOfPages) {
80  // set $modifiedObjects to NULL if the page does not exist
81  $modifiedObjects = null;
82  } else {
83  // modify query
84  $this->itemsPerPage = (int)$this->configuration['itemsPerPage'];
85  $query = $this->objects->getQuery();
86  $query->setLimit($this->itemsPerPage);
87  $this->offset = $this->itemsPerPage * ($this->currentPage - 1);
88  if ($this->currentPage > 1) {
89  $query->setOffset($this->offset);
90  }
91  $modifiedObjects = $query->execute();
92  }
93  $this->view->assign('contentArguments', [
94  $this->widgetConfiguration['as'] => $modifiedObjects
95  ]);
96  $this->view->assign('configuration', $this->configuration);
97  $this->view->assign('pagination', $this->buildPagination());
98  }
99 
105  protected function buildPagination()
106  {
107  $endRecord = $this->offset + $this->itemsPerPage;
108  if ($endRecord > $this->numberOfObjects) {
109  $endRecord = $this->numberOfObjects;
110  }
111  $pagination = [
112  'current' => $this->currentPage,
113  'numberOfPages' => $this->numberOfPages,
114  'hasLessPages' => $this->currentPage > 1,
115  'hasMorePages' => $this->currentPage < $this->numberOfPages,
116  'startRecord' => $this->offset + 1,
117  'endRecord' => $endRecord
118  ];
119  if ($this->currentPage < $this->numberOfPages) {
120  $pagination['nextPage'] = $this->currentPage + 1;
121  }
122  if ($this->currentPage > 1) {
123  $pagination['previousPage'] = $this->currentPage - 1;
124  }
125  return $pagination;
126  }
127 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)