1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
5
|
|
|
* @author Pavel Janda <[email protected]> |
6
|
|
|
* @package Ublaboo |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid; |
10
|
|
|
|
11
|
|
|
use Nette; |
12
|
|
|
use LeanMapper; |
13
|
|
|
use Nextras; |
14
|
|
|
use DibiRow; |
15
|
|
|
use Ublaboo\DataGrid\Utils\PropertyAccessHelper; |
16
|
|
|
use Nette\Utils\Html; |
17
|
|
|
use Ublaboo\DataGrid\Exception\DataGridException; |
18
|
|
|
use Nette\Database\Table\ActiveRow; |
19
|
|
|
|
20
|
|
|
class Row extends Nette\Object |
21
|
1 |
|
{ |
22
|
|
|
|
23
|
1 |
|
/** |
24
|
|
|
* @var DataGrid |
25
|
|
|
*/ |
26
|
|
|
protected $datagrid; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var mixed |
30
|
|
|
*/ |
31
|
|
|
protected $item; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $primary_key; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var mixed |
40
|
|
|
*/ |
41
|
|
|
protected $id; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Html |
45
|
|
|
*/ |
46
|
|
|
protected $control; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param DataGrid $datagrid |
51
|
|
|
* @param mixed $item |
52
|
|
|
* @param string $primary_key |
53
|
|
|
*/ |
54
|
|
|
public function __construct(DataGrid $datagrid, $item, $primary_key) |
55
|
|
|
{ |
56
|
1 |
|
$this->control = Html::el('tr'); |
57
|
1 |
|
$this->datagrid = $datagrid; |
58
|
1 |
|
$this->item = $item; |
59
|
1 |
|
$this->primary_key = $primary_key; |
60
|
1 |
|
$this->id = $this->getValue($primary_key); |
61
|
|
|
|
62
|
1 |
|
if ($datagrid->hasColumnsSummary()) { |
63
|
|
|
$datagrid->getColumnsSummary()->add($this); |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get id value of item |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function getId() |
73
|
|
|
{ |
74
|
1 |
|
return $this->id; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get item value of key |
80
|
|
|
* @param mixed $key |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function getValue($key) |
84
|
|
|
{ |
85
|
1 |
|
if ($this->item instanceof LeanMapper\Entity) { |
86
|
1 |
|
return $this->getLeanMapperEntityProperty($this->item, $key); |
87
|
|
|
|
88
|
1 |
|
} else if ($this->item instanceof Nextras\Orm\Entity\Entity) { |
89
|
|
|
return $this->getNextrasEntityProperty($this->item, $key); |
90
|
|
|
|
91
|
1 |
|
} else if ($this->item instanceof DibiRow) { |
|
|
|
|
92
|
|
|
return $this->item->{$this->formatDibiRowKey($key)}; |
93
|
|
|
|
94
|
1 |
|
} else if ($this->item instanceof ActiveRow) { |
95
|
|
|
if (preg_match("/^:([a-zA-Z0-9_$]*)\.([a-zA-Z0-9_$]*)$/", $key, $matches)) { |
96
|
|
|
$relatedTable = $matches[1]; |
97
|
|
|
$relatedColumn = $matches[2]; |
98
|
|
|
|
99
|
|
|
return $this->item->related($relatedTable)->fetch()->{$relatedColumn}; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (preg_match("/^([a-zA-Z0-9_$]*)\.([a-zA-Z0-9_$]*)$/", $key, $matches)) { |
103
|
|
|
$referredTable = $matches[1]; |
104
|
|
|
$referredColumn = $matches[2]; |
105
|
|
|
|
106
|
|
|
return $this->item->ref($referredTable)->{$referredColumn}; |
107
|
|
|
} |
108
|
|
|
return $this->item->{$key}; |
109
|
|
|
|
110
|
1 |
|
} else if ($this->item instanceof Nette\Database\Row) { |
111
|
|
|
return $this->item->{$key}; |
112
|
|
|
|
113
|
1 |
|
} else if (is_array($this->item)) { |
114
|
1 |
|
return $this->item[$key]; |
115
|
|
|
|
116
|
|
|
} else { |
117
|
|
|
/** |
118
|
|
|
* Doctrine entity |
119
|
|
|
*/ |
120
|
1 |
|
return $this->getDoctrineEntityProperty($this->item, $key); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Html |
128
|
|
|
*/ |
129
|
|
|
public function getControl() |
130
|
|
|
{ |
131
|
1 |
|
return $this->control; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getControlClass() |
139
|
|
|
{ |
140
|
1 |
|
if (!$class = $this->control->class) { |
141
|
|
|
return ''; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
return implode(' ', array_keys($class)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* LeanMapper: Access object properties to get a item value |
150
|
|
|
* @param LeanMapper\Entity $item |
151
|
|
|
* @param mixed $key |
152
|
1 |
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key) |
|
|
|
|
155
|
|
|
{ |
156
|
1 |
|
$properties = explode('.', $key); |
157
|
1 |
|
$value = $item; |
158
|
|
|
|
159
|
1 |
|
while ($property = array_shift($properties)) { |
160
|
1 |
|
if (!isset($value->{$property})) { |
161
|
|
|
if ($this->datagrid->strict_entity_property) { |
162
|
|
|
throw new DataGridException(sprintf( |
163
|
|
|
'Target Property [%s] is not an object or is empty, trying to get [%s]', |
164
|
|
|
$value, str_replace('.', '->', $key) |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return NULL; |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
$value = $value->{$property}; |
172
|
1 |
|
} |
173
|
|
|
|
174
|
1 |
|
return $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Nextras: Access object properties to get a item value |
180
|
|
|
* @param Nextras\Orm\Entity\Entity $item |
181
|
|
|
* @param string $key |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function getNextrasEntityProperty(Nextras\Orm\Entity\Entity $item, $key) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$properties = explode('.', $key); |
187
|
|
|
$value = $item; |
188
|
|
|
|
189
|
|
|
while ($property = array_shift($properties)) { |
190
|
|
|
if (!isset($value->{$property})) { |
191
|
|
|
if ($this->datagrid->strict_entity_property) { |
192
|
|
|
throw new DataGridException(sprintf( |
193
|
|
|
'Target Property [%s] is not an object or is empty, trying to get [%s]', |
194
|
|
|
$value, str_replace('.', '->', $key) |
195
|
|
|
)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return NULL; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$value = $value->{$property}; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $value; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
/** |
209
|
1 |
|
* Doctrine: Access object properties to get a item value |
210
|
|
|
* @param mixed $item |
211
|
|
|
* @param mixed $key |
212
|
|
|
* @return mixed |
213
|
|
|
*/ |
214
|
|
|
public function getDoctrineEntityProperty($item, $key) |
215
|
|
|
{ |
216
|
1 |
|
$properties = explode('.', $key); |
217
|
1 |
|
$value = $item; |
218
|
1 |
|
$accessor = PropertyAccessHelper::getAccessor(); |
219
|
|
|
|
220
|
1 |
|
while ($property = array_shift($properties)) { |
221
|
1 |
|
if (!is_object($value) && !$value) { |
222
|
|
|
if ($this->datagrid->strict_entity_property) { |
223
|
|
|
throw new DataGridException(sprintf( |
224
|
|
|
'Target Property [%s] is not an object or is empty, trying to get [%s]', |
225
|
|
|
$value, str_replace('.', '->', $key) |
226
|
|
|
)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return NULL; |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
$value = $accessor->getValue($value, $property); |
233
|
1 |
|
} |
234
|
|
|
|
235
|
1 |
|
return $value; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get original item |
241
|
|
|
* @return mixed |
242
|
|
|
*/ |
243
|
|
|
public function getItem() |
244
|
|
|
{ |
245
|
|
|
return $this->item; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Has particular row group actions allowed? |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function hasGroupAction() |
254
|
|
|
{ |
255
|
|
|
$condition = $this->datagrid->getRowCondition('group_action'); |
256
|
|
|
|
257
|
|
|
return $condition ? $condition($this->item) : TRUE; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Has particular row a action allowed? |
263
|
|
|
* @param mixed $key |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
|
|
public function hasAction($key) |
267
|
|
|
{ |
268
|
|
|
$condition = $this->datagrid->getRowCondition('action', $key); |
269
|
|
|
|
270
|
|
|
return $condition ? $condition($this->item) : TRUE; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Has particular row inlie edit allowed? |
276
|
|
|
* @return bool |
277
|
|
|
*/ |
278
|
|
|
public function hasInlineEdit() |
279
|
|
|
{ |
280
|
|
|
$condition = $this->datagrid->getRowCondition('inline_edit'); |
281
|
|
|
|
282
|
|
|
return $condition ? $condition($this->item) : TRUE; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $key |
288
|
|
|
* @param Column\Column $column |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
|
|
public function applyColumnCallback($key, Column\Column $column) |
292
|
|
|
{ |
293
|
|
|
$callback = $this->datagrid->getColumnCallback($key); |
294
|
|
|
|
295
|
|
|
if ($callback !== NULL) { |
296
|
|
|
call_user_func($callback, $column, $this->getItem()); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $column; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Key may contain ".", get rid of it (+ the table alias) |
305
|
|
|
* |
306
|
|
|
* @param string $key |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
private function formatDibiRowKey($key) |
310
|
|
|
{ |
311
|
|
|
if ($offset = strpos($key, '.')) { |
312
|
|
|
return substr($key, $offset + 1); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $key; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
} |
319
|
|
|
|