[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 * 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 /**
20 * Class PaginateController
21 *
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, 'recordsLabel' => '');
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 $numberOfPages = 1;
44
45 /**
46 * @var integer
47 */
48 protected $offset = 0;
49
50 /**
51 * @var integer
52 */
53 protected $itemsPerPage = 0;
54
55 /**
56 * @var integer
57 */
58 protected $numberOfObjects = 0;
59
60 /**
61 * @return void
62 */
63 public function initializeAction() {
64 $this->objects = $this->widgetConfiguration['objects'];
65 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], FALSE);
66 $this->numberOfObjects = count($this->objects);
67 $this->numberOfPages = ceil($this->numberOfObjects / (int)$this->configuration['itemsPerPage']);
68 }
69
70 /**
71 * @param integer $currentPage
72 * @return void
73 */
74 public function indexAction($currentPage = 1) {
75 // set current page
76 $this->currentPage = (int)$currentPage;
77 if ($this->currentPage < 1) {
78 $this->currentPage = 1;
79 }
80 if ($this->currentPage > $this->numberOfPages) {
81 // set $modifiedObjects to NULL if the page does not exist
82 $modifiedObjects = NULL;
83 } else {
84 // modify query
85 $this->itemsPerPage = (int)$this->configuration['itemsPerPage'];
86 $query = $this->objects->getQuery();
87 $query->setLimit($this->itemsPerPage);
88 $this->offset = $this->itemsPerPage * ($this->currentPage - 1);
89 if ($this->currentPage > 1) {
90 $query->setOffset($this->offset);
91 }
92 $modifiedObjects = $query->execute();
93 }
94 $this->view->assign('contentArguments', array(
95 $this->widgetConfiguration['as'] => $modifiedObjects
96 ));
97 $this->view->assign('configuration', $this->configuration);
98 $this->view->assign('pagination', $this->buildPagination());
99 }
100
101 /**
102 * Returns an array with the keys "current", "numberOfPages", "nextPage", "previousPage", "startRecord", "endRecord"
103 *
104 * @return array
105 */
106 protected function buildPagination() {
107 $endRecord = $this->offset + $this->itemsPerPage;
108 if ($endRecord > $this->numberOfObjects) {
109 $endRecord = $this->numberOfObjects;
110 }
111 $pagination = array(
112 'current' => $this->currentPage,
113 'numberOfPages' => $this->numberOfPages,
114 'hasLessPages' => $this->currentPage > 1,
115 'hasMorePages' => $this->currentPage < $this->numberOfPages,
116 'startRecord' => $this->offset + 1,
117 'endRecord' => $endRecord
118 );
119 if ($this->currentPage < $this->numberOfPages) {
120 $pagination['nextPage'] = $this->currentPage + 1;
121 }
122 if ($this->currentPage > 1) {
123 $pagination['previousPage'] = $this->currentPage - 1;
124 }
125 return $pagination;
126 }
127 }