friendica/src/Content/BoundariesPager.php

141 lines
4.6 KiB
PHP
Raw Normal View History

2020-02-16 08:23:27 +00:00
<?php
/**
2023-01-01 14:36:24 +00:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-02-16 08:23:27 +00:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Content;
2020-02-16 16:53:52 +00:00
use Friendica\Core\L10n;
2020-02-16 08:23:27 +00:00
use Friendica\Core\Renderer;
use Friendica\Util\Network;
use Friendica\Util\Strings;
/**
* This pager should be used by lists using the min_id†/max_id† parameters
2020-02-16 08:23:27 +00:00
*
2020-02-16 16:53:52 +00:00
* This pager automatically identifies if the sorting is done increasingly or decreasingly if the first item id†
* and last item id† are different. Otherwise it defaults to decreasingly like reverse chronological lists.
*
* In this context, "id" refers to the value of the column that the item list is ordered by.
2020-02-16 08:23:27 +00:00
*/
class BoundariesPager extends Pager
{
protected $first_item_id;
protected $last_item_id;
protected $first_page = true;
/**
* Instantiates a new Pager with the base parameters.
*
2020-02-16 16:53:52 +00:00
* @param L10n $l10n
2020-02-16 08:23:27 +00:00
* @param string $queryString The query string of the current page
2020-02-16 16:53:52 +00:00
* @param string $first_item_id The id† of the first item in the displayed item list
* @param string $last_item_id The id† of the last item in the displayed item list
* @param integer $itemsPerPage An optional number of items per page to override the default value
2020-02-16 08:23:27 +00:00
*/
public function __construct(L10n $l10n, string $queryString, string $first_item_id = null, string $last_item_id = null, int $itemsPerPage = 50)
2020-02-16 08:23:27 +00:00
{
2020-02-16 16:53:52 +00:00
parent::__construct($l10n, $queryString, $itemsPerPage);
2020-02-16 08:23:27 +00:00
$this->first_item_id = $first_item_id;
$this->last_item_id = $last_item_id;
$parsed = parse_url($this->getBaseQueryString());
if (!empty($parsed['query'])) {
2020-02-16 08:23:27 +00:00
parse_str($parsed['query'], $queryParameters);
$this->first_page = !($queryParameters['min_id'] ?? null) && !($queryParameters['max_id'] ?? null);
2020-02-16 08:23:27 +00:00
unset($queryParameters['min_id']);
2020-02-16 08:23:27 +00:00
unset($queryParameters['max_id']);
$parsed['query'] = http_build_query($queryParameters);
$url = Network::unparseURL($parsed);
$this->setQueryString($url);
}
}
2022-07-01 00:50:03 +00:00
public function getStart(): int
2020-02-16 08:23:27 +00:00
{
throw new \BadMethodCallException();
}
2022-07-01 00:50:03 +00:00
public function getPage(): int
2020-02-16 08:23:27 +00:00
{
throw new \BadMethodCallException();
}
/**
* Minimal pager (newer/older)
*
* This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
* The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
*
* Example usage:
*
* $params = ['order' => ['sort_field' => true], 'limit' => $itemsPerPage];
* $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
*
* $pager = new BoundariesPager($a->query_string, $items[0]['sort_field'], $items[count($items) - 1]['sort_field'], $itemsPerPage);
2020-02-16 08:23:27 +00:00
*
* $html = $pager->renderMinimal(count($items));
*
* @param int $itemCount The number of displayed items on the page
* @return string HTML string of the pager
* @throws \Exception
*/
public function renderMinimal(int $itemCount): string
2020-02-16 08:23:27 +00:00
{
$displayedItemCount = max(0, intval($itemCount));
$data = [
'class' => 'pager',
'prev' => [
'url' => Strings::ensureQueryParameter($this->baseQueryString .
($this->first_item_id >= $this->last_item_id ?
'&min_id=' . $this->first_item_id : '&max_id=' . $this->first_item_id)
2020-02-16 08:23:27 +00:00
),
2020-02-16 16:53:52 +00:00
'text' => $this->l10n->t('newer'),
2020-02-16 08:23:27 +00:00
'class' => 'previous' . ($this->first_page ? ' disabled' : '')
],
'next' => [
'url' => Strings::ensureQueryParameter($this->baseQueryString .
($this->first_item_id >= $this->last_item_id ?
'&max_id=' . $this->last_item_id : '&min_id=' . $this->last_item_id)
2020-02-16 08:23:27 +00:00
),
2020-02-16 16:53:52 +00:00
'text' => $this->l10n->t('older'),
2020-02-16 08:23:27 +00:00
'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
]
];
$tpl = Renderer::getMarkupTemplate('paginate.tpl');
return Renderer::replaceMacros($tpl, ['pager' => $data]);
}
/**
* Unsupported method, must be type-compatible
*/
public function renderFull(int $itemCount): string
2020-02-16 08:23:27 +00:00
{
throw new \BadMethodCallException();
}
}