1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Twig; |
4
|
|
|
|
5
|
|
|
class DataTableTwigExtension extends \Twig_Extension |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $container; |
9
|
|
|
|
10
|
|
|
public function __construct($container) |
11
|
|
|
{ |
12
|
|
|
$this->container = $container; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function getFunctions() |
16
|
|
|
{ |
17
|
|
|
return array( |
18
|
|
|
new \Twig_SimpleFunction('bootstrap_datatable', array($this, 'getDataTableRender'), array('is_safe' => array('html'))), |
19
|
|
|
new \Twig_SimpleFunction('bootstrap_datatable_row', array($this, 'getDataTableRowRender'), array('is_safe' => array('html'))), |
20
|
|
|
); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getFilters() |
24
|
|
|
{ |
25
|
|
|
return array( |
26
|
|
|
new \Twig_SimpleFilter('toArray', array($this, 'toArrayFilter')), |
27
|
|
|
new \Twig_SimpleFilter('toHeadersArray', array($this, 'toHeadersFilter')), |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getDataTableRender($elements, $options = array()) |
32
|
|
|
{ |
33
|
|
|
$options = array_merge(array('bootstrap' => "Default"), $options); |
34
|
|
|
|
35
|
|
|
return $this |
36
|
|
|
->container |
37
|
|
|
->get('templating') |
38
|
|
|
->render( |
39
|
|
|
'KnpRadBundle:Twig:Datatable/' . $options['bootstrap'] . '/datatable.html.twig', |
40
|
|
|
array('elements' => $elements, 'options' => $options) |
41
|
|
|
) |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getDataTableRowRender($element, $headers, $options = array()) |
46
|
|
|
{ |
47
|
|
|
$options = array_merge(array('bootstrap' => "Default"), $options); |
48
|
|
|
|
49
|
|
|
$routes = isset($options['routes']) |
50
|
|
|
? $options['routes'] |
51
|
|
|
: array() |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
return $this |
55
|
|
|
->container |
56
|
|
|
->get('templating') |
57
|
|
|
->render( |
58
|
|
|
'KnpRadBundle:Twig:Datatable/' . $options['bootstrap'] . '/datatable_row.html.twig', |
59
|
|
|
array( |
60
|
|
|
'element' => $element, |
61
|
|
|
'headers' => $headers, |
62
|
|
|
'options' => $options, |
63
|
|
|
'routes' => $routes |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function toHeadersFilter($els, $options = array ()) |
70
|
|
|
{ |
71
|
|
|
|
72
|
|
|
$fields = isset($options['fields']) |
73
|
|
|
? $options['fields'] |
74
|
|
|
: array() |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
$headers = array(); |
78
|
|
|
|
79
|
|
|
foreach ($els as $el) { |
|
|
|
|
80
|
|
|
|
81
|
|
|
if (is_object($el)) { |
|
|
|
|
82
|
|
|
|
83
|
|
|
foreach ($this->getGettersWithoutParameters($el) as $method) { |
84
|
|
|
preg_match('#get(?P<name>.*)#', $method->name, $matches); |
85
|
|
|
$name = strtolower($matches['name']); |
86
|
|
|
if (!in_array($name, $headers) && (0 === count($fields) || array_key_exists($name, $fields))) { |
87
|
|
|
$headers[$name] = array_key_exists($name, $fields) |
88
|
|
|
? $fields[$name] |
89
|
|
|
: $name |
90
|
|
|
; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
} elseif (is_array($el)) { |
|
|
|
|
95
|
|
|
|
96
|
|
|
foreach ($el as $key => $value) { |
97
|
|
|
if (!in_array($key, $headers) && (0 === count($fields) || array_key_exists($key, $fields))) { |
98
|
|
|
$headers[$key] = array_key_exists($key, $fields) |
99
|
|
|
? $fields[$key] |
100
|
|
|
: $key |
101
|
|
|
; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $headers; |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function toArrayFilter($el, $options = array ()) |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
$fields = isset($options['fields']) |
117
|
|
|
? $options['fields'] |
118
|
|
|
: array() |
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
$values = array(); |
122
|
|
|
|
123
|
|
|
if (is_object($el)) { |
|
|
|
|
124
|
|
|
|
125
|
|
|
foreach ($this->getGettersWithoutParameters($el) as $method) { |
126
|
|
|
preg_match('#get(?P<name>.*)#', $method->name, $matches); |
127
|
|
|
|
128
|
|
|
$name = strtolower($matches['name']); |
129
|
|
|
if (0 === count($fields) || array_key_exists($name, $fields)) { |
130
|
|
|
$value = $el->{$method->name}(); |
131
|
|
|
if (!is_object($value) && !is_array($value)) { |
132
|
|
|
$values[$name] = $value; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
} elseif (is_array($el)) { |
|
|
|
|
138
|
|
|
|
139
|
|
|
foreach ($el as $key => $value) { |
140
|
|
|
if (!is_object($value) && !is_array($value)) { |
141
|
|
|
$values[$key] = $value; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $values; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getGettersWithoutParameters($el) |
151
|
|
|
{ |
152
|
|
|
$rfl = new \ReflectionClass($el); |
153
|
|
|
|
154
|
|
|
return array_filter( |
155
|
|
|
$rfl->getMethods(\ReflectionMethod::IS_PUBLIC), |
156
|
|
|
function ($method) { |
157
|
|
|
return preg_match('#get.*#', $method->name) && 0 === count($method->getParameters()); |
158
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getName() |
164
|
|
|
{ |
165
|
|
|
return 'knp_rad.twig.datatable'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|