-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTableBuilder.php
executable file
·270 lines (228 loc) · 7.14 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
268
269
270
<?php
namespace NeuroSYS\DoctrineDatatables;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use NeuroSYS\DoctrineDatatables\Field\Entity;
use NeuroSYS\DoctrineDatatables\Field\AbstractField;
use NeuroSYS\DoctrineDatatables\Field\MultiField;
class TableBuilder
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
/**
* @var Table
*/
private $table;
/**
* @var FieldRegistry
*/
private $registry;
/**
* @var RendererInterface
*/
private $renderer;
/**
* @var string Root entity class name
*/
protected $className;
protected $index;
protected $parent;
public function __construct(EntityManager $em, array $request, FieldRegistry $registry = null, RendererInterface $renderer = null)
{
$this->em = $em;
if (null === $registry) {
$registry = new FieldRegistry();
}
$this->registry = $registry;
$this->request = new Request($request);
$this->renderer = $renderer;
}
public function getQueryBuilder()
{
return $this->getTable()->getQueryBuilder();
}
public function getTable()
{
return $this->table;
}
/**
* @param string $className Root entity class name
* @param string $alias Alias of root entity
* @return $this
*/
public function from($className, $alias)
{
$table = new Table($className, $alias, $this->em, $this->request, $this->renderer);
//$table->from($className, $alias);
$this->setTable($table);
return $this;
}
public function setRenderer(RendererInterface $renderer)
{
$this->table->setRenderer($renderer);
}
/**
* @param QueryBuilder $qb QueryBuilder instance
* @throws \Exception
*/
public function setQueryBuilder(QueryBuilder $qb)
{
if (!$this->table) {
$from = $qb->getDQLPart('from');
$from = array_shift($from);
/**
* @var \Doctrine\ORM\Query\Expr\From $from
*/
$this->table = new Table($from->getFrom(), $from->getAlias(), $this->em, $this->request, $this->renderer);
}
$this->getTable()->setQueryBuilder($qb);
return $this;
}
protected function getCurrentIndex()
{
if (null != $this->index) {
return $this->index;
} elseif (null !== $this->table->getIndex()) {
return $this->getTable()->getIndex();
} else {
return count($this->getTable()->getFields());
}
}
public function setIndex($index)
{
$this->index = $index;
}
public function with($name)
{
//$field = new MultiField($this->getTable(), $name);
//$field->setIndex($this->getCurrentIndex());
//$field->setParent($this->getTable());
$builder = new TableBuilder($this->em, $this->request->all(), $this->registry, $this->renderer);
$this->setIndex($this->getCurrentIndex());
$builder->setTable($this->getTable());
$this->getTable()->setField($name, $field);
$builder->parent = $this;
return $this->parent;
}
public function end()
{
return $this->parent;
}
protected function setTable(Entity $table)
{
$this->table = $table;
}
public function join($fullName, $alias)
{
return $this->leftJoin($fullName, $alias);
}
public function leftJoin($fullName, $alias)
{
$this->_join($fullName, $alias, 'LEFT');
return $this;
}
public function innerJoin($fullName, $alias)
{
$this->_join($fullName, $alias, 'INNER');
return $this;
}
private function _join($fullName, $alias, $type)
{
if (null !== $this->table->getIndex()) {
$index = $this->getTable()->getIndex();
} else {
$index = count($this->getTable()->getFields());
}
list($parentAlias, $name) = explode('.', $fullName);
$parent = $this->getTable()->getEntity($parentAlias);
if (!$parent) {
throw new \Exception("Parent entity not found for " . $fullName);
}
$parent->join($name, $alias, $type);
return $this;
}
/**
* @param $name Hint name
* @param $value Hint value
* @return $this
*/
public function setHint($name, $value)
{
$this->getTable()->setHint($name, $value);
return $this;
}
/**
* @param string $name Name of field (supported dot notation for relations)
* @param string $type Type of field
* @return $this
*/
public function add($type, $select = null, $filter = true, $options = array())
{
if (null !== $this->getTable()->getIndex()) {
$index = $this->getTable()->getIndex();
} else {
$index = count($this->getTable()->getFields());
}
$name = $this->request->get('mDataProp', $index) ?: $index;
if (true === $filter || null === $filter) {
$filter = $select;
}
if (empty($select)) {
$select = array();
} else {
if (!is_array($select)) {
$select = explode(',', $select);
}
foreach ($select as $key => $value) {
if (is_numeric($key)) {
if (strpos(strtolower($value), ' as ') === false) { // check for non entity field
list($parentAlias, $fieldName) = explode('.', trim($value));
if (!isset($select[trim($parentAlias)])) {
$select[trim($parentAlias)] = array();
}
} else {
$parentAlias = '';
$fieldName = $value;
}
$select[trim($parentAlias)][] = $fieldName;
unset($select[$key]);
}
}
}
if (empty($filter)) {
$filter = array();
} else {
if (!is_array($filter)) {
$filter = explode(',', $filter);
foreach ($filter as $key => $value) {
if (strpos($value, '.') === false) {
throw new \Exception("Filter must contain entity alias, '" . $filter . "' given");
}
}
}
/*foreach ($filter as $key => $value) {
if (!is_numeric($key)) {
$filter[] = $key . '.' . $value;
unset($filter[$key]);
}
}*/
}
/**
* @var AbstractField $field
*/
$field = $this->registry->resolve($type, $this->getTable(), $options);
$field->setIndex($index);
$field->setSelect($select);
$field->setSearchFields($filter);
if (isset($options['template'])) {
$field->setTemplate($options['template']);
}
if (isset($options['context'])) {
$field->setContext($options['context']);
}
$this->getTable()->setField($name, $field);
return $this;
}
}