|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoDraw\Draw\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use WebinoDraw\Dom\NodeList; |
|
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
15
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Pagination |
|
19
|
|
|
*/ |
|
20
|
|
|
class Pagination extends Element |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Draw helper service name |
|
24
|
|
|
*/ |
|
25
|
|
|
const SERVICE = 'webinodrawpagination'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $defaultSpec = [ |
|
31
|
|
|
'paginator' => 'WebinoDrawPaginator', |
|
32
|
|
|
'var' => [ |
|
33
|
|
|
'fetch' => [ |
|
34
|
|
|
'pageHref' => 'page.href' |
|
35
|
|
|
], |
|
36
|
|
|
], |
|
37
|
|
|
'instructions' => [ |
|
38
|
|
|
'snippet' => [ |
|
39
|
|
|
'locator' => '.', |
|
40
|
|
|
'html' => '{$snippet}', |
|
41
|
|
|
'render' => ['snippet' => 'webino-draw/snippet/pagination'], |
|
42
|
|
|
], |
|
43
|
|
|
// TODO do not use ?1 on the first page by default |
|
44
|
|
|
'pages' => [ |
|
45
|
|
|
'locator' => 'xpath=.//li[2]', |
|
46
|
|
|
'loop' => [ |
|
47
|
|
|
'base' => 'pagesInRange', |
|
48
|
|
|
'instructions' => [ |
|
49
|
|
|
'active' => [ |
|
50
|
|
|
'locator' => '.', |
|
51
|
|
|
'attribs' => ['class' => '{$active}'], |
|
52
|
|
|
], |
|
53
|
|
|
'value' => [ |
|
54
|
|
|
'locator' => 'a', |
|
55
|
|
|
'value' => '{$number}', |
|
56
|
|
|
|
|
57
|
|
|
'attribs' => [ |
|
58
|
|
|
'href' => '{$pageHref}?{$number}{$params}', |
|
59
|
|
|
'title' => '{$number}', |
|
60
|
|
|
], |
|
61
|
|
|
], |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
], |
|
65
|
|
|
'first' => [ |
|
66
|
|
|
'locator' => 'xpath=.//li[1]', |
|
67
|
|
|
'attribs' => ['class' => '{$_class} previous-page'], |
|
68
|
|
|
|
|
69
|
|
|
'onVar' => [ |
|
70
|
|
|
'on-first' => [ |
|
71
|
|
|
'var' => '{$previous}', |
|
72
|
|
|
'equalTo' => '', |
|
73
|
|
|
|
|
74
|
|
|
'instructions' => [ |
|
75
|
|
|
'remove' => [ |
|
76
|
|
|
'locator' => '.', |
|
77
|
|
|
'remove' => '.', |
|
78
|
|
|
], |
|
79
|
|
|
], |
|
80
|
|
|
], |
|
81
|
|
|
], |
|
82
|
|
|
'instructions' => [ |
|
83
|
|
|
'link' => [ |
|
84
|
|
|
'locator' => 'a', |
|
85
|
|
|
'attribs' => [ |
|
86
|
|
|
'href' => '{$pageHref}?{$previous}{$params}', |
|
87
|
|
|
'title' => '{$previous}', |
|
88
|
|
|
], |
|
89
|
|
|
], |
|
90
|
|
|
], |
|
91
|
|
|
], |
|
92
|
|
|
'last' => [ |
|
93
|
|
|
'locator' => 'xpath=.//li[last()]', |
|
94
|
|
|
'attribs' => ['class' => '{$_class} next-page'], |
|
95
|
|
|
|
|
96
|
|
|
'onVar' => [ |
|
97
|
|
|
'on-last' => [ |
|
98
|
|
|
'var' => '{$next}', |
|
99
|
|
|
'equalTo' => '', |
|
100
|
|
|
|
|
101
|
|
|
'instructions' => [ |
|
102
|
|
|
'remove' => [ |
|
103
|
|
|
'locator' => '.', |
|
104
|
|
|
'remove' => '.', |
|
105
|
|
|
], |
|
106
|
|
|
], |
|
107
|
|
|
], |
|
108
|
|
|
], |
|
109
|
|
|
'instructions' => [ |
|
110
|
|
|
'link' => [ |
|
111
|
|
|
'locator' => 'a', |
|
112
|
|
|
'attribs' => [ |
|
113
|
|
|
'href' => '{$pageHref}?{$next}{$params}', |
|
114
|
|
|
'title' => '{$next}', |
|
115
|
|
|
], |
|
116
|
|
|
], |
|
117
|
|
|
], |
|
118
|
|
|
], |
|
119
|
|
|
'empty' => [ |
|
120
|
|
|
'locator' => '.', |
|
121
|
|
|
'onEmpty' => ['remove' => '.'], |
|
122
|
|
|
], |
|
123
|
|
|
], |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @var array |
|
128
|
|
|
*/ |
|
129
|
|
|
protected $params = []; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @var ServiceLocatorInterface |
|
133
|
|
|
*/ |
|
134
|
|
|
protected $services; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @todo PaginatorProvider instead of ServiceLocator |
|
138
|
|
|
* @param ServiceLocatorInterface $services |
|
139
|
|
|
*/ |
|
140
|
|
|
public function __construct(ServiceLocatorInterface $services) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->services = $services; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $name |
|
147
|
|
|
* @param string $value |
|
148
|
|
|
* @return self |
|
149
|
|
|
*/ |
|
150
|
|
|
public function addParam($name, $value) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->params[$name] = $value; |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param NodeList $nodes |
|
158
|
|
|
* @param array $spec |
|
159
|
|
|
* @return self |
|
160
|
|
|
*/ |
|
161
|
|
|
public function drawNodes(NodeList $nodes, array $spec) |
|
162
|
|
|
{ |
|
163
|
|
|
$localSpec = ArrayUtils::merge(self::$defaultSpec, $spec); |
|
164
|
|
|
$paginatorName = $localSpec['paginator']; |
|
165
|
|
|
|
|
166
|
|
|
if (!$this->services->has($paginatorName)) { |
|
167
|
|
|
return $this->drawEmpty($nodes, $localSpec); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/* @var $paginator \Zend\Paginator\Paginator */ |
|
171
|
|
|
$paginator = $this->services->get($paginatorName); |
|
172
|
|
|
$pages = $paginator->getPages(); |
|
173
|
|
|
|
|
174
|
|
|
if (1 >= $pages->pageCount) { |
|
175
|
|
|
return $this->drawEmpty($nodes, $localSpec); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$curPageNo = $paginator->getCurrentPageNumber(); |
|
179
|
|
|
$pagesInRange = []; |
|
180
|
|
|
|
|
181
|
|
|
foreach ($pages->pagesInRange as $pageNo) { |
|
182
|
|
|
$pagesInRange[] = [ |
|
183
|
|
|
'number' => $pageNo, |
|
184
|
|
|
'active' => ($curPageNo == $pageNo) ? 'active' : '', |
|
185
|
|
|
]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->setVars( |
|
189
|
|
|
array_merge( |
|
190
|
|
|
$this->getVars(), |
|
191
|
|
|
(array) $pages, |
|
192
|
|
|
[ |
|
193
|
|
|
'current' => $curPageNo, |
|
194
|
|
|
'pagesInRange' => $pagesInRange, |
|
195
|
|
|
'params' => !empty($this->params) |
|
196
|
|
|
? '&' . http_build_query($this->params, '', '&') |
|
197
|
|
|
: '', |
|
198
|
|
|
] |
|
199
|
|
|
) |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
return parent::drawNodes($nodes, $localSpec); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param NodeList $nodes |
|
207
|
|
|
* @param array $spec |
|
208
|
|
|
* @return $this |
|
209
|
|
|
*/ |
|
210
|
|
|
private function drawEmpty(NodeList $nodes, array $spec) |
|
211
|
|
|
{ |
|
212
|
|
|
unset($spec['instructions']['snippet']); |
|
213
|
|
|
return parent::drawNodes($nodes, $spec); |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.