Revert "[CLEANUP] Rework/simplify copyright header and remove @package"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Be / Widget / Controller / PaginateController.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Be\Widget\Controller;
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 * */
21 class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController {
22
23 /**
24 * @var array
25 */
26 protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE, 'recordsLabel' => '');
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
30 */
31 protected $objects;
32
33 /**
34 * @var integer
35 */
36 protected $currentPage = 1;
37
38 /**
39 * @var integer
40 */
41 protected $numberOfPages = 1;
42
43 /**
44 * @var integer
45 */
46 protected $offset = 0;
47
48 /**
49 * @var integer
50 */
51 protected $itemsPerPage = 0;
52
53 /**
54 * @var integer
55 */
56 protected $numberOfObjects = 0;
57
58 /**
59 * @return void
60 */
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
68 /**
69 * @param integer $currentPage
70 * @return void
71 */
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
99 /**
100 * Returns an array with the keys "current", "numberOfPages", "nextPage", "previousPage", "startRecord", "endRecord"
101 *
102 * @return array
103 */
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 }