Passed
Push — master ( 9a9b82...74dc3c )
by Jean-Christophe
02:28
created

HtmlFormField::setField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 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
use Ajax\semantic\html\base\constants\Direction;
9
use Ajax\semantic\html\elements\HtmlLabel;
10
use Ajax\semantic\components\validation\FieldValidation;
11
use Ajax\semantic\html\collections\form\traits\FieldTrait;
12
use Ajax\semantic\html\base\constants\Size;
13
14
class HtmlFormField extends HtmlSemDoubleElement {
15
	use FieldTrait;
16
	protected $_container;
17
	protected $_validation;
18
	public function __construct($identifier, $field,$label=NULL) {
19
		parent::__construct($identifier, "div","field");
20
		$this->content=array();
21
		$this->_states=[State::ERROR,State::DISABLED];
22
		if(isset($label) && $label!=="")
23
			$this->setLabel($label);
24
		$this->setField($field);
25
		$this->_validation=NULL;
26
	}
27
28
	public function addPointingLabel($label,$pointing=Direction::NONE){
29
		$labelO=new HtmlLabel("",$label);
30
		$labelO->setPointing($pointing);
31
		$this->addContent($labelO,$pointing==="below" || $pointing==="right");
32
		return $labelO;
33
	}
34
35
	public function setLabel($label){
36
		$labelO=$label;
37
		if(\is_string($label)){
38
			$labelO=new HtmlSemDoubleElement("","label","");
39
			$labelO->setContent($label);
40
			$labelO->setProperty("for", \str_replace("field-", "",$this->identifier));
41
		}
42
		$this->content["label"]=$labelO;
43
	}
44
45
	public function setField($field){
46
		$this->content["field"]=$field;
47
	}
48
49
	/**
50
	 * Returns the label or null
51
	 * @return mixed
52
	 */
53
	public function getLabel(){
54
		if(\array_key_exists("label", $this->content))
55
			return $this->content["label"];
56
	}
57
58
	/**
59
	 * Return the field
60
	 * @return mixed
61
	 */
62
	public function getField(){
63
		return $this->content["field"];
64
	}
65
66
	/**
67
	 * Return the field with data
68
	 * @return mixed
69
	 */
70
	public function getDataField(){
71
		return $this->content["field"];
72
	}
73
74
	/**
75
	 * puts the label before or behind
76
	 */
77
	public function swapLabel(){
78
		$label=$this->getLabel();
79
		unset($this->content["label"]);
80
		$this->content["label"]=$label;
81
	}
82
83
	/**
84
	 * Defines the field width
85
	 * @param int $width
86
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
87
	 */
88
	public function setWidth($width){
89
		if(\is_int($width)){
0 ignored issues
show
introduced by
The condition is_int($width) is always true.
Loading history...
90
			$width=Wide::getConstants()["W".$width];
91
		}
92
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
93
		if(isset($this->_container)){
94
			$this->_container->setEqualWidth(false);
95
		}
96
		return $this->addToPropertyCtrl("class", "wide",array("wide"));
97
	}
98
99
	/**
100
	 * Field displays an error state
101
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
102
	 */
103
	public function setError(){
104
		return $this->addToProperty("class", "error");
105
	}
106
107
	public function setInline(){
108
		return $this->addToProperty("class", "inline");
109
	}
110
111
	public function jsState($state){
112
		return $this->jsDoJquery("addClass",$state);
113
	}
114
115
	public function setContainer($_container) {
116
		$this->_container=$_container;
117
		return $this;
118
	}
119
120
	public function setReadonly(){
121
		$this->getDataField()->setProperty("readonly", "");
122
	}
123
124
	public function addRule($type,$prompt=NULL,$value=NULL){
125
		$field=$this->getDataField();
126
		if(isset($field)){
127
			if(!isset($this->_validation)){
128
				$this->_validation=new FieldValidation($field->getIdentifier());
129
			}
130
			if($type==="empty"){
131
				$this->addToProperty("class","required");
132
			}
133
			$this->_validation->addRule($type,$prompt,$value);
134
		}
135
		return $this;
136
	}
137
	
138
	public function setOptional($optional=true){
139
		$field=$this->getDataField();
140
		if(isset($field)){
141
			if(!isset($this->_validation)){
142
				$this->_validation=new FieldValidation($field->getIdentifier());
143
			}
144
			$this->_validation->setOptional($optional);
145
		}
146
	}
147
148
	public function addRules(array $rules){
149
		foreach ($rules as $rule){
150
			$this->addRule($rule);
151
		}
152
		return $this;
153
	}
154
155
	public function setRules(array $rules){
156
		$this->_validation=null;
157
		return $this->addRules($rules);
158
	}
159
160
	public function addIcon($icon,$direction=Direction::LEFT){
161
		$field=$this->getField();
162
		return $field->addIcon($icon,$direction);
163
	}
164
165
	public function getValidation() {
166
		return $this->_validation;
167
	}
168
	
169
	public function setSize($size) {
170
		return $this->getField()->addToPropertyCtrl("class", $size, Size::getConstants());
171
	}
172
}
173