Completed
Push — master ( 462f88...b88904 )
by Jean-Christophe
03:19
created

HtmlButton::setColor()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 2
nop 1
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
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 View Code Duplication
	public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
		$this->content=new HtmlButton("button-" . $this->identifier, $this->content);
113
		$this->content->setTagName("div");
114
		$label=new HtmlLabel("label-" . $this->identifier, $label, $icon,"a");
115
		$label->setBasic();
116
		$this->addContent($label, $before);
117
		return $label;
118
	}
119
120
	/*
121
	 * (non-PHPdoc)
122
	 * @see \Ajax\common\html\BaseHtml::fromArray()
123
	 */
124
	public function fromArray($array) {
125
		$array=parent::fromArray($array);
126
		foreach ( $array as $key => $value ) {
127
			$this->setProperty($key, $value);
128
		}
129
		return $array;
130
	}
131
132
	/**
133
	 * hint towards a positive consequence
134
	 * @return HtmlButton
135
	 */
136
	public function setPositive() {
137
		return $this->addToProperty("class", "positive");
138
	}
139
140
	public function setColor($color){
141
		//TODO check button content
142
		if(\is_array($this->content)){
143
			foreach ($this->content as $content){
144
				if($content instanceof HtmlButton)
145
					$content->setColor($color);
146
			}
147
		}
148
		else
149
			parent::setColor($color);
150
		return $this;
151
	}
152
153
	/**
154
	 * hint towards a negative consequence
155
	 * @return HtmlButton
156
	 */
157
	public function setNegative() {
158
		return $this->addToProperty("class", "negative");
159
	}
160
161
	/**
162
	 * formatted to toggle on/off
163
	 * @return HtmlButton
164
	 */
165
	public function setToggle() {
166
		$this->onCreate("$('#".$this->identifier."').state();");
167
		return $this->addToProperty("class", "toggle");
168
	}
169
170
	/**
171
	 *
172
	 * @return HtmlButton
173
	 */
174
	public function setCircular() {
175
		return $this->addToProperty("class", "circular");
176
	}
177
178
	/**
179
	 * button is less pronounced
180
	 * @return HtmlButton
181
	 */
182
	public function setBasic() {
183
		return $this->addToProperty("class", "basic");
184
	}
185
186
	public function setEmphasis($value) {
187
		return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants());
188
	}
189
190
	public function setLoading() {
191
		return $this->addToProperty("class", "loading");
192
	}
193
194
	/**
195
	 * Returns a new social Button
196
	 * @param string $identifier
197
	 * @param string $social
198
	 * @param string $value
199
	 * @return HtmlButton
200
	 */
201
	public static function social($identifier, $social, $value=NULL) {
202
		if ($value === NULL)
203
			$value=\ucfirst($social);
204
		$return=new HtmlButton($identifier, $value);
205
		$return->addIcon($social);
206
		return $return->addToPropertyCtrl("class", $social, Social::getConstants());
207
	}
208
209
	/**
210
	 * Returns a new labeled Button
211
	 * @param string $identifier
212
	 * @param string $value
213
	 * @param string $icon
214
	 * @param boolean $before
215
	 * @return \Ajax\semantic\html\elements\HtmlButton
216
	 */
217
	public static function labeled($identifier, $value, $icon, $before=true) {
218
		$result=new HtmlButton($identifier, $value);
219
		$result->addIcon($icon, $before, true);
220
		return $result;
221
	}
222
223
	/**
224
	 * Returns a new icon Button
225
	 * @param string $identifier
226
	 * @param string $icon
227
	 * @return HtmlButton
228
	 */
229
	public static function icon($identifier, $icon) {
230
		$result=new HtmlButton($identifier);
231
		$result->asIcon($icon);
232
		return $result;
233
	}
234
235
	/**
236
	 * {@inheritDoc}
237
	 * @see HtmlSemDoubleElement::asLink()
238
	 */
239
	public function asLink($href=NULL,$target=NULL) {
240
		$lnk=new HtmlLink("lnk-".$this->identifier,$href,$this->content,$target);
241
		$this->content=$lnk;
242
		return $this;
243
	}
244
}