1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\elements; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
6
|
|
|
use Ajax\semantic\html\base\traits\LabeledIconTrait; |
7
|
|
|
use Ajax\semantic\html\base\constants\Emphasis; |
8
|
|
|
use Ajax\semantic\html\base\constants\Social; |
9
|
|
|
use Ajax\semantic\html\elements\html5\HtmlLink; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Semantic Button component |
13
|
|
|
* @see http://phpmv-ui.kobject.net/index/direct/main/31 |
14
|
|
|
* @see http://semantic-ui.com/elements/button.html * @author jc |
15
|
|
|
* @version 1.001 |
16
|
|
|
*/ |
17
|
|
|
class HtmlButton extends HtmlSemDoubleElement { |
18
|
|
|
use LabeledIconTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructs an HTML Semantic button |
22
|
|
|
* @param string $identifier HTML id |
23
|
|
|
* @param string $value value of the Button |
24
|
|
|
* @param string $cssStyle btn-default, btn-primary... |
25
|
|
|
* @param string $onClick JS Code for click event |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) { |
|
|
|
|
28
|
|
|
parent::__construct($identifier, "button", "ui button"); |
29
|
|
|
$this->content=$value; |
30
|
|
|
if (isset($cssStyle)) { |
31
|
|
|
$this->setStyle($cssStyle); |
32
|
|
|
} |
33
|
|
|
if (isset($onClick)) { |
34
|
|
|
$this->onClick($onClick); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set the button value |
40
|
|
|
* @param string $value |
41
|
|
|
* @return \Ajax\semantic\html\HtmlButton |
42
|
|
|
*/ |
43
|
|
|
public function setValue($value) { |
44
|
|
|
$this->content=$value; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* define the button style |
50
|
|
|
* @param string|int $cssStyle |
51
|
|
|
* @return \Ajax\semantic\html\HtmlButton default : "" |
52
|
|
|
*/ |
53
|
|
|
public function setStyle($cssStyle) { |
54
|
|
|
return $this->addToProperty("class", $cssStyle); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setFocusable($value=true) { |
58
|
|
|
if ($value === true) |
59
|
|
|
$this->setProperty("tabindex", "0"); |
60
|
|
|
else { |
61
|
|
|
$this->removeProperty("tabindex"); |
62
|
|
|
} |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setAnimated($content, $animation="") { |
67
|
|
|
$this->setTagName("div"); |
68
|
|
|
$this->addToProperty("class", "animated " . $animation); |
69
|
|
|
$visible=new HtmlSemDoubleElement("visible-" . $this->identifier, "div"); |
70
|
|
|
$visible->setClass("visible content"); |
71
|
|
|
$visible->setContent($this->content); |
72
|
|
|
$hidden=new HtmlSemDoubleElement("hidden-" . $this->identifier, "div"); |
73
|
|
|
$hidden->setClass("hidden content"); |
74
|
|
|
$hidden->setContent($content); |
75
|
|
|
$this->content=array ($visible,$hidden ); |
76
|
|
|
return $hidden; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @param string|HtmlIcon $icon |
82
|
|
|
* @return \Ajax\semantic\html\elements\HtmlButton |
83
|
|
|
*/ |
84
|
|
|
public function asIcon($icon) { |
85
|
|
|
$iconO=$icon; |
86
|
|
|
if (\is_string($icon)) { |
87
|
|
|
$iconO=new HtmlIcon("icon-" . $this->identifier, $icon); |
88
|
|
|
} |
89
|
|
|
$this->addToProperty("class", "icon"); |
90
|
|
|
$this->content=$iconO; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function asSubmit() { |
95
|
|
|
$this->setProperty("type", "submit"); |
96
|
|
|
return $this->setTagName("button"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add and return a button label |
101
|
|
|
* @param string $caption |
|
|
|
|
102
|
|
|
* @param string $before |
103
|
|
|
* @param string $icon |
104
|
|
|
* @return \Ajax\semantic\html\elements\HtmlLabel |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function addLabel($label, $before=false, $icon=NULL) { |
|
|
|
|
107
|
|
|
$this->tagName="div"; |
108
|
|
|
$this->addToProperty("class", "labeled"); |
109
|
|
|
$this->content=new HtmlButton("button-" . $this->identifier, $this->content); |
110
|
|
|
$this->content->setTagName("div"); |
111
|
|
|
$label=new HtmlLabel("label-" . $this->identifier, $label, "a"); |
112
|
|
|
if(isset($icon)) |
113
|
|
|
$label->addIcon($icon); |
114
|
|
|
$label->setBasic(); |
115
|
|
|
$this->addContent($label, $before); |
|
|
|
|
116
|
|
|
return $label; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
* (non-PHPdoc) |
121
|
|
|
* @see \Ajax\common\html\BaseHtml::fromArray() |
122
|
|
|
*/ |
123
|
|
|
public function fromArray($array) { |
124
|
|
|
$array=parent::fromArray($array); |
125
|
|
|
foreach ( $array as $key => $value ) { |
126
|
|
|
$this->setProperty($key, $value); |
127
|
|
|
} |
128
|
|
|
return $array; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* hint towards a positive consequence |
133
|
|
|
* @return \Ajax\semantic\html\elements\HtmlButton |
134
|
|
|
*/ |
135
|
|
|
public function setPositive() { |
136
|
|
|
return $this->addToProperty("class", "positive"); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* hint towards a negative consequence |
141
|
|
|
* @return \Ajax\semantic\html\elements\HtmlButton |
142
|
|
|
*/ |
143
|
|
|
public function setNegative() { |
144
|
|
|
return $this->addToProperty("class", "negative"); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* formatted to toggle on/off |
149
|
|
|
* @return \Ajax\semantic\html\elements\HtmlButton |
150
|
|
|
*/ |
151
|
|
|
public function setToggle() { |
152
|
|
|
return $this->addToProperty("class", "toggle"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* @return \Ajax\semantic\html\elements\HtmlButton |
158
|
|
|
*/ |
159
|
|
|
public function setCircular() { |
160
|
|
|
return $this->addToProperty("class", "circular"); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* button is less pronounced |
165
|
|
|
* @return \Ajax\semantic\html\elements\HtmlButton |
166
|
|
|
*/ |
167
|
|
|
public function setBasic() { |
168
|
|
|
return $this->addToProperty("class", "basic"); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setEmphasis($value) { |
172
|
|
|
return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function setLoading() { |
176
|
|
|
return $this->addToProperty("class", "loading"); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public static function social($identifier, $social, $value=NULL) { |
180
|
|
|
if ($value === NULL) |
181
|
|
|
$value=\ucfirst($social); |
182
|
|
|
$return=new HtmlButton($identifier, $value); |
183
|
|
|
$return->addIcon($social); |
184
|
|
|
return $return->addToPropertyCtrl("class", $social, Social::getConstants()); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public static function labeled($identifier, $value, $icon, $before=true) { |
188
|
|
|
$result=new HtmlButton($identifier, $value); |
189
|
|
|
$result->addIcon($icon, $before, true); |
190
|
|
|
return $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public static function icon($identifier, $icon) { |
194
|
|
|
$result=new HtmlButton($identifier); |
195
|
|
|
$result->asIcon($icon); |
196
|
|
|
return $result; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function asLink($href=NULL) { |
200
|
|
|
$lnk=new HtmlLink("lnk-".$this->identifier,$href,$this->content); |
201
|
|
|
$this->content=$lnk; |
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.