Completed
Push — master ( d27f99...f4e97d )
by Jean-Christophe
03:35
created

HtmlDropdown::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 2
Ratio 25 %

Importance

Changes 0
Metric Value
dl 2
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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
use Ajax\semantic\html\collections\form\traits\FieldTrait;
14
15
class HtmlDropdown extends HtmlSemDoubleElement {
16
	use FieldTrait,LabeledIconTrait {
17
		addIcon as addIconP;
18
	}
19
	protected $mClass="menu";
20
	protected $mTagName="div";
21
	protected $items=array ();
22
	protected $_params=array("action"=>"nothing","on"=>"hover");
23
	protected $input;
24
	protected $value;
25
	protected $_associative;
26
27
	public function __construct($identifier, $value="", $items=array(),$associative=true) {
28
		parent::__construct($identifier, "div");
29
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
30
		$this->setProperty("class", "ui dropdown");
31
		$content=new HtmlSemDoubleElement("text-".$this->identifier,"div");
32
		$content->setClass("text");
33
		$this->setValue($value);
34
		$content->wrap("",new HtmlIcon("", "dropdown"));
35
		$this->content=array($content);
36
		$this->tagName="div";
37
		$this->_associative=$associative;
38
		$this->addItems($items);
39
	}
40
41
	public function getField(){
42
		return $this->input;
43
	}
44
45
	public function addItem($item,$value=NULL,$image=NULL,$description=NULL){
46
		$itemO=$this->beforeAddItem($item,$value,$image,$description);
47
		$this->items[]=$itemO;
48
		return $itemO;
49
	}
50
51
	public function addIcon($icon,$before=true,$labeled=false){
52
		$this->addIconP($icon,$before,$labeled);
53
		return $this->getElementById("text-".$this->identifier, $this->content)->setWrapAfter("");
54
	}
55
56
	public function addIcons($icons){
57
		$count=$this->count();
58
		for ($i=0;$i<\sizeof($icons) && $i<$count;$i++){
59
			$this->getItem($i)->addIcon($icons[$i]);
60
		}
61
	}
62
63
	/**
64
	 * Insert an item at a position
65
	 * @param mixed $item
66
	 * @param number $position
67
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
68
	 */
69
	public function insertItem($item,$position=0){
70
		$itemO=$this->beforeAddItem($item);
71
		 $start = array_slice($this->items, 0, $position);
72
		 $end = array_slice($this->items, $position);
73
		 $start[] = $item;
74
		 $this->items=array_merge($start, $end);
75
		 return $itemO;
76
	}
77
78
	protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){
79
		$itemO=$item;
80
		if(\is_array($item)){
81
			$description=JArray::getValue($item, "description", 3);
82
			$value=JArray::getValue($item, "value", 1);
83
			$image=JArray::getValue($item, "image", 2);
84
			$item=JArray::getValue($item, "item", 0);
85
		}
86
		if(!$item instanceof HtmlDropdownItem){
87
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image,$description);
88
		}elseif($itemO instanceof HtmlDropdownItem){
89
			$this->addToProperty("class", "vertical");
90
		}
91
		return $itemO;
92
	}
93
94
	/* (non-PHPdoc)
95
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
96
	 */
97
	public function fromDatabaseObject($object, $function) {
98
		$this->addItem($function($object));
99
	}
100
101
	public function addInput($name){
102
		if(!isset($name))
103
			$name="input-".$this->identifier;
104
		$this->setAction("activate");
105
		$this->input=new HtmlInput($name,"hidden");
106
	}
107
108
	/**
109
	 * Adds a search input item
110
	 * @param string $placeHolder
111
	 * @param string $icon
112
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
113
	 */
114
	public function addSearchInputItem($placeHolder=NULL,$icon=NULL){
115
		return $this->addItem(HtmlDropdownItem::searchInput($placeHolder,$icon));
116
	}
117
118
	/**
119
	 * Adds a divider item
120
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
121
	 */
122
	public function addDividerItem(){
123
		return $this->addItem(HtmlDropdownItem::divider());
124
	}
125
126
	/**
127
	 * Adds an header item
128
	 * @param string $caption
129
	 * @param string $icon
130
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
131
	 */
132
	public function addHeaderItem($caption=NULL,$icon=NULL){
133
		return $this->addItem(HtmlDropdownItem::header($caption,$icon));
134
	}
135
136
	/**
137
	 * Adds an item with a circular label
138
	 * @param string $caption
139
	 * @param string $color
140
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
141
	 */
142
	public function addCircularLabelItem($caption,$color){
143
		return $this->addItem(HtmlDropdownItem::circular($caption, $color));
144
	}
145
146
	/**
147
	 * @param string $caption
148
	 * @param string $image
149
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
150
	 */
151
	public function addMiniAvatarImageItem($caption,$image){
152
		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...
153
	}
154
155
	public function addItems($items){
156
		if(\is_array($items) && $this->_associative){
157
			foreach ($items as $k=>$v){
158
				$this->addItem($v)->setData($k);
159
			}
160
		}else{
161
			foreach ($items as $item){
162
				$this->addItem($item);
163
			}
164
		}
165
	}
166
167
	public function getItem($index){
168
		return $this->items[$index];
169
	}
170
171
	/**
172
	 * @return int
173
	 */
174
	public function count(){
175
		return \sizeof($this->items);
176
	}
177
	/**
178
	 * @param boolean $dropdown
179
	 */
180
	public function asDropdown($dropdown){
181
		if($dropdown===false){
182
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
183
			$dropdown="menu";
184
		}else{
185
			$dropdown="dropdown";
186
			$this->mClass="menu";
187
		}
188
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
189
	}
190
191
	public function setVertical(){
192
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
193
	}
194
195
	public function setInline(){
196
		return $this->addToPropertyCtrl("class", "inline",["inline"]);
197
	}
198
199
	public function setSimple(){
200
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
201
	}
202
203
	public function asButton($floating=false){
204
		if($floating)
205
			$this->addToProperty("class", "floating");
206
		$this->removePropertyValue("class", "selection");
207
		return $this->addToProperty("class", "button");
208
	}
209
210
	public function asSelect($name=NULL,$multiple=false,$selection=true){
211
		if(isset($name))
212
			$this->addInput($name);
213
		if($multiple)
214
			$this->addToProperty("class", "multiple");
215
		if ($selection){
216
			if($this->propertyContains("class", "button")===false)
217
				$this->addToPropertyCtrl("class", "selection",array("selection"));
218
		}
219
		return $this;
220
	}
221
222
	public function asSearch($name=NULL,$multiple=false,$selection=true){
223
		$this->asSelect($name,$multiple,$selection);
224
		return $this->addToProperty("class", "search");
225
	}
226
227
	public function setSelect($name=NULL,$multiple=false){
228
		if(!isset($name))
229
			$name="select-".$this->identifier;
230
		$this->input=null;
231
		if($multiple){
232
			$this->setProperty("multiple", true);
233
			$this->addToProperty("class", "multiple");
234
		}
235
		$this->setAction("activate");
236
		$this->tagName="select";
237
		$this->setProperty("name", $name);
238
		$this->content=null;
239
		foreach ($this->items as $item){
240
			$item->asOption();
241
		}
242
		return $this;
243
	}
244
245
	public function asSubmenu($pointing=NULL){
246
		$this->setClass("ui dropdown link item");
247
		if(isset($pointing)){
248
			$this->setPointing($pointing);
249
		}
250
		return $this;
251
	}
252
253
	public function setPointing($value=Direction::NONE){
254
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
255
	}
256
257
	public function setValue($value){
258
		$this->value=$value;
259
		return $this;
260
	}
261
	private function applyValue(){
262
		$value=$this->value;
263
		if(isset($this->input) && isset($value)){
264
			$this->input->setProperty("value", $value);
265
		}else{
266
			$this->setProperty("value", $value);
267
		}
268
			$textElement=$this->getElementById("text-".$this->identifier, $this->content);
269
			if(isset($textElement))
270
				$textElement->setContent($value);
271
		return $this;
272
	}
273
274
	/*
275
	 * (non-PHPdoc)
276
	 * @see BaseHtml::run()
277
	 */
278
	public function run(JsUtils $js) {
279
		if($this->propertyContains("class", "simple")===false){
280 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...
281
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->_params);
282
			$this->addEventsOnRun($js);
283
			return $this->_bsComponent;
284
		}
285
	}
286
287
	public function setCompact(){
288
		return $this->addToPropertyCtrl("class", "compact", array("compact"));
289
	}
290
291
	public function setAction($action){
292
		$this->_params["action"]=$action;
293
	}
294
295
	public function setFullTextSearch($value){
296
		$this->_params["fullTextSearch"]=$value;
297
	}
298
299
	public function compile(JsUtils $js=NULL, &$view=NULL) {
300
		$this->applyValue();
301
		return parent::compile($js,$view);
302
	}
303
304
	public function getInput() {
305
		return $this->input;
306
	}
307
308
}