Completed
Push — master ( fdca69...db492e )
by Jean-Christophe
03:16
created

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