1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Button view helper styled for Bootstrap 3. |
5
|
|
|
* |
6
|
|
|
* @author Leandro Silva <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @category LosUi |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/Lansoweb/LosUi/blob/master/LICENSE MIT License |
11
|
|
|
* |
12
|
|
|
* @link http://github.com/LansoWeb/LosUi |
13
|
|
|
* @link http://getbootstrap.com/css/#buttons |
14
|
|
|
*/ |
15
|
|
|
namespace LosUi\View\Helper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Button view helper styled for Bootstrap 3. |
19
|
|
|
* |
20
|
|
|
* @author Leandro Silva <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @category LosUi |
23
|
|
|
* |
24
|
|
|
* @license https://github.com/Lansoweb/LosUi/blob/master/LICENSE MIT License |
25
|
|
|
* |
26
|
|
|
* @link http://github.com/LansoWeb/LosUi |
27
|
|
|
* @link http://getbootstrap.com/css/#buttons |
28
|
|
|
*/ |
29
|
|
|
class Button |
30
|
|
|
{ |
31
|
|
|
const TYPE_BUTTON = 0; |
32
|
|
|
|
33
|
|
|
const TYPE_ANCHOR = 1; |
34
|
|
|
|
35
|
|
|
const TYPE_INPUT = 2; |
36
|
|
|
|
37
|
|
|
protected $formatButton = '<button type="button" class="btn %s"%s>%s</button>'; |
38
|
|
|
|
39
|
|
|
protected $formatAnchor = '<a href="%s" role="button" class="btn %s"%s>%s</a>'; |
40
|
|
|
|
41
|
|
|
protected $formatInput = '<input type="%s" value="%s" class="btn %s"%s>'; |
42
|
|
|
|
43
|
|
|
protected $type = self::TYPE_BUTTON; |
44
|
|
|
|
45
|
|
|
protected $value = null; |
46
|
|
|
|
47
|
|
|
protected $size = null; |
48
|
|
|
|
49
|
|
|
protected $isBlock = false; |
50
|
|
|
|
51
|
|
|
protected $isActive = false; |
52
|
|
|
|
53
|
|
|
protected $isDisabled = false; |
54
|
|
|
|
55
|
|
|
protected $anchor = '#'; |
56
|
|
|
|
57
|
|
|
protected $anchorTarget = null; |
58
|
|
|
|
59
|
|
|
protected $inputType = 'button'; |
60
|
|
|
|
61
|
|
|
protected $id = null; |
62
|
|
|
|
63
|
|
|
protected $name = null; |
64
|
|
|
|
65
|
|
|
public function asAnchor($anchor = '#', $target = null) |
66
|
|
|
{ |
67
|
|
|
$this->type = self::TYPE_ANCHOR; |
68
|
|
|
$this->anchor = $anchor; |
69
|
|
|
$this->anchorTarget = $target; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function asInput($type = 'button') |
75
|
|
|
{ |
76
|
|
|
if ($type != 'button' && $type != 'submit' && $type != 'reset') { |
77
|
|
|
throw new \InvalidArgumentException('Input type must be "button", "submit" or "reset"'); |
78
|
|
|
} |
79
|
|
|
$this->type = self::TYPE_INPUT; |
80
|
|
|
$this->inputType = $type; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function asButton() |
86
|
|
|
{ |
87
|
|
|
$this->type = self::TYPE_BUTTON; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* Color methods */ |
93
|
|
|
public function useDefault($value, $style = null) |
94
|
|
|
{ |
95
|
|
|
return $this->render($value, 'btn-default', $style); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function primary($value, $style = null) |
99
|
|
|
{ |
100
|
|
|
return $this->render($value, 'btn-primary', $style); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function success($value, $style = null) |
104
|
|
|
{ |
105
|
|
|
return $this->render($value, 'btn-success', $style); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function info($value, $style = null) |
109
|
|
|
{ |
110
|
|
|
return $this->render($value, 'btn-info', $style); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function warning($value, $style = null) |
114
|
|
|
{ |
115
|
|
|
return $this->render($value, 'btn-warning', $style); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function danger($value, $style = null) |
119
|
|
|
{ |
120
|
|
|
return $this->render($value, 'btn-danger', $style); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function link($value, $style = null) |
124
|
|
|
{ |
125
|
|
|
return $this->render($value, 'btn-link', $style); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/* Size methods */ |
129
|
|
|
public function setLarge() |
130
|
|
|
{ |
131
|
|
|
$this->size = 'btn-lg'; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setSmall() |
137
|
|
|
{ |
138
|
|
|
$this->size = 'btn-sm'; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setExtraSmall() |
144
|
|
|
{ |
145
|
|
|
$this->size = 'btn-xs'; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/* Block method */ |
151
|
|
|
public function isBlock($block = true) |
152
|
|
|
{ |
153
|
|
|
if (! is_bool($block)) { |
154
|
|
|
$block = false; |
155
|
|
|
} |
156
|
|
|
$this->isBlock = $block; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/* Active method */ |
162
|
|
|
public function isActive($active = true) |
163
|
|
|
{ |
164
|
|
|
if (! is_bool($active)) { |
165
|
|
|
$active = false; |
166
|
|
|
} |
167
|
|
|
$this->isActive = $active; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/* Disabled method */ |
173
|
|
|
public function isDisabled($disabled = true) |
174
|
|
|
{ |
175
|
|
|
if (! is_bool($disabled)) { |
176
|
|
|
$disabled = false; |
177
|
|
|
} |
178
|
|
|
$this->isDisabled = $disabled; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function setValue($value) |
184
|
|
|
{ |
185
|
|
|
$this->value = $value; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function setId($id) |
191
|
|
|
{ |
192
|
|
|
$this->id = $id; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setName($name) |
198
|
|
|
{ |
199
|
|
|
$this->name = $name; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/* default methods */ |
205
|
|
|
public function render($value, $class = 'btn-default', $style = null) |
206
|
|
|
{ |
207
|
|
|
$class = trim($class); |
208
|
|
|
|
209
|
|
|
$extra = ''; |
210
|
|
|
if ($this->isDisabled) { |
211
|
|
|
$extra .= ' disabled="disabled"'; |
212
|
|
|
} |
213
|
|
|
if ($this->id !== null) { |
214
|
|
|
$extra .= ' id="'.$this->id.'"'; |
215
|
|
|
} |
216
|
|
|
if ($this->name !== null) { |
217
|
|
|
$extra .= ' name="'.$this->name.'"'; |
218
|
|
|
} |
219
|
|
|
if ($style !== null) { |
220
|
|
|
$extra .= ' style="'.$style.'"'; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if ($this->isActive) { |
224
|
|
|
$class .= ' active'; |
225
|
|
|
} |
226
|
|
|
if ($this->isBlock) { |
227
|
|
|
$class .= ' btn-block'; |
228
|
|
|
} |
229
|
|
|
if ($this->size !== null) { |
230
|
|
|
$class .= ' '.$this->size; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if ($this->type == self::TYPE_ANCHOR) { |
234
|
|
|
if ($this->anchorTarget !== null) { |
235
|
|
|
$extra .= ' target="'.$this->anchorTarget.'"'; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return sprintf($this->formatAnchor, $this->anchor, $class, $extra, $value); |
239
|
|
|
} elseif ($this->type == self::TYPE_INPUT) { |
240
|
|
|
return sprintf($this->formatInput, $this->inputType, $value, $class, $extra); |
241
|
|
|
} else { |
242
|
|
|
return sprintf($this->formatButton, $class, $extra, $value); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function __invoke($value = null, $class = 'default', $style = null) |
247
|
|
|
{ |
248
|
|
|
if ($value) { |
249
|
|
|
return $this->render($value, "btn-$class", $style); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|