forked from Tactics/TableBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableBuilder.php
268 lines (221 loc) · 6.53 KB
/
TableBuilder.php
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license intableation, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tactics\TableBundle;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Description of TableBuilderInterface
*
* @author gert
*/
class TableBuilder implements \IteratorAggregate, TableBuilderInterface
{
/**
* The table factory.
*
* @var TableFactoryInterface
*/
protected $factory;
/**
* The type of table this builder creates
*
* @var string
*/
protected $type;
/**
* The name of the table this builder creates
*
* @var string
*/
protected $name;
/**
* List of columns in the TableBuilder
*
* @var array
*/
protected $columns = array();
/**
* @var array The options for the table builder
*/
protected $options = array();
/**
* Creates a new table builder.
*
* @param string $name
* @param TableFactoryInterface $factory
* @param array $options
*/
public function __construct($name, $type = '', TableFactoryInterface $factory, array $options = array())
{
$this->factory = $factory;
$this->type = $type;
$this->name = $name;
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$this->options = $resolver->resolve($options);
}
/**
* {@inheritdoc}
*/
public function add($name, $type = null, $headerTypeOrOptions = null, array $options = array())
{
$headerType = is_string($headerTypeOrOptions) ? $headerTypeOrOptions : null;
$options = is_array($headerTypeOrOptions) ? $headerTypeOrOptions : $options;
$this->columns[$name] = array(
'type' => $type,
'header_type' => $headerType,
'options' => $options,
);
return $this;
}
/**
* Extracts options from array with a specific namespace prefix and returns
* an array with only the options from the matching namespace, namespace removed
*
* eg array('column/type' => 'one') becomes array('type' => 'one')
*
* @param array $options
* @param string $ns
* @return array with options in namespace, namespace in key removed
*/
protected function extractNamespacedOptions($options, $ns)
{
$extracted = array();
foreach ($options as $key => $value) {
if (strpos($key, $ns) !== false) {
$arr = explode('/', $key);
$extracted[end($arr)] = $value;
}
}
return $extracted;
}
/**
* {@inheritdoc}
*/
public function create($name, $type = null, $headerType = null, array $options = array())
{
// todo replace quick hack to clean up options, this is dirty.
$columnOptions = $this->extractNamespacedOptions($options, 'column');
$headerOptions = $this->extractNamespacedOptions($options, 'header');
// Set default types if none specified.
if (null === $type) {
$type = $this->options['column_type'];
}
if (null === $headerType) {
$headerType = $this->options['header_type'];
}
if ($type == 'actions') {
foreach ($columnOptions['actions'] as $action => $options) {
if (
isset($options['required_roles']) &&
$options['required_roles']
) {
$security = $this->factory->getContainer()->get('security.context');
$disabled = true;
foreach ($options['required_roles'] as $role) {
if ($security->isGranted($role)) {
$disabled = false;
break;
}
}
$columnOptions['actions'][$action]['disabled'] = $disabled ? : (isset($options['disabled']) ? $options['disabled'] : $disabled);
}
}
}
if (null !== $type) {
$headerName = isset($headerOptions['value']) ? $headerOptions['value'] : ucfirst($name);
$header = $this->factory->createColumnHeader($headerName, $headerType, $headerOptions);
return $this->factory->createColumn($name, $type, $header, $columnOptions);
}
//return $this->factory->createBuilderForProperty($name, null, $options, $this);
}
/**
* {@inheritdoc}
*/
public function get($name)
{
return $this->create($this->columns[$name]);
}
/**
* {@inheritdoc}
*/
public function remove($name)
{
unset($this->columns[$name]);
}
/**
* {@inheritdoc}
*/
public function has($name)
{
return isset($this->columns[$name]);
}
/**
* {@inheritdoc}
*
* todo: de-rocketscience this please, headache.
* todo: seriously :p
*/
public function all()
{
$columns = array();
foreach($this->columns as $name => $config)
{
$headerType = (isset($config['header_type'])) ?
$config['header_type'] : null;
$columns[$name] = $this->create($name, $config['type'], $headerType, $config['options']);
}
return $columns;
}
/**
* Returns the number of columns
*/
public function count()
{
return count($this->columns);
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
$columns = $this->all();
return new \ArrayIterator($columns);
}
/**
* {@inheritdoc}
*/
public function getTableFactory()
{
return $this->factory;
}
/**
* {@inheritdoc}
*/
public function getTable($options = array())
{
$table = $this->factory->createTable($this->name, $this->type, $options);
foreach ($this as $column)
{
$table->add($column);
}
return $table;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults(array(
'column_type' => 'text',
'header_type' => 'text'
));
}
}