X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FPager.php;h=74600a71ce95e677ac60871ee50ac08024d04901;hb=ae2e8beaaca342a0d5ced45beb0fc6c1b4f8a4a4;hp=a95fc7cf656351de1c5a812e8e8b58ec2c516f37;hpb=7ee8bdafc53a88fe51f233e14169bb4af0d376ab;p=friendica.git diff --git a/src/Content/Pager.php b/src/Content/Pager.php index a95fc7cf65..74600a71ce 100644 --- a/src/Content/Pager.php +++ b/src/Content/Pager.php @@ -1,51 +1,72 @@ . + * + */ namespace Friendica\Content; use Friendica\Core\L10n; +use Friendica\Core\Renderer; +use Friendica\Util\Strings; /** * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details. - * - * @author Hypolite Petovan */ class Pager { - /** - * @var integer - */ + /** @var int Default count of items per page */ + const ITEMS_PER_PAGE = 50; + + /** @var integer */ private $page = 1; - /** - * @var integer - */ - private $itemsPerPage = 50; + /** @var integer */ + protected $itemsPerPage = self::ITEMS_PER_PAGE; + /** @var string */ + protected $baseQueryString = ''; - /** - * @var string - */ - private $baseQueryString = ''; + /** @var L10n */ + protected $l10n; /** * Instantiates a new Pager with the base parameters. * * Guesses the page number from the GET parameter 'page'. * - * @param string $queryString The query string of the current page - * @param integer $itemsPerPage An optional number of items per page to override the default value + * @param L10n $l10n + * @param string $queryString The query string of the current page + * @param int $itemsPerPage An optional number of items per page to override the default value */ - public function __construct($queryString, $itemsPerPage = 50) + public function __construct(L10n $l10n, string $queryString, int $itemsPerPage = 50) { + $this->l10n = $l10n; + $this->setQueryString($queryString); $this->setItemsPerPage($itemsPerPage); - $this->setPage(defaults($_GET, 'page', 1)); + $this->setPage((int)($_GET['page'] ?? 0) ?: 1); } /** * Returns the start offset for a LIMIT clause. Starts at 0. * - * @return integer + * @return int */ - public function getStart() + public function getStart(): int { return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage); } @@ -53,9 +74,9 @@ class Pager /** * Returns the number of items per page * - * @return integer + * @return int */ - public function getItemsPerPage() + public function getItemsPerPage(): int { return $this->itemsPerPage; } @@ -63,9 +84,9 @@ class Pager /** * Returns the current page number * - * @return type + * @return int */ - public function getPage() + public function getPage(): int { return $this->page; } @@ -81,15 +102,15 @@ class Pager */ public function getBaseQueryString() { - return $this->baseQueryString; + return Strings::ensureQueryParameter($this->baseQueryString); } /** * Sets the number of items per page, 1 minimum. * - * @param integer $itemsPerPage + * @param int $itemsPerPage */ - public function setItemsPerPage($itemsPerPage) + public function setItemsPerPage(int $itemsPerPage) { $this->itemsPerPage = max(1, intval($itemsPerPage)); } @@ -97,47 +118,31 @@ class Pager /** * Sets the current page number. Starts at 1. * - * @param integer $page + * @param int $page */ - public function setPage($page) + public function setPage(int $page) { - $this->page = max(1, intval($page)); + $this->page = max(1, $page); } /** * Sets the base query string from a full query string. * - * Strips the 'page' parameter, and remove the 'q=' string for some reason. + * Strips the 'page' parameter * * @param string $queryString */ - public function setQueryString($queryString) + public function setQueryString(string $queryString) { $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString); - $stripped = str_replace('q=', '', $stripped); $stripped = trim($stripped, '/'); $this->baseQueryString = $stripped; } /** - * Ensures the provided URI has its query string punctuation in order. - * - * @param string $uri - * @return string - */ - private function ensureQueryParameter($uri) - { - if (strpos($uri, '?') === false && ($pos = strpos($uri, '&')) !== false) { - $uri = substr($uri, 0, $pos) . '?' . substr($uri, $pos + 1); - } - - return $uri; - } - - /** - * @brief Minimal pager (newer/older) + * 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. @@ -151,33 +156,34 @@ class Pager * * $html = $pager->renderMinimal(count($items)); * - * @param integer $itemCount The number of displayed items on the page + * @param int $itemCount The number of displayed items on the page * @return string HTML string of the pager + * @throws \Exception */ - public function renderMinimal($itemCount) + public function renderMinimal(int $itemCount): string { $displayedItemCount = max(0, intval($itemCount)); $data = [ 'class' => 'pager', 'prev' => [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), - 'text' => L10n::t('newer'), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), + 'text' => $this->l10n->t('newer'), 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '') ], 'next' => [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), - 'text' => L10n::t('older'), - 'class' => 'next' . ($displayedItemCount <= 0 ? ' disabled' : '') + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), + 'text' => $this->l10n->t('older'), + 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '') ] ]; - $tpl = get_markup_template('paginate.tpl'); - return replace_macros($tpl, ['pager' => $data]); + $tpl = Renderer::getMarkupTemplate('paginate.tpl'); + return Renderer::replaceMacros($tpl, ['pager' => $data]); } /** - * @brief Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last) + * Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last) * * This mode presents page numbers as well as first, previous, next and last links. * The itemCount is the total number of items including those not displayed. @@ -193,25 +199,26 @@ class Pager * * $html = $pager->renderFull(); * - * @param integer $itemCount The total number of items including those note displayed on the page + * @param int $itemCount The total number of items including those note displayed on the page * @return string HTML string of the pager + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function renderFull($itemCount) + public function renderFull(int $itemCount): string { - $totalItemCount = max(0, intval($itemCount)); + $totalItemCount = max(0, $itemCount); $data = []; $data['class'] = 'pagination'; if ($totalItemCount > $this->getItemsPerPage()) { $data['first'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'), - 'text' => L10n::t('first'), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'), + 'text' => $this->l10n->t('first'), 'class' => $this->getPage() == 1 ? 'disabled' : '' ]; $data['prev'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), - 'text' => L10n::t('prev'), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), + 'text' => $this->l10n->t('prev'), 'class' => $this->getPage() == 1 ? 'disabled' : '' ]; @@ -237,7 +244,7 @@ class Pager ]; } else { $pages[$i] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i), 'text' => $i, 'class' => 'n' ]; @@ -253,7 +260,7 @@ class Pager ]; } else { $pages[$i] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i), 'text' => $i, 'class' => 'n' ]; @@ -265,18 +272,18 @@ class Pager $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages); $data['next'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), - 'text' => L10n::t('next'), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), + 'text' => $this->l10n->t('next'), 'class' => $this->getPage() == $lastpage ? 'disabled' : '' ]; $data['last'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage), - 'text' => L10n::t('last'), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage), + 'text' => $this->l10n->t('last'), 'class' => $this->getPage() == $lastpage ? 'disabled' : '' ]; } - $tpl = get_markup_template('paginate.tpl'); - return replace_macros($tpl, ['pager' => $data]); + $tpl = Renderer::getMarkupTemplate('paginate.tpl'); + return Renderer::replaceMacros($tpl, ['pager' => $data]); } }