Passed
Push — master ( c653a4...a7d880 )
by Jean-Christophe
02:17
created

DialogButton::addFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ajax\ui\components;
3
4
use Ajax\JsUtils;
5
use Ajax\common\components\BaseComponent;
6
use Ajax\service\JString;
7
8
/**
9
 * JQuery UI Button for the Dialog Component
10
 *
11
 * @author jc
12
 * @version 1.001
13
 */
14
class DialogButton extends BaseComponent {
15
16
	private function addFunction($jsCode) {
17
		if (! JString::startsWith($jsCode, "function"))
18
			$jsCode = "%function(){" . $jsCode . "}%";
19
		return $jsCode;
20
	}
21
22
	public function __construct($caption, $jsCode, $event = "click") {
23
		parent::__construct(NULL);
24
		$this->params["text"] = $caption;
25
		$this->params[$event] = $this->addFunction($jsCode);
26
	}
27
28
	public function __toString() {
29
		return $this->getScript();
30
	}
31
32
	/*
33
	 * (non-PHPdoc)
34
	 * @see \Ajax\common\BaseComponent::getScript()
35
	 */
36
	public function getScript() {
37
		return json_encode($this->params, JSON_UNESCAPED_SLASHES);
38
	}
39
40
	public static function cancelButton($caption = "Annuler") {
41
		return new DialogButton($caption, "$( this ).dialog( 'close' );");
42
	}
43
44
	/**
45
	 *
46
	 * @param JsUtils $js
47
	 * @param string $url
48
	 * @param string $form
49
	 * @param string $responseElement
50
	 * @param string $caption
51
	 * @param array $parameters
52
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
53
	 * @return DialogButton
54
	 */
55
	public static function submitButton(JsUtils $js, $url, $form, $responseElement, $caption = "Okay", $parameters = []) {
56
		return new DialogButton($caption, $js->postFormDeferred($url, $form, $responseElement, $parameters) . ";$( this ).dialog( 'close' );");
57
	}
58
}
59