[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 file is part of the TYPO3 CMS project.
6 *
7 * It is backported from the TYPO3 Flow package "TYPO3.Fluid".
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Utility\ArrayUtility;
20 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
21 use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
22 use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController;
23
24 /**
25 * Class PaginateController
26 */
27 class PaginateController extends AbstractWidgetController {
28
29 /**
30 * @var array
31 */
32 protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE, 'maximumNumberOfLinks' => 99, 'addQueryStringMethod' => '');
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
36 */
37 protected $objects;
38
39 /**
40 * @var integer
41 */
42 protected $currentPage = 1;
43
44 /**
45 * @var integer
46 */
47 protected $maximumNumberOfLinks = 99;
48
49 /**
50 * @var integer
51 */
52 protected $numberOfPages = 1;
53
54 /**
55 * @return void
56 */
57 public function initializeAction() {
58 $this->objects = $this->widgetConfiguration['objects'];
59 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], FALSE);
60 $this->numberOfPages = ceil(count($this->objects) / (int)$this->configuration['itemsPerPage']);
61 $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
62 }
63
64 /**
65 * @param integer $currentPage
66 * @return void
67 */
68 public function indexAction($currentPage = 1) {
69 // set current page
70 $this->currentPage = (int)$currentPage;
71 if ($this->currentPage < 1) {
72 $this->currentPage = 1;
73 }
74 if ($this->currentPage > $this->numberOfPages) {
75 // set $modifiedObjects to NULL if the page does not exist
76 $modifiedObjects = NULL;
77 } else {
78 // modify query
79 $itemsPerPage = (int)$this->configuration['itemsPerPage'];
80 $query = $this->objects->getQuery();
81 $query->setLimit($itemsPerPage);
82 if ($this->currentPage > 1) {
83 $query->setOffset((int)($itemsPerPage * ($this->currentPage - 1)));
84 }
85 $modifiedObjects = $query->execute();
86 }
87 $this->view->assign('contentArguments', array(
88 $this->widgetConfiguration['as'] => $modifiedObjects
89 ));
90 $this->view->assign('configuration', $this->configuration);
91 $this->view->assign('pagination', $this->buildPagination());
92 }
93
94 /**
95 * If a certain number of links should be displayed, adjust before and after
96 * amounts accordingly.
97 *
98 * @return void
99 */
100 protected function calculateDisplayRange() {
101 $maximumNumberOfLinks = $this->maximumNumberOfLinks;
102 if ($maximumNumberOfLinks > $this->numberOfPages) {
103 $maximumNumberOfLinks = $this->numberOfPages;
104 }
105 $delta = floor($maximumNumberOfLinks / 2);
106 $this->displayRangeStart = $this->currentPage - $delta;
107 $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
108 if ($this->displayRangeStart < 1) {
109 $this->displayRangeEnd -= $this->displayRangeStart - 1;
110 }
111 if ($this->displayRangeEnd > $this->numberOfPages) {
112 $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
113 }
114 $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
115 $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
116 }
117
118 /**
119 * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
120 *
121 * @return array
122 */
123 protected function buildPagination() {
124 $this->calculateDisplayRange();
125 $pages = array();
126 for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
127 $pages[] = array('number' => $i, 'isCurrent' => $i === $this->currentPage);
128 }
129 $pagination = array(
130 'pages' => $pages,
131 'current' => $this->currentPage,
132 'numberOfPages' => $this->numberOfPages,
133 'displayRangeStart' => $this->displayRangeStart,
134 'displayRangeEnd' => $this->displayRangeEnd,
135 'hasLessPages' => $this->displayRangeStart > 2,
136 'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages
137 );
138 if ($this->currentPage < $this->numberOfPages) {
139 $pagination['nextPage'] = $this->currentPage + 1;
140 }
141 if ($this->currentPage > 1) {
142 $pagination['previousPage'] = $this->currentPage - 1;
143 }
144 return $pagination;
145 }
146 }