Revert "[CLEANUP] Rework/simplify copyright header and remove @package"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Widget / Controller / PaginateController.php
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 * */
23 class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController {
24
25 /**
26 * @var array
27 */
28 protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE, 'maximumNumberOfLinks' => 99, 'addQueryStringMethod' => '');
29
30 /**
31 * @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
32 */
33 protected $objects;
34
35 /**
36 * @var integer
37 */
38 protected $currentPage = 1;
39
40 /**
41 * @var integer
42 */
43 protected $maximumNumberOfLinks = 99;
44
45 /**
46 * @var integer
47 */
48 protected $numberOfPages = 1;
49
50 /**
51 * @return void
52 */
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
60 /**
61 * @param integer $currentPage
62 * @return void
63 */
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
90 /**
91 * If a certain number of links should be displayed, adjust before and after
92 * amounts accordingly.
93 *
94 * @return void
95 */
96 protected function calculateDisplayRange() {
97 $maximumNumberOfLinks = $this->maximumNumberOfLinks;
98 if ($maximumNumberOfLinks > $this->numberOfPages) {
99 $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
114 /**
115 * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
116 *
117 * @return array
118 */
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 }