Completed
Push — master ( 06f62d...424d4e )
by Jean-Christophe
03:13
created

HtmlDropdown   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 159
Duplicated Lines 6.92 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 36
c 6
b 0
f 0
lcom 1
cbo 8
dl 11
loc 159
rs 8.8

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A addItem() 0 10 3
A fromDatabaseObject() 0 3 1
A addInput() 0 6 2
A addItems() 11 11 4
A asDropdown() 0 10 2
A setVertical() 0 3 1
A setSimple() 0 3 1
A asButton() 0 5 2
A asSelect() 0 9 4
A asSearch() 0 4 1
A setSelect() 0 17 4
A asSubmenu() 0 7 2
A setPointing() 0 3 1
A setValue() 0 11 3
A run() 0 8 3
A setAction() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\JsUtils;
8
use Ajax\semantic\html\elements\HtmlIcon;
9
use Ajax\common\html\html5\HtmlInput;
10
use Ajax\service\JArray;
11
use Ajax\semantic\html\base\constants\Direction;
12
13
class HtmlDropdown extends HtmlSemDoubleElement {
14
	protected $mClass="menu";
15
	protected $mTagName="div";
16
	protected $items=array ();
17
	protected $params=array("action"=>"auto","on"=>"hover");
18
	protected $input;
19
20
	public function __construct($identifier, $value="", $items=array()) {
21
		parent::__construct($identifier, "div");
22
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
23
		$this->setProperty("class", "ui dropdown");
24
		$content=new HtmlSemDoubleElement("text-".$this->identifier,"div");
25
		$content->setClass("text");
26
		$content->setContent($value);
27
		$content->wrap("",new HtmlIcon("", "dropdown"));
28
		$this->content=array($content);
29
		$this->tagName="div";
30
		$this->addItems($items);
31
	}
32
33
	public function addItem($item,$value=NULL,$image=NULL){
34
		$itemO=$item;
35
		if(\is_object($item)===false){
36
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image);
37
		}elseif($itemO instanceof HtmlDropdownItem){
38
			$this->addToProperty("class", "vertical");
39
		}
40
		$this->items[]=$itemO;
41
		return $itemO;
42
	}
43
44
	/* (non-PHPdoc)
45
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
46
	 */
47
	public function fromDatabaseObject($object, $function) {
48
		$this->addItem($function($object));
49
	}
50
51
	public function addInput($name){
52
		if(!isset($name))
53
			$name="input-".$this->identifier;
54
		$this->setAction("activate");
55
		$this->input=new HtmlInput($name,"hidden");
56
	}
57
58 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...
59
		if(JArray::isAssociative($items)){
60
			foreach ($items as $k=>$v){
61
				$this->addItem($v)->setData($k);
62
			}
63
		}else{
64
			foreach ($items as $item){
65
				$this->addItem($item);
66
			}
67
		}
68
	}
69
70
	/**
71
	 * @param boolean $dropdown
72
	 */
73
	public function asDropdown($dropdown){
74
		if($dropdown===false){
75
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
76
			$dropdown="menu";
77
		}else{
78
			$dropdown="dropdown";
79
			$this->mClass="menu";
80
		}
81
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
82
	}
83
84
	public function setVertical(){
85
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
86
	}
87
88
	public function setSimple(){
89
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
90
	}
91
92
	public function asButton($floating=false){
93
		if($floating)
94
			$this->addToProperty("class", "floating");
95
		return $this->addToProperty("class", "button");
96
	}
97
98
	public function asSelect($name=NULL,$multiple=false,$selection=true){
99
		if(isset($name))
100
			$this->addInput($name);
101
		if($multiple)
102
			$this->addToProperty("class", "multiple");
103
		if ($selection)
104
			$this->addToPropertyCtrl("class", "selection",array("selection"));
105
		return $this;
106
	}
107
108
	public function asSearch($name=NULL,$multiple=false,$selection=false){
109
		$this->asSelect($name,$multiple,$selection);
110
		return $this->addToProperty("class", "search");
111
	}
112
113
	public function setSelect($name=NULL,$multiple=false){
114
		if(!isset($name))
115
			$name="select-".$this->identifier;
116
		$this->input=null;
117
		if($multiple){
118
			$this->setProperty("multiple", true);
119
			$this->addToProperty("class", "multiple");
120
		}
121
		$this->setAction("activate");
122
		$this->tagName="select";
123
		$this->setProperty("name", $name);
124
		$this->content=null;
125
		foreach ($this->items as $item){
126
			$item->asOption();
127
		}
128
		return $this;
129
	}
130
131
	public function asSubmenu($pointing=NULL){
132
		$this->setClass("ui dropdown link item");
133
		if(isset($pointing)){
134
			$this->setPointing($pointing);
135
		}
136
		return $this;
137
	}
138
139
	public function setPointing($value=Direction::NONE){
140
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
141
	}
142
143
	public function setValue($value){
144
		if(isset($this->input)){
145
			$this->input->setProperty("value", $value);
146
		}else{
147
			$this->setProperty("value", $value);
148
		}
149
		$textElement=$this->getElementById("text-".$this->identifier, $this->content);
150
		if(isset($textElement))
151
			$textElement->setContent($value);
152
		return $this;
153
	}
154
155
	/*
156
	 * (non-PHPdoc)
157
	 * @see BaseHtml::run()
158
	 */
159
	public function run(JsUtils $js) {
160
		if($this->propertyContains("class", "simple")===false){
161
			if(isset($this->_bsComponent)===false)
162
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->params);
163
			$this->addEventsOnRun($js);
164
			return $this->_bsComponent;
165
		}
166
	}
167
168
	public function setAction($action){
169
		$this->params["action"]=$action;
170
	}
171
}