TYPO3  7.6
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  * It is free software; you can redistribute it and/or modify it under *
6  * the terms of the GNU Lesser General Public License, either version 3 *
7  * of the License, or (at your option) any later version. *
8  * *
9  * *
10  * This script is distributed in the hope that it will be useful, but *
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
13  * General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with the script. *
17  * If not, see http://www.gnu.org/licenses/lgpl.html *
18  * *
19  * The TYPO3 project - inspiring people to share! *
20  * */
22 {
26  protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => false, 'insertBelow' => true, 'recordsLabel' => '');
27 
31  protected $objects;
32 
36  protected $currentPage = 1;
37 
41  protected $numberOfPages = 1;
42 
46  protected $offset = 0;
47 
51  protected $itemsPerPage = 0;
52 
56  protected $numberOfObjects = 0;
57 
61  public function initializeAction()
62  {
63  $this->objects = $this->widgetConfiguration['objects'];
64  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
65  $this->numberOfObjects = count($this->objects);
66  $this->numberOfPages = ceil($this->numberOfObjects / (int)$this->configuration['itemsPerPage']);
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', array(
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 = array(
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 }