Completed
Push — master ( a4369d...ea5934 )
by Jean-Christophe
02:50
created

HtmlModal   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 178
Duplicated Lines 14.61 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 5
dl 26
loc 178
rs 8.8
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A setHeader() 0 4 1
A setContent() 0 4 1
A setActions() 0 12 3
B addAction() 0 15 5
A getAction() 0 3 1
A addContent() 0 4 1
A addImageContent() 13 13 3
A addIconContent() 13 13 3
A addElementInPart() 0 4 1
A showDimmer() 0 5 2
A setInverted() 0 4 1
A setBasic() 0 3 1
A setTransition() 0 3 1
A renderView() 0 3 1
A forward() 0 3 1
A compile() 0 4 1
A run() 0 6 2
A jsDo() 0 3 1
A jsHide() 0 3 1
A onHidden() 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\JsUtils;
7
use Ajax\service\JArray;
8
use Ajax\common\html\BaseHtml;
9
use Ajax\semantic\html\elements\HtmlButton;
10
use Ajax\semantic\html\elements\HtmlImage;
11
use Ajax\semantic\html\elements\HtmlIcon;
12
13
class HtmlModal extends HtmlSemDoubleElement {
14
	protected $_params=array();
15
	protected $_paramParts=array();
16
17
	public function __construct($identifier, $header="", $content="", $actions=null) {
18
		parent::__construct($identifier, "div","ui modal");
19
		if(isset($header)){
20
			$this->setHeader($header);
21
		}
22
		if(isset($content)){
23
			$this->setContent($content);
24
		}
25
		if(isset($actions)){
26
			$this->setActions($actions);
27
		}
28
	}
29
30
	public function setHeader($value) {
31
		$this->content["header"]=new HtmlSemDoubleElement("header-" . $this->identifier, "a", "header", $value);
32
		return $this;
33
	}
34
35
	public function setContent($value) {
36
		$this->content["content"]=new HtmlSemDoubleElement("content-" . $this->identifier, "div", "content", $value);
37
		return $this;
38
	}
39
40
	public function setActions($actions) {
41
		$this->content["actions"]=new HtmlSemDoubleElement("content-" . $this->identifier, "div", "actions");
42
		if(\is_array($actions)){
43
			foreach ($actions as $action){
44
				$this->addAction($action);
45
			}
46
		}
47
		else{
48
			$this->addAction($actions);
49
		}
50
		return $this;
51
	}
52
53
	public function addAction($action){
54
		if(!$action instanceof BaseHtml){
55
			$class="";
56
			if(\array_search($action, ["Okay","Yes"])!==false){
57
				$class="approve";
58
			}
59
			if(\array_search($action, ["Close","Cancel","No"])!==false){
60
				$class="cancel";
61
			}
62
			$action=new HtmlButton("action-".$this->identifier,$action);
63
			if($class!=="")
64
				$action->addToProperty("class", $class);
65
		}
66
		return $this->addElementInPart($action, "actions");
67
	}
68
69
	/**
70
	 * @param int $index
71
	 * @return HtmlButton
72
	 */
73
	public function getAction($index){
74
		return $this->content["actions"]->getContent()[$index];
75
	}
76
77
	public function addContent($content,$before=false){
78
		$this->content["content"]->addContent($content,$before);
79
		return $this;
80
	}
81
82 View Code Duplication
	public function addImageContent($image,$description=NULL){
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...
83
		$content=$this->content["content"];
84
		if(isset($description)){
85
			$description=new HtmlSemDoubleElement("description-".$this->identifier,"div","description",$description);
86
			$content->addContent($description,true);
87
		}
88
		if($image!==""){
89
			$img=new HtmlImage("image-".$this->identifier,$image,"","medium");
90
			$content->addContent($img,true);
91
			$content->addToProperty("class","image");
92
		}
93
		return $this;
94
	}
95
96 View Code Duplication
	public function addIconContent($icon,$description=NULL){
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...
97
		$content=$this->content["content"];
98
		if(isset($description)){
99
			$description=new HtmlSemDoubleElement("description-".$this->identifier,"div","description",$description);
100
			$content->addContent($description,true);
101
		}
102
		if($icon!==""){
103
			$img=new HtmlIcon("image-".$this->identifier,$icon);
104
			$content->addContent($img,true);
105
			$content->addToProperty("class","image");
106
		}
107
		return $this;
108
	}
109
110
	private function addElementInPart($element,$part) {
111
		$this->content[$part]->addContent($element);
112
		return $element;
113
	}
114
115
	public function showDimmer($value){
116
		$value=$value?"show":"hide";
117
		$this->_paramParts[]=["'".$value." dimmer'"];
118
		return $this;
119
	}
120
121
	public function setInverted($recursive=true){
122
		$this->_params["inverted"]=true;
123
		return $this;
124
	}
125
126
	public function setBasic(){
127
		return $this->addToProperty("class", "basic");
128
	}
129
130
131
	public function setTransition($value){
132
		$this->_paramParts[]=["'setting'","'transition'","'".$value."'"];
133
	}
134
135
	/**
136
	 * render the content of an existing view : $controller/$action and set the response to the modal content
137
	 * @param JsUtils $js
138
	 * @param Controller $initialController
139
	 * @param string $viewName
140
	 * @param $params The parameters to pass to the view
141
	 */
142
	public function renderView(JsUtils $js,$initialController,$viewName, $params=array()) {
143
		return $this->setContent($js->renderContent($initialController, $viewName,$params));
144
	}
145
146
	/**
147
	 * render the content of $controller::$action and set the response to the modal content
148
	 * @param JsUtils $js
149
	 * @param Controller $initialControllerInstance
150
	 * @param string $controllerName the controller name
151
	 * @param string $actionName the action name
152
	 * @param array $params
153
	 */
154
	public function forward(JsUtils $js,$initialControllerInstance,$controllerName,$actionName,$params=NULL){
155
		return $this->setContent($js->forward($initialControllerInstance, $controllerName, $actionName,$params));
156
	}
157
158
	/**
159
	 *
160
	 * {@inheritDoc}
161
	 *
162
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
163
	 */
164
	public function compile(JsUtils $js=NULL, &$view=NULL) {
165
		$this->content=JArray::sortAssociative($this->content, ["header","content","actions" ]);
166
		return parent::compile($js, $view);
167
	}
168
	/*
169
	 * (non-PHPdoc)
170
	 * @see BaseHtml::run()
171
	 */
172
	public function run(JsUtils $js) {
173
		if(isset($this->_bsComponent)===false)
174
			$this->_bsComponent=$js->semantic()->modal("#".$this->identifier,$this->_params,$this->_paramParts);
175
		$this->addEventsOnRun($js);
176
		return $this->_bsComponent;
177
	}
178
179
	public function jsDo($behavior) {
180
		return "$('#".$this->identifier."').modal('".$behavior."');";
181
	}
182
183
	public function jsHide() {
184
		return $this->jsDo("hide");
185
	}
186
187
	public function onHidden($js){
188
		$this->_params["onHidden"]=$js;
189
	}
190
}
191