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

FieldsTrait::createCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\html\collections\form\traits;
4
5
use Ajax\service\JArray;
6
use Ajax\semantic\html\collections\form\HtmlFormInput;
7
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
8
use Ajax\semantic\html\elements\HtmlButton;
9
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
10
use Ajax\semantic\html\collections\form\HtmlFormRadio;
11
use Ajax\semantic\html\collections\form\HtmlFormField;
12
use Ajax\common\html\html5\HtmlTextarea;
13
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
14
use Ajax\semantic\html\base\HtmlSemDoubleElement;
15
use Ajax\semantic\html\elements\HtmlButtonGroups;
16
use Ajax\semantic\html\collections\form\HtmlFormFields;
17
18
/**
19
 * @author jc
20
 * @property string $identifier
21
 */
22
trait FieldsTrait {
23
	abstract public function addFields($fields=NULL,$label=NULL);
24
	abstract public function addItem($item);
25
	abstract public function getItem($index);
26
	abstract public function count();
27
28
	protected function createItem($value){
29
		if(\is_array($value)){
30
			$itemO=new HtmlFormInput(JArray::getDefaultValue($value, "id",""),JArray::getDefaultValue($value, "label",null),JArray::getDefaultValue($value, "type", "text"),JArray::getDefaultValue($value, "value",""),JArray::getDefaultValue($value, "placeholder",JArray::getDefaultValue($value, "label",null)));
31
			return $itemO;
32
		}elseif(\is_object($value)){
33
			$itemO=new HtmlFormField("field-".$this->identifier, $value);
34
			return $itemO;
35
		}else
36
			return new HtmlFormInput($value);
37
	}
38
39
	protected function createCondition($value){
40
		return \is_object($value)===false || $value instanceof \Ajax\semantic\html\elements\HtmlInput;
41
	}
42
43
	public function addInputs($inputs,$fieldslabel=null){
44
		$fields=array();
45
		foreach ($inputs as $input){
46
			\extract($input);
47
			$f=new HtmlFormInput("","");
48
			$f->fromArray($input);
49
			$fields[]=$f;
50
		}
51
		return $this->addFields($fields,$fieldslabel);
52
	}
53
54
	/**
55
	 * Sets the values of a property for each Field of each item in the collection
56
	 * @param string $property
57
	 * @param array|mixed $values
58
	 * @return HtmlFormFields
59
	 */
60
	public function setFieldsPropertyValues($property,$values){
61
		$i=0;
62
		if(\is_array($values)===false){
63
			$values=\array_fill(0, $this->count(),$values);
64
		}
65
		foreach ($values as $value){
66
			$c=$this->content[$i++];
67
			if(isset($c)){
68
				$df=$c->getDataField();
69
				$df->setProperty($property,$value);
70
			}
71
			else{
72
				return $this;
73
			}
74
		}
75
		return $this;
76
	}
77
78
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
79
		$field=$this->getItem($index);
80
		if(isset($field)){
81
			$field->addRule($type,$prompt,$value);
82
		}
83
		return $this;
84
	}
85
86
	public function addFieldRules($index,$rules){
87
		$field=$this->getItem($index);
88
		if(isset($field)){
89
			$field->addRules($rules);
90
		}
91
		return $this;
92
	}
93
94
	/**
95
	 * Adds a new dropdown element
96
	 * @param string $identifier
97
	 * @param array $items
98
	 * @param string $label
99
	 * @param string $value
100
	 * @param boolean $multiple
101
	 * @return HtmlFormDropdown
102
	 */
103
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
104
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
105
	}
106
107
	/**
108
	 * Adds a new button groups
109
	 * @param string $identifier
110
	 * @param array $elements
111
	 * @param boolean $asIcons
112
	 * @return HtmlButtonGroups
113
	 */
114
	public function addButtonGroups($identifier,$elements=[],$asIcons=false){
115
		return $this->addItem(new HtmlButtonGroups($identifier,$elements,$asIcons));
116
	}
117
118
	/**
119
	 * Adds a button with a dropdown button
120
	 * @param string $identifier
121
	 * @param string $value
122
	 * @param array $items
123
	 * @param boolean $asCombo
124
	 * @param string $icon
125
	 * @return HtmlButtonGroups
126
	 */
127
	public function addDropdownButton($identifier,$value,$items=[],$asCombo=false,$icon=null){
128
		return $this->addItem(HtmlButton::dropdown($identifier, $value,$items,$asCombo,$icon));
129
	}
130
131
	/**
132
	 * @param string $identifier
133
	 * @param string $label
134
	 * @param string $type
135
	 * @param string $value
136
	 * @param string $placeholder
137
	 * @return HtmlFormInput
138
	 */
139
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
140
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
141
	}
142
143
	/**
144
	 * @param string $identifier
145
	 * @param string $label
146
	 * @param string $value
147
	 * @param string $placeholder
148
	 * @param int $rows
149
	 * @return HtmlTextarea
150
	 */
151
	public function addTextarea($identifier, $label,$value=NULL,$placeholder=NULL,$rows=5){
152
		return $this->addItem(new HtmlFormTextarea($identifier,$label,$value,$placeholder,$rows));
153
	}
154
155
	public function addPassword($identifier, $label=NULL){
156
		return $this->addItem(new HtmlFormInput($identifier,$label,"password","",""));
157
	}
158
159
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
160
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
161
	}
162
	
163
	public function addButtonIcon($identifier,$icon,$cssStyle=NULL,$onClick=NULL){
164
		$bt=new HtmlButton($identifier);
165
		$bt->asIcon($icon);
166
		if(isset($onClick))
167
			$bt->onClick($onClick);
168
		if (isset($cssStyle))
169
			$bt->addClass($cssStyle);
170
		return $this->addItem($bt);
171
	}
172
173
	/**
174
	 * @param string $identifier
175
	 * @param string $label
176
	 * @param string $value
177
	 * @param string $type
178
	 * @return HtmlFormCheckbox
179
	 */
180
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
181
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
182
	}
183
184
	public function addRadio($identifier, $name,$label=NULL,$value=NULL){
185
		return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value));
186
	}
187
188
	public function addElement($identifier,$content,$label,$tagName="div",$baseClass=""){
189
		$div=new HtmlSemDoubleElement($identifier,$tagName,$baseClass,$content);
190
		return $this->addItem(new HtmlFormField("field-".$identifier, $div,$label));
191
	}
192
}
193