1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Grido (http://grido.bugyik.cz) |
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; |
13
|
|
|
|
14
|
|
|
use Grido\Grid; |
15
|
|
|
use Nette\Utils\Html; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Toolbar button. |
19
|
|
|
* |
20
|
|
|
* @package Grido |
21
|
|
|
* @subpackage Components |
22
|
|
|
* @author Petr Bugyík |
23
|
|
|
* |
24
|
|
|
* @property-read Html $element |
25
|
|
|
* @property-write Html $elementPrototype |
26
|
|
|
* @property string $options |
27
|
|
|
*/ |
28
|
|
|
class Button extends Component |
29
|
1 |
|
{ |
30
|
|
|
const ID = 'buttons'; |
31
|
|
|
|
32
|
|
|
/** @var string first param for method $presenter->link() */ |
33
|
|
|
protected $destination; |
34
|
|
|
|
35
|
|
|
/** @var array second param for method $presenter->link() */ |
36
|
|
|
protected $arguments = []; |
37
|
|
|
|
38
|
|
|
/** @var Html <a> html tag */ |
39
|
|
|
protected $elementPrototype; |
40
|
|
|
|
41
|
|
|
/** @var array */ |
42
|
|
|
protected $options = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Grid $grid |
46
|
|
|
* @param string $name |
47
|
|
|
* @param string $label |
48
|
|
|
* @param string $destination - first param for method $presenter->link() |
49
|
|
|
* @param array $arguments - second param for method $presenter->link() |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Grid $grid, $name, $label = NULL, $destination = NULL, array $arguments = []) |
52
|
|
|
{ |
53
|
1 |
|
$this->label = $label; |
54
|
1 |
|
$this->destination = $destination; |
55
|
1 |
|
$this->arguments = $arguments; |
56
|
|
|
|
57
|
1 |
|
$this->addComponentToGrid($grid, $name); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets name of icon. |
62
|
|
|
* @param string $name |
63
|
|
|
* @return Action |
64
|
|
|
*/ |
65
|
|
|
public function setIcon($name) |
66
|
|
|
{ |
67
|
1 |
|
$this->setOption('icon', $name); |
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sets user-specific option. |
73
|
|
|
* @param string $key |
74
|
|
|
* @param mixed $value |
75
|
|
|
* @return Action |
76
|
|
|
*/ |
77
|
|
|
public function setOption($key, $value) |
78
|
|
|
{ |
79
|
1 |
|
if ($value === NULL) { |
80
|
1 |
|
unset($this->options[$key]); |
81
|
|
|
|
82
|
1 |
|
} else { |
83
|
1 |
|
$this->options[$key] = $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets html element. |
91
|
|
|
* @param Html $elementPrototype |
92
|
|
|
* @return Button |
93
|
|
|
*/ |
94
|
|
|
public function setElementPrototype(Html $elementPrototype) |
95
|
|
|
{ |
96
|
1 |
|
$this->elementPrototype = $elementPrototype; |
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/**********************************************************************************************/ |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Html |
104
|
|
|
* @internal |
105
|
|
|
*/ |
106
|
|
|
public function getElement() |
107
|
|
|
{ |
108
|
1 |
|
$element = clone $this->getElementPrototype(); |
109
|
|
|
|
110
|
1 |
|
$href = $this->presenter->link($this->getDestination(), $this->getArguments()); |
111
|
1 |
|
$element->href($href); |
112
|
|
|
|
113
|
1 |
|
return $element; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns element prototype (<a> html tag). |
118
|
|
|
* @return Html |
119
|
|
|
* @throws Exception |
120
|
|
|
*/ |
121
|
|
|
public function getElementPrototype() |
122
|
|
|
{ |
123
|
1 |
|
if ($this->elementPrototype === NULL) { |
124
|
1 |
|
$this->elementPrototype = Html::el('a') |
125
|
1 |
|
->setClass(['grid-button-' . $this->getName()]) |
126
|
1 |
|
->setText($this->label); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
1 |
|
if (isset($this->elementPrototype->class)) { |
130
|
1 |
|
$this->elementPrototype->class = (array) $this->elementPrototype->class; |
131
|
1 |
|
} |
132
|
|
|
|
133
|
1 |
|
return $this->elementPrototype; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns user-specific option. |
138
|
|
|
* @param string $key |
139
|
|
|
* @param mixed $default |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
public function getOption($key, $default = NULL) |
143
|
|
|
{ |
144
|
1 |
|
return isset($this->options[$key]) |
145
|
1 |
|
? $this->options[$key] |
146
|
1 |
|
: $default; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns user-specific options. |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getOptions() |
154
|
|
|
{ |
155
|
1 |
|
return $this->options; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
* @internal |
161
|
|
|
*/ |
162
|
|
|
public function getDestination() |
163
|
|
|
{ |
164
|
1 |
|
if ($this->destination === NULL) { |
165
|
1 |
|
$this->destination = $this->getName(); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
return $this->destination; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return array |
173
|
|
|
* @internal |
174
|
|
|
*/ |
175
|
|
|
public function getArguments() |
176
|
|
|
{ |
177
|
1 |
|
return $this->arguments; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/**********************************************************************************************/ |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @throws Exception |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function render() |
187
|
|
|
{ |
188
|
1 |
|
echo $this->getElement(); |
189
|
1 |
|
} |
190
|
|
|
} |
191
|
|
|
|