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\DataGrid; |
13
|
|
|
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException; |
14
|
|
|
use Ublaboo\DataGrid\Exception\DataGridException; |
15
|
|
|
use Ublaboo\DataGrid\Row; |
16
|
|
|
|
17
|
|
|
class Action extends Column |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|callable |
22
|
|
|
*/ |
23
|
|
|
protected $title; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|callable |
27
|
|
|
*/ |
28
|
|
|
protected $class; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|callable |
32
|
|
|
*/ |
33
|
|
|
protected $icon; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DataGrid |
37
|
|
|
*/ |
38
|
|
|
protected $grid; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $href; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $name; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $params; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array|callable |
57
|
|
|
*/ |
58
|
|
|
protected $confirm; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $data_attributes = []; |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param DataGrid $grid |
68
|
|
|
* @param string $href |
69
|
|
|
* @param string $name |
70
|
|
|
* @param array $params |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function __construct(DataGrid $grid, $href, $name, $params) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->grid = $grid; |
75
|
|
|
$this->href = $href; |
76
|
|
|
$this->name = $name; |
77
|
|
|
$this->params = $params; |
78
|
|
|
|
79
|
|
|
$this->class = 'btn btn-xs btn-default'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Render row item into template |
85
|
|
|
* @param Row $row |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function render(Row $row) |
89
|
|
|
{ |
90
|
|
|
/** |
91
|
|
|
* Renderer function may be used |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
if ($renderer = $this->getRenderer()) { |
|
|
|
|
94
|
|
|
if (!$renderer->getConditionCallback()) { |
95
|
|
|
return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (call_user_func_array($this->getRenderer(), [$row->getItem()])) { |
99
|
|
|
return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$link = $this->createLink($row); |
104
|
|
|
|
105
|
|
|
$a = Html::el('a')->href($link); |
106
|
|
|
|
107
|
|
View Code Duplication |
if ($this->icon) { |
|
|
|
|
108
|
|
|
$a->add(Html::el('span')->class(DataGrid::$icon_prefix.$this->getIcon($row))); |
109
|
|
|
|
110
|
|
|
if (strlen($this->name)) { |
111
|
|
|
$a->add(' '); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->data_attributes) { |
|
|
|
|
116
|
|
|
foreach ($this->data_attributes as $key => $value) { |
117
|
|
|
$a->data($key, $value); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$a->add($this->translate($this->name)); |
122
|
|
|
|
123
|
|
|
if ($this->title) { $a->title($this->translate($this->getTitle($row))); } |
124
|
|
|
if ($this->class) { $a->class($this->getClass($row)); } |
125
|
|
|
if ($confirm = $this->getConfirm($row)) { $a->data('confirm', $this->translate($confirm)); } |
126
|
|
|
|
127
|
|
|
return $a; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set attribute title |
133
|
|
|
* @param string|callable $title |
134
|
|
|
* @return static |
135
|
|
|
*/ |
136
|
|
|
public function setTitle($title) |
137
|
|
|
{ |
138
|
|
|
if (!is_string($title) && !is_callable($title)) { |
139
|
|
|
throw new DataGridException( |
140
|
|
|
'Action title has to be either string or callback, that will return string' |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->title = $title; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get attribute title |
152
|
|
|
* @param Row $row |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function getTitle(Row $row) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
/** |
158
|
|
|
* If user callback was used for setting action title, it has to return string |
159
|
|
|
*/ |
160
|
|
|
if (is_callable($this->title)) { |
161
|
|
|
$title = call_user_func($this->title, $row->getItem()); |
162
|
|
|
|
163
|
|
|
if (!is_string($title)) { |
164
|
|
|
throw new DataGridException('Action class callback has to return string'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $title; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->title; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set attribute class |
176
|
|
|
* @param string|callable $class |
177
|
|
|
* @return static |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function setClass($class) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
if (!is_string($class) && !is_callable($class)) { |
182
|
|
|
throw new DataGridException( |
183
|
|
|
'Action class has to be either string or callback, that will return string' |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->class = $class; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get attribute class |
195
|
|
|
* @param Row $row |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function getClass(Row $row) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
/** |
201
|
|
|
* If user callback was used for setting action class, it has to return string |
202
|
|
|
*/ |
203
|
|
|
if (is_callable($this->class)) { |
204
|
|
|
$class = call_user_func($this->class, $row->getItem()); |
205
|
|
|
|
206
|
|
|
if (!is_string($class)) { |
207
|
|
|
throw new DataGridException('Action class callback has to return string'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $class; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this->class; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set icon |
219
|
|
|
* @param string|callable $icon |
220
|
|
|
* @return static|callable |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function setIcon($icon) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
if (!is_string($icon) && !is_callable($icon)) { |
225
|
|
|
throw new DataGridException( |
226
|
|
|
'Action icon has to be either string or callback, that will return string' |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$this->icon = $icon; |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get icon |
238
|
|
|
* @param Row $row |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
View Code Duplication |
public function getIcon(Row $row) |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
/** |
244
|
|
|
* If user callback was used for setting action icon, it has to return string |
245
|
|
|
*/ |
246
|
|
|
if (is_callable($this->icon)) { |
247
|
|
|
$icon = call_user_func($this->icon, $row->getItem()); |
248
|
|
|
|
249
|
|
|
if (!is_string($icon)) { |
250
|
|
|
throw new DataGridException('Action icon callback has to return string'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $icon; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $this->icon; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set confirm dialog |
262
|
|
|
* @param string|callable $message |
263
|
|
|
* @param string $column |
264
|
|
|
* @return static |
265
|
|
|
*/ |
266
|
|
|
public function setConfirm($message, $column = NULL) |
267
|
|
|
{ |
268
|
|
|
if (!is_string($message) && !is_callable($message)) { |
269
|
|
|
throw new DataGridException( |
270
|
|
|
'Action message has to be either string or callback, that will return string' |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$this->confirm = [$message, $column]; |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get confirm dialog for particular row item |
282
|
|
|
* @param Row $row |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
public function getConfirm(Row $row) |
286
|
|
|
{ |
287
|
|
|
if (!$this->confirm) { |
288
|
|
|
return NULL; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$question = $this->confirm[0]; |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* If user callback was used for setting action confirmation dialog, it has to return string |
295
|
|
|
*/ |
296
|
|
|
if (is_callable($question)) { |
297
|
|
|
$question = call_user_func($question, $row->getItem()); |
298
|
|
|
|
299
|
|
|
if (!is_string($question)) { |
300
|
|
|
throw new DataGridException('Action confirmation dialog callback has to return string'); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (!$this->confirm[1]) { |
305
|
|
|
return $question; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return str_replace('%s', $row->getValue($this->confirm[1]), $question); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Setting data attributes |
314
|
|
|
* @param string $key |
315
|
|
|
* @param mixed $value |
316
|
|
|
*/ |
317
|
|
|
public function setDataAttribute($key, $value) |
318
|
|
|
{ |
319
|
|
|
$this->data_attributes[$key] = $value; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Create link to custom destination |
325
|
|
|
* @param Row $row |
326
|
|
|
* @return string |
327
|
|
|
* @throws DataGridHasToBeAttachedToPresenterComponentException |
328
|
|
|
*/ |
329
|
|
|
protected function createLink(Row $row) |
330
|
|
|
{ |
331
|
|
|
try { |
332
|
|
|
$parent = $this->grid->getParent(); |
333
|
|
|
} catch (DataGridHasToBeAttachedToPresenterComponentException $e) { |
334
|
|
|
$parent = $this->grid->getPresenter(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $parent->link($this->href, $this->getItemParams($row)); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
343
|
|
|
* @param Row $row |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
View Code Duplication |
protected function getItemParams(Row $row) |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
$return = []; |
349
|
|
|
|
350
|
|
|
foreach ($this->params as $param_name => $param) { |
351
|
|
|
$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return $return; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Translator helper |
360
|
|
|
* @param string $message |
361
|
|
|
* @return string |
362
|
|
|
*/ |
363
|
|
|
protected function translate($message) |
364
|
|
|
{ |
365
|
|
|
return $this->grid->getTranslator()->translate($message); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
} |
369
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.