Passed
Push — master ( 5c767a...47de24 )
by Jean-Christophe
06:45
created

HtmlFormField::setInline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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