1<?php 2 3/* 4 * This file is part of the TYPO3 CMS project. 5 * 6 * It is free software; you can redistribute it and/or modify it under 7 * the terms of the GNU General Public License, either version 2 8 * of the License, or any later version. 9 * 10 * For the full copyright and license information, please read the 11 * LICENSE.txt file that was distributed with this source code. 12 * 13 * The TYPO3 project - inspiring people to share! 14 */ 15 16namespace TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller; 17 18use TYPO3\CMS\Core\Utility\ArrayUtility; 19use TYPO3\CMS\Extbase\Persistence\ObjectStorage; 20use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; 21use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController; 22 23/** 24 * Class PaginateController 25 */ 26class PaginateController extends AbstractWidgetController 27{ 28 /** 29 * @var array 30 */ 31 protected $configuration = [ 32 'itemsPerPage' => 10, 33 'insertAbove' => false, 34 'insertBelow' => true, 35 'maximumNumberOfLinks' => 99, 36 'addQueryStringMethod' => '', 37 'section' => '' 38 ]; 39 40 /** 41 * @var QueryResultInterface|ObjectStorage|array 42 */ 43 protected $objects; 44 45 /** 46 * @var int 47 */ 48 protected $currentPage = 1; 49 50 /** 51 * @var int 52 */ 53 protected $maximumNumberOfLinks = 99; 54 55 /** 56 * @var int 57 */ 58 protected $numberOfPages = 1; 59 60 /** 61 * @var int 62 */ 63 protected $displayRangeStart; 64 65 /** 66 * @var int 67 */ 68 protected $displayRangeEnd; 69 70 /** 71 * Initializes the current information on which page the visitor is. 72 */ 73 public function initializeAction() 74 { 75 $this->objects = $this->widgetConfiguration['objects']; 76 ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false); 77 $itemsPerPage = (int)$this->configuration['itemsPerPage']; 78 $this->numberOfPages = $itemsPerPage > 0 ? ceil(count($this->objects) / $itemsPerPage) : 0; 79 $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks']; 80 } 81 82 /** 83 * @param int $currentPage 84 */ 85 public function indexAction($currentPage = 1) 86 { 87 // set current page 88 $this->currentPage = (int)$currentPage; 89 if ($this->currentPage < 1) { 90 $this->currentPage = 1; 91 } 92 if ($this->currentPage > $this->numberOfPages) { 93 // set $modifiedObjects to NULL if the page does not exist 94 $modifiedObjects = null; 95 } else { 96 // modify query 97 $itemsPerPage = (int)$this->configuration['itemsPerPage']; 98 $offset = 0; 99 if ($this->objects instanceof QueryResultInterface) { 100 $offset = (int)$this->objects->getQuery()->getOffset(); 101 } 102 if ($this->currentPage > 1) { 103 $offset = $offset + ((int)($itemsPerPage * ($this->currentPage - 1))); 104 } 105 $modifiedObjects = $this->prepareObjectsSlice($itemsPerPage, $offset); 106 } 107 $this->view->assign('contentArguments', [ 108 $this->widgetConfiguration['as'] => $modifiedObjects 109 ]); 110 $this->view->assign('configuration', $this->configuration); 111 $this->view->assign('pagination', $this->buildPagination()); 112 } 113 114 /** 115 * If a certain number of links should be displayed, adjust before and after 116 * amounts accordingly. 117 */ 118 protected function calculateDisplayRange() 119 { 120 $maximumNumberOfLinks = $this->maximumNumberOfLinks; 121 if ($maximumNumberOfLinks > $this->numberOfPages) { 122 $maximumNumberOfLinks = $this->numberOfPages; 123 } 124 $delta = floor($maximumNumberOfLinks / 2); 125 $this->displayRangeStart = $this->currentPage - $delta; 126 $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0); 127 if ($this->displayRangeStart < 1) { 128 $this->displayRangeEnd -= $this->displayRangeStart - 1; 129 } 130 if ($this->displayRangeEnd > $this->numberOfPages) { 131 $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages; 132 } 133 $this->displayRangeStart = (int)max($this->displayRangeStart, 1); 134 $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages); 135 } 136 137 /** 138 * Returns an array with the keys "pages", "current", "numberOfPages", 139 * "nextPage" & "previousPage" 140 * 141 * @return array 142 */ 143 protected function buildPagination() 144 { 145 $this->calculateDisplayRange(); 146 $pages = []; 147 for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) { 148 $pages[] = ['number' => $i, 'isCurrent' => $i === $this->currentPage]; 149 } 150 $pagination = [ 151 'pages' => $pages, 152 'current' => $this->currentPage, 153 'numberOfPages' => $this->numberOfPages, 154 'displayRangeStart' => $this->displayRangeStart, 155 'displayRangeEnd' => $this->displayRangeEnd, 156 'hasLessPages' => $this->displayRangeStart > 2, 157 'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages 158 ]; 159 if ($this->currentPage < $this->numberOfPages) { 160 $pagination['nextPage'] = $this->currentPage + 1; 161 } 162 if ($this->currentPage > 1) { 163 $pagination['previousPage'] = $this->currentPage - 1; 164 } 165 return $pagination; 166 } 167 168 /** 169 * @param int $itemsPerPage 170 * @param int $offset 171 * 172 * @return array|QueryResultInterface 173 * @throws \InvalidArgumentException 174 */ 175 protected function prepareObjectsSlice($itemsPerPage, $offset) 176 { 177 if ($this->objects instanceof QueryResultInterface) { 178 $currentRange = $offset + $itemsPerPage; 179 $endOfRange = min($currentRange, count($this->objects)); 180 $query = $this->objects->getQuery(); 181 $query->setLimit($itemsPerPage); 182 if ($offset > 0) { 183 $query->setOffset($offset); 184 if ($currentRange > $endOfRange) { 185 $newLimit = $endOfRange - $offset; 186 $query->setLimit($newLimit); 187 } 188 } 189 $modifiedObjects = $query->execute(); 190 return $modifiedObjects; 191 } 192 if ($this->objects instanceof ObjectStorage) { 193 $modifiedObjects = []; 194 $objectArray = $this->objects->toArray(); 195 $endOfRange = min($offset + $itemsPerPage, count($objectArray)); 196 for ($i = $offset; $i < $endOfRange; $i++) { 197 $modifiedObjects[] = $objectArray[$i]; 198 } 199 return $modifiedObjects; 200 } 201 if (is_array($this->objects)) { 202 $modifiedObjects = array_slice($this->objects, $offset, $itemsPerPage); 203 return $modifiedObjects; 204 } 205 throw new \InvalidArgumentException( 206 'The ViewHelper "' . static::class 207 . '" accepts as argument "QueryResultInterface", "\SplObjectStorage", "ObjectStorage" or an array. ' 208 . 'given: ' . get_class($this->objects), 209 1385547291 210 ); 211 } 212} 213