This repository was archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathControl.php
More file actions
270 lines (228 loc) · 5.32 KB
/
Copy pathControl.php
File metadata and controls
270 lines (228 loc) · 5.32 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* Control.php
*
* @copyright More in license.md
* @license http://www.ipublikuj.eu
* @author Adam Kadlec http://www.ipublikuj.eu
* @package iPublikuj:VisualPaginator!
* @subpackage Components
* @since 5.0
*
* @date 12.03.14
*/
namespace IPub\VisualPaginator\Components;
use Nette;
use Nette\Application;
use Nette\Localization;
use Nette\Utils;
use IPub;
use IPub\VisualPaginator;
use IPub\VisualPaginator\Exceptions;
/**
* Visual paginator control
*
* @package iPublikuj:VisualPaginator!
* @subpackage Components
*
* @method onShowPage(Control $self, $page)
*
* @property Application\UI\ITemplate $template
*/
class Control extends Application\UI\Control
{
/**
* @persistent int
*/
public $page = 1;
/**
* Events
*
* @var array
*/
public $onShowPage;
/**
* @var Utils\Paginator
*/
protected $paginator;
/**
* @var string
*/
protected $templateFile;
/**
* @var int
*/
protected $displayRelatedPages;
/**
* @var Localization\ITranslator
*/
protected $translator;
/**
* @var bool
*/
protected $useAjax = TRUE;
/**
* @param Localization\ITranslator $translator
*/
public function injectTranslator(Localization\ITranslator $translator = NULL)
{
$this->translator = $translator;
}
/**
* @param NULL|string $templateFile
* @param Nette\ComponentModel\IContainer $parent
* @param null $name
*/
public function __construct(
$templateFile = NULL,
$displayRelatedPages = NULL,
Nette\ComponentModel\IContainer $parent = NULL, $name = NULL
) {
if ($templateFile) {
$this->setTemplateFile($templateFile);
}
$this->displayRelatedPages = (int)$displayRelatedPages;
}
/**
* Render control
*/
public function render()
{
// Check if control has template
if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
// Assign vars to template
$this->template->steps = $this->getSteps();
$this->template->paginator = $this->getPaginator();
$this->template->handle = 'showPage!';
$this->template->useAjax = $this->useAjax;
// Check if translator is available
if ($this->getTranslator() instanceof Localization\ITranslator) {
$this->template->setTranslator($this->getTranslator());
}
// If template was not defined before...
if ($this->template->getFile() === NULL) {
// ...try to get base component template file
$templateFile = !empty($this->templateFile) ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR .'default.latte';
$this->template->setFile($templateFile);
}
// Render component template
$this->template->render();
} else {
throw new Exceptions\InvalidStateException('Visual paginator control is without template.');
}
}
/**
* @return $this
*/
public function enableAjax()
{
$this->useAjax = TRUE;
return $this;
}
/**
* @return $this
*/
public function disableAjax()
{
$this->useAjax = FALSE;
return $this;
}
/**
* @return Utils\Paginator
*/
public function getPaginator()
{
// Check if paginator is created
if (!$this->paginator) {
$this->paginator = new Utils\Paginator;
}
return $this->paginator;
}
/**
* Change default control template path
*
* @param string $templateFile
*
* @return $this
*
* @throws Exceptions\FileNotFoundException
*/
public function setTemplateFile($templateFile)
{
// Check if template file exists...
if (!is_file($templateFile)) {
// ...check if extension template is used
if (is_file(__DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR . $templateFile)) {
$templateFile = __DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR . $templateFile;
} else {
// ...if not throw exception
throw new Exceptions\FileNotFoundException('Template file "'. $templateFile .'" was not found.');
}
}
$this->templateFile = $templateFile;
return $this;
}
/**
* @param Localization\ITranslator $translator
*
* @return $this
*/
public function setTranslator(Localization\ITranslator $translator)
{
$this->translator = $translator;
return $this;
}
/**
* @return Localization\ITranslator|null
*/
public function getTranslator()
{
if ($this->translator instanceof Localization\ITranslator) {
return $this->translator;
}
return NULL;
}
/**
* @return array
*/
public function getSteps()
{
// Get Nette paginator
$paginator = $this->getPaginator();
// Get actual paginator page
$page = $paginator->page;
if ($paginator->pageCount < 2) {
$steps = [$page];
} else {
$relatedPages = $this->displayRelatedPages ?: 3;
$arr = range(max($paginator->firstPage, $page - $relatedPages), min($paginator->lastPage, $page + $relatedPages));
$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));
}
return $steps;
}
/**
* Loads state information
*
* @param array
*
* @return void
*/
public function loadState(array $params): void
{
parent::loadState($params);
$this->getPaginator()->page = $this->page;
}
/**
* @param int $page
*/
public function handleShowPage($page)
{
$this->onShowPage($this, $page);
}
}