Completed
Push — master ( b43144...5df1fc )
by Jean-Christophe
03:58
created

HtmlFormField::setReadonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\Wide;
7
use Ajax\semantic\html\base\constants\State;
8
9
class HtmlFormField extends HtmlSemDoubleElement {
10
	protected $_container;
11
	public function __construct($identifier, $field,$label=NULL) {
12
		parent::__construct($identifier, "div","field");
13
		$this->content=array();
14
		$this->_states=[State::ERROR,State::DISABLED];
15
		if(isset($label))
16
			$this->setLabel($label);
17
		$this->setField($field);
18
	}
19
20
	public function setLabel($label){
21
		$labelO=$label;
22
		if(\is_string($label)){
23
			$labelO=new HtmlSemDoubleElement("","label");
24
			$labelO->setContent($label);
25
			$labelO->setProperty("for", \str_replace("field-", "",$this->identifier));
26
		}
27
		$this->content["label"]=$labelO;
28
	}
29
30
	public function setField($field){
31
		$this->content["field"]=$field;
32
	}
33
34
	/**
35
	 * Returns the label or null
36
	 * @return mixed
37
	 */
38
	public function getLabel(){
39
		if(\array_key_exists("label", $this->content))
40
			return $this->content["label"];
41
	}
42
43
	/**
44
	 * Return the field
45
	 * @return mixed
46
	 */
47
	public function getField(){
48
		return $this->content["field"];
49
	}
50
51
	/**
52
	 * puts the label before or behind
53
	 */
54
	public function swapLabel(){
55
		$label=$this->getLabel();
56
		unset($this->content["label"]);
57
		$this->content["label"]=$label;
58
	}
59
60
	/**
61
	 * Defines the field width
62
	 * @param int $width
63
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
64
	 */
65
	public function setWidth($width){
66
		if(\is_int($width)){
67
			$width=Wide::getConstants()["W".$width];
68
		}
69
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
70
		if(isset($this->_container)){
71
			$this->_container->setEqualWidth(false);
72
		}
73
		return $this->addToPropertyCtrl("class", "wide",array("wide"));
74
	}
75
76
	/**
77
	 * Field displays an error state
78
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
79
	 */
80
	public function setError(){
81
		return $this->addToProperty("class", "error");
82
	}
83
84
	public function jsState($state){
85
		return $this->jsDoJquery("addClass",$state);
86
	}
87
88
	public function setContainer($_container) {
89
		$this->_container=$_container;
90
		return $this;
91
	}
92
93
	public function setReadonly(){
94
		$this->getField()->setProperty("readonly", "");
95
	}
96
97
}