[FEATURE] Backend paginate widget
[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 /**
22
23 */
24 class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController {
25
26 /**
27 * @var array
28 */
29 protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE, 'recordsLabel' => '');
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
33 */
34 protected $objects;
35
36 /**
37 * @var integer
38 */
39 protected $currentPage = 1;
40
41 /**
42 * @var integer
43 */
44 protected $numberOfPages = 1;
45
46 /**
47 * @var integer
48 */
49 protected $offset = 0;
50
51 /**
52 * @var integer
53 */
54 protected $itemsPerPage = 0;
55
56 /**
57 * @var integer
58 */
59 protected $numberOfObjects = 0;
60
61 /**
62 * @return void
63 */
64 public function initializeAction() {
65 $this->objects = $this->widgetConfiguration['objects'];
66 $this->configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->configuration, $this->widgetConfiguration['configuration'], TRUE);
67 $this->numberOfObjects = count($this->objects);
68 $this->numberOfPages = ceil($this->numberOfObjects / (int) $this->configuration['itemsPerPage']);
69 }
70
71 /**
72 * @param integer $currentPage
73 * @return void
74 */
75 public function indexAction($currentPage = 1) {
76 // set current page
77 $this->currentPage = (int) $currentPage;
78 if ($this->currentPage < 1) {
79 $this->currentPage = 1;
80 }
81 if ($this->currentPage > $this->numberOfPages) {
82 // set $modifiedObjects to NULL if the page does not exist
83 $modifiedObjects = NULL;
84 } else {
85 // modify query
86 $this->itemsPerPage = (int) $this->configuration['itemsPerPage'];
87 $query = $this->objects->getQuery();
88 $query->setLimit($this->itemsPerPage);
89 $this->offset = $this->itemsPerPage * ($this->currentPage - 1);
90 if ($this->currentPage > 1) {
91 $query->setOffset($this->offset);
92 }
93 $modifiedObjects = $query->execute();
94 }
95 $this->view->assign('contentArguments', array(
96 $this->widgetConfiguration['as'] => $modifiedObjects
97 ));
98 $this->view->assign('configuration', $this->configuration);
99 $this->view->assign('pagination', $this->buildPagination());
100 }
101
102 /**
103 * Returns an array with the keys "current", "numberOfPages", "nextPage", "previousPage", "startRecord", "endRecord"
104 *
105 * @return array
106 */
107 protected function buildPagination() {
108 $endRecord = $this->offset + $this->itemsPerPage + 1;
109 if ($endRecord > $this->numberOfObjects) {
110 $endRecord = $this->numberOfObjects;
111 }
112 $pagination = array(
113 'current' => $this->currentPage,
114 'numberOfPages' => $this->numberOfPages,
115 'hasLessPages' => $this->currentPage > 1,
116 'hasMorePages' => $this->currentPage < $this->numberOfPages,
117 'startRecord' => $this->offset + 1,
118 'endRecord' => $endRecord
119 );
120 if ($this->currentPage < $this->numberOfPages) {
121 $pagination['nextPage'] = $this->currentPage + 1;
122 }
123 if ($this->currentPage > 1) {
124 $pagination['previousPage'] = $this->currentPage - 1;
125 }
126 return $pagination;
127 }
128
129 }
130
131
132 ?>