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\Network;
27 use Friendica\Util\Strings;
30 * This pager should be used by lists using the since_id/max_id parameters
32 * In this context, "id" refers to the value of the column that the list is ordered by.
33 * This pager automatically identifies if the sorting is done increasingly or decreasingly if the first item id
34 * and last item id are different. Otherwise it defaults to decreasingly like reverse chronological lists.
36 class BoundariesPager extends Pager
38 protected $first_item_id;
39 protected $last_item_id;
40 protected $first_page = true;
43 * Instantiates a new Pager with the base parameters.
45 * @param string $queryString The query string of the current page
46 * @param string $first_item_id The i
47 * @param string $last_item_id
48 * @param integer $itemsPerPage An optional number of items per page to override the default value
50 public function __construct($queryString, $first_item_id = null, $last_item_id = null, $itemsPerPage = 50)
52 parent::__construct($queryString, $itemsPerPage);
54 $this->first_item_id = $first_item_id;
55 $this->last_item_id = $last_item_id;
57 $parsed = parse_url($this->getBaseQueryString());
59 parse_str($parsed['query'], $queryParameters);
61 $this->first_page = !($queryParameters['since_id'] ?? null) && !($queryParameters['max_id'] ?? null);
63 unset($queryParameters['since_id']);
64 unset($queryParameters['max_id']);
66 $parsed['query'] = http_build_query($queryParameters);
68 $url = Network::unparseURL($parsed);
70 $this->setQueryString($url);
74 public function getStart()
76 throw new \BadMethodCallException();
79 public function getPage()
81 throw new \BadMethodCallException();
85 * Minimal pager (newer/older)
87 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
88 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
92 * $params = ['order' => ['sort_field' => true], 'limit' => $itemsPerPage];
93 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
95 * $pager = new BoundariesPager($a->query_string, $items[0]['sort_field'], $items[coutn($items) - 1]['sort_field'], $itemsPerPage);
97 * $html = $pager->renderMinimal(count($items));
99 * @param int $itemCount The number of displayed items on the page
100 * @return string HTML string of the pager
103 public function renderMinimal(int $itemCount)
105 $displayedItemCount = max(0, intval($itemCount));
110 'url' => Strings::ensureQueryParameter($this->baseQueryString .
111 ($this->first_item_id >= $this->last_item_id ?
112 '&since_id=' . $this->first_item_id : '&max_id=' . $this->first_item_id)
114 'text' => DI::l10n()->t('newer'),
115 'class' => 'previous' . ($this->first_page ? ' disabled' : '')
118 'url' => Strings::ensureQueryParameter($this->baseQueryString .
119 ($this->first_item_id >= $this->last_item_id ?
120 '&max_id=' . $this->last_item_id : '&since_id=' . $this->last_item_id)
122 'text' => DI::l10n()->t('older'),
123 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
127 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
128 return Renderer::replaceMacros($tpl, ['pager' => $data]);
131 public function renderFull($itemCount)
133 throw new \BadMethodCallException();