|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Control.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license http://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
|
8
|
|
|
* @package iPublikuj:VisualPaginator! |
|
9
|
|
|
* @subpackage Components |
|
10
|
|
|
* @since 5.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 12.03.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace IPub\VisualPaginator\Components; |
|
16
|
|
|
|
|
17
|
|
|
use Nette; |
|
18
|
|
|
use Nette\Application; |
|
19
|
|
|
use Nette\Localization; |
|
20
|
|
|
use Nette\Utils; |
|
21
|
|
|
|
|
22
|
|
|
use IPub; |
|
23
|
|
|
use IPub\VisualPaginator; |
|
24
|
|
|
use IPub\VisualPaginator\Exceptions; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Visual paginator control |
|
28
|
|
|
* |
|
29
|
|
|
* @package iPublikuj:VisualPaginator! |
|
30
|
|
|
* @subpackage Components |
|
31
|
|
|
* |
|
32
|
|
|
* @method onShowPage(Control $self, $page) |
|
33
|
|
|
* |
|
34
|
|
|
* @property Application\UI\ITemplate $template |
|
35
|
|
|
*/ |
|
36
|
|
|
class Control extends Application\UI\Control |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @persistent int |
|
40
|
|
|
*/ |
|
41
|
|
|
public $page = 1; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Events |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
public $onShowPage; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var Utils\Paginator |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $paginator; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $templateFile; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var int |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $displayRelatedPages; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var Localization\ITranslator |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $translator; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var bool |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $useAjax = TRUE; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Localization\ITranslator $translator |
|
77
|
|
|
*/ |
|
78
|
|
|
public function injectTranslator(Localization\ITranslator $translator = NULL) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->translator = $translator; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param NULL|string $templateFile |
|
85
|
|
|
* @param Nette\ComponentModel\IContainer $parent |
|
86
|
|
|
* @param null $name |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __construct( |
|
89
|
|
|
$templateFile = NULL, |
|
90
|
|
|
$displayRelatedPages = NULL, |
|
91
|
|
|
Nette\ComponentModel\IContainer $parent = NULL, $name = NULL |
|
92
|
|
|
) { |
|
93
|
|
|
if ($templateFile) { |
|
|
|
|
|
|
94
|
|
|
$this->setTemplateFile($templateFile); |
|
95
|
|
|
} |
|
96
|
|
|
$this->displayRelatedPages = (int)$displayRelatedPages; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Render control |
|
101
|
|
|
*/ |
|
102
|
|
|
public function render() |
|
103
|
|
|
{ |
|
104
|
|
|
// Check if control has template |
|
105
|
|
|
if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) { |
|
|
|
|
|
|
106
|
|
|
// Assign vars to template |
|
107
|
|
|
$this->template->steps = $this->getSteps(); |
|
108
|
|
|
$this->template->paginator = $this->getPaginator(); |
|
109
|
|
|
$this->template->handle = 'showPage!'; |
|
110
|
|
|
$this->template->useAjax = $this->useAjax; |
|
111
|
|
|
|
|
112
|
|
|
// Check if translator is available |
|
113
|
|
|
if ($this->getTranslator() instanceof Localization\ITranslator) { |
|
|
|
|
|
|
114
|
|
|
$this->template->setTranslator($this->getTranslator()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// If template was not defined before... |
|
118
|
|
|
if ($this->template->getFile() === NULL) { |
|
119
|
|
|
// ...try to get base component template file |
|
120
|
|
|
$templateFile = !empty($this->templateFile) ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR .'default.latte'; |
|
121
|
|
|
$this->template->setFile($templateFile); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Render component template |
|
125
|
|
|
$this->template->render(); |
|
126
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
|
|
throw new Exceptions\InvalidStateException('Visual paginator control is without template.'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function enableAjax() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->useAjax = TRUE; |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
|
|
public function disableAjax() |
|
146
|
|
|
{ |
|
147
|
|
|
$this->useAjax = FALSE; |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return Utils\Paginator |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getPaginator() |
|
156
|
|
|
{ |
|
157
|
|
|
// Check if paginator is created |
|
158
|
|
|
if (!$this->paginator) { |
|
159
|
|
|
$this->paginator = new Utils\Paginator; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->paginator; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Change default control template path |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $templateFile |
|
169
|
|
|
* |
|
170
|
|
|
* @return $this |
|
171
|
|
|
* |
|
172
|
|
|
* @throws Exceptions\FileNotFoundException |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setTemplateFile($templateFile) |
|
175
|
|
|
{ |
|
176
|
|
|
// Check if template file exists... |
|
177
|
|
|
if (!is_file($templateFile)) { |
|
178
|
|
|
// ...check if extension template is used |
|
179
|
|
|
if (is_file(__DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR . $templateFile)) { |
|
180
|
|
|
$templateFile = __DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR . $templateFile; |
|
181
|
|
|
|
|
182
|
|
|
} else { |
|
183
|
|
|
// ...if not throw exception |
|
184
|
|
|
throw new Exceptions\FileNotFoundException('Template file "'. $templateFile .'" was not found.'); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->templateFile = $templateFile; |
|
189
|
|
|
|
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param Localization\ITranslator $translator |
|
195
|
|
|
* |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function setTranslator(Localization\ITranslator $translator) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->translator = $translator; |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return Localization\ITranslator|null |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getTranslator() |
|
209
|
|
|
{ |
|
210
|
|
|
if ($this->translator instanceof Localization\ITranslator) { |
|
|
|
|
|
|
211
|
|
|
return $this->translator; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return NULL; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return array |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getSteps() |
|
221
|
|
|
{ |
|
222
|
|
|
// Get Nette paginator |
|
223
|
|
|
$paginator = $this->getPaginator(); |
|
224
|
|
|
|
|
225
|
|
|
// Get actual paginator page |
|
226
|
|
|
$page = $paginator->page; |
|
227
|
|
|
|
|
228
|
|
|
if ($paginator->pageCount < 2) { |
|
229
|
|
|
$steps = [$page]; |
|
230
|
|
|
|
|
231
|
|
|
} else { |
|
232
|
|
|
$relatedPages = $this->displayRelatedPages ?: 3; |
|
233
|
|
|
$arr = range(max($paginator->firstPage, $page - $relatedPages), min($paginator->lastPage, $page + $relatedPages)); |
|
234
|
|
|
$count = 4; |
|
235
|
|
|
$quotient = ($paginator->pageCount - 1) / $count; |
|
236
|
|
|
|
|
237
|
|
|
for ($i = 0; $i <= $count; $i++) { |
|
238
|
|
|
$arr[] = round($quotient * $i) + $paginator->firstPage; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
sort($arr); |
|
242
|
|
|
|
|
243
|
|
|
$steps = array_values(array_unique($arr)); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $steps; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Loads state information |
|
251
|
|
|
* |
|
252
|
|
|
* @param array |
|
253
|
|
|
* |
|
254
|
|
|
* @return void |
|
255
|
|
|
*/ |
|
256
|
|
|
public function loadState(array $params): void |
|
257
|
|
|
{ |
|
258
|
|
|
parent::loadState($params); |
|
259
|
|
|
|
|
260
|
|
|
$this->getPaginator()->page = $this->page; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @param int $page |
|
265
|
|
|
*/ |
|
266
|
|
|
public function handleShowPage($page) |
|
267
|
|
|
{ |
|
268
|
|
|
$this->onShowPage($this, $page); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: