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\Renderer;
26 use Friendica\Util\Strings;
29 * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
40 private $itemsPerPage = 50;
45 private $baseQueryString = '';
48 * Instantiates a new Pager with the base parameters.
50 * Guesses the page number from the GET parameter 'page'.
52 * @param string $queryString The query string of the current page
53 * @param integer $itemsPerPage An optional number of items per page to override the default value
55 public function __construct($queryString, $itemsPerPage = 50)
57 $this->setQueryString($queryString);
58 $this->setItemsPerPage($itemsPerPage);
59 $this->setPage(($_GET['page'] ?? 0) ?: 1);
63 * Returns the start offset for a LIMIT clause. Starts at 0.
67 public function getStart()
69 return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
73 * Returns the number of items per page
77 public function getItemsPerPage()
79 return $this->itemsPerPage;
83 * Returns the current page number
87 public function getPage()
93 * Returns the base query string.
95 * Warning: this isn't the same value as passed to the constructor.
96 * See setQueryString() for the inventory of transformations
101 public function getBaseQueryString()
103 return Strings::ensureQueryParameter($this->baseQueryString);
107 * Sets the number of items per page, 1 minimum.
109 * @param integer $itemsPerPage
111 public function setItemsPerPage($itemsPerPage)
113 $this->itemsPerPage = max(1, intval($itemsPerPage));
117 * Sets the current page number. Starts at 1.
119 * @param integer $page
121 public function setPage($page)
123 $this->page = max(1, intval($page));
127 * Sets the base query string from a full query string.
129 * Strips the 'page' parameter, and remove the 'q=' string for some reason.
131 * @param string $queryString
133 public function setQueryString($queryString)
135 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
137 $stripped = trim($stripped, '/');
139 $this->baseQueryString = $stripped;
143 * Minimal pager (newer/older)
145 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
146 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
150 * $pager = new Pager($a->query_string);
152 * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
153 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
155 * $html = $pager->renderMinimal(count($items));
157 * @param integer $itemCount The number of displayed items on the page
158 * @return string HTML string of the pager
159 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
161 public function renderMinimal($itemCount)
163 $displayedItemCount = max(0, intval($itemCount));
168 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
169 'text' => DI::l10n()->t('newer'),
170 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
173 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
174 'text' => DI::l10n()->t('older'),
175 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
179 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
180 return Renderer::replaceMacros($tpl, ['pager' => $data]);
184 * Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
186 * This mode presents page numbers as well as first, previous, next and last links.
187 * The itemCount is the total number of items including those not displayed.
191 * $total = DBA::count($table, $condition);
193 * $pager = new Pager($a->query_string, $total);
195 * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
196 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
198 * $html = $pager->renderFull();
200 * @param integer $itemCount The total number of items including those note displayed on the page
201 * @return string HTML string of the pager
202 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
204 public function renderFull($itemCount)
206 $totalItemCount = max(0, intval($itemCount));
210 $data['class'] = 'pagination';
211 if ($totalItemCount > $this->getItemsPerPage()) {
213 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'),
214 'text' => DI::l10n()->t('first'),
215 'class' => $this->getPage() == 1 ? 'disabled' : ''
218 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
219 'text' => DI::l10n()->t('prev'),
220 'class' => $this->getPage() == 1 ? 'disabled' : ''
223 $numpages = $totalItemCount / $this->getItemsPerPage();
226 $numstop = $numpages;
228 // Limit the number of displayed page number buttons.
230 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
231 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
236 for ($i = $numstart; $i <= $numstop; $i++) {
237 if ($i == $this->getPage()) {
241 'class' => 'current active'
245 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
252 if (($totalItemCount % $this->getItemsPerPage()) != 0) {
253 if ($i == $this->getPage()) {
257 'class' => 'current active'
261 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
268 $data['pages'] = $pages;
270 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
273 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
274 'text' => DI::l10n()->t('next'),
275 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
278 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
279 'text' => DI::l10n()->t('last'),
280 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
284 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
285 return Renderer::replaceMacros($tpl, ['pager' => $data]);