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