Completed
Push — master ( 263c12...bbe42f )
by Jean-Christophe
06:53
created

HtmlModal::addElementInPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
	public function addContent($content,$before=false){
70
		$this->content["content"]->addContent($content,$before);
71
		return $this;
72
	}
73
74 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...
75
		$content=$this->content["content"];
76
		if(isset($description)){
77
			$description=new HtmlSemDoubleElement("description-".$this->identifier,"div","description",$description);
78
			$content->addContent($description,true);
79
		}
80
		if($image!==""){
81
			$img=new HtmlImage("image-".$this->identifier,$image,"","medium");
82
			$content->addContent($img,true);
83
			$content->addToProperty("class","image");
84
		}
85
		return $this;
86
	}
87
88 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...
89
		$content=$this->content["content"];
90
		if(isset($description)){
91
			$description=new HtmlSemDoubleElement("description-".$this->identifier,"div","description",$description);
92
			$content->addContent($description,true);
93
		}
94
		if($icon!==""){
95
			$img=new HtmlIcon("image-".$this->identifier,$icon);
96
			$content->addContent($img,true);
97
			$content->addToProperty("class","image");
98
		}
99
		return $this;
100
	}
101
102
	private function addElementInPart($element,$part) {
103
		$this->content[$part]->addContent($element);
104
		return $element;
105
	}
106
107
	public function showDimmer($value){
108
		$value=$value?"show":"hide";
109
		$this->_paramParts[]=["'".$value." dimmer'"];
110
		return $this;
111
	}
112
113
	public function setInverted($recursive=true){
114
		$this->_params["inverted"]=true;
115
		return $this;
116
	}
117
118
	public function setBasic(){
119
		return $this->addToProperty("class", "basic");
120
	}
121
122
123
	public function setTransition($value){
124
		$this->_paramParts[]=["'setting'","'transition'","'".$value."'"];
125
	}
126
127
	/**
128
	 * render the content of an existing view : $controller/$action and set the response to the modal content
129
	 * @param JsUtils $js
130
	 * @param Controller $initialController
131
	 * @param string $viewName
132
	 * @param $params The parameters to pass to the view
133
	 */
134
	public function renderView(JsUtils $js,$initialController,$viewName, $params=array()) {
135
		return $this->setContent($js->renderContent($initialController, $viewName,$params));
136
	}
137
138
	/**
139
	 * render the content of $controller::$action and set the response to the modal content
140
	 * @param JsUtils $js
141
	 * @param Controller $initialControllerInstance
142
	 * @param string $controllerName the controller name
143
	 * @param string $actionName the action name
144
	 * @param array $params
145
	 */
146
	public function forward(JsUtils $js,$initialControllerInstance,$controllerName,$actionName,$params=NULL){
147
		return $this->setContent($js->forward($initialControllerInstance, $controllerName, $actionName,$params));
148
	}
149
150
	/**
151
	 *
152
	 * {@inheritDoc}
153
	 *
154
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
155
	 */
156
	public function compile(JsUtils $js=NULL, &$view=NULL) {
157
		$this->content=JArray::sortAssociative($this->content, ["header","content","actions" ]);
158
		return parent::compile($js, $view);
159
	}
160
	/*
161
	 * (non-PHPdoc)
162
	 * @see BaseHtml::run()
163
	 */
164
	public function run(JsUtils $js) {
165
		if(isset($this->_bsComponent)===false)
166
			$this->_bsComponent=$js->semantic()->modal("#".$this->identifier,$this->_params,$this->_paramParts);
167
		$this->addEventsOnRun($js);
168
		return $this->_bsComponent;
169
	}
170
171
	public function jsDo($behavior) {
172
		return "$('#".$this->identifier."').modal('".$behavior."');";
173
	}
174
175
	public function jsHide() {
176
		return $this->jsDo("hide");
177
	}
178
}
179