Completed
Push — master ( 916f6f...ea2a7d )
by Jean-Christophe
03:42
created

AbstractCheckbox   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 91
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setType() 0 3 1
A setLabel() 0 9 2
A setField() 0 3 1
A getLabel() 0 4 2
A getField() 0 3 1
A swapLabel() 0 5 1
A setReadonly() 0 4 1
A attachEvent() 0 9 3
A attachEvents() 0 8 3
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
	public function setType($checkboxType) {
21
		return $this->addToPropertyCtrl("class", $checkboxType, CheckboxType::getConstants());
22
	}
23
24
	public function setLabel($label) {
25
		$labelO=$label;
26
		if (\is_string($label)) {
27
			$labelO=new HtmlSemDoubleElement("", "label", "");
28
			$labelO->setContent($label);
29
			$labelO->setProperty("for", $this->getField()->getIdentifier());
30
		}
31
		$this->content["label"]=$labelO;
32
	}
33
34
	public function setField($field) {
35
		$this->content["field"]=$field;
36
	}
37
38
	/**
39
	 * Returns the label or null
40
	 * @return mixed
41
	 */
42
	public function getLabel() {
43
		if (\array_key_exists("label", $this->content))
44
			return $this->content["label"];
45
	}
46
47
	/**
48
	 * Return the field
49
	 * @return mixed
50
	 */
51
	public function getField() {
52
		return $this->content["field"];
53
	}
54
55
	/**
56
	 * puts the label before or behind
57
	 */
58
	public function swapLabel() {
59
		$label=$this->getLabel();
60
		unset($this->content["label"]);
61
		$this->content["label"]=$label;
62
	}
63
64
	public function setReadonly() {
65
		$this->getField()->setProperty("disabled", "disabled");
66
		return $this->addToProperty("class", "read-only");
67
	}
68
69
	/**
70
	 * Attach $this to $selector and fire $action
71
	 * @param string $selector jquery selector of the associated element
72
	 * @param string $action action to execute : check, uncheck or NULL for toggle
73
	 * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox
74
	 */
75
	public function attachEvent($selector, $action=NULL) {
76
		if (isset($action)!==false||\is_numeric($action)===true) {
77
			$js='$("#%identifier%").checkbox("attach events", "'.$selector.'", "'.$action.'");';
78
		} else {
79
			$js='$("#%identifier%").checkbox("attach events", "'.$selector.'");';
80
		}
81
		$js=\str_replace("%identifier%", $this->identifier, $js);
82
		return $this->executeOnRun($js);
83
	}
84
85
	/**
86
	 * Attach $this to an array of $action=>$selector
87
	 * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"]
88
	 * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox
89
	 */
90
	public function attachEvents($events=array()) {
91
		if (\is_array($events)) {
92
			foreach ( $events as $action => $selector ) {
93
				$this->attachEvent($selector, $action);
94
			}
95
		}
96
		return $this;
97
	}
98
}