Passed
Push — master ( afb7ea...2c24bb )
by Jean-Christophe
02:47
created

HtmlList::onCkItemChange()   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);
0 ignored issues
show
Bug introduced by
The method getPart() 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\view\HtmlViewContent or Ajax\semantic\html\content\HtmlAbsractItem or Ajax\semantic\html\collections\table\HtmlTable or Ajax\semantic\html\content\view\HtmlViewItem or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\bootstrap\html\HtmlForm 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

35
		return $this->getItem($index)->/** @scrutinizer ignore-call */ getPart($partName);
Loading history...
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
	protected function getItemToAdd($item){
54
		$itemO=parent::getItemToAdd($item);
55
		if($itemO instanceof AbstractCheckbox)
56
			$itemO->addClass("item");
57
		return $itemO;
58
	}
59
60
	public function setCelled() {
61
		return $this->addToProperty("class", "celled");
62
	}
63
64
	public function setBulleted() {
65
		return $this->addToProperty("class", "bulleted");
66
	}
67
68
	public function setOrdered() {
69
		return $this->addToProperty("class", "ordered");
70
	}
71
72
	public function run(JsUtils $js) {
73
		if ($this->_hasCheckedList === true) {
74
			$jsCode=include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php';
75
			$jsCode=\str_replace("%identifier%", "#" . $this->identifier, $jsCode);
76
			$jsCode=\str_replace("%fireOnInit%", $this->_fireOnInit, $jsCode);
77
			$jsCode=\str_replace("%onChange%", $this->_ckItemChange, $jsCode);
78
			$this->executeOnRun($jsCode);
79
		}
80
		return parent::run($js);
81
	}
82
	
83
	public function onCkItemChange($jsCode){
84
		$this->_ckItemChange=$jsCode;
85
	}
86
87
	/**
88
	 * @param boolean $fireOnInit
89
	 */
90
	public function setFireOnInit($fireOnInit) {
91
		$this->_fireOnInit = $fireOnInit;
92
	}
93
94
	public function setRelaxed() {
95
		return $this->addToProperty("class", "relaxed");
96
	}
97
98
	public function setSelection() {
99
		return $this->addToProperty("class", "selection");
100
	}
101
102
	public function setDivided() {
103
		return $this->addToProperty("class", "divided");
104
	}
105
106
	public function setHorizontal() {
107
		return $this->addToProperty("class", "horizontal");
108
	}
109
110
	/**
111
	 * Adds a grouped checked box to the list
112
	 * @param array $items
113
	 * @param string|array|null $masterItem
114
	 * @param array|null $values
115
	 * @param string $notAllChecked
116
	 * @return HtmlList
117
	 */
118
	public function addCheckedList($items=array(), $masterItem=NULL, $values=array(),$notAllChecked=false,$name=null) {
119
		$count=$this->count();
120
		$identifier=$this->identifier . "-" . $count;
121
		if (isset($masterItem)) {
122
			if(\is_array($masterItem)){
123
				$masterO=new HtmlFormCheckbox("master-" . $identifier, @$masterItem[0],@$masterItem[1]);
124
				if(isset($name))
125
					$masterO->setName($name);
126
				if(isset($masterItem[1])){
127
					if(\array_search($masterItem[1], $values)!==false){
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type null; however, parameter $haystack of array_search() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

127
					if(\array_search($masterItem[1], /** @scrutinizer ignore-type */ $values)!==false){
Loading history...
128
						$masterO->getDataField()->setProperty("checked", "");
129
					}
130
				}
131
			}else{
132
				$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
133
			}
134
			if($notAllChecked){
135
				$masterO->getDataField()->addClass("_notAllChecked");
136
			}
137
			$masterO->getHtmlCk()->addToProperty("class", "master");
138
			$masterO->setClass("item");
139
			$this->addItem($masterO);
140
		}
141
		$fields=array ();
142
		$i=0;
143
		foreach ( $items as $val => $caption ) {
144
			$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
145
			if (\array_search($val, $values) !== false) {
146
				$itemO->getDataField()->setProperty("checked", "");
147
			}
148
			if(isset($name))
149
				$itemO->setName($name);
150
			$itemO->setClass("item");
151
			$fields[]=$itemO;
152
		}
153
		if (isset($masterO) === true) {
154
			$list=new HtmlList("", $fields);
155
			$list->setClass("list");
156
			$masterO->addContent($list);
157
		} else {
158
			$this->addList($fields);
159
		}
160
		$this->_hasCheckedList=true;
161
		return $this;
162
	}
163
164
	public function setIcons($icons){
165
		if(!\is_array($icons)){
166
			$icons=\array_fill(0, \sizeof($this->content), $icons);
167
		}
168
		$max=\min(\sizeof($icons),\sizeof($this->content));
169
		for($i=0;$i<$max;$i++){
170
			$this->content[$i]->addIcon($icons[$i]);
171
		}
172
		return $this;
173
	}
174
}
175