Passed
Push — master ( bfca28...51d1e9 )
by Jean-Christophe
02:16
created

HtmlButton::setPositive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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\modules\HtmlDropdown;
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
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlButton extends HtmlSemDoubleElement {
19
	use LabeledIconTrait;
20
21
	/**
22
	 * Constructs an HTML Semantic button
23
	 * @param string $identifier HTML id
24
	 * @param string $value value of the Button
25
	 * @param string $cssStyle btn-default, btn-primary...
26
	 * @param string $onClick JS Code for click event
27
	 */
28
	public function __construct($identifier, $value=null, $cssStyle=null, $onClick=null) {
29
		parent::__construct($identifier, "button", "ui button");
30
		$this->content=$value;
31
		if (isset($cssStyle)) {
32
			$this->setStyle($cssStyle);
33
		}
34
		if (isset($onClick)) {
35
			$this->onClick($onClick);
36
		}
37
	}
38
39
	/**
40
	 * Set the button value
41
	 * @param string $value
42
	 * @return HtmlButton
43
	 */
44
	public function setValue($value) {
45
		$this->content=$value;
46
		return $this;
47
	}
48
49
	/**
50
	 * define the button style
51
	 * @param string|int $cssStyle
52
	 * @return HtmlButton default : ""
53
	 */
54
	public function setStyle($cssStyle) {
55
		return $this->addToProperty("class", $cssStyle);
56
	}
57
58
	public function setFocusable($value=true) {
59
		if ($value === true)
60
			$this->setProperty("tabindex", "0");
61
		else {
62
			$this->removeProperty("tabindex");
63
		}
64
		return $this;
65
	}
66
67
	public function setAnimated($content, $animation="") {
68
		$this->setTagName("div");
69
		$this->addToProperty("class", "animated " . $animation);
70
		$visible=new HtmlSemDoubleElement("visible-" . $this->identifier, "div");
71
		$visible->setClass("visible content");
72
		$visible->setContent($this->content);
73
		$hidden=new HtmlSemDoubleElement("hidden-" . $this->identifier, "div");
74
		$hidden->setClass("hidden content");
75
		$hidden->setContent($content);
76
		$this->content=array ($visible,$hidden );
77
		return $hidden;
78
	}
79
80
	/**
81
	 *
82
	 * @param string|HtmlIcon $icon
83
	 * @return HtmlButton
84
	 */
85
	public function asIcon($icon) {
86
		$iconO=$icon;
87
		if (\is_string($icon)) {
88
			$iconO=new HtmlIcon("icon-" . $this->identifier, $icon);
89
		}
90
		$this->addToProperty("class", "icon");
91
		$this->content=$iconO;
92
		return $this;
93
	}
94
95
	public function asSubmit() {
96
		$this->setProperty("type", "submit");
97
		return $this->setTagName("button");
98
	}
99
100
	/**
101
	 * Add and return a button label
102
	 * @param string $label
103
	 * @param boolean $before
104
	 * @param string $icon
105
	 * @return HtmlLabel
106
	 */
107
	public function addLabel($label, $before=false, $icon=NULL) {
108
		$this->tagName="div";$prefix="";
109
		if($before)
110
			$prefix="left ";
111
		$this->addToProperty("class", $prefix."labeled");
112
		$isIcon=(isset($this->content[0]) && $this->content[0] instanceof HtmlIcon);
113
		$this->content=new HtmlButton("button-" . $this->identifier, $this->content);
114
		if($isIcon){
115
			$this->content->addClass("icon");
116
		}
117
		$this->content->setTagName("div");
118
		$label=new HtmlLabel("label-" . $this->identifier, $label, $icon,"a");
119
		$label->setBasic();
120
		$this->addContent($label, $before);
121
		return $label;
122
	}
123
124
	/*
125
	 * (non-PHPdoc)
126
	 * @see \Ajax\common\html\BaseHtml::fromArray()
127
	 */
128
	public function fromArray($array) {
129
		$array=parent::fromArray($array);
130
		foreach ( $array as $key => $value ) {
131
			$this->setProperty($key, $value);
132
		}
133
		return $array;
134
	}
135
136
	/**
137
	 * hint towards a positive consequence
138
	 * @return HtmlButton
139
	 */
140
	public function setPositive() {
141
		return $this->addToProperty("class", "positive");
142
	}
143
144
	public function setColor($color){
145
		if(\is_array($this->content)){
146
			foreach ($this->content as $content){
147
				if($content instanceof HtmlButton)
148
					$content->setColor($color);
149
			}
150
		}
151
		else
152
			parent::setColor($color);
153
		return $this;
154
	}
155
156
	/**
157
	 * hint towards a negative consequence
158
	 * @return HtmlButton
159
	 */
160
	public function setNegative() {
161
		return $this->addToProperty("class", "negative");
162
	}
163
164
	/**
165
	 * formatted to toggle on/off
166
	 * @return HtmlButton
167
	 */
168
	public function setToggle() {
169
		$this->onCreate("$('#".$this->identifier."').state();");
170
		return $this->addToProperty("class", "toggle");
171
	}
172
173
	/**
174
	 *
175
	 * @return HtmlButton
176
	 */
177
	public function setCircular() {
178
		return $this->addToProperty("class", "circular");
179
	}
180
181
	/**
182
	 * button is less pronounced
183
	 * @return HtmlButton
184
	 */
185
	public function setBasic() {
186
		return $this->addToProperty("class", "basic");
187
	}
188
189
	public function setEmphasis($value) {
190
		return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants());
191
	}
192
193
	public function setLoading() {
194
		return $this->addToProperty("class", "loading");
195
	}
196
197
	/**
198
	 * Returns a new social Button
199
	 * @param string $identifier
200
	 * @param string $social
201
	 * @param string $value
202
	 * @return HtmlButton
203
	 */
204
	public static function social($identifier, $social, $value=NULL) {
205
		if ($value === NULL)
206
			$value=\ucfirst($social);
207
		$return=new HtmlButton($identifier, $value);
208
		$return->addIcon($social);
209
		return $return->addToPropertyCtrl("class", $social, Social::getConstants());
210
	}
211
212
	/**
213
	 * Returns a new labeled Button
214
	 * @param string $identifier
215
	 * @param string $value
216
	 * @param string $icon
217
	 * @param boolean $before
218
	 * @return \Ajax\semantic\html\elements\HtmlButton
219
	 */
220
	public static function labeled($identifier, $value, $icon, $before=true) {
221
		$result=new HtmlButton($identifier, $value);
222
		$result->addIcon($icon, $before, true);
223
		return $result;
224
	}
225
226
	/**
227
	 * Returns a new icon Button
228
	 * @param string $identifier
229
	 * @param string $icon
230
	 * @return HtmlButton
231
	 */
232
	public static function icon($identifier, $icon) {
233
		$result=new HtmlButton($identifier);
234
		$result->asIcon($icon);
235
		return $result;
236
	}
237
238
	/**
239
	 * {@inheritDoc}
240
	 * @see HtmlSemDoubleElement::asLink()
241
	 */
242
	public function asLink($href=NULL,$target=NULL) {
243
		parent::asLink($href,$target);
244
		return $this;
245
	}
246
247
	/**
248
	 * Returns a button with a dropdown button
249
	 * @param string $identifier
250
	 * @param string $value
251
	 * @param array $items
252
	 * @param boolean $asCombo
253
	 * @param string $icon
254
	 * @return HtmlButtonGroups
255
	 */
256
	public static function dropdown($identifier,$value,$items=[],$asCombo=false,$icon=null){
257
		$result=new HtmlButtonGroups($identifier,[$value]);
258
		$dd=$result->addDropdown($items,$asCombo);
259
		if(isset($icon) && $dd instanceof HtmlDropdown)
260
			$dd->setIcon($icon);
261
		return $result;
262
	}
263
}
264