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
|
|
|
|
13
|
|
|
trait FieldsTrait { |
14
|
|
|
public abstract function addFields($fields=NULL,$label=NULL); |
15
|
|
|
public abstract function addItem($item); |
16
|
|
|
|
17
|
|
|
protected function createItem($value){ |
18
|
|
|
if(\is_array($value)){ |
19
|
|
|
$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))); |
20
|
|
|
return $itemO; |
21
|
|
|
}elseif(\is_object($value)){ |
22
|
|
|
$itemO=new HtmlFormField("field-".$this->identifier, $value); |
|
|
|
|
23
|
|
|
return $itemO; |
24
|
|
|
}else |
25
|
|
|
return new HtmlFormInput($value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function createCondition($value){ |
29
|
|
|
return \is_object($value)===false || $value instanceof \Ajax\semantic\html\elements\HtmlInput; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function addInputs($inputs,$fieldslabel=null){ |
33
|
|
|
$fields=array(); |
34
|
|
|
foreach ($inputs as $input){ |
35
|
|
|
\extract($input); |
36
|
|
|
$f=new HtmlFormInput("",""); |
37
|
|
|
$f->fromArray($input); |
38
|
|
|
$fields[]=$f; |
39
|
|
|
} |
40
|
|
|
return $this->addFields($fields,$fieldslabel); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){ |
44
|
|
|
$field=$this->getItem($index); |
|
|
|
|
45
|
|
|
if(isset($field)){ |
46
|
|
|
$field->addRule($type,$prompt,$value); |
47
|
|
|
} |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addFieldRules($index,$rules){ |
52
|
|
|
$field=$this->getItem($index); |
|
|
|
|
53
|
|
|
if(isset($field)){ |
54
|
|
|
$field->addRules($rules); |
55
|
|
|
} |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $identifier |
61
|
|
|
* @param array $items |
62
|
|
|
* @param string $label |
63
|
|
|
* @param string $value |
64
|
|
|
* @param boolean $multiple |
65
|
|
|
* @return \Ajax\common\html\HtmlDoubleElement |
66
|
|
|
*/ |
67
|
|
|
public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){ |
68
|
|
|
return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $identifier |
73
|
|
|
* @param string $label |
74
|
|
|
* @param string $type |
75
|
|
|
* @param string $value |
76
|
|
|
* @param string $placeholder |
77
|
|
|
* @return HtmlFormInput |
78
|
|
|
*/ |
79
|
|
|
public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){ |
80
|
|
|
return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function addPassword($identifier, $label=NULL){ |
84
|
|
|
return $this->addItem(new HtmlFormInput($identifier,$label,"password","","")); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){ |
88
|
|
|
return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){ |
92
|
|
|
return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function addRadio($identifier, $name,$label=NULL,$value=NULL){ |
96
|
|
|
return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value)); |
97
|
|
|
} |
98
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: