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