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\Column; |
10
|
|
|
|
11
|
|
|
use Nette\Utils\Html; |
12
|
|
|
use Ublaboo\DataGrid\Utils\ItemDetailForm; |
13
|
|
|
use Ublaboo\DataGrid\DataGrid; |
14
|
|
|
use Ublaboo; |
15
|
|
|
use Ublaboo\DataGrid\Traits; |
16
|
|
|
use Ublaboo\DataGrid\Exception\DataGridItemDetailException; |
17
|
|
|
|
18
|
|
|
class ItemDetail |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
use Traits\ButtonIconTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* (renderer | template | block) |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $type; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $template; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callable |
36
|
|
|
*/ |
37
|
|
|
protected $renderer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $title = 'show'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $class = 'btn btn-xs btn-default ajax'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $icon = 'eye'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $text = ''; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var DataGrid |
61
|
|
|
*/ |
62
|
|
|
protected $grid; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string|bool |
66
|
|
|
*/ |
67
|
|
|
protected $primary_where_column; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var ItemDetailForm |
71
|
|
|
*/ |
72
|
|
|
protected $form; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $template_parameters = []; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param DataGrid $grid |
82
|
|
|
* @param string $primary_where_column |
83
|
|
|
*/ |
84
|
|
|
public function __construct(DataGrid $grid, $primary_where_column) |
85
|
|
|
{ |
86
|
|
|
$this->grid = $grid; |
87
|
|
|
$this->primary_where_column = $primary_where_column; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Render row item detail button |
93
|
|
|
* @param Row $row |
94
|
|
|
* @return Html |
95
|
|
|
*/ |
96
|
|
|
public function renderButton($row) |
97
|
|
|
{ |
98
|
|
|
$a = Html::el('a') |
99
|
|
|
->href($this->grid->link('getItemDetail!', ['id' => $row->getId()])) |
100
|
|
|
->data('toggle-detail', $row->getId()) |
101
|
|
|
->data('toggle-detail-grid', $this->grid->getName()); |
102
|
|
|
|
103
|
|
|
$this->tryAddIcon($a, $this->getIcon(), $this->getText()); |
104
|
|
|
|
105
|
|
|
$a->add($this->text); |
106
|
|
|
|
107
|
|
|
if ($this->title) { $a->title($this->title); } |
108
|
|
|
if ($this->class) { $a->class($this->class); } |
109
|
|
|
|
110
|
|
|
return $a; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Render item detail |
116
|
|
|
* @param mixed $item |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function render($item) |
120
|
|
|
{ |
121
|
|
|
if ($this->getType() == 'block') { |
122
|
|
|
throw new DataGridItemDetailException( |
123
|
|
|
'ItemDetail is set to render as block, but block #detail is not defined' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return call_user_func($this->getRenderer(), $item); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get primary column for where clause |
133
|
|
|
* @return string|bool |
134
|
|
|
*/ |
135
|
|
|
public function getPrimaryWhereColumn() |
136
|
|
|
{ |
137
|
|
|
return $this->primary_where_column; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set attribute title |
143
|
|
|
* @param string $title |
144
|
|
|
*/ |
145
|
|
|
public function setTitle($title) |
146
|
|
|
{ |
147
|
|
|
$this->title = $title; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get attribute title |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getTitle() |
158
|
|
|
{ |
159
|
|
|
return $this->title; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set attribute class |
165
|
|
|
* @param string $class |
166
|
|
|
*/ |
167
|
|
|
public function setClass($class) |
168
|
|
|
{ |
169
|
|
|
$this->class = $class; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get attribute class |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getClass() |
180
|
|
|
{ |
181
|
|
|
return $this->class; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set icon |
187
|
|
|
* @param string $icon |
188
|
|
|
*/ |
189
|
|
|
public function setIcon($icon) |
190
|
|
|
{ |
191
|
|
|
$this->icon = $icon; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get icon |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function getIcon() |
202
|
|
|
{ |
203
|
|
|
return $this->icon; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set text |
209
|
|
|
* @param string $text |
210
|
|
|
*/ |
211
|
|
|
public function setText($text) |
212
|
|
|
{ |
213
|
|
|
$this->text = $text; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get text |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
public function getText() |
224
|
|
|
{ |
225
|
|
|
return $this->text; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set item detail type |
231
|
|
|
* @param string $type |
232
|
|
|
*/ |
233
|
|
|
public function setType($type) |
234
|
|
|
{ |
235
|
|
|
$this->type = (string) $type; |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get item detail type |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function getType() |
246
|
|
|
{ |
247
|
|
|
return $this->type; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Set item detail template |
253
|
|
|
* @param string $template |
254
|
|
|
*/ |
255
|
|
|
public function setTemplate($template) |
256
|
|
|
{ |
257
|
|
|
$this->template = (string) $template; |
258
|
|
|
|
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get item detail template |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
public function getTemplate() |
268
|
|
|
{ |
269
|
|
|
return $this->template; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Set item detail renderer |
275
|
|
|
* @param callable $renderer |
276
|
|
|
*/ |
277
|
|
|
public function setRenderer($renderer) |
278
|
|
|
{ |
279
|
|
|
$this->renderer = $renderer; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Get item detail renderer |
287
|
|
|
* @return callable |
288
|
|
|
*/ |
289
|
|
|
public function getRenderer() |
290
|
|
|
{ |
291
|
|
|
return $this->renderer; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param ItemDetailForm $form |
297
|
|
|
* @return static |
298
|
|
|
*/ |
299
|
|
|
public function setForm(ItemDetailForm $form) |
300
|
|
|
{ |
301
|
|
|
$this->form = $form; |
302
|
|
|
|
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return ItemDetailForm |
309
|
|
|
*/ |
310
|
|
|
public function getForm() |
311
|
|
|
{ |
312
|
|
|
return $this->form; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param array $template_parameters |
318
|
|
|
* @return static |
319
|
|
|
*/ |
320
|
|
|
public function setTemplateParameters(array $template_parameters) |
321
|
|
|
{ |
322
|
|
|
$this->template_parameters = $template_parameters; |
323
|
|
|
|
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
public function getTemplateVariables() |
329
|
|
|
{ |
330
|
|
|
return $this->template_parameters; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|