Passed
Push — master ( 30b096...92726b )
by Jean-Christophe
02:16
created

HtmlList::getItem()   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\HtmlSemCollection;
6
use Ajax\semantic\html\content\HtmlListItem;
7
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
8
use Ajax\JsUtils;
9
use Ajax\semantic\html\modules\checkbox\AbstractCheckbox;
10
11
class HtmlList extends HtmlSemCollection {
12
	protected $_hasCheckedList;
13
	protected $_fireOnInit=true;
14
	protected $_ckItemChange="";
15
16
	public function __construct($identifier, $items=array()) {
17
		parent::__construct($identifier, "div", "ui list");
18
		$this->addItems($items);
19
		$this->_hasCheckedList=false;
20
	}
21
22
	protected function createItem($value) {
23
		$count=$this->count();
24
		$item=new HtmlListItem("item-" . $this->identifier . "-" . $count, $value);
25
		return $item;
26
	}
27
28
	public function addHeader($niveau, $content) {
29
		$header=new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page");
30
		$this->wrap($header);
31
		return $header;
32
	}
33
34
	public function getItemPart($index,$partName="header"){
35
		return $this->getItem($index)->getPart($partName);
36
	}
37
38
	public function itemsAs($tagName) {
39
		return $this->contentAs($tagName);
40
	}
41
42
	public function asLinks($hrefs=[],$target=NUll) {
43
		$this->addToPropertyCtrl("class", "link", array ("link" ));
44
		return parent::asLinks($hrefs,$target);
45
	}
46
47
	public function addList($items=array()) {
48
		$list=new HtmlList("", $items);
49
		$list->setClass("list");
50
		return $this->addItem($list);
51
	}
52
	
53
	/**
54
	 * @return HtmlListItem
55
	 */
56
	public function getItem($index){
57
		return parent::getItem($index);
58
	}
59
60
	protected function getItemToAdd($item){
61
		$itemO=parent::getItemToAdd($item);
62
		if($itemO instanceof AbstractCheckbox)
63
			$itemO->addClass("item");
64
		return $itemO;
65
	}
66
67
	public function setCelled() {
68
		return $this->addToProperty("class", "celled");
69
	}
70
71
	public function setBulleted() {
72
		return $this->addToProperty("class", "bulleted");
73
	}
74
75
	public function setOrdered() {
76
		return $this->addToProperty("class", "ordered");
77
	}
78
79
	public function run(JsUtils $js) {
80
		if ($this->_hasCheckedList === true) {
81
			$jsCode=include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php';
82
			$jsCode=\str_replace("%identifier%", "#" . $this->identifier, $jsCode);
83
			$jsCode=\str_replace("%fireOnInit%", $this->_fireOnInit, $jsCode);
84
			$jsCode=\str_replace("%onChange%", $this->_ckItemChange, $jsCode);
85
			$this->executeOnRun($jsCode);
86
		}
87
		return parent::run($js);
88
	}
89
	
90
	public function onCkItemChange($jsCode){
91
		$this->_ckItemChange=$jsCode;
92
	}
93
94
	/**
95
	 * @param boolean $fireOnInit
96
	 */
97
	public function setFireOnInit($fireOnInit) {
98
		$this->_fireOnInit = $fireOnInit;
99
	}
100
101
	public function setRelaxed() {
102
		return $this->addToProperty("class", "relaxed");
103
	}
104
105
	public function setSelection() {
106
		return $this->addToProperty("class", "selection");
107
	}
108
109
	public function setDivided() {
110
		return $this->addToProperty("class", "divided");
111
	}
112
113
	public function setHorizontal() {
114
		return $this->addToProperty("class", "horizontal");
115
	}
116
117
	/**
118
	 * Adds a grouped checked box to the list
119
	 * @param array $items
120
	 * @param string|array|null $masterItem
121
	 * @param array $values
122
	 * @param string $notAllChecked
123
	 * @return HtmlList
124
	 */
125
	public function addCheckedList($items=array(), $masterItem=NULL, $values=array(),$notAllChecked=false,$name=null) {
126
		$count=$this->count();
127
		$identifier=$this->identifier . "-" . $count;
128
		if (isset($masterItem)) {
129
			if(\is_array($masterItem)){
130
				$masterO=new HtmlFormCheckbox("master-" . $identifier, @$masterItem[0],@$masterItem[1]);
131
				if(isset($name))
132
					$masterO->setName($name);
133
				if(isset($masterItem[1])){
134
					if(\array_search($masterItem[1], $values)!==false){
135
						$masterO->getDataField()->setProperty("checked", "");
136
					}
137
				}
138
			}else{
139
				$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
140
			}
141
			if($notAllChecked){
142
				$masterO->getDataField()->addClass("_notAllChecked");
143
			}
144
			$masterO->getHtmlCk()->addToProperty("class", "master");
145
			$masterO->setClass("item");
146
			$this->addItem($masterO);
147
		}
148
		$fields=array ();
149
		$i=0;
150
		foreach ( $items as $val => $caption ) {
151
			$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
152
			if (\array_search($val, $values) !== false) {
153
				$itemO->getDataField()->setProperty("checked", "");
154
			}
155
			if(isset($name))
156
				$itemO->setName($name);
157
			$itemO->setClass("item");
158
			$fields[]=$itemO;
159
		}
160
		if (isset($masterO) === true) {
161
			$list=new HtmlList("", $fields);
162
			$list->setClass("list");
163
			$masterO->addContent($list);
164
		} else {
165
			$this->addList($fields);
166
		}
167
		$this->_hasCheckedList=true;
168
		return $this;
169
	}
170
171
	public function setIcons($icons){
172
		if(!\is_array($icons)){
173
			$icons=\array_fill(0, \sizeof($this->content), $icons);
174
		}
175
		$max=\min(\sizeof($icons),\sizeof($this->content));
176
		for($i=0;$i<$max;$i++){
177
			$this->content[$i]->addIcon($icons[$i]);
178
		}
179
		return $this;
180
	}
181
}
182