Completed
Push — master ( 2aad8b...141d55 )
by Jean-Christophe
03:21
created

HtmlButtonGroups::addElements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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
8
/**
9
 * Semantic UI Buttongroups component
10
 * @see http://semantic-ui.com/elements/button.html
11
 * @author jc
12
 * @version 1.001
13
 */
14
class HtmlButtonGroups extends HtmlSemCollection {
15
16
	public function __construct($identifier, $elements=array(), $asIcons=false) {
17
		parent::__construct($identifier, "div", "ui buttons");
18
		if ($asIcons === true)
19
			$this->asIcons();
20
		$this->addElements($elements, $asIcons);
21
	}
22
	protected function createItem($value){
23
		return new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content), $value);
24
	}
25
26
27
	public function addElement($element, $asIcon=false) {
28
		$item=$this->addItem($element);
29
		if($asIcon)
30
			$item->asIcon($element);
31
		return $item;
32
	}
33
34
	public function addElements($elements, $asIcons=false) {
35
		foreach ( $elements as $element ) {
36
			$this->addElement($element, $asIcons);
37
		}
38
		return $this;
39
	}
40
41
	public function insertOr($aferIndex=0, $or="or") {
42
		$orElement=new HtmlSemDoubleElement("or-" . $this->identifier, "div", "or");
43
		$orElement->setProperty("data-text", $or);
44
		array_splice($this->content, $aferIndex + 1, 0, array ($orElement ));
45
		return $this;
46
	}
47
48
	/*
49
	 * (non-PHPdoc)
50
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
51
	 */
52
	public function fromArray($array) {
53
		$this->addElements($array);
54
	}
55
56
	public function asIcons() {
57
		foreach ( $this->content as $item ) {
58
			if($item instanceof HtmlButton)
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
	public function getElement($index) {
78
		return $this->getItem($index);
79
	}
80
81
	public function setElement($index, $button) {
82
		$this->setItem($index, $button);
83
		return $this;
84
	}
85
86
	/*
87
	 * (non-PHPdoc)
88
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
89
	 */
90
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
91
		foreach ( $this->content as $element ) {
92
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
93
		}
94
		return $this;
95
	}
96
97
	public function getElements() {
98
		return $this->content;
99
	}
100
101
	public function addClasses($classes=array()) {
102
		$i=0;
103
		foreach ( $this->content as $button ) {
104
			$button->addToProperty("class", $classes[$i++]);
105
		}
106
		return $this;
107
	}
108
109
	/*
110
	 * (non-PHPdoc)
111
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
112
	 */
113
	public function fromDatabaseObject($object, $function) {
114
		$this->addElement($function($object));
115
	}
116
}