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 = str_replace('q=', '', $stripped);
138 $stripped = trim($stripped, '/');
140 $this->baseQueryString = $stripped;
144 * Minimal pager (newer/older)
146 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
147 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
151 * $pager = new Pager($a->query_string);
153 * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
154 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
156 * $html = $pager->renderMinimal(count($items));
158 * @param integer $itemCount The number of displayed items on the page
159 * @return string HTML string of the pager
160 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
162 public function renderMinimal($itemCount)
164 $displayedItemCount = max(0, intval($itemCount));
169 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
170 'text' => DI::l10n()->t('newer'),
171 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
174 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
175 'text' => DI::l10n()->t('older'),
176 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
180 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
181 return Renderer::replaceMacros($tpl, ['pager' => $data]);
185 * Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
187 * This mode presents page numbers as well as first, previous, next and last links.
188 * The itemCount is the total number of items including those not displayed.
192 * $total = DBA::count($table, $condition);
194 * $pager = new Pager($a->query_string, $total);
196 * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
197 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
199 * $html = $pager->renderFull();
201 * @param integer $itemCount The total number of items including those note displayed on the page
202 * @return string HTML string of the pager
203 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
205 public function renderFull($itemCount)
207 $totalItemCount = max(0, intval($itemCount));
211 $data['class'] = 'pagination';
212 if ($totalItemCount > $this->getItemsPerPage()) {
214 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'),
215 'text' => DI::l10n()->t('first'),
216 'class' => $this->getPage() == 1 ? 'disabled' : ''
219 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
220 'text' => DI::l10n()->t('prev'),
221 'class' => $this->getPage() == 1 ? 'disabled' : ''
224 $numpages = $totalItemCount / $this->getItemsPerPage();
227 $numstop = $numpages;
229 // Limit the number of displayed page number buttons.
231 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
232 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
237 for ($i = $numstart; $i <= $numstop; $i++) {
238 if ($i == $this->getPage()) {
242 'class' => 'current active'
246 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
253 if (($totalItemCount % $this->getItemsPerPage()) != 0) {
254 if ($i == $this->getPage()) {
258 'class' => 'current active'
262 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
269 $data['pages'] = $pages;
271 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
274 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
275 'text' => DI::l10n()->t('next'),
276 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
279 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
280 'text' => DI::l10n()->t('last'),
281 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
285 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
286 return Renderer::replaceMacros($tpl, ['pager' => $data]);