Skip to content

Commit

Permalink
If current page is bigger than page counter, replace it with max. ava…
Browse files Browse the repository at this point in the history
…ilable. Closes KnpLabs#118 KnpLabs#120

Cleanup code a bit
  • Loading branch information
stloyd committed Nov 16, 2012
1 parent 959d1dd commit 0d719a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Pagination/SlidingPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ public function sortable($title, $key, $options = array(), $params = array(), $t
} else {
$options['class'] = 'sortable';
}

if (is_array($title) && array_key_exists($direction, $title)) {
$title = $title[$direction];
}

$params = array_merge(
$params,
array(
Expand Down Expand Up @@ -179,6 +181,10 @@ public function getPaginationData()
$pageCount = intval(ceil($this->totalCount / $this->numItemsPerPage));
$current = $this->currentPageNumber;

if ($pageCount < $current) {
$this->currentPageNumber = $current = $pageCount;
}

if ($this->pageRange > $pageCount) {
$this->pageRange = $pageCount;
}
Expand Down Expand Up @@ -213,6 +219,7 @@ public function getPaginationData()
if ($current + 1 <= $pageCount) {
$viewData['next'] = $current + 1;
}

$viewData['pagesInRange'] = $pages;
$viewData['firstPageInRange'] = min($pages);
$viewData['lastPageInRange'] = max($pages);
Expand Down
18 changes: 10 additions & 8 deletions Subscriber/SlidingPaginationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ public function __construct(EngineInterface $engine, RouterHelper $routerHelper,

public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$request = $event->getRequest();
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}

$request = $event->getRequest();

$this->route = $request->attributes->get('_route');
$this->params = array_merge($request->query->all(), $request->attributes->all());
foreach ($this->params as $key => $param) {
if (substr($key, 0, 1) == '_') {
unset($this->params[$key]);
}
$this->route = $request->attributes->get('_route');
$this->params = array_merge($request->query->all(), $request->attributes->all());
foreach ($this->params as $key => $param) {
if (substr($key, 0, 1) == '_') {
unset($this->params[$key]);
}
}
}
Expand Down

0 comments on commit 0d719a9

Please sign in to comment.