|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Grido (http://grido.bugyik.cz) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Grido\Components; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Paginating grid. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Grido |
|
18
|
|
|
* @subpackage Components |
|
19
|
|
|
* @author Petr Bugyík |
|
20
|
|
|
* |
|
21
|
|
|
* @property-read int $page |
|
22
|
|
|
* @property-read array $steps |
|
23
|
|
|
* @property-read int $countEnd |
|
24
|
|
|
* @property-read int $countBegin |
|
25
|
|
|
* @property-write \Grido\Grid $grid |
|
26
|
|
|
*/ |
|
27
|
|
|
class Paginator extends \Nette\Utils\Paginator |
|
28
|
1 |
|
{ |
|
29
|
|
|
const DEFAULT_STEP_COUNT = 4; |
|
30
|
|
|
const DEFAULT_STEP_RANGE = 3; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int */ |
|
33
|
|
|
protected $page; |
|
34
|
|
|
|
|
35
|
|
|
/** @var array */ |
|
36
|
|
|
protected $steps = []; |
|
37
|
|
|
|
|
38
|
|
|
/** @var int */ |
|
39
|
|
|
protected $countBegin; |
|
40
|
|
|
|
|
41
|
|
|
/** @var int */ |
|
42
|
|
|
protected $countEnd; |
|
43
|
|
|
|
|
44
|
|
|
/** @var \Grido\Grid */ |
|
45
|
|
|
protected $grid; |
|
46
|
|
|
|
|
47
|
|
|
/** @var int */ |
|
48
|
|
|
private $stepCount = self::DEFAULT_STEP_COUNT; |
|
49
|
|
|
|
|
50
|
|
|
/** @var int */ |
|
51
|
|
|
private $stepRange = self::DEFAULT_STEP_RANGE; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \Grido\Grid $grid |
|
55
|
|
|
* @return Paginator |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setGrid(\Grido\Grid $grid) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->grid = $grid; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param int $stepRange |
|
65
|
|
|
* @return Paginator |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setStepRange($stepRange) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$this->stepRange = $stepRange; |
|
70
|
1 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param int $stepCount |
|
75
|
|
|
* @return Paginator |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setStepCount($stepCount) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->stepCount = (int) $stepCount; |
|
80
|
1 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/**********************************************************************************************/ |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getPage() |
|
89
|
|
|
{ |
|
90
|
1 |
|
if ($this->page === NULL) { |
|
91
|
1 |
|
$this->page = parent::getPage(); |
|
|
|
|
|
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return $this->page; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getSteps() |
|
101
|
|
|
{ |
|
102
|
1 |
|
if (empty($this->steps)) { |
|
103
|
1 |
|
$arr = range( |
|
104
|
1 |
|
max($this->getFirstPage(), $this->getPage() - $this->stepRange), |
|
105
|
1 |
|
min($this->getLastPage(), $this->getPage() + $this->stepRange) |
|
106
|
1 |
|
); |
|
107
|
|
|
|
|
108
|
1 |
|
$quotient = ($this->getPageCount() - 1) / $this->stepCount; |
|
109
|
|
|
|
|
110
|
1 |
|
for ($i = 0; $i <= $this->stepCount; $i++) { |
|
111
|
1 |
|
$arr[] = (int) (round($quotient * $i) + $this->getFirstPage()); |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
sort($arr); |
|
115
|
1 |
|
$this->steps = array_values(array_unique($arr)); |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return $this->steps; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return int |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getCountBegin() |
|
125
|
|
|
{ |
|
126
|
|
|
if ($this->countBegin === NULL) { |
|
127
|
|
|
$this->countBegin = $this->grid->getCount() > 0 ? $this->getOffset() + 1 : 0; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this->countBegin; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return int |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getCountEnd() |
|
137
|
|
|
{ |
|
138
|
|
|
if ($this->countEnd === NULL) { |
|
139
|
|
|
$this->countEnd = $this->grid->getCount() > 0 |
|
140
|
|
|
? min($this->grid->getCount(), $this->getPage() * $this->grid->getPerPage()) |
|
141
|
|
|
: 0; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->countEnd; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.