X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FPager.php;h=c12608d45d84bbfb10173486c59978230f943522;hb=fd706cf9a7a0c4700838a1f00b12d8fd37323b7c;hp=185c5506d9df57f2bfa0f6f70713120704d5ba8c;hpb=f1ebaf5e7d6b276c92c2fb436f5258596a6f0975;p=friendica.git diff --git a/src/Content/Pager.php b/src/Content/Pager.php index 185c5506d9..c12608d45d 100644 --- a/src/Content/Pager.php +++ b/src/Content/Pager.php @@ -3,6 +3,8 @@ 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. @@ -19,10 +21,6 @@ class Pager * @var integer */ private $itemsPerPage = 50; - /** - * @var integer - */ - private $itemCount = 0; /** * @var string @@ -35,15 +33,13 @@ class Pager * Guesses the page number from the GET parameter 'page'. * * @param string $queryString The query string of the current page - * @param integer $itemCount The total item count (for the full mode) or null (for the minimal mode) * @param integer $itemsPerPage An optional number of items per page to override the default value */ - public function __construct($queryString, $itemCount = null, $itemsPerPage = 50) + public function __construct($queryString, $itemsPerPage = 50) { $this->setQueryString($queryString); $this->setItemsPerPage($itemsPerPage); - $this->setItemCount(defaults($itemCount, $this->getItemsPerPage())); - $this->setPage(defaults($_GET, 'page', 1)); + $this->setPage(($_GET['page'] ?? 0) ?: 1); } /** @@ -69,7 +65,7 @@ class Pager /** * Returns the current page number * - * @return type + * @return int */ public function getPage() { @@ -80,14 +76,14 @@ class Pager * Returns the base query string. * * Warning: this isn't the same value as passed to the constructor. - * See setQueryString for the inventory of transformations + * See setQueryString() for the inventory of transformations * * @see setBaseQuery() * @return string */ public function getBaseQueryString() { - return $this->baseQueryString; + return Strings::ensureQueryParameter($this->baseQueryString); } /** @@ -110,16 +106,6 @@ class Pager $this->page = max(1, intval($page)); } - /** - * Sets the item count, 0 minimum. - * - * @param integer $itemCount - */ - public function setItemCount($itemCount) - { - $this->itemCount = max(0, intval($itemCount)); - } - /** * Sets the base query string from a full query string. * @@ -137,21 +123,6 @@ class Pager $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) * @@ -169,27 +140,28 @@ class Pager * * @param integer $itemCount The number of displayed items on the page * @return string HTML string of the pager + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public function renderMinimal($itemCount) { - $this->setItemCount($itemCount); + $displayedItemCount = max(0, intval($itemCount)); $data = [ 'class' => 'pager', 'prev' => [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), 'text' => L10n::t('newer'), 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '') ], 'next' => [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), 'text' => L10n::t('older'), - 'class' => 'next' . ($this->itemCount <= 0 ? ' disabled' : '') + '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]); } /** @@ -209,26 +181,30 @@ class Pager * * $html = $pager->renderFull(); * + * @param integer $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() + public function renderFull($itemCount) { + $totalItemCount = max(0, intval($itemCount)); + $data = []; $data['class'] = 'pagination'; - if ($this->itemCount > $this->getItemsPerPage()) { + if ($totalItemCount > $this->getItemsPerPage()) { $data['first'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'), 'text' => L10n::t('first'), 'class' => $this->getPage() == 1 ? 'disabled' : '' ]; $data['prev'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)), 'text' => L10n::t('prev'), 'class' => $this->getPage() == 1 ? 'disabled' : '' ]; - $numpages = $this->itemCount / $this->getItemsPerPage(); + $numpages = $totalItemCount / $this->getItemsPerPage(); $numstart = 1; $numstop = $numpages; @@ -250,14 +226,14 @@ class Pager ]; } else { $pages[$i] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i), 'text' => $i, 'class' => 'n' ]; } } - if (($this->itemCount % $this->getItemsPerPage()) != 0) { + if (($totalItemCount % $this->getItemsPerPage()) != 0) { if ($i == $this->getPage()) { $pages[$i] = [ 'url' => '#', @@ -266,7 +242,7 @@ class Pager ]; } else { $pages[$i] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i), 'text' => $i, 'class' => 'n' ]; @@ -278,18 +254,18 @@ class Pager $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages); $data['next'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), 'text' => L10n::t('next'), 'class' => $this->getPage() == $lastpage ? 'disabled' : '' ]; $data['last'] = [ - 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage), + 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage), 'text' => 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]); } }