1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\elements; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Semantic UI Buttongroups component |
9
|
|
|
* @see http://semantic-ui.com/elements/button.html |
10
|
|
|
* @author jc |
11
|
|
|
* @version 1.001 |
12
|
|
|
*/ |
13
|
|
|
class HtmlButtonGroups extends HtmlSemDoubleElement { |
14
|
|
|
|
15
|
|
|
public function __construct($identifier, $elements=array(), $asIcons=false) { |
16
|
|
|
parent::__construct($identifier, "div", "ui buttons"); |
17
|
|
|
$this->content=array (); |
18
|
|
|
if ($asIcons === true) |
19
|
|
|
$this->asIcons(); |
20
|
|
|
$this->addElements($elements, $asIcons); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function addElement($element, $asIcon=false) { |
24
|
|
|
$elementO=$element; |
25
|
|
|
if (\is_string($element)) { |
26
|
|
|
if ($asIcon) { |
27
|
|
|
$elementO=new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content)); |
28
|
|
|
$elementO->asIcon($element); |
29
|
|
|
} else |
30
|
|
|
$elementO=new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content), $element); |
31
|
|
|
} |
32
|
|
|
$this->addContent($elementO); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function addElements($elements, $asIcons=false) { |
36
|
|
|
foreach ( $elements as $element ) { |
37
|
|
|
$this->addElement($element, $asIcons); |
38
|
|
|
} |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function insertOr($aferIndex=0, $or="or") { |
43
|
|
|
$orElement=new HtmlSemDoubleElement("or-" . $this->identifier, "div", "or"); |
44
|
|
|
$orElement->setProperty("data-text", $or); |
45
|
|
|
array_splice($this->content, $aferIndex + 1, 0, array ($orElement )); |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* (non-PHPdoc) |
51
|
|
|
* @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
52
|
|
|
*/ |
53
|
|
|
public function fromArray($array) { |
54
|
|
|
$this->addElements($array); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function asIcons() { |
58
|
|
|
foreach ( $this->content as $item ) { |
59
|
|
|
$item->asIcon($item->getContent()); |
60
|
|
|
} |
61
|
|
|
return $this->addToProperty("class", "icons"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setVertical() { |
65
|
|
|
return $this->addToProperty("class", "vertical"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setLabeled() { |
69
|
|
|
return $this->addToProperty("class", "labeled icon"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the element at index |
74
|
|
|
* @param int $index |
75
|
|
|
* @return HtmlButton |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function getElement($index) { |
|
|
|
|
78
|
|
|
if (is_int($index)) |
79
|
|
|
return $this->content[$index]; |
80
|
|
|
else { |
81
|
|
|
$elm=$this->getElementById($index, $this->content); |
82
|
|
|
return $elm; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setElement($index, $button) { |
87
|
|
|
$this->content[$index]=$button; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* (non-PHPdoc) |
93
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::on() |
94
|
|
|
*/ |
95
|
|
|
public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
96
|
|
|
foreach ( $this->content as $element ) { |
97
|
|
|
$element->on($event, $jsCode, $stopPropagation, $preventDefault); |
98
|
|
|
} |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getElements() { |
103
|
|
|
return $this->content; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function addClasses($classes=array()) { |
107
|
|
|
$i=0; |
108
|
|
|
foreach ( $this->content as $button ) { |
109
|
|
|
$button->addToProperty("class", $classes[$i++]); |
110
|
|
|
} |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/* |
115
|
|
|
* (non-PHPdoc) |
116
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
117
|
|
|
*/ |
118
|
|
|
public function fromDatabaseObject($object, $function) { |
119
|
|
|
$this->addElement($function($object)); |
120
|
|
|
} |
121
|
|
|
} |
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.