Passed
Push — master ( d2596c...fa0f64 )
by Jean-Christophe
02:03
created

HtmlModal::setDraggable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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.02
13
 */
14
class HtmlModal extends BaseHtml {
15
	protected $title="HtmlModal Title";
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 object $initialController
114
	 * @param string $viewName
115
	 * @param array $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);
119
	}
120
121
	/**
122
	 * render the content of $controller::$action and set the response to the modal content
123
	 * @param JsUtils $js
124
	 * @param object $initialControllerInstance
125
	 * @param string $controllerName the controller name
126
	 * @param string $actionName the action name
127
	 * @param array $params
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, on the dialog title.
187
	 * needs JQuery UI
188
	 * @param boolean $value
189
	 */
190
	public function draggable($value=true) {
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
		$this->backdrop=$value;
205
		return $this;
206
	}
207
}
208