forked from janmarek/gridito-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseButton.php
207 lines (158 loc) · 3.41 KB
/
BaseButton.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
<?php
namespace Gridito;
use Nette\Application\PresenterComponent;
use Nette\Application\ForbiddenRequestException;
use Nette\Web\Html;
/**
* Button base
*
* @author Jan Marek
* @license MIT
*/
abstract class BaseButton extends PresenterComponent {
// <editor-fold defaultstate="collapsed" desc="variables">
/** @var string */
private $label;
/** @var callback */
private $handler;
/** @var string */
private $icon;
/** @var bool|callback */
private $visible = true;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="getters & setters">
/**
* Get label
* @return string
*/
public function getLabel() {
return $this->label;
}
/**
* Set label
* @param string $label
* @return BaseButton
*/
public function setLabel($label) {
$this->label = $label;
return $this;
}
/**
* Get handler
* @return callback
*/
public function getHandler() {
return $this->handler;
}
/**
* Set handler
* @param callback $handler
* @return BaseButton
*/
public function setHandler($handler) {
$this->handler = $handler;
return $this;
}
/**
* Get jQuery UI icon
* @return string
*/
public function getIcon() {
return $this->icon;
}
/**
* Set jQuery UI icon
* @param string $icon
* @return BaseButton
*/
public function setIcon($icon) {
$this->icon = $icon;
return $this;
}
/**
* Get visible
* @return bool|callback
*/
public function getVisible() {
return $this->visible;
}
/**
* Is button visible
* @param mixed $row
* @return bool
*/
public function isVisible($row = null) {
return is_bool($this->visible) ? $this->visible : call_user_func($this->visible, $row);
}
/**
* Set visible
* @param bool|callback $visible
* @return BaseButton
*/
public function setVisible($visible) {
if (!is_bool($visible) && !\is_callable($visible)) {
throw new \InvalidArgumentException("Argument should be callable or boolean.");
}
$this->visible = $visible;
return $this;
}
/**
* @return Grid
*/
public function getGrid() {
return $this->getParent()->getParent();
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="signals">
/**
* Handle click signal
* @param string $token security token
* @param mixed $pk
*/
public function handleClick($token, $pk = null) {
$grid = $this->getGrid();
if ($token !== $this->getGrid()->getSecurityToken()) {
throw new ForbiddenRequestException("Security token does not match. Possible CSRF attack.");
}
call_user_func($this->handler, $grid->getModel()->processActionParam($pk));
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="rendering">
/**
* Get button link
* @param mixed $row
* @return string
*/
protected function getButtonLink($row) {
$grid = $this->getGrid();
$params["token"] = $grid->getSecurityToken();
if ($row) {
$params["pk"] = $row->{$grid->getPrimaryKey()};
}
return $this->link("click!", $params);
}
/**
* Create button element
* @param mixed $row
* @return Html
*/
protected function createButton($row = null) {
$el = Html::el("a")
->href($this->getButtonLink($row))
->setText($this->label);
if ($this->icon) {
$el->icon("ui-icon-" . $this->icon);
}
return $el;
}
/**
* Render button
* @param mixed $row
*/
public function render($row = null) {
if ($this->isVisible($row)) {
echo $this->createButton($row);
}
}
// </editor-fold>
}