|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Grido\Components\Actions; |
|
13
|
|
|
|
|
14
|
|
|
use Grido\Exception; |
|
15
|
|
|
use Nette\Utils\Html; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Action on one row. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Grido |
|
21
|
|
|
* @subpackage Components\Actions |
|
22
|
|
|
* @author Petr Bugyík |
|
23
|
|
|
* |
|
24
|
|
|
* @property-read Html $element |
|
25
|
|
|
* @property-write callback $customRender |
|
26
|
|
|
* @property-write callback $disable |
|
27
|
|
|
* @property Html $elementPrototype |
|
28
|
|
|
* @property string $primaryKey |
|
29
|
|
|
* @property string $options |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class Action extends \Grido\Components\Component |
|
32
|
1 |
|
{ |
|
33
|
|
|
const ID = 'actions'; |
|
34
|
|
|
|
|
35
|
|
|
/** @var Html <a> html tag */ |
|
36
|
|
|
protected $elementPrototype; |
|
37
|
|
|
|
|
38
|
|
|
/** @var callback for custom rendering */ |
|
39
|
|
|
protected $customRender; |
|
40
|
|
|
|
|
41
|
1 |
|
/** @var string - name of primary key f.e.: link->('Article:edit', array($primaryKey => 1)) */ |
|
42
|
|
|
protected $primaryKey; |
|
43
|
|
|
|
|
44
|
|
|
/** @var callback for disabling */ |
|
45
|
|
|
protected $disable; |
|
46
|
|
|
|
|
47
|
|
|
/** @var string */ |
|
48
|
|
|
protected $options; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \Grido\Grid $grid |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param string $label |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($grid, $name, $label) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->addComponentToGrid($grid, $name); |
|
58
|
|
|
|
|
59
|
1 |
|
$this->type = get_class($this); |
|
60
|
1 |
|
$this->label = $this->translate($label); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Sets html element. |
|
65
|
|
|
* @param Html $elementPrototype |
|
66
|
|
|
* @return Action |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setElementPrototype(Html $elementPrototype) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$this->elementPrototype = $elementPrototype; |
|
71
|
1 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Sets callback for custom rendering. |
|
76
|
|
|
* @param callback |
|
77
|
|
|
* @return Action |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setCustomRender($callback) |
|
80
|
1 |
|
{ |
|
81
|
1 |
|
$this->customRender = $callback; |
|
82
|
1 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Sets primary key. |
|
87
|
|
|
* @param string $primaryKey |
|
88
|
|
|
* @return Action |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setPrimaryKey($primaryKey) |
|
91
|
|
|
{ |
|
92
|
1 |
|
$this->primaryKey = $primaryKey; |
|
93
|
1 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Sets callback for disable. |
|
98
|
|
|
* Callback should return TRUE if the action is not allowed for current item. |
|
99
|
|
|
* @param callback |
|
100
|
|
|
* @return Action |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setDisable($callback) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$this->disable = $callback; |
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Sets client side confirm. |
|
110
|
|
|
* @param string|callback $confirm |
|
111
|
|
|
* @return Action |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setConfirm($confirm) |
|
114
|
|
|
{ |
|
115
|
1 |
|
$this->setOption('confirm', $confirm); |
|
116
|
1 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Sets name of icon. |
|
121
|
|
|
* @param string $name |
|
122
|
|
|
* @return Action |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setIcon($name) |
|
125
|
|
|
{ |
|
126
|
1 |
|
$this->setOption('icon', $name); |
|
127
|
1 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sets user-specific option. |
|
132
|
|
|
* @param string $key |
|
133
|
|
|
* @param mixed $value |
|
134
|
|
|
* @return Action |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setOption($key, $value) |
|
137
|
|
|
{ |
|
138
|
1 |
|
if ($value === NULL) { |
|
139
|
1 |
|
unset($this->options[$key]); |
|
140
|
|
|
|
|
141
|
1 |
|
} else { |
|
142
|
1 |
|
$this->options[$key] = $value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/**********************************************************************************************/ |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns element prototype (<a> html tag). |
|
152
|
|
|
* @return Html |
|
153
|
|
|
* @throws Exception |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getElementPrototype() |
|
156
|
|
|
{ |
|
157
|
1 |
|
if ($this->elementPrototype === NULL) { |
|
158
|
1 |
|
$this->elementPrototype = Html::el('a') |
|
159
|
1 |
|
->setClass(['grid-action-' . $this->getName()]) |
|
160
|
1 |
|
->setText($this->label); |
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
if (isset($this->elementPrototype->class)) { |
|
164
|
1 |
|
$this->elementPrototype->class = (array) $this->elementPrototype->class; |
|
165
|
1 |
|
} |
|
166
|
|
|
|
|
167
|
1 |
|
return $this->elementPrototype; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return string |
|
172
|
|
|
* @internal |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getPrimaryKey() |
|
175
|
|
|
{ |
|
176
|
1 |
|
if ($this->primaryKey === NULL) { |
|
177
|
1 |
|
$this->primaryKey = $this->grid->getPrimaryKey(); |
|
178
|
1 |
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
return $this->primaryKey; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param mixed $row |
|
185
|
|
|
* @return Html |
|
186
|
|
|
* @internal |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getElement($row) |
|
189
|
|
|
{ |
|
190
|
1 |
|
$element = clone $this->getElementPrototype(); |
|
191
|
|
|
|
|
192
|
1 |
|
if ($confirm = $this->getOption('confirm')) { |
|
193
|
1 |
|
$confirm = is_callable($confirm) |
|
194
|
1 |
|
? call_user_func_array($confirm, [$row]) |
|
195
|
1 |
|
: $confirm; |
|
196
|
|
|
|
|
197
|
1 |
|
$value = is_array($confirm) |
|
198
|
1 |
|
? vsprintf($this->translate(array_shift($confirm)), $confirm) |
|
199
|
1 |
|
: $this->translate($confirm); |
|
200
|
|
|
|
|
201
|
1 |
|
$element->setAttribute('data-grido-confirm', $value); |
|
202
|
1 |
|
} |
|
203
|
|
|
|
|
204
|
1 |
|
return $element; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Returns user-specific option. |
|
209
|
|
|
* @param string $key |
|
210
|
|
|
* @param mixed $default |
|
211
|
|
|
* @return mixed |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getOption($key, $default = NULL) |
|
214
|
|
|
{ |
|
215
|
1 |
|
return isset($this->options[$key]) |
|
216
|
1 |
|
? $this->options[$key] |
|
217
|
1 |
|
: $default; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Returns user-specific options. |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getOptions() |
|
225
|
|
|
{ |
|
226
|
1 |
|
return $this->options; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/**********************************************************************************************/ |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param mixed $row |
|
233
|
|
|
* @throws Exception |
|
234
|
|
|
* @return void |
|
235
|
|
|
*/ |
|
236
|
|
|
public function render($row) |
|
237
|
|
|
{ |
|
238
|
1 |
|
if (!$row || ($this->disable && call_user_func_array($this->disable, [$row]))) { |
|
239
|
1 |
|
return; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
1 |
|
$element = $this->getElement($row); |
|
243
|
|
|
|
|
244
|
1 |
|
if ($this->customRender) { |
|
245
|
1 |
|
echo call_user_func_array($this->customRender, [$row, $element]); |
|
246
|
1 |
|
return; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
1 |
|
echo $element->render(); |
|
250
|
1 |
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|