TYPO3 CMS  TYPO3_6-2
PaginateController.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
24 
28  protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE, 'maximumNumberOfLinks' => 99, 'addQueryStringMethod' => '');
29 
33  protected $objects;
34 
38  protected $currentPage = 1;
39 
43  protected $maximumNumberOfLinks = 99;
44 
48  protected $numberOfPages = 1;
49 
53  public function initializeAction() {
54  $this->objects = $this->widgetConfiguration['objects'];
55  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], FALSE);
56  $this->numberOfPages = ceil(count($this->objects) / (int)$this->configuration['itemsPerPage']);
57  $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
58  }
59 
64  public function indexAction($currentPage = 1) {
65  // set current page
66  $this->currentPage = (int)$currentPage;
67  if ($this->currentPage < 1) {
68  $this->currentPage = 1;
69  }
70  if ($this->currentPage > $this->numberOfPages) {
71  // set $modifiedObjects to NULL if the page does not exist
72  $modifiedObjects = NULL;
73  } else {
74  // modify query
75  $itemsPerPage = (int)$this->configuration['itemsPerPage'];
76  $query = $this->objects->getQuery();
77  $query->setLimit($itemsPerPage);
78  if ($this->currentPage > 1) {
79  $query->setOffset((int)($itemsPerPage * ($this->currentPage - 1)));
80  }
81  $modifiedObjects = $query->execute();
82  }
83  $this->view->assign('contentArguments', array(
84  $this->widgetConfiguration['as'] => $modifiedObjects
85  ));
86  $this->view->assign('configuration', $this->configuration);
87  $this->view->assign('pagination', $this->buildPagination());
88  }
89 
96  protected function calculateDisplayRange() {
98  if ($maximumNumberOfLinks > $this->numberOfPages) {
100  }
101  $delta = floor($maximumNumberOfLinks / 2);
102  $this->displayRangeStart = $this->currentPage - $delta;
103  $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
104  if ($this->displayRangeStart < 1) {
105  $this->displayRangeEnd -= $this->displayRangeStart - 1;
106  }
107  if ($this->displayRangeEnd > $this->numberOfPages) {
108  $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
109  }
110  $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
111  $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
112  }
113 
119  protected function buildPagination() {
120  $this->calculateDisplayRange();
121  $pages = array();
122  for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
123  $pages[] = array('number' => $i, 'isCurrent' => $i === $this->currentPage);
124  }
125  $pagination = array(
126  'pages' => $pages,
127  'current' => $this->currentPage,
128  'numberOfPages' => $this->numberOfPages,
129  'displayRangeStart' => $this->displayRangeStart,
130  'displayRangeEnd' => $this->displayRangeEnd,
131  'hasLessPages' => $this->displayRangeStart > 2,
132  'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages
133  );
134  if ($this->currentPage < $this->numberOfPages) {
135  $pagination['nextPage'] = $this->currentPage + 1;
136  }
137  if ($this->currentPage > 1) {
138  $pagination['previousPage'] = $this->currentPage - 1;
139  }
140  return $pagination;
141  }
142 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)