Completed
Push — master ( 506112...a79a4a )
by Jean-Christophe
02:53
created

AbstractCheckbox::setLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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