Completed
Push — master ( 138cdf...e53430 )
by Jean-Christophe
03:16
created

HtmlModal::jsDo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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