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\DataGridHasToBeAttachedToPresenterComponentException; |
14
|
|
|
use Ublaboo\DataGrid\Row; |
15
|
|
|
|
16
|
|
|
class Action extends Column |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $title; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $icon; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DataGrid |
36
|
|
|
*/ |
37
|
|
|
protected $grid; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $href; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $name; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $params; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $confirm; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var callable|NULL |
61
|
|
|
*/ |
62
|
|
|
protected $disable_callback; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param DataGrid $grid |
67
|
|
|
* @param string $href |
68
|
|
|
* @param string $name |
69
|
|
|
* @param array $params |
70
|
|
|
*/ |
71
|
|
|
public function __construct(DataGrid $grid, $href, $name, $params) |
72
|
|
|
{ |
73
|
|
|
$this->grid = $grid; |
74
|
|
|
$this->href = $href; |
75
|
|
|
$this->name = $name; |
76
|
|
|
$this->params = $params; |
77
|
|
|
|
78
|
|
|
$this->class = 'btn btn-xs btn-default'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Render row item into template |
84
|
|
|
* @param Row $row |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function render(Row $row) |
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* Disable function may be used (user may not have permission to see this action) |
91
|
|
|
*/ |
92
|
|
|
if ($this->disable_callback && call_user_func_array($this->disable_callback, [$row->getItem()])) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Renderer function may be used |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
if ($renderer = $this->getRenderer()) { |
|
|
|
|
100
|
|
|
if (!$renderer->getConditionCallback()) { |
101
|
|
|
return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (call_user_func_array($this->getRenderer(), [$row->getItem()])) { |
105
|
|
|
return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$parent = $this->grid->getParent(); |
111
|
|
|
} catch (DataGridHasToBeAttachedToPresenterComponentException $e) { |
|
|
|
|
112
|
|
|
$parent = $this->grid->getPresenter(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$a = Html::el('a') |
116
|
|
|
->href($parent->link($this->href, $this->getItemParams($row))); |
117
|
|
|
|
118
|
|
View Code Duplication |
if ($this->icon) { |
|
|
|
|
119
|
|
|
$a->add(Html::el('span')->class(DataGrid::$icon_prefix.$this->icon)); |
120
|
|
|
|
121
|
|
|
if (strlen($this->name)) { |
122
|
|
|
$a->add(' '); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$a->add($this->name); |
127
|
|
|
|
128
|
|
|
if ($this->title) { $a->title($this->title); } |
129
|
|
|
if ($this->class) { $a->class($this->class); } |
130
|
|
|
if ($confirm = $this->getConfirm($row)) { $a->data('confirm', $confirm); } |
131
|
|
|
|
132
|
|
|
return $a; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set attribute title |
138
|
|
|
* @param string $title |
139
|
|
|
* @return static |
140
|
|
|
*/ |
141
|
|
|
public function setTitle($title) |
142
|
|
|
{ |
143
|
|
|
$this->title = $title; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get attribute title |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getTitle() |
154
|
|
|
{ |
155
|
|
|
return $this->title; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set attribute class |
161
|
|
|
* @param string $class |
162
|
|
|
* @return static |
163
|
|
|
*/ |
164
|
|
|
public function setClass($class) |
165
|
|
|
{ |
166
|
|
|
$this->class = $class; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get attribute class |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getClass() |
177
|
|
|
{ |
178
|
|
|
return $this->class; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set icon |
184
|
|
|
* @param string $icon |
185
|
|
|
* @return static |
186
|
|
|
*/ |
187
|
|
|
public function setIcon($icon) |
188
|
|
|
{ |
189
|
|
|
$this->icon = $icon; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get icon |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function getIcon() |
200
|
|
|
{ |
201
|
|
|
return $this->icon; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Set confirm dialog |
207
|
|
|
* @param string $message |
208
|
|
|
* @param string $column |
209
|
|
|
* @return static |
210
|
|
|
*/ |
211
|
|
|
public function setConfirm($message, $column = NULL) |
212
|
|
|
{ |
213
|
|
|
$this->confirm = [$message, $column]; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get confirm dialog for particular row item |
221
|
|
|
* @param Row $row |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function getConfirm(Row $row) |
225
|
|
|
{ |
226
|
|
|
if (!$this->confirm) { |
|
|
|
|
227
|
|
|
return NULL; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (!$this->confirm[1]) { |
231
|
|
|
return $this->confirm[0]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return str_replace('%s', $row->getValue($this->confirm[1]), $this->confirm[0]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get callback for disable |
241
|
|
|
* @return callable|NULL |
242
|
|
|
*/ |
243
|
|
|
public function getDisable() |
244
|
|
|
{ |
245
|
|
|
return $this->disable_callback; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Set callback for disable. |
251
|
|
|
* If callback returns TRUE, action will not be rendered |
252
|
|
|
* @param callable $callback |
253
|
|
|
* @return static |
254
|
|
|
*/ |
255
|
|
|
public function setDisable($callback) |
256
|
|
|
{ |
257
|
|
|
$this->disable_callback = $callback; |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
265
|
|
|
* @param Row $row |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
protected function getItemParams(Row $row) |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
$return = []; |
271
|
|
|
|
272
|
|
|
foreach ($this->params as $param_name => $param) { |
273
|
|
|
$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $return; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
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.