TYPO3  7.6
Widget/Controller/PaginateController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller;
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  * */
27 
32 {
36  protected $configuration = array(
37  'itemsPerPage' => 10,
38  'insertAbove' => false,
39  'insertBelow' => true,
40  'maximumNumberOfLinks' => 99,
41  'addQueryStringMethod' => '',
42  'section' => ''
43  );
44 
48  protected $objects;
49 
53  protected $currentPage = 1;
54 
58  protected $maximumNumberOfLinks = 99;
59 
63  protected $numberOfPages = 1;
64 
68  protected $displayRangeStart = null;
69 
73  protected $displayRangeEnd = null;
74 
78  public function initializeAction()
79  {
80  $this->objects = $this->widgetConfiguration['objects'];
81  ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
82  $this->numberOfPages = ceil(count($this->objects) / (int)$this->configuration['itemsPerPage']);
83  $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
84  }
85 
90  public function indexAction($currentPage = 1)
91  {
92  // set current page
93  $this->currentPage = (int)$currentPage;
94  if ($this->currentPage < 1) {
95  $this->currentPage = 1;
96  }
97  if ($this->currentPage > $this->numberOfPages) {
98  // set $modifiedObjects to NULL if the page does not exist
99  $modifiedObjects = null;
100  } else {
101  // modify query
102  $itemsPerPage = (int)$this->configuration['itemsPerPage'];
103  $offset = 0;
104  if ($this->currentPage > 1) {
105  $offset = ((int)($itemsPerPage * ($this->currentPage - 1)));
106  }
107  $modifiedObjects = $this->prepareObjectsSlice($itemsPerPage, $offset);
108  }
109  $this->view->assign('contentArguments', array(
110  $this->widgetConfiguration['as'] => $modifiedObjects
111  ));
112  $this->view->assign('configuration', $this->configuration);
113  $this->view->assign('pagination', $this->buildPagination());
114  }
115 
122  protected function calculateDisplayRange()
123  {
125  if ($maximumNumberOfLinks > $this->numberOfPages) {
127  }
128  $delta = floor($maximumNumberOfLinks / 2);
129  $this->displayRangeStart = $this->currentPage - $delta;
130  $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
131  if ($this->displayRangeStart < 1) {
132  $this->displayRangeEnd -= $this->displayRangeStart - 1;
133  }
134  if ($this->displayRangeEnd > $this->numberOfPages) {
135  $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
136  }
137  $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
138  $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
139  }
140 
147  protected function buildPagination()
148  {
149  $this->calculateDisplayRange();
150  $pages = array();
151  for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
152  $pages[] = array('number' => $i, 'isCurrent' => $i === $this->currentPage);
153  }
154  $pagination = array(
155  'pages' => $pages,
156  'current' => $this->currentPage,
157  'numberOfPages' => $this->numberOfPages,
158  'displayRangeStart' => $this->displayRangeStart,
159  'displayRangeEnd' => $this->displayRangeEnd,
160  'hasLessPages' => $this->displayRangeStart > 2,
161  'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages
162  );
163  if ($this->currentPage < $this->numberOfPages) {
164  $pagination['nextPage'] = $this->currentPage + 1;
165  }
166  if ($this->currentPage > 1) {
167  $pagination['previousPage'] = $this->currentPage - 1;
168  }
169  return $pagination;
170  }
171 
179  protected function prepareObjectsSlice($itemsPerPage, $offset)
180  {
181  if ($this->objects instanceof QueryResultInterface) {
182  $query = $this->objects->getQuery();
183  $query->setLimit($itemsPerPage);
184  if ($offset > 0) {
185  $query->setOffset($offset);
186  }
187  $modifiedObjects = $query->execute();
188  return $modifiedObjects;
189  } elseif ($this->objects instanceof ObjectStorage) {
190  $modifiedObjects = array();
191  $endOfRange = $offset + $itemsPerPage;
192  for ($i = $offset; $i < $endOfRange; $i++) {
193  $modifiedObjects[] = $this->objects->toArray()[$i];
194  }
195  return $modifiedObjects;
196  } elseif (is_array($this->objects)) {
197  $modifiedObjects = array_slice($this->objects, $offset, $itemsPerPage);
198  return $modifiedObjects;
199  } else {
200  throw new \InvalidArgumentException('The view helper "' . get_class($this)
201  . '" accepts as argument "QueryResultInterface", "\SplObjectStorage", "ObjectStorage" or an array. '
202  . 'given: ' . get_class($this->objects), 1385547291
203  );
204  }
205  }
206 }