Passed
Push — master ( d75a73...4a339e )
by Jean-Christophe
02:07
created

HtmlButtonGroups::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 5
rs 9.4285
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|HtmlButton
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);
43
	}
44
45
46
	/**
47
	 * @param mixed $element
48
	 * @param boolean $asIcon
49
	 * @return HtmlButton|HtmlDropdown
50
	 */
51
	public function addElement($element, $asIcon=false) {
52
		$item=$this->addItem($element);
53
		if($asIcon)
54
			$item->asIcon($element);
0 ignored issues
show
Bug introduced by
The method asIcon() does not exist on Ajax\common\html\HtmlDoubleElement. It seems like you code against a sub-type of Ajax\common\html\HtmlDoubleElement such as Ajax\semantic\html\content\HtmlDropdownItem or Ajax\semantic\html\modules\HtmlDimmer or Ajax\semantic\html\elements\HtmlButton or Ajax\semantic\html\elements\HtmlHeader or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\semantic\html\base\HtmlSemNavElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
			$item->/** @scrutinizer ignore-call */ 
55
          asIcon($element);
Loading history...
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 parent::getItem($index);
116
	}
117
	
118
	/**
119
	 * @return HtmlButton
120
	 */
121
	public function getItem($index) {
122
		return parent::getItem($index);
123
	}
124
125
	public function setElement($index, $button) {
126
		$this->setItem($index, $button);
127
		return $this;
128
	}
129
130
	/*
131
	 * (non-PHPdoc)
132
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
133
	 */
134
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
135
		foreach ( $this->content as $element ) {
136
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
137
		}
138
		return $this;
139
	}
140
141
	public function getElements() {
142
		return $this->content;
143
	}
144
145
	public function addClasses($classes=array()) {
146
		$i=0;
147
		if(!\is_array($classes)){
148
			$classes=array_fill (0,$this->count(),$classes);
149
		}
150
		foreach ( $this->content as $button ) {
151
			$button->addToProperty("class", $classes[$i++]);
152
		}
153
		return $this;
154
	}
155
156
	/*
157
	 * (non-PHPdoc)
158
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
159
	 */
160
	public function fromDatabaseObject($object, $function) {
161
		$this->addElement($function($object));
162
	}
163
164
	public function run(JsUtils $js){
165
		$result= parent::run($js);
166
		return $result->setItemSelector(".ui.button");
167
	}
168
169
	public function getDropdown() {
170
		return $this->_dropdown;
171
	}
172
173
}
174