3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content;
24 use Friendica\Core\L10n;
25 use Friendica\Core\Renderer;
26 use Friendica\Util\Strings;
29 * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
36 protected $itemsPerPage = 50;
38 protected $baseQueryString = '';
44 * Instantiates a new Pager with the base parameters.
46 * Guesses the page number from the GET parameter 'page'.
49 * @param string $queryString The query string of the current page
50 * @param integer $itemsPerPage An optional number of items per page to override the default value
52 public function __construct(L10n $l10n, $queryString, $itemsPerPage = 50)
56 $this->setQueryString($queryString);
57 $this->setItemsPerPage($itemsPerPage);
58 $this->setPage(($_GET['page'] ?? 0) ?: 1);
62 * Returns the start offset for a LIMIT clause. Starts at 0.
66 public function getStart()
68 return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
72 * Returns the number of items per page
76 public function getItemsPerPage()
78 return $this->itemsPerPage;
82 * Returns the current page number
86 public function getPage()
92 * Returns the base query string.
94 * Warning: this isn't the same value as passed to the constructor.
95 * See setQueryString() for the inventory of transformations
100 public function getBaseQueryString()
102 return Strings::ensureQueryParameter($this->baseQueryString);
106 * Sets the number of items per page, 1 minimum.
108 * @param integer $itemsPerPage
110 public function setItemsPerPage($itemsPerPage)
112 $this->itemsPerPage = max(1, intval($itemsPerPage));
116 * Sets the current page number. Starts at 1.
118 * @param integer $page
120 public function setPage($page)
122 $this->page = max(1, intval($page));
126 * Sets the base query string from a full query string.
128 * Strips the 'page' parameter, and remove the 'q=' string for some reason.
130 * @param string $queryString
132 public function setQueryString($queryString)
134 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
136 $stripped = trim($stripped, '/');
138 $this->baseQueryString = $stripped;
142 * Minimal pager (newer/older)
144 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
145 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
149 * $pager = new Pager($a->query_string);
151 * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
152 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
154 * $html = $pager->renderMinimal(count($items));
156 * @param int $itemCount The number of displayed items on the page
157 * @return string HTML string of the pager
160 public function renderMinimal(int $itemCount)
162 $displayedItemCount = max(0, intval($itemCount));
167 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
168 'text' => $this->l10n->t('newer'),
169 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
172 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
173 'text' => $this->l10n->t('older'),
174 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
178 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
179 return Renderer::replaceMacros($tpl, ['pager' => $data]);
183 * Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
185 * This mode presents page numbers as well as first, previous, next and last links.
186 * The itemCount is the total number of items including those not displayed.
190 * $total = DBA::count($table, $condition);
192 * $pager = new Pager($a->query_string, $total);
194 * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
195 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
197 * $html = $pager->renderFull();
199 * @param integer $itemCount The total number of items including those note displayed on the page
200 * @return string HTML string of the pager
201 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
203 public function renderFull($itemCount)
205 $totalItemCount = max(0, intval($itemCount));
209 $data['class'] = 'pagination';
210 if ($totalItemCount > $this->getItemsPerPage()) {
212 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'),
213 'text' => $this->l10n->t('first'),
214 'class' => $this->getPage() == 1 ? 'disabled' : ''
217 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
218 'text' => $this->l10n->t('prev'),
219 'class' => $this->getPage() == 1 ? 'disabled' : ''
222 $numpages = $totalItemCount / $this->getItemsPerPage();
225 $numstop = $numpages;
227 // Limit the number of displayed page number buttons.
229 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
230 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
235 for ($i = $numstart; $i <= $numstop; $i++) {
236 if ($i == $this->getPage()) {
240 'class' => 'current active'
244 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
251 if (($totalItemCount % $this->getItemsPerPage()) != 0) {
252 if ($i == $this->getPage()) {
256 'class' => 'current active'
260 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
267 $data['pages'] = $pages;
269 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
272 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
273 'text' => $this->l10n->t('next'),
274 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
277 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
278 'text' => $this->l10n->t('last'),
279 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
283 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
284 return Renderer::replaceMacros($tpl, ['pager' => $data]);