Completed
Push — master ( 2461e8...b569b1 )
by Jean-Christophe
04:01
created

AbstractHtmlFormRadioCheckbox::setInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\collections\form\HtmlFormInput;
6
use Ajax\semantic\html\base\constants\CheckboxType;
7
/**
8
 * Abstract class for Semantic Radio and Checkbox
9
 * @author jc
10
 * @version 1.001
11
 */
12
abstract class AbstractHtmlFormRadioCheckbox extends HtmlFormField {
13
	protected $_input;
14
	protected $_params=array();
15
16
	public function __construct($identifier, $name=NULL,$label=NULL,$value=NULL,$type=NULL) {
17
		$input=new HtmlFormInput($identifier,$label,"checkbox",$value);
18
		parent::__construct("ck-field-".$identifier, $input);
19
		if(isset($label)){
20
			$input->swapLabel();
21
			$label=$input->getLabel();
22
			$label->setClass="hidden";
23
			$label->setProperty("tabindex",0);
24
		}
25
		$this->_input=$input;
26
		$input->getField()->addToProperty("class","hidden");
27
	}
28
29
	public function setType($checkboxType){
30
		return $this->_input->addToPropertyCtrl("class", $checkboxType, CheckboxType::getConstants());
31
	}
32
33
	public function getInput() {
34
		return $this->_input;
35
	}
36
37
	public function setInput($_input) {
38
		$this->_input=$_input;
39
		return $this;
40
	}
41
42
	public function setReadonly(){
43
		$this->_input->getField()->setProperty("disabled","disabled");
44
		return $this->_input->addToProperty("class","read-only");
45
	}
46
47
	/**
48
	 * Attach $this to $selector and fire $action
49
	 * @param string $selector jquery selector of the associated element
50
	 * @param string $action action to execute : check, uncheck or NULL for toggle
51
	 * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox
52
	 */
53
	public function attachEvent($selector,$action=NULL){
54
		if(isset($action)!==false || \is_numeric($action)===true){
55
			$js='$("#%identifier%").checkbox("attach events", "'.$selector.'", "'.$action.'");';
56
		}else{
57
			$js='$("#%identifier%").checkbox("attach events", "'.$selector.'");';
58
		}
59
		$js=\str_replace("%identifier%", $this->_input->getIdentifier(), $js);
60
		return $this->executeOnRun($js);
61
	}
62
63
	/**
64
	 * Attach $this to an array of $action=>$selector
65
	 * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"]
66
	 * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox
67
	 */
68
	public function attachEvents($events=array()){
69
		if(\is_array($events)){
70
			foreach ($events as $action=>$selector){
71
				$this->attachEvent($selector,$action);
72
			}
73
		}
74
		return $this;
75
	}
76
77
}