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

HtmlModal::renderContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 5
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
6
use Ajax\JsUtils;
7
use Ajax\common\html\BaseHtml;
8
9
/**
10
 * Twitter Bootstrap HTML Modal component
11
 * @author jc
12
 * @version 1.001
13
 */
14
class HtmlModal extends BaseHtml {
15
	protected $title="Titre de ma boîte";
16
	protected $content="";
17
	protected $buttons=array ();
18
	protected $showOnStartup=false;
19
	protected $draggable=false;
20
	protected $validCondition=NULL;
21
	protected $backdrop=true;
22
23
	/**
24
	 *
25
	 * @param string $identifier the id
26
	 */
27
	public function __construct($identifier, $title="", $content="", $buttonCaptions=array()) {
28
		parent::__construct($identifier);
29
		$this->_template=include 'templates/tplModal.php';
30
		$this->buttons=array ();
31
		$this->title=$title;
32
		$this->content=$content;
33
		foreach ( $buttonCaptions as $button ) {
34
			$this->addButton($button);
35
		}
36
	}
37
38
	/**
39
	 * Add a button
40
	 * @param string $value the button caption
41
	 * @param string $style one of "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger"
42
	 * @return HtmlButton
43
	 */
44
	public function addButton($value="Okay", $style="btn-primary") {
45
		$btn=new HtmlButton($this->identifier."-".$value);
46
		$btn->setStyle($style);
47
		$btn->setValue($value);
48
		$this->buttons []=$btn;
49
		return $btn;
50
	}
51
52
	/**
53
	 * Add a cancel button (dismiss)
54
	 * @param string $value
55
	 * @return HtmlButton
56
	 */
57
	public function addCancelButton($value="Annuler") {
58
		$btn=$this->addButton($value, "btn-default");
59
		$btn->setProperty("data-dismiss", "modal");
60
		return $btn;
61
	}
62
63
	/**
64
	 * Add an Okay button (close the box only if $(identifier).valid===true)
65
	 * @param string $value
66
	 * @return HtmlButton
67
	 */
68
	public function addOkayButton($value="Okay",$jsCode="") {
69
		$btn=$this->addButton($value, "btn-primary");
70
		$btn->onClick("if(".$this->getValidCondition()."){ ".$jsCode."$('#".$this->identifier."').modal('hide');}");
71
		return $btn;
72
	}
73
74
	protected function getDefaultValidCondition() {
75
		return "$('#".$this->identifier."').prop('valid')";
76
	}
77
78
	public function setValidCondition($js) {
79
		$this->validCondition=$js;
80
	}
81
82
	public function getValidCondition() {
83
		if ($this->validCondition==NULL) {
84
			return $this->getDefaultValidCondition();
85
		} else {
86
			return $this->validCondition;
87
		}
88
	}
89
90
	public function setValid() {
91
		$this->validCondition="1==1";
92
	}
93
94
	/**
95
	 * set the content of the modal
96
	 * @param string $content
97
	 */
98
	public function setContent($content) {
99
		$this->content=$content;
100
	}
101
102
	/**
103
	 * set the title of the modal
104
	 * @param string $title
105
	 */
106
	public function setTitle($title) {
107
		$this->title=$title;
108
	}
109
110
	/**
111
	 * render the content of an existing view : $controller/$action and set the response to the modal content
112
	 * @param JsUtils $js
113
	 * @param Controller $initialController
114
	 * @param string $viewName
115
	 * @param $params The parameters to pass to the view
116
	 */
117
	public function renderView(JsUtils $js,$initialController,$viewName, $params=array()) {
118
		$this->content=$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...
119
	}
120
121
	/**
122
	 * render the content of $controller::$action and set the response to the modal content
123
	 * @param JsUtils $js
124
	 * @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...
125
	 * @param Controller $initialControllerInstance
126
	 * @param string $controllerName the controller name
127
	 * @param string $actionName the action name
128
	 */
129
	public function forward(JsUtils $js,$initialControllerInstance,$controllerName,$actionName,$params=NULL){
130
		$this->content=$js->forward($initialControllerInstance, $controllerName, $actionName,$params);
131
	}
132
133
	/*
134
	 * (non-PHPdoc)
135
	 * @see BaseHtml::run()
136
	 */
137
	public function run(JsUtils $js) {
138
		if($this->content instanceof BaseHtml){
139
			$this->content->run($js);
140
		}
141
		$this->_bsComponent=$js->bootstrap()->modal("#".$this->identifier, array (
142
				"show" => $this->showOnStartup
143
		));
144
		if ($this->draggable)
145
			$this->_bsComponent->setDraggable(true);
146
		$this->_bsComponent->setBackdrop($this->backdrop);
147
		$this->addEventsOnRun($js);
148
		return $this->_bsComponent;
149
	}
150
151
	public function getButton($index) {
152
		if (is_int($index))
153
			return $this->buttons [$index];
154
		else
155
			return $this->getElementById($index, $this->buttons);
156
	}
157
158
	public function showOnCreate() {
159
		$this->showOnStartup=true;
160
		return $this;
161
	}
162
163
	public function jsShow() {
164
		return "$('#{$this->identifier}').modal('show');";
165
	}
166
167
	public function jsHide() {
168
		return "$('#{$this->identifier}').modal('hide');";
169
	}
170
171
	public function jsGetContent(JsUtils $js, $url) {
172
		return $js->getDeferred($url, "#".$this->identifier." .modal-body");
173
	}
174
175
	public function jsSetTitle($title) {
176
		return "$('#".$this->identifier." .modal-title').html('".$title."');";
177
	}
178
179
	public function jsHideButton($index) {
180
		$btn=$this->getButton($index);
181
		if ($btn)
182
			return "$('#".$btn->getIdentifier()."').hide();";
183
	}
184
185
	/**
186
	 * Allow modal to be moved using the mouse.
187
	 * needs JQuery UI
188
	 * @param boolean $value
189
	 */
190
	public function setDraggable($value) {
191
		$this->draggable=$value;
192
		if ($value) {
193
			$this->backdrop=false;
194
		}
195
	}
196
197
	/**
198
	 * Includes a modal-backdrop element.
199
	 * Alternatively, specify static for a backdrop which doesn't close the modal on click.
200
	 * @param Boolean $value default : true
201
	 * @return HtmlModal
202
	 */
203
	public function setBackdrop($value) {
204
		return $this->backdrop=$value;
205
	}
206
}