1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\PaginationProvider\Helper; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
6
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Pagination data processor |
10
|
|
|
* |
11
|
|
|
* Common data processor for all templating engines |
12
|
|
|
* |
13
|
|
|
* @author Rafał Wrzeszcz <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Processor |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var UrlGeneratorInterface |
19
|
|
|
*/ |
20
|
|
|
protected $urlGenerator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TranslatorInterface |
24
|
|
|
*/ |
25
|
|
|
protected $translator; |
26
|
|
|
|
27
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator) |
28
|
|
|
{ |
29
|
|
|
$this->urlGenerator = $urlGenerator; |
30
|
|
|
$this->translator = $translator; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generates pagination template data |
35
|
|
|
* |
36
|
|
|
* @param array $queryParams |
37
|
|
|
* @param array $viewParams |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function render($pagination, array $queryParams = array(), array $viewParams = array()) |
42
|
|
|
{ |
43
|
|
|
$data = $pagination->getPaginationData(); |
44
|
|
|
|
45
|
|
|
$data['route'] = $pagination->getRoute(); |
46
|
|
|
$data['query'] = array_merge($pagination->getParams(), $queryParams); |
47
|
|
|
|
48
|
|
|
return array_merge( |
49
|
|
|
$pagination->getPaginatorOptions(), // options given to paginator when paginated |
50
|
|
|
$pagination->getCustomParameters(), // all custom parameters for view |
51
|
|
|
$viewParams, // additional custom parameters for view |
52
|
|
|
$data // merging base route parameters last, to avoid broke of integrity |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a sort url for the field named $title |
58
|
|
|
* and identified by $key which consists of |
59
|
|
|
* alias and field. $options holds all link |
60
|
|
|
* parameters like "alt, class" and so on. |
61
|
|
|
* |
62
|
|
|
* $key example: "article.title" |
63
|
|
|
* |
64
|
|
|
* @param string $title |
65
|
|
|
* @param string $key |
66
|
|
|
* @param array $options |
67
|
|
|
* @param array $params |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function sortable($pagination, $title, $key, $options = array(), $params = array()) |
71
|
|
|
{ |
72
|
|
|
$options = array_merge(array( |
73
|
|
|
'absolute' => false, |
74
|
|
|
'translationParameters' => array(), |
75
|
|
|
'translationDomain' => null, |
76
|
|
|
'translationCount' => null, |
77
|
|
|
), $options); |
78
|
|
|
|
79
|
|
|
$params = array_merge($pagination->getParams(), $params); |
80
|
|
|
|
81
|
|
|
$direction = isset($options[$pagination->getPaginatorOption('sortDirectionParameterName')]) |
82
|
|
|
? $options[$pagination->getPaginatorOption('sortDirectionParameterName')] |
83
|
|
|
: (isset($options['defaultDirection']) ? $options['defaultDirection'] : 'asc') |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
$sorted = $pagination->isSorted($key, $params); |
87
|
|
|
|
88
|
|
|
if ($sorted) { |
89
|
|
|
$direction = $params[$pagination->getPaginatorOption('sortDirectionParameterName')]; |
90
|
|
|
$direction = (strtolower($direction) == 'asc') ? 'desc' : 'asc'; |
91
|
|
|
$class = $direction == 'asc' ? 'desc' : 'asc'; |
92
|
|
|
|
93
|
|
|
if (isset($options['class'])) { |
94
|
|
|
$options['class'] .= ' ' . $class; |
95
|
|
|
} else { |
96
|
|
|
$options['class'] = $class; |
97
|
|
|
} |
98
|
|
|
} else { |
99
|
|
|
$options['class'] = 'sortable'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (is_array($title) && array_key_exists($direction, $title)) { |
103
|
|
|
$title = $title[$direction]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$params = array_merge( |
107
|
|
|
$params, |
108
|
|
|
array( |
109
|
|
|
$pagination->getPaginatorOption('sortFieldParameterName') => $key, |
110
|
|
|
$pagination->getPaginatorOption('sortDirectionParameterName') => $direction, |
111
|
|
|
$pagination->getPaginatorOption('pageParameterName') => 1 // reset to 1 on sort |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$options['href'] = $this->urlGenerator->generate($pagination->getRoute(), $params, $options['absolute']); |
116
|
|
|
|
117
|
|
|
if (null !== $options['translationDomain']) { |
118
|
|
|
if (null !== $options['translationCount']) { |
119
|
|
|
$title = $this->translator->transChoice($title, $options['translationCount'], $options['translationParameters'], $options['translationDomain']); |
120
|
|
|
} else { |
121
|
|
|
$title = $this->translator->trans($title, $options['translationParameters'], $options['translationDomain']); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!isset($options['title'])) { |
126
|
|
|
$options['title'] = $title; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
unset($options['absolute'], $options['translationDomain'], $options['translationParameters']); |
130
|
|
|
|
131
|
|
|
return array_merge( |
132
|
|
|
$pagination->getPaginatorOptions(), |
133
|
|
|
$pagination->getCustomParameters(), |
134
|
|
|
compact('options', 'title', 'direction', 'sorted', 'key') |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Create a filter url for the field named $title |
140
|
|
|
* and identified by $key which consists of |
141
|
|
|
* alias and field. $options holds all link |
142
|
|
|
* parameters like "alt, class" and so on. |
143
|
|
|
* |
144
|
|
|
* $key example: "article.title" |
145
|
|
|
* |
146
|
|
|
* @param string $title |
|
|
|
|
147
|
|
|
* @param string $key |
|
|
|
|
148
|
|
|
* @param array $options |
149
|
|
|
* @param array $params |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function filter($pagination, array $fields, $options = array(), $params = array()) |
153
|
|
|
{ |
154
|
|
|
$options = array_merge(array( |
155
|
|
|
'absolute' => false, |
156
|
|
|
'translationParameters' => array(), |
157
|
|
|
'translationDomain' => null, |
158
|
|
|
'button' => 'Filter', |
159
|
|
|
), $options); |
160
|
|
|
|
161
|
|
|
$params = array_merge($pagination->getParams(), $params); |
162
|
|
|
$params[$pagination->getPaginatorOption('pageParameterName')] = 1; // reset to 1 on filter |
163
|
|
|
|
164
|
|
|
$filterFieldName = $pagination->getPaginatorOption('filterFieldParameterName'); |
165
|
|
|
$filterValueName = $pagination->getPaginatorOption('filterValueParameterName'); |
166
|
|
|
|
167
|
|
|
$selectedField = isset($params[$filterFieldName]) ? $params[$filterFieldName] : null; |
168
|
|
|
$selectedValue = isset($params[$filterValueName]) ? $params[$filterValueName] : null; |
169
|
|
|
|
170
|
|
|
$action = $this->urlGenerator->generate($pagination->getRoute(), $params, $options['absolute']); |
|
|
|
|
171
|
|
|
|
172
|
|
|
foreach ($fields as $field => $title) { |
173
|
|
|
$fields[$field] = $this->translator->trans($title, $options['translationParameters'], $options['translationDomain']); |
174
|
|
|
} |
175
|
|
|
$options['button'] = $this->translator->trans($options['button'], $options['translationParameters'], $options['translationDomain']); |
176
|
|
|
|
177
|
|
|
unset($options['absolute'], $options['translationDomain'], $options['translationParameters']); |
178
|
|
|
|
179
|
|
|
return array_merge( |
180
|
|
|
$pagination->getPaginatorOptions(), |
181
|
|
|
$pagination->getCustomParameters(), |
182
|
|
|
compact('fields', 'action', 'filterFieldName', 'filterValueName', 'selectedField', 'selectedValue', 'options') |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.