forked from pvy/nette-visual-paginator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualPaginator.php
77 lines (67 loc) · 1.68 KB
/
VisualPaginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace Unio;
use Nette\Application\UI\Control;
use Nette\Utils\Paginator;
/**
* Visual paginator control.
*
* @author David Grudl
* @copyright Copyright (c) 2009 David Grudl
* @package Nette Extras
* @property array onShowPage Pokud je nastaven nejaky handler, pouzije se ajax. Jinak natvrdo link na 'this'.
*/
class VisualPaginator extends Control
{
/** @persistent */
public $page = 1;
/** @var array */
public $onShowPage;
/** @var Paginator */
private $paginator;
public function handleShowPage($page): void
{
// vyvolat události
$this->onShowPage($this, $page);
}
/**
* Renders paginator.
*/
public function render(): void
{
$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(dirname(__FILE__) . '/template.latte');
$this->template->ajax = !empty($this->onShowPage);
$this->template->render();
}
public function getPaginator(): Paginator
{
if (!$this->paginator) {
$this->paginator = new Paginator;
}
return $this->paginator;
}
/**
* Loads state informations.
*/
public function loadState(array $params): void
{
parent::loadState($params);
$this->getPaginator()->page = $this->page;
}
}