TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Be/Widget/Controller/PaginateController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller;
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 
73  public function indexAction($currentPage = 1)
74  {
75  // set current page
76  $this->currentPage = (int)$currentPage;
77  if ($this->currentPage < 1) {
78  $this->currentPage = 1;
79  }
80  if ($this->currentPage > $this->numberOfPages) {
81  // set $modifiedObjects to NULL if the page does not exist
82  $modifiedObjects = null;
83  } else {
84  // modify query
85  $this->itemsPerPage = (int)$this->configuration['itemsPerPage'];
86  $query = $this->objects->getQuery();
87  $query->setLimit($this->itemsPerPage);
88  $this->offset = $this->itemsPerPage * ($this->currentPage - 1);
89  if ($this->currentPage > 1) {
90  $query->setOffset($this->offset);
91  }
92  $modifiedObjects = $query->execute();
93  }
94  $this->view->assign('contentArguments', [
95  $this->widgetConfiguration['as'] => $modifiedObjects
96  ]);
97  $this->view->assign('configuration', $this->configuration);
98  $this->view->assign('pagination', $this->buildPagination());
99  }
100 
106  protected function buildPagination()
107  {
108  $endRecord = $this->offset + $this->itemsPerPage;
109  if ($endRecord > $this->numberOfObjects) {
110  $endRecord = $this->numberOfObjects;
111  }
112  $pagination = [
113  'current' => $this->currentPage,
114  'numberOfPages' => $this->numberOfPages,
115  'hasLessPages' => $this->currentPage > 1,
116  'hasMorePages' => $this->currentPage < $this->numberOfPages,
117  'startRecord' => $this->offset + 1,
118  'endRecord' => $endRecord
119  ];
120  if ($this->currentPage < $this->numberOfPages) {
121  $pagination['nextPage'] = $this->currentPage + 1;
122  }
123  if ($this->currentPage > 1) {
124  $pagination['previousPage'] = $this->currentPage - 1;
125  }
126  return $pagination;
127  }
128 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)