1 | <?php |
||
19 | class Action extends Column |
||
20 | { |
||
21 | |||
22 | use Traits\ButtonIconTrait; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | public static $data_confirm_attribute_name = 'datagrid-confirm'; |
||
28 | |||
29 | /** |
||
30 | * @var string|callable |
||
31 | */ |
||
32 | protected $title; |
||
33 | |||
34 | /** |
||
35 | * @var string|callable |
||
36 | */ |
||
37 | protected $class; |
||
38 | |||
39 | /** |
||
40 | * @var string|callable |
||
41 | */ |
||
42 | protected $icon; |
||
43 | |||
44 | /** |
||
45 | * @var DataGrid |
||
46 | */ |
||
47 | protected $grid; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $href; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $name; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $params; |
||
63 | |||
64 | /** |
||
65 | * @var array|callable |
||
66 | */ |
||
67 | protected $confirm; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $data_attributes = []; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @param DataGrid $grid |
||
77 | * @param string $href |
||
78 | * @param string $name |
||
79 | * @param array $params |
||
80 | */ |
||
81 | public function __construct(DataGrid $grid, $href, $name, $params) |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Render row item into template |
||
94 | * @param Row $row |
||
95 | * @return mixed |
||
96 | */ |
||
97 | public function render(Row $row) |
||
98 | { |
||
99 | /** |
||
100 | * Renderer function may be used |
||
101 | */ |
||
102 | try { |
||
103 | return $this->useRenderer($row); |
||
104 | } catch (DataGridColumnRendererException $e) { |
||
|
|||
105 | /** |
||
106 | * Do not use renderer |
||
107 | */ |
||
108 | } |
||
109 | |||
110 | $link = $this->createLink($this->href, $this->getItemParams($row, $this->params)); |
||
111 | |||
112 | $a = Html::el('a')->href($link); |
||
113 | |||
114 | $this->tryAddIcon($a, $this->getIcon($row), $this->getName()); |
||
115 | |||
116 | if (!empty($this->data_attributes)) { |
||
117 | foreach ($this->data_attributes as $key => $value) { |
||
118 | $a->data($key, $value); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $a->add($this->translate($this->getName())); |
||
123 | |||
124 | if ($this->title) { |
||
125 | $a->title($this->translate($this->getTitle($row))); |
||
126 | } |
||
127 | |||
128 | if ($this->class) { |
||
129 | $a->class($this->getClass($row)); |
||
130 | } |
||
131 | |||
132 | if ($confirm = $this->getConfirm($row)) { |
||
133 | $a->data(static::$data_confirm_attribute_name, $confirm); |
||
134 | } |
||
135 | |||
136 | return $a; |
||
137 | } |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Set attribute title |
||
142 | * @param string|callable $title |
||
143 | * @return static |
||
144 | * @throws DataGridException |
||
145 | */ |
||
146 | public function setTitle($title) |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Get attribute title |
||
158 | * @param Row $row |
||
159 | * @return string |
||
160 | * @throws DataGridException |
||
161 | */ |
||
162 | public function getTitle(Row $row) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Set attribute class |
||
173 | * @param string|callable $class |
||
174 | * @return static |
||
175 | * @throws DataGridException |
||
176 | */ |
||
177 | public function setClass($class) |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Get attribute class |
||
189 | * @param Row $row |
||
190 | * @return string |
||
191 | * @throws DataGridException |
||
192 | */ |
||
193 | public function getClass(Row $row) |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Set icon |
||
204 | * @param string|callable $icon |
||
205 | * @return static|callable |
||
206 | * @throws DataGridException |
||
207 | */ |
||
208 | public function setIcon($icon) |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Get icon |
||
220 | * @param Row $row |
||
221 | * @return string |
||
222 | * @throws DataGridException |
||
223 | */ |
||
224 | public function getIcon(Row $row) |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Set confirm dialog |
||
235 | * @param string|callable $message |
||
236 | * @param string $column |
||
237 | * @return static |
||
238 | * @throws DataGridException |
||
239 | */ |
||
240 | public function setConfirm($message, $column = NULL) |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Get confirm dialog for particular row item |
||
252 | * @param Row $row |
||
253 | * @return string |
||
254 | * @throws DataGridException |
||
255 | */ |
||
256 | public function getConfirm(Row $row) |
||
279 | |||
280 | |||
281 | /** |
||
282 | * Setting data attributes |
||
283 | * @param string $key |
||
284 | * @param mixed $value |
||
285 | * @return static |
||
286 | */ |
||
287 | public function setDataAttribute($key, $value) |
||
293 | |||
294 | |||
295 | /** |
||
296 | * Check whether given property is string or callable |
||
297 | * @param mixed $property |
||
298 | * @return void |
||
299 | * @throws DataGridException |
||
300 | */ |
||
301 | protected function checkPropertyStringOrCallable($property, $name) |
||
309 | |||
310 | |||
311 | /** |
||
312 | * Check whether given property is string or callable |
||
313 | * in that case call callback and check property and return it |
||
314 | * @param Row $row |
||
315 | * @param string|callable|null $property |
||
316 | * @param string $name |
||
317 | * @return string |
||
318 | * @throws DataGridException |
||
319 | */ |
||
320 | public function getPropertyStringOrCallableGetString(Row $row, $property, $name) |
||
344 | |||
345 | |||
346 | /** |
||
347 | * Translator helper |
||
348 | * @param string $message |
||
349 | * @return string |
||
350 | */ |
||
351 | protected function translate($message) |
||
355 | |||
356 | } |
||
357 |