Completed
Push — master ( 2c01a5...172c54 )
by Jean-Christophe
03:09
created

HtmlDropdown::addHeaderItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Ajax\semantic\html\modules;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\content\HtmlDropdownItem;
7
use Ajax\semantic\html\elements\HtmlIcon;
8
use Ajax\common\html\html5\HtmlInput;
9
use Ajax\service\JArray;
10
use Ajax\semantic\html\base\constants\Direction;
11
use Ajax\semantic\html\base\traits\LabeledIconTrait;
12
use Ajax\JsUtils;
13
14
class HtmlDropdown extends HtmlSemDoubleElement {
15
	use LabeledIconTrait {
16
		addIcon as addIconP;
17
	}
18
	protected $mClass="menu";
19
	protected $mTagName="div";
20
	protected $items=array ();
21
	protected $_params=array("action"=>"nothing","on"=>"hover");
22
	protected $input;
23
24
	public function __construct($identifier, $value="", $items=array()) {
25
		parent::__construct($identifier, "div");
26
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
27
		$this->setProperty("class", "ui dropdown");
28
		$content=new HtmlSemDoubleElement("text-".$this->identifier,"div");
29
		$content->setClass("text");
30
		$content->setContent($value);
31
		$content->wrap("",new HtmlIcon("", "dropdown"));
32
		$this->content=array($content);
33
		$this->tagName="div";
34
		$this->addItems($items);
35
	}
36
37
	public function addItem($item,$value=NULL,$image=NULL,$description=NULL){
38
		$itemO=$this->beforeAddItem($item,$value,$image,$description);
39
		$this->items[]=$itemO;
40
		return $itemO;
41
	}
42
43
	public function addIcon($icon,$before=true,$labeled=false){
44
		$this->addIconP($icon,$before,$labeled);
45
		return $this->getElementById("text-".$this->identifier, $this->content)->setWrapAfter("");
46
	}
47
48
	/**
49
	 * Insert an item at a position
50
	 * @param mixed $item
51
	 * @param number $position
52
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
53
	 */
54
	public function insertItem($item,$position=0){
55
		$itemO=$this->beforeAddItem($item);
56
		 $start = array_slice($this->items, 0, $position);
57
		 $end = array_slice($this->items, $position);
58
		 $start[] = $item;
59
		 $this->items=array_merge($start, $end);
60
		 return $itemO;
61
	}
62
63
	protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){
64
		$itemO=$item;
65
		if(\is_array($item)){
66
			$description=JArray::getValue($item, "description", 3);
67
			$value=JArray::getValue($item, "value", 1);
68
			$image=JArray::getValue($item, "image", 2);
69
			$item=JArray::getValue($item, "item", 0);
70
		}
71
		if(!$item instanceof HtmlDropdownItem){
72
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image,$description);
73
		}elseif($itemO instanceof HtmlDropdownItem){
74
			$this->addToProperty("class", "vertical");
75
		}
76
		return $itemO;
77
	}
78
79
	/* (non-PHPdoc)
80
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
81
	 */
82
	public function fromDatabaseObject($object, $function) {
83
		$this->addItem($function($object));
84
	}
85
86
	public function addInput($name){
87
		if(!isset($name))
88
			$name="input-".$this->identifier;
89
		$this->setAction("activate");
90
		$this->input=new HtmlInput($name,"hidden");
91
	}
92
93
	/**
94
	 * Adds a search input item
95
	 * @param string $placeHolder
96
	 * @param string $icon
97
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
98
	 */
99
	public function addSearchInputItem($placeHolder=NULL,$icon=NULL){
100
		return $this->addItem(HtmlDropdownItem::searchInput($placeHolder,$icon));
101
	}
102
103
	/**
104
	 * Adds a divider item
105
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
106
	 */
107
	public function addDividerItem(){
108
		return $this->addItem(HtmlDropdownItem::divider());
109
	}
110
111
	/**
112
	 * Adds an header item
113
	 * @param string $caption
114
	 * @param string $icon
115
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
116
	 */
117
	public function addHeaderItem($caption=NULL,$icon=NULL){
118
		return $this->addItem(HtmlDropdownItem::header($caption,$icon));
119
	}
120
121
	/**
122
	 * Adds an item with a circular label
123
	 * @param string $caption
124
	 * @param string $color
125
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
126
	 */
127
	public function addCircularLabelItem($caption,$color){
128
		return $this->addItem(HtmlDropdownItem::circular($caption, $color));
129
	}
130
131
	/**
132
	 * @param string $caption
133
	 * @param string $image
134
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
135
	 */
136
	public function addMiniAvatarImageItem($caption,$image){
137
		return $this->addItem(HtmlDropdownItem::avatar($caption, $image));
0 ignored issues
show
Documentation introduced by
$caption is of type string, but the function expects a object<Ajax\semantic\html\content\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$image is of type string, but the function expects a object<Ajax\semantic\html\content\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
	}
139
140 View Code Duplication
	public function addItems($items){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
		if(JArray::isAssociative($items)){
142
			foreach ($items as $k=>$v){
143
				$this->addItem($v)->setData($k);
144
			}
145
		}else{
146
			foreach ($items as $item){
147
				$this->addItem($item);
148
			}
149
		}
150
	}
151
152
	/**
153
	 * @return int
154
	 */
155
	public function count(){
156
		return \sizeof($this->items);
157
	}
158
	/**
159
	 * @param boolean $dropdown
160
	 */
161
	public function asDropdown($dropdown){
162
		if($dropdown===false){
163
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
164
			$dropdown="menu";
165
		}else{
166
			$dropdown="dropdown";
167
			$this->mClass="menu";
168
		}
169
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
170
	}
171
172
	public function setVertical(){
173
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
174
	}
175
176
	public function setSimple(){
177
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
178
	}
179
180
	public function asButton($floating=false){
181
		if($floating)
182
			$this->addToProperty("class", "floating");
183
		$this->removePropertyValue("class", "selection");
184
		return $this->addToProperty("class", "button");
185
	}
186
187
	public function asSelect($name=NULL,$multiple=false,$selection=true){
188
		if(isset($name))
189
			$this->addInput($name);
190
		if($multiple)
191
			$this->addToProperty("class", "multiple");
192
		if ($selection){
193
			if($this->propertyContains("class", "button")===false)
194
				$this->addToPropertyCtrl("class", "selection",array("selection"));
195
		}
196
		return $this;
197
	}
198
199
	public function asSearch($name=NULL,$multiple=false,$selection=true){
200
		$this->asSelect($name,$multiple,$selection);
201
		return $this->addToProperty("class", "search");
202
	}
203
204
	public function setSelect($name=NULL,$multiple=false){
205
		if(!isset($name))
206
			$name="select-".$this->identifier;
207
		$this->input=null;
208
		if($multiple){
209
			$this->setProperty("multiple", true);
210
			$this->addToProperty("class", "multiple");
211
		}
212
		$this->setAction("activate");
213
		$this->tagName="select";
214
		$this->setProperty("name", $name);
215
		$this->content=null;
216
		foreach ($this->items as $item){
217
			$item->asOption();
218
		}
219
		return $this;
220
	}
221
222
	public function asSubmenu($pointing=NULL){
223
		$this->setClass("ui dropdown link item");
224
		if(isset($pointing)){
225
			$this->setPointing($pointing);
226
		}
227
		return $this;
228
	}
229
230
	public function setPointing($value=Direction::NONE){
231
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
232
	}
233
234
	public function setValue($value){
235
		if(isset($this->input)){
236
			$this->input->setProperty("value", $value);
237
		}else{
238
			$this->setProperty("value", $value);
239
		}
240
		$textElement=$this->getElementById("text-".$this->identifier, $this->content);
241
		if(isset($textElement))
242
			$textElement->setContent($value);
243
		return $this;
244
	}
245
246
	/*
247
	 * (non-PHPdoc)
248
	 * @see BaseHtml::run()
249
	 */
250
	public function run(JsUtils $js) {
251
		if($this->propertyContains("class", "simple")===false){
252 View Code Duplication
			if(isset($this->_bsComponent)===false)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->_params);
254
			$this->addEventsOnRun($js);
255
			return $this->_bsComponent;
256
		}
257
	}
258
259
	public function setCompact(){
260
		return $this->addToPropertyCtrl("class", "compact", array("compact"));
261
	}
262
263
	public function setAction($action){
264
		$this->_params["action"]=$action;
265
	}
266
267
	public function setFullTextSearch($value){
268
		$this->_params["fullTextSearch"]=$value;
269
	}
270
}