Passed
Push — master ( fdcf95...dcc4d7 )
by Jean-Christophe
03:10
created

AbstractCheckbox   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
eloc 56
dl 0
loc 141
rs 10
c 6
b 1
f 2
wmc 27

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setChecked() 0 7 2
A __construct() 0 9 2
A setOnUnchecked() 0 3 1
A setField() 0 2 1
A swapLabel() 0 4 1
A attachEvent() 0 8 3
A setOnChecked() 0 3 1
A getLabel() 0 3 2
A forceValue() 0 3 1
A setLabel() 0 11 3
A getField() 0 2 1
A setOnChange() 0 3 1
A setType() 0 2 1
A run() 0 4 2
A setFitted() 0 2 1
A attachEvents() 0 7 3
A setReadonly() 0 3 1
1
<?php
2
namespace Ajax\semantic\html\modules\checkbox;
3
4
use Ajax\semantic\html\base\HtmlSemDoubleElement;
5
use Ajax\semantic\html\base\constants\CheckboxType;
6
use Ajax\JsUtils;
7
8
abstract class AbstractCheckbox extends HtmlSemDoubleElement {
9
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, $value);
20
		$this->setLibraryId($identifier);
21
	}
22
23
	public function setChecked($value = true) {
24
		if ($value === true) {
25
			$this->getField()->setProperty("checked", "checked");
26
		} else {
27
			$this->getField()->removeProperty("checked");
28
		}
29
		return $this;
30
	}
31
32
	public function forceValue($value = 'true') {
33
		$this->getField()->forceValue($value);
34
		return $this;
35
	}
36
37
	public function setType($checkboxType) {
38
		return $this->addToPropertyCtrl("class", $checkboxType, CheckboxType::getConstants());
39
	}
40
41
	public function setLabel($label, $value = null) {
42
		$labelO = $label;
43
		if (\is_string($label)) {
44
			$labelO = new HtmlSemDoubleElement("", "label", "");
45
			$labelO->setContent($label);
46
			$labelO->setProperty("for", $this->getField()
47
				->getIdentifier());
48
			if (isset($value))
49
				$labelO->setProperty("data-value", $value);
50
		}
51
		$this->content["label"] = $labelO;
52
	}
53
54
	public function setField($field) {
55
		$this->content["field"] = $field;
56
	}
57
58
	/**
59
	 * Returns the label or null
60
	 *
61
	 * @return mixed
62
	 */
63
	public function getLabel() {
64
		if (\array_key_exists("label", $this->content))
65
			return $this->content["label"];
66
	}
67
68
	/**
69
	 * Return the field
70
	 *
71
	 * @return mixed
72
	 */
73
	public function getField() {
74
		return $this->content["field"];
75
	}
76
77
	/**
78
	 * puts the label before or behind
79
	 */
80
	public function swapLabel() {
81
		$label = $this->getLabel();
82
		unset($this->content["label"]);
83
		$this->content["label"] = $label;
84
	}
85
86
	public function setReadonly() {
87
		$this->getField()->setProperty("disabled", "disabled");
88
		return $this->addToProperty("class", "read-only");
89
	}
90
91
	/**
92
	 * Attach $this to $selector and fire $action
93
	 *
94
	 * @param string $selector
95
	 *        	jquery selector of the associated element
96
	 * @param string $action
97
	 *        	action to execute : check, uncheck or NULL for toggle
98
	 * @return AbstractCheckbox
99
	 */
100
	public function attachEvent($selector, $action = NULL) {
101
		if (isset($action) || \is_numeric($action) === true) {
102
			$js = '$("#%identifier%").checkbox("attach events", "' . $selector . '", "' . $action . '");';
103
		} else {
104
			$js = '$("#%identifier%").checkbox("attach events", "' . $selector . '");';
105
		}
106
		$js = \str_replace("%identifier%", $this->identifier, $js);
107
		return $this->executeOnRun($js);
108
	}
109
110
	/**
111
	 * Attach $this to an array of $action=>$selector
112
	 *
113
	 * @param array $events
114
	 *        	associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"]
115
	 * @return AbstractCheckbox
116
	 */
117
	public function attachEvents($events = array()) {
118
		if (\is_array($events)) {
0 ignored issues
show
introduced by
The condition is_array($events) is always true.
Loading history...
119
			foreach ($events as $action => $selector) {
120
				$this->attachEvent($selector, $action);
121
			}
122
		}
123
		return $this;
124
	}
125
126
	public function setFitted() {
127
		return $this->addToProperty("class", "fitted");
128
	}
129
130
	public function setOnChecked($jsCode) {
131
		$this->_params["onChecked"] = $jsCode;
132
		return $this;
133
	}
134
135
	public function setOnUnchecked($jsCode) {
136
		$this->_params["onUnchecked"] = $jsCode;
137
		return $this;
138
	}
139
140
	public function setOnChange($jsCode) {
141
		$this->_params["onChange"] = $jsCode;
142
		return $this;
143
	}
144
145
	public function run(JsUtils $js) {
146
		if (! isset($this->_bsComponent))
147
			$this->_bsComponent = $js->semantic()->checkbox("#" . $this->identifier, $this->_params);
148
		return parent::run($js);
149
	}
150
}
151