1 <?php
2
3 /**
4 * The rex_pager-class implements all the logic
5 * which is necessary to implement some sort of pagination.
6 *
7 * @author staabm
8 *
9 * @package redaxo\core
10 */
11 class rex_pager
12 {
13 private $rowCount;
14 private $rowsPerPage;
15 private $cursorName;
16
17 /**
18 * Constructs a rex_pager.
19 *
20 * @param int $rowsPerPage The number of rows which should be displayed on one page
21 * @param string $cursorName The name of the parameter used for pagination
22 */
23 public function __construct($rowsPerPage = 30, $cursorName = 'start')
24 {
25 $this->rowsPerPage = $rowsPerPage;
26 $this->cursorName = $cursorName;
27 }
28
29 /**
30 * Sets the row count.
31 *
32 * @param int $rowCount
33 */
34 public function setRowCount($rowCount)
35 {
36 $this->rowCount = $rowCount;
37 }
38
39 /**
40 * Returns the number of rows for pagination.
41 *
42 * @return int The number of rows
43 */
44 public function getRowCount()
45 {
46 return $this->rowCount;
47 }
48
49 /**
50 * Returns the number of pages
51 * which result from the given number of rows and the rows per page.
52 *
53 * @return int The number of pages
54 */
55 public function getPageCount()
56 {
57 return $this->getLastPage() + 1;
58 }
59
60 /**
61 * Returns the number of rows which will be displayed on a page.
62 *
63 * @return int The rows displayed on a page
64 */
65 public function getRowsPerPage()
66 {
67 return $this->rowsPerPage;
68 }
69
70 /**
71 * Returns the current pagination position.
72 *
73 * When the parameter pageNo is given, the cursor for the given page is returned.
74 * When no parameter is given, the cursor for active page is returned.
75 *
76 * @param int $pageNo
77 *
78 * @return int
79 */
80 public function getCursor($pageNo = null)
81 {
82 if (null === $pageNo) {
83 $cursor = rex_request($this->cursorName, 'int', 0);
84 } else {
85 $cursor = $pageNo * $this->rowsPerPage;
86 }
87
88 return $cursor;
89 }
90
91 /**
92 * Validates the cursor.
93 *
94 * @param int $cursor
95 *
96 * @return int
97 */
98 public function validateCursor($cursor)
99 {
100 // $cursor innerhalb des zulässigen Zahlenbereichs?
101 if ($cursor < 0) {
102 $cursor = 0;
103 } elseif ($cursor > $this->rowCount) {
104 $cursor = (int) ($this->rowCount / $this->rowsPerPage) * $this->rowsPerPage;
105 }
106
107 return $cursor;
108 }
109
110 /**
111 * Returns the name of the parameter which is used for pagination.
112 *
113 * @return string The name of the cursor
114 */
115 public function getCursorName()
116 {
117 return $this->cursorName;
118 }
119
120 /**
121 * Returns the first page for pagination.
122 *
123 * @return int The first page number
124 */
125 public function getFirstPage()
126 {
127 return 0;
128 }
129
130 /**
131 * Returns the previous page in respect to the current page.
132 *
133 * @return int The previous page number
134 */
135 public function getPrevPage()
136 {
137 $prevPage = $this->getCurrentPage() - 1;
138 if ($prevPage < $this->getFirstPage()) {
139 return $this->getFirstPage();
140 }
141 return $prevPage;
142 }
143
144 /**
145 * Returns the number of the current page.
146 *
147 * @return int The current page number
148 */
149 public function getCurrentPage()
150 {
151 $cursor = rex_request($this->cursorName, 'int', null);
152
153 if (null === $cursor) {
154 return $this->getFirstPage();
155 }
156
157 return (int) ($cursor / $this->rowsPerPage);
158 }
159
160 /**
161 * Returns the number of the next page in respect to the current page.
162 *
163 * @return int The next page number
164 */
165 public function getNextPage()
166 {
167 $nextPage = $this->getCurrentPage() + 1;
168 if ($nextPage > $this->getLastPage()) {
169 return $this->getLastPage();
170 }
171 return $nextPage;
172 }
173
174 /**
175 * Return the page number of the last page.
176 *
177 * @return int the last page number
178 */
179 public function getLastPage()
180 {
181 return ceil($this->rowCount / $this->rowsPerPage) - 1;
182 }
183
184 /**
185 * Checks wheter the given page number is the active/current page.
186 *
187 * @param int $pageNo
188 *
189 * @return bool True when the given pageNo is the current page, otherwise False
190 */
191 public function isActivePage($pageNo)
192 {
193 return $pageNo == $this->getCurrentPage();
194 }
195 }
196