Completed
Push — master ( b23d1e...03b6f4 )
by Jean-Christophe
04:33
created

HtmlModal::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 8
nop 4
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=array()) {
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) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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
	public function setTransition($value){
127
		$this->_paramParts[]=["'setting'","'transition'","'".$value."'"];
128
	}
129
130
	/**
131
	 * render the content of an existing view : $controller/$action and set the response to the modal content
132
	 * @param JsUtils $js
133
	 * @param Controller $initialController
134
	 * @param string $viewName
135
	 * @param $params The parameters to pass to the view
136
	 */
137
	public function renderView(JsUtils $js,$initialController,$viewName, $params=array()) {
138
		return $this->setContent($js->renderContent($initialController, $viewName,$params));
0 ignored issues
show
Documentation introduced by
$viewName is of type string, but the function expects a object<Ajax\view>.

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
	/**
142
	 * render the content of $controller::$action and set the response to the modal content
143
	 * @param JsUtils $js
144
	 * @param string $title The panel title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
145
	 * @param Controller $initialControllerInstance
146
	 * @param string $controllerName the controller name
147
	 * @param string $actionName the action name
148
	 */
149
	public function forward(JsUtils $js,$initialControllerInstance,$controllerName,$actionName,$params=NULL){
150
		return $this->setContent($js->forward($initialControllerInstance, $controllerName, $actionName,$params));
151
	}
152
153
	/**
154
	 *
155
	 * {@inheritDoc}
156
	 *
157
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
158
	 */
159
	public function compile(JsUtils $js=NULL, &$view=NULL) {
160
		$this->content=JArray::sortAssociative($this->content, ["header","content","actions" ]);
161
		return parent::compile($js, $view);
162
	}
163
	/*
164
	 * (non-PHPdoc)
165
	 * @see BaseHtml::run()
166
	 */
167
	public function run(JsUtils $js) {
168
		if(isset($this->_bsComponent)===false)
169
			$this->_bsComponent=$js->semantic()->modal("#".$this->identifier,$this->_params,$this->_paramParts);
170
		$this->addEventsOnRun($js);
171
		return $this->_bsComponent;
172
	}
173
}