This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathDatatableTwigExtension.php
More file actions
256 lines (229 loc) · 7.39 KB
/
Copy pathDatatableTwigExtension.php
File metadata and controls
256 lines (229 loc) · 7.39 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
<?php
/*
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sg\DatatablesBundle\Twig;
use Closure;
use Sg\DatatablesBundle\Datatable\Action\Action;
use Sg\DatatablesBundle\Datatable\Column\ColumnInterface;
use Sg\DatatablesBundle\Datatable\DatatableInterface;
use Sg\DatatablesBundle\Datatable\Filter\FilterInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
class DatatableTwigExtension extends Twig_Extension
{
/**
* The PropertyAccessor.
*
* @var PropertyAccessor
*/
protected $accessor;
public function __construct()
{
$this->accessor = PropertyAccess::createPropertyAccessor();
}
//-------------------------------------------------
// Twig_ExtensionInterface
//-------------------------------------------------
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sg_datatables_twig_extension';
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new Twig_SimpleFunction(
'sg_datatables_render',
[$this, 'datatablesRender'],
['is_safe' => ['html'], 'needs_environment' => true]
),
new Twig_SimpleFunction(
'sg_datatables_render_html',
[$this, 'datatablesRenderHtml'],
['is_safe' => ['html'], 'needs_environment' => true]
),
new Twig_SimpleFunction(
'sg_datatables_render_js',
[$this, 'datatablesRenderJs'],
['is_safe' => ['html'], 'needs_environment' => true]
),
new Twig_SimpleFunction(
'sg_datatables_render_filter',
[$this, 'datatablesRenderFilter'],
['is_safe' => ['html'], 'needs_environment' => true]
),
new Twig_SimpleFunction(
'sg_datatables_render_multiselect_actions',
[$this, 'datatablesRenderMultiselectActions'],
['is_safe' => ['html'], 'needs_environment' => true]
),
];
}
/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new Twig_SimpleFilter('sg_datatables_bool_var', [$this, 'boolVar']),
];
}
//-------------------------------------------------
// Functions
//-------------------------------------------------
/**
* Renders the template.
*
* @return string
*/
public function datatablesRender(Twig_Environment $twig, DatatableInterface $datatable)
{
$datatable->computeTotals();
return $twig->render(
'@SgDatatables/datatable/datatable.html.twig',
[
'sg_datatables_view' => $datatable,
]
);
}
/**
* Renders the html template.
*
* @return string
*/
public function datatablesRenderHtml(Twig_Environment $twig, DatatableInterface $datatable)
{
$datatable->computeTotals();
return $twig->render(
'@SgDatatables/datatable/datatable_html.html.twig',
[
'sg_datatables_view' => $datatable,
]
);
}
/**
* Renders the js template.
*
* @return string
*/
public function datatablesRenderJs(Twig_Environment $twig, DatatableInterface $datatable)
{
return $twig->render(
'@SgDatatables/datatable/datatable_js.html.twig',
[
'sg_datatables_view' => $datatable,
]
);
}
/**
* Renders a Filter template.
*
* @param string $position
*
* @return string
*/
public function datatablesRenderFilter(Twig_Environment $twig, DatatableInterface $datatable, ColumnInterface $column, $position)
{
/** @var FilterInterface $filter */
$filter = $this->accessor->getValue($column, 'filter');
$index = $this->accessor->getValue($column, 'index');
$searchColumn = $this->accessor->getValue($filter, 'searchColumn');
if (null !== $searchColumn) {
$columns = $datatable->getColumnBuilder()->getColumnNames();
$searchColumnIndex = $columns[$searchColumn];
} else {
$searchColumnIndex = $index;
}
return $twig->render(
$filter->getTemplate(),
[
'column' => $column,
'search_column_index' => $searchColumnIndex,
'datatable_name' => $datatable->getName(),
'position' => $position,
]
);
}
/**
* Renders the MultiselectColumn Actions.
*
* @param int $pipeline
*
* @return string
*/
public function datatablesRenderMultiselectActions(Twig_Environment $twig, ColumnInterface $multiselectColumn, $pipeline)
{
$parameters = [];
$values = [];
$actions = $this->accessor->getValue($multiselectColumn, 'actions');
$domId = $this->accessor->getValue($multiselectColumn, 'renderActionsToId');
$datatableName = $this->accessor->getValue($multiselectColumn, 'datatableName');
/** @var Action $action */
foreach ($actions as $actionKey => $action) {
$routeParameters = $action->getRouteParameters();
if (\is_array($routeParameters)) {
foreach ($routeParameters as $key => $value) {
$parameters[$actionKey][$key] = $value;
}
} elseif ($routeParameters instanceof Closure) {
$parameters[$actionKey] = \call_user_func($routeParameters);
} else {
$parameters[$actionKey] = [];
}
if ($action->isButton()) {
if (null !== $action->getButtonValue()) {
$values[$actionKey] = $action->getButtonValue();
if (\is_bool($values[$actionKey])) {
$values[$actionKey] = (int) $values[$actionKey];
}
if (true === $action->isButtonValuePrefix()) {
$values[$actionKey] = 'sg-datatables-'.$datatableName.'-multiselect-button-'.$actionKey.'-'.$values[$actionKey];
}
} else {
$values[$actionKey] = null;
}
}
}
return $twig->render(
'@SgDatatables/datatable/multiselect_actions.html.twig',
[
'actions' => $actions,
'route_parameters' => $parameters,
'values' => $values,
'datatable_name' => $datatableName,
'dom_id' => $domId,
'pipeline' => $pipeline,
]
);
}
//-------------------------------------------------
// Filters
//-------------------------------------------------
/**
* Renders: {{ var ? 'true' : 'false' }}.
*
* @return string
*/
public function boolVar($value)
{
if ($value) {
return 'true';
}
return 'false';
}
}