Passed
Push — master ( 71a1f0...84bf4a )
by Jean-Christophe
03:31 queued 19s
created

HtmlButtonGroups::getElement()   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 1
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\HtmlSemCollection;
7
use Ajax\JsUtils;
8
use Ajax\semantic\html\modules\HtmlDropdown;
9
10
/**
11
 * Semantic UI Buttongroups component
12
 * @see http://phpmv-ui.kobject.net/index/direct/main/50
13
 * @see http://semantic-ui.com/elements/button.html
14
 * @author jc
15
 * @version 1.002
16
 */
17
class HtmlButtonGroups extends HtmlSemCollection {
18
	protected $_dropdown;
19
	public function __construct($identifier, $elements=array(), $asIcons=false) {
20
		parent::__construct($identifier, "div", "ui buttons");
21
		if ($asIcons === true)
22
			$this->asIcons();
23
		$this->addElements($elements, $asIcons);
24
	}
25
	protected function createItem($value){
26
		return new HtmlButton("", $value);
27
	}
28
29
	/**
30
	 * @param array $items
31
	 * @param boolean $asCombo
32
	 * @return HtmlDropdown
33
	 */
34
	public function addDropdown($items,$asCombo=false){
35
		$dd= new HtmlDropdown("dd-".$this->identifier,null,$items);
36
		$dd->asButton();
37
		if($asCombo){
38
			$dd->setAction("combo");
39
			$dd->addToProperty("class", "combo");
40
		}
41
		$this->_dropdown=$dd;
42
		return $this->addElement($dd);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addElement($dd) returns the type Ajax\semantic\html\elements\HtmlButton which is incompatible with the documented return type Ajax\semantic\html\modules\HtmlDropdown.
Loading history...
43
	}
44
45
46
	/**
47
	 * @param mixed $element
48
	 * @param boolean $asIcon
49
	 * @return HtmlButton
50
	 */
51
	public function addElement($element, $asIcon=false) {
52
		$item=$this->addItem($element);
53
		if($asIcon)
54
			$item->asIcon($element);
55
		return $item;
56
	}
57
58
	public function addElements($elements, $asIcons=false) {
59
		foreach ( $elements as $element ) {
60
			$this->addElement($element, $asIcons);
61
		}
62
		return $this;
63
	}
64
65
	public function insertOr($aferIndex=0, $or="or") {
66
		$orElement=new HtmlSemDoubleElement("", "div", "or");
67
		$orElement->setProperty("data-text", $or);
68
		array_splice($this->content, $aferIndex + 1, 0, array ($orElement ));
69
		return $this;
70
	}
71
72
	/*
73
	 * (non-PHPdoc)
74
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
75
	 */
76
	public function fromArray($array) {
77
		$this->addElements($array);
78
	}
79
80
	public function asIcons() {
81
		foreach ( $this->content as $item ) {
82
			if($item instanceof HtmlButton)
83
			$item->asIcon($item->getContent());
84
		}
85
		return $this->addToProperty("class", "icons");
86
	}
87
88
	/**
89
	 * Adds an icon on each button
90
	 * @param array $icons
91
	 * @return HtmlButtonGroups
92
	 */
93
	public function addIcons($icons){
94
		foreach ( $this->content as $index=>$item ) {
95
			if($item instanceof HtmlButton && isset($icons[$index]))
96
				$item->addIcon($icons[$index]);
97
		}
98
		return $this;
99
	}
100
101
	public function setVertical() {
102
		return $this->addToProperty("class", "vertical");
103
	}
104
105
	public function setLabeled() {
106
		return $this->addToProperty("class", "labeled icon");
107
	}
108
109
	/**
110
	 * Return the element at index
111
	 * @param int $index
112
	 * @return HtmlButton
113
	 */
114
	public function getElement($index) {
115
		return $this->getItem($index);
116
	}
117
118
	public function setElement($index, $button) {
119
		$this->setItem($index, $button);
120
		return $this;
121
	}
122
123
	/*
124
	 * (non-PHPdoc)
125
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
126
	 */
127
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
128
		foreach ( $this->content as $element ) {
129
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
130
		}
131
		return $this;
132
	}
133
134
	public function getElements() {
135
		return $this->content;
136
	}
137
138
	public function addClasses($classes=array()) {
139
		$i=0;
140
		if(!\is_array($classes)){
141
			$classes=array_fill (0,$this->count(),$classes);
142
		}
143
		foreach ( $this->content as $button ) {
144
			$button->addToProperty("class", $classes[$i++]);
145
		}
146
		return $this;
147
	}
148
149
	/*
150
	 * (non-PHPdoc)
151
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
152
	 */
153
	public function fromDatabaseObject($object, $function) {
154
		$this->addElement($function($object));
155
	}
156
157
	public function run(JsUtils $js){
158
		$result= parent::run($js);
159
		return $result->setItemSelector(".ui.button");
160
	}
161
162
	public function getDropdown() {
163
		return $this->_dropdown;
164
	}
165
166
}
167