Completed
Push — master ( da7bd7...a4feba )
by Jean-Christophe
03:22
created

AbstractCheckbox::setOnChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\modules\checkbox;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\CheckboxType;
7
use Ajax\JsUtils;
8
9
abstract class AbstractCheckbox extends HtmlSemDoubleElement {
10
	protected $_params=[];
11
12
	public function __construct($identifier, $name=NULL, $label=NULL, $value=NULL, $inputType="checkbox", $type="checkbox") {
13
		parent::__construct("ck-".$identifier, "div", "ui ".$type);
14
		$this->_identifier=$identifier;
15
		$field=new \Ajax\common\html\html5\HtmlInput($identifier, $inputType, $value);
16
		$field->setProperty("name", $name);
17
		$this->setField($field);
18
		if (isset($label))
19
			$this->setLabel($label);
20
	}
21
22 View Code Duplication
	public function setChecked($value=true){
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...
23
		if($value===true){
24
			$this->getField()->setProperty("checked", "checked");
25
		}else{
26
			$this->getField()->removeProperty("checked");
27
		}
28
		return $this;
29
	}
30
31
	public function setType($checkboxType) {
32
		return $this->addToPropertyCtrl("class", $checkboxType, CheckboxType::getConstants());
33
	}
34
35
	public function setLabel($label) {
36
		$labelO=$label;
37
		if (\is_string($label)) {
38
			$labelO=new HtmlSemDoubleElement("", "label", "");
39
			$labelO->setContent($label);
40
			$labelO->setProperty("for", $this->getField()->getIdentifier());
41
		}
42
		$this->content["label"]=$labelO;
43
	}
44
45
	public function setField($field) {
46
		$this->content["field"]=$field;
47
	}
48
49
	/**
50
	 * Returns the label or null
51
	 * @return mixed
52
	 */
53
	public function getLabel() {
54
		if (\array_key_exists("label", $this->content))
55
			return $this->content["label"];
56
	}
57
58
	/**
59
	 * Return the field
60
	 * @return mixed
61
	 */
62
	public function getField() {
63
		return $this->content["field"];
64
	}
65
66
	/**
67
	 * puts the label before or behind
68
	 */
69
	public function swapLabel() {
70
		$label=$this->getLabel();
71
		unset($this->content["label"]);
72
		$this->content["label"]=$label;
73
	}
74
75
	public function setReadonly() {
76
		$this->getField()->setProperty("disabled", "disabled");
77
		return $this->addToProperty("class", "read-only");
78
	}
79
80
	/**
81
	 * Attach $this to $selector and fire $action
82
	 * @param string $selector jquery selector of the associated element
83
	 * @param string $action action to execute : check, uncheck or NULL for toggle
84
	 * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox
85
	 */
86
	public function attachEvent($selector, $action=NULL) {
87
		if (isset($action)||\is_numeric($action)===true) {
88
			$js='$("#%identifier%").checkbox("attach events", "'.$selector.'", "'.$action.'");';
89
		} else {
90
			$js='$("#%identifier%").checkbox("attach events", "'.$selector.'");';
91
		}
92
		$js=\str_replace("%identifier%", $this->identifier, $js);
93
		return $this->executeOnRun($js);
94
	}
95
96
	/**
97
	 * Attach $this to an array of $action=>$selector
98
	 * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"]
99
	 * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox
100
	 */
101
	public function attachEvents($events=array()) {
102
		if (\is_array($events)) {
103
			foreach ( $events as $action => $selector ) {
104
				$this->attachEvent($selector, $action);
105
			}
106
		}
107
		return $this;
108
	}
109
110
	public function setFitted() {
111
		return $this->addToProperty("class", "fitted");
112
	}
113
114
	public function setOnChecked($jsCode){
115
		$this->_params["onChecked"]=$jsCode;
116
		return $this;
117
	}
118
119
	public function setOnUnchecked($jsCode){
120
		$this->_params["onUnchecked"]=$jsCode;
121
		return $this;
122
	}
123
124
	public function setOnChange($jsCode){
125
		$this->_params["onChange"]=$jsCode;
126
		return $this;
127
	}
128
129
	public function run(JsUtils $js) {
130
			$this->_bsComponent=$js->semantic()->checkbox("#" . $this->identifier, $this->_params);
131
			return parent::run($js);
132
	}
133
}