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