Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / page / MultipleLinkPage.class.php
CommitLineData
158bd3ca
TD
1<?php
2namespace wcf\page;
157054c9 3use wcf\data\DatabaseObjectList;
158bd3ca 4use wcf\system\event\EventHandler;
7b9ff46b 5use wcf\system\exception\ParentClassException;
158bd3ca
TD
6use wcf\system\exception\SystemException;
7use wcf\system\WCF;
158bd3ca
TD
8
9/**
10 * Provides default implementations for a multiple link page.
11 * Handles the page number parameter automatically.
12 *
13 * @author Marcel Werk
c839bd49 14 * @copyright 2001-2018 WoltLab GmbH
158bd3ca 15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 16 * @package WoltLabSuite\Core\Page
158bd3ca
TD
17 */
18abstract class MultipleLinkPage extends AbstractPage {
19 /**
21489986 20 * current page number
9f959ced 21 * @var integer
158bd3ca
TD
22 */
23 public $pageNo = 0;
24
25 /**
21489986 26 * number of all pages
9f959ced 27 * @var integer
158bd3ca
TD
28 */
29 public $pages = 0;
30
31 /**
21489986 32 * number of items shown per page
9f959ced 33 * @var integer
158bd3ca
TD
34 */
35 public $itemsPerPage = 20;
36
37 /**
21489986 38 * number of all items
9f959ced 39 * @var integer
158bd3ca
TD
40 */
41 public $items = 0;
42
43 /**
21489986 44 * indicates the range of the listed items
9f959ced 45 * @var integer
158bd3ca
TD
46 */
47 public $startIndex = 0;
48
49 /**
21489986 50 * indicates the range of the listed items.
9f959ced 51 * @var integer
158bd3ca
TD
52 */
53 public $endIndex = 0;
54
55 /**
56 * DatabaseObjectList object
4e25add7 57 * @var DatabaseObjectList
d726f13d 58 */
158bd3ca
TD
59 public $objectList = null;
60
61 /**
62 * class name for DatabaseObjectList
158bd3ca 63 * @var string
d726f13d 64 */
158bd3ca
TD
65 public $objectListClassName = '';
66
03935e3a
MW
67 /**
68 * selected sort field
9f959ced 69 * @var string
03935e3a
MW
70 */
71 public $sortField = '';
72
73 /**
74 * selected sort order
9f959ced 75 * @var string
03935e3a
MW
76 */
77 public $sortOrder = '';
78
158bd3ca 79 /**
0fcfe5f6 80 * @inheritDoc
d726f13d 81 */
158bd3ca
TD
82 public $sqlLimit = 0;
83
84 /**
0fcfe5f6 85 * @inheritDoc
158bd3ca
TD
86 */
87 public $sqlOffset = '';
88
89 /**
0fcfe5f6 90 * @inheritDoc
d726f13d 91 */
158bd3ca
TD
92 public $sqlOrderBy = '';
93
94 /**
0fcfe5f6 95 * @inheritDoc
158bd3ca
TD
96 */
97 public function readParameters() {
98 parent::readParameters();
99
100 // read page number parameter
101 if (isset($_REQUEST['pageNo'])) $this->pageNo = intval($_REQUEST['pageNo']);
102 }
103
104 /**
0fcfe5f6 105 * @inheritDoc
158bd3ca
TD
106 */
107 public function readData() {
108 parent::readData();
109
110 // initialize database object list
111 $this->initObjectList();
112
dc9c6eb9
TD
113 EventHandler::getInstance()->fireAction($this, 'afterInitObjectList');
114
158bd3ca
TD
115 // calculates page number
116 $this->calculateNumberOfPages();
117
118 // read objects
119 if ($this->items) {
120 $this->sqlLimit = $this->itemsPerPage;
121 $this->sqlOffset = ($this->pageNo - 1) * $this->itemsPerPage;
0e12e80e 122 if ($this->sortField && $this->sortOrder) {
5a827f16 123 if ($this->objectList !== null) {
c4b201a9
TD
124 $alias = $this->objectList->getDatabaseTableAlias();
125 $this->sqlOrderBy = $this->sortField." ".$this->sortOrder.", ".($alias ? $alias."." : "").$this->objectList->getDatabaseTableIndexName()." ".$this->sortOrder;
5a827f16
MS
126 }
127 else {
128 $this->sqlOrderBy = $this->sortField." ".$this->sortOrder;
129 }
0e12e80e 130 }
158bd3ca
TD
131 $this->readObjects();
132 }
133 }
134
135 /**
136 * Initializes DatabaseObjectList instance.
9f959ced 137 */
158bd3ca
TD
138 protected function initObjectList() {
139 if (empty($this->objectListClassName)) {
140 throw new SystemException('DatabaseObjectList class name not specified.');
141 }
142
157054c9 143 if (!is_subclass_of($this->objectListClassName, DatabaseObjectList::class)) {
7b9ff46b 144 throw new ParentClassException($this->objectListClassName, DatabaseObjectList::class);
158bd3ca
TD
145 }
146
147 $this->objectList = new $this->objectListClassName();
148 }
149
150 /**
151 * Reads object list.
9f959ced 152 */
158bd3ca
TD
153 protected function readObjects() {
154 $this->objectList->sqlLimit = $this->sqlLimit;
155 $this->objectList->sqlOffset = $this->sqlOffset;
386e9a01 156 if ($this->sqlOrderBy) $this->objectList->sqlOrderBy = $this->sqlOrderBy;
ed84e780
AE
157
158 EventHandler::getInstance()->fireAction($this, 'beforeReadObjects');
159
158bd3ca 160 $this->objectList->readObjects();
158bd3ca
TD
161 }
162
163 /**
164 * Calculates the number of pages and
165 * handles the given page number parameter.
166 */
167 public function calculateNumberOfPages() {
168 // call calculateNumberOfPages event
169 EventHandler::getInstance()->fireAction($this, 'calculateNumberOfPages');
170
171 // calculate number of pages
172 $this->items = $this->countItems();
173 $this->pages = intval(ceil($this->items / $this->itemsPerPage));
174
175 // correct active page number
176 if ($this->pageNo > $this->pages) $this->pageNo = $this->pages;
177 if ($this->pageNo < 1) $this->pageNo = 1;
178
179 // calculate start and end index
180 $this->startIndex = ($this->pageNo - 1) * $this->itemsPerPage;
181 $this->endIndex = $this->startIndex + $this->itemsPerPage;
182 $this->startIndex++;
183 if ($this->endIndex > $this->items) $this->endIndex = $this->items;
184 }
185
186 /**
187 * Counts the displayed items.
188 *
189 * @return integer
190 */
191 public function countItems() {
192 // call countItems event
193 EventHandler::getInstance()->fireAction($this, 'countItems');
194
195 return $this->objectList->countObjects();
196 }
197
0c5a43ac 198 /**
28410a97 199 * Returns true if current page is the first page.
0c5a43ac
AE
200 *
201 * @return boolean
202 */
203 public function isFirstPage() {
204 return ($this->pageNo == 1);
205 }
206
207 /**
28410a97 208 * Returns true if current page is the last page.
0c5a43ac
AE
209 *
210 * @return boolean
211 */
212 public function isLastPage() {
213 return ($this->items == $this->endIndex);
214 }
215
158bd3ca 216 /**
0fcfe5f6 217 * @inheritDoc
158bd3ca
TD
218 */
219 public function assignVariables() {
220 parent::assignVariables();
221
222 // assign page parameters
058cbd6a 223 WCF::getTPL()->assign([
158bd3ca
TD
224 'pageNo' => $this->pageNo,
225 'pages' => $this->pages,
226 'items' => $this->items,
227 'itemsPerPage' => $this->itemsPerPage,
228 'startIndex' => $this->startIndex,
229 'endIndex' => $this->endIndex,
b153f464 230 'objects' => $this->objectList
058cbd6a 231 ]);
158bd3ca 232 }
dcb3a44c 233}