From e90e66a18bc6478d9b67aa76407532ba7867c8f2 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Fri, 21 Jul 2017 10:01:16 -0300 Subject: [PATCH 1/7] Add Header Link Pagination --- application/Config/Pager.php | 3 ++- system/HTTP/Response.php | 19 ++++++++++++++++++- system/Pager/Views/default_header.php | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 system/Pager/Views/default_header.php diff --git a/application/Config/Pager.php b/application/Config/Pager.php index bfb8ec554e4b..5c15ebd45fd1 100644 --- a/application/Config/Pager.php +++ b/application/Config/Pager.php @@ -19,7 +19,8 @@ class Pager extends BaseConfig */ public $templates = [ 'default_full' => 'CodeIgniter\Pager\Views\default_full', - 'default_simple' => 'CodeIgniter\Pager\Views\default_simple' + 'default_simple' => 'CodeIgniter\Pager\Views\default_simple', + 'default_header' => 'CodeIgniter\Pager\Views\default_header' ]; /* diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index 534352f17d79..ece2bbd0a7ed 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -45,7 +45,7 @@ */ class RedirectException extends \Exception { - + } /** @@ -343,6 +343,23 @@ public function setDate(\DateTime $date) //-------------------------------------------------------------------- + /** + * Set the Link Header + * + * @param \CodeIgniter\Pager\Pager $pager + * + * @return Response + */ + public function setLink(Pager $pager) + { + // http://tools.ietf.org/html/rfc5988 + $this->setHeader('Link', $pager->links('default', 'default_header')); + + return $this; + } + + //-------------------------------------------------------------------- + /** * Sets the Content Type header for this response with the mime type * and, optionally, the charset. diff --git a/system/Pager/Views/default_header.php b/system/Pager/Views/default_header.php new file mode 100644 index 000000000000..f83b48931046 --- /dev/null +++ b/system/Pager/Views/default_header.php @@ -0,0 +1,18 @@ +setSurroundCount(0); +$links = ''; +if ($pager->hasPrevious()) +{ + $links .= '<' . $pager->getFirst() . '>; rel="first",'; + $links .= '<' . $pager->getPrevious() . '>; rel="prev"'; +} +if ($pager->hasPrevious() && $pager->hasNext()) +{ + $links .= ','; +} +if ($pager->hasNext()) +{ + $links .= '<' . $pager->getNext() . '>; rel="next",'; + $links .= '<' . $pager->getLast() . '>; rel="last"'; +} +echo $links; From c39416d31e7ab640aa18e969fac4f57e0c0719b6 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Fri, 16 Nov 2018 10:33:32 -0200 Subject: [PATCH 2/7] Update Link header --- application/Config/Pager.php | 2 +- system/HTTP/Response.php | 30 +++++++++++++++++++++++---- system/Pager/Views/default_head.php | 16 ++++++++++++++ system/Pager/Views/default_header.php | 18 ---------------- 4 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 system/Pager/Views/default_head.php delete mode 100644 system/Pager/Views/default_header.php diff --git a/application/Config/Pager.php b/application/Config/Pager.php index 5c15ebd45fd1..9b6ed83cb948 100644 --- a/application/Config/Pager.php +++ b/application/Config/Pager.php @@ -20,7 +20,7 @@ class Pager extends BaseConfig public $templates = [ 'default_full' => 'CodeIgniter\Pager\Views\default_full', 'default_simple' => 'CodeIgniter\Pager\Views\default_simple', - 'default_header' => 'CodeIgniter\Pager\Views\default_header' + 'default_head' => 'CodeIgniter\Pager\Views\default_head', ]; /* diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index 10aa3222320b..1a17b58ecb92 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -40,6 +40,7 @@ use Config\App; use Config\Format; use CodeIgniter\HTTP\Exceptions\HTTPException; +use CodeIgniter\Pager\PagerInterface; /** * Redirect exception @@ -386,14 +387,35 @@ public function setDate(\DateTime $date) /** * Set the Link Header * - * @param \CodeIgniter\Pager\Pager $pager + * @param \CodeIgniter\Pager\PagerInterface $pager + * + * @see http://tools.ietf.org/html/rfc5988 * * @return Response */ - public function setLink(Pager $pager) + public function setLink(PagerInterface $pager) { - // http://tools.ietf.org/html/rfc5988 - $this->setHeader('Link', $pager->links('default', 'default_header')); + //$pager->setSurroundCount(0); + $links = ''; + + if ($previous = $pager->getPreviousPageURI()) + { + $links .= '<' . $pager->getPageURI($pager->getFirstPage()) . '>; rel="first",'; + $links .= '<' . $previous . '>; rel="prev"'; + } + + if (($next = $pager->getNextPageURI()) && $previous) + { + $links .= ','; + } + + if ($next) + { + $links .= '<' . $next . '>; rel="next",'; + $links .= '<' . $pager->getPageURI($pager->getLastPage()) . '>; rel="last"'; + } + + $this->setHeader('Link', $links); return $this; } diff --git a/system/Pager/Views/default_head.php b/system/Pager/Views/default_head.php new file mode 100644 index 000000000000..e09b4456274b --- /dev/null +++ b/system/Pager/Views/default_head.php @@ -0,0 +1,16 @@ +getPreviousPageURI()) +{ + echo '' . PHP_EOL; +} + +echo '' . PHP_EOL; + +if ($pager->getNextPageURI()) +{ + echo '' . PHP_EOL; +} diff --git a/system/Pager/Views/default_header.php b/system/Pager/Views/default_header.php deleted file mode 100644 index f83b48931046..000000000000 --- a/system/Pager/Views/default_header.php +++ /dev/null @@ -1,18 +0,0 @@ -setSurroundCount(0); -$links = ''; -if ($pager->hasPrevious()) -{ - $links .= '<' . $pager->getFirst() . '>; rel="first",'; - $links .= '<' . $pager->getPrevious() . '>; rel="prev"'; -} -if ($pager->hasPrevious() && $pager->hasNext()) -{ - $links .= ','; -} -if ($pager->hasNext()) -{ - $links .= '<' . $pager->getNext() . '>; rel="next",'; - $links .= '<' . $pager->getLast() . '>; rel="last"'; -} -echo $links; From 35870ecf9cdb77b575893553ff0d15f29448bad7 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Sat, 17 Nov 2018 11:03:08 -0200 Subject: [PATCH 3/7] Makes Pager build HTML head and HTTP Header links --- system/HTTP/Response.php | 1 - system/Pager/PagerInterface.php | 24 ++++++++++++++++++++++++ system/Pager/PagerRenderer.php | 23 +++++++++++++++++++++++ system/Pager/Views/default_full.php | 8 +++++++- system/Pager/Views/default_head.php | 14 ++++++++------ system/Pager/Views/default_simple.php | 8 +++++++- 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index 1a17b58ecb92..65dd88a00c0e 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -395,7 +395,6 @@ public function setDate(\DateTime $date) */ public function setLink(PagerInterface $pager) { - //$pager->setSurroundCount(0); $links = ''; if ($previous = $pager->getPreviousPageURI()) diff --git a/system/Pager/PagerInterface.php b/system/Pager/PagerInterface.php index 4c8e2957051a..85cfcdec45dd 100644 --- a/system/Pager/PagerInterface.php +++ b/system/Pager/PagerInterface.php @@ -127,6 +127,19 @@ public function getCurrentPage(string $group = 'default'): int; //-------------------------------------------------------------------- + /** + * Returns the URI for a specific page for the specified group. + * + * @param integer|null $page + * @param string $group + * @param boolean $returnObject + * + * @return string|\CodeIgniter\HTTP\URI + */ + public function getPageURI(int $page = null, string $group = 'default', $returnObject = false); + + //-------------------------------------------------------------------- + /** * Tells whether this group of results has any more pages of results. * @@ -138,6 +151,17 @@ public function hasMore(string $group = 'default'): bool; //-------------------------------------------------------------------- + /** + * Returns the first page. + * + * @param string $group + * + * @return integer + */ + public function getFirstPage(string $group = 'default'); + + //-------------------------------------------------------------------- + /** * Returns the last page, if we have a total that we can calculate with. * diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index 9da20e680024..87ede2809da6 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -223,6 +223,29 @@ public function getLast(): string //-------------------------------------------------------------------- + /** + * Returns the URI of the current page. + * + * @return string + */ + public function getCurrent(): string + { + $uri = clone $this->uri; + + if ($this->segment === 0) + { + $uri->addQuery('page', $this->current); + } + else + { + $uri->setSegment($this->segment, $this->current); + } + + return (string) $uri; + } + + //-------------------------------------------------------------------- + /** * Returns an array of links that should be displayed. Each link * is represented by another array containing of the URI the link diff --git a/system/Pager/Views/default_full.php b/system/Pager/Views/default_full.php index 4eb10aa79235..cc1b8e52b8d7 100644 --- a/system/Pager/Views/default_full.php +++ b/system/Pager/Views/default_full.php @@ -1,4 +1,10 @@ -setSurroundCount(2) ?> +setSurroundCount(2); +?>