3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Network;
27 use Friendica\Util\Strings;
30 * This pager should be used by lists using the min_id†/max_id† parameters
32 * This pager automatically identifies if the sorting is done increasingly or decreasingly if the first item id†
33 * and last item id† are different. Otherwise it defaults to decreasingly like reverse chronological lists.
35 * † In this context, "id" refers to the value of the column that the item list is ordered by.
37 class BoundariesPager extends Pager
39 protected $first_item_id;
40 protected $last_item_id;
41 protected $first_page = true;
44 * Instantiates a new Pager with the base parameters.
47 * @param string $queryString The query string of the current page
48 * @param string $first_item_id The id† of the first item in the displayed item list
49 * @param string $last_item_id The id† of the last item in the displayed item list
50 * @param integer $itemsPerPage An optional number of items per page to override the default value
52 public function __construct(L10n $l10n, $queryString, $first_item_id = null, $last_item_id = null, $itemsPerPage = 50)
54 parent::__construct($l10n, $queryString, $itemsPerPage);
56 $this->first_item_id = $first_item_id;
57 $this->last_item_id = $last_item_id;
59 $parsed = parse_url($this->getBaseQueryString());
60 if (!empty($parsed['query'])) {
61 parse_str($parsed['query'], $queryParameters);
63 $this->first_page = !($queryParameters['min_id'] ?? null) && !($queryParameters['max_id'] ?? null);
65 unset($queryParameters['min_id']);
66 unset($queryParameters['max_id']);
68 $parsed['query'] = http_build_query($queryParameters);
70 $url = Network::unparseURL($parsed);
72 $this->setQueryString($url);
76 public function getStart()
78 throw new \BadMethodCallException();
81 public function getPage()
83 throw new \BadMethodCallException();
87 * Minimal pager (newer/older)
89 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
90 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
94 * $params = ['order' => ['sort_field' => true], 'limit' => $itemsPerPage];
95 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
97 * $pager = new BoundariesPager($a->query_string, $items[0]['sort_field'], $items[coutn($items) - 1]['sort_field'], $itemsPerPage);
99 * $html = $pager->renderMinimal(count($items));
101 * @param int $itemCount The number of displayed items on the page
102 * @return string HTML string of the pager
105 public function renderMinimal(int $itemCount)
107 $displayedItemCount = max(0, intval($itemCount));
112 'url' => Strings::ensureQueryParameter($this->baseQueryString .
113 ($this->first_item_id >= $this->last_item_id ?
114 '&min_id=' . $this->first_item_id : '&max_id=' . $this->first_item_id)
116 'text' => $this->l10n->t('newer'),
117 'class' => 'previous' . ($this->first_page ? ' disabled' : '')
120 'url' => Strings::ensureQueryParameter($this->baseQueryString .
121 ($this->first_item_id >= $this->last_item_id ?
122 '&max_id=' . $this->last_item_id : '&min_id=' . $this->last_item_id)
124 'text' => $this->l10n->t('older'),
125 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
129 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
130 return Renderer::replaceMacros($tpl, ['pager' => $data]);
133 public function renderFull($itemCount)
135 throw new \BadMethodCallException();