Completed
Push — master ( 172c54...50fc65 )
by Jean-Christophe
04:31 queued 01:31
created

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