Last active
January 2, 2016 01:09
-
-
Save matej21/8228368 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Nette Framework Extras | |
* | |
* This source file is subject to the New BSD License. | |
* @copyright Copyright (c) 2009 David Grudl | |
* @license New BSD License | |
*/ | |
use Nette\Application\UI\Control; | |
use Nette\Utils\Paginator; | |
/** | |
* Visual paginator control. | |
* | |
* @author David Grudl | |
* @copyright Copyright (c) 2009 David Grudl | |
* @package Nette Extras | |
*/ | |
class VisualPaginator extends Control | |
{ | |
/** @var Paginator */ | |
private $paginator; | |
/** @persistent */ | |
public $page = 1; | |
/** | |
* @return Nette\Paginator | |
*/ | |
public function getPaginator() | |
{ | |
if (!$this->paginator) { | |
$this->paginator = new Paginator; | |
} | |
return $this->paginator; | |
} | |
/** | |
* Renders paginator. | |
* @return void | |
*/ | |
public function render() | |
{ | |
$paginator = $this->getPaginator(); | |
$page = $paginator->page; | |
if ($paginator->pageCount < 2) { | |
$steps = array($page); | |
} else { | |
$arr = range(max($paginator->firstPage, $page - 3), min($paginator->lastPage, $page + 3)); | |
$count = 4; | |
$quotient = ($paginator->pageCount - 1) / $count; | |
for ($i = 0; $i <= $count; $i++) { | |
$arr[] = round($quotient * $i) + $paginator->firstPage; | |
} | |
sort($arr); | |
$steps = array_values(array_unique($arr)); | |
} | |
$this->template->steps = $steps; | |
$this->template->paginator = $paginator; | |
$this->template->setFile(__DIR__ . '/template.latte'); | |
$this->template->render(); | |
} | |
/** | |
* Loads state informations. | |
* @param array | |
* @return void | |
*/ | |
public function loadState(array $params) | |
{ | |
parent::loadState($params); | |
$this->getPaginator()->page = $this->page; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment