1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\widgets\dataform; |
3
|
|
|
use Ajax\semantic\html\elements\HtmlLabel; |
4
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormRadio; |
5
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormTextarea; |
6
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormField; |
7
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormInput; |
8
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormCheckbox; |
9
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormDropdown; |
10
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormFields; |
11
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author jc |
15
|
|
|
* @property FormInstanceViewer $_instanceViewer |
16
|
|
|
* @property object $_modelInstance; |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
trait FormFieldAsTrait{ |
20
|
|
|
|
21
|
|
|
abstract protected function _getFieldIdentifier($prefix); |
22
|
|
|
abstract public function setValueFunction($index,$callback); |
23
|
|
|
|
24
|
|
|
private function _getLabelField($caption,$icon=NULL){ |
|
|
|
|
25
|
|
|
$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon); |
26
|
|
|
return $label; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param HtmlFormField $element |
31
|
|
|
* @param array $attributes |
32
|
|
|
*/ |
33
|
|
|
protected function _applyAttributes($element,&$attributes,$index){ |
34
|
|
|
$this->_addRules($element, $attributes); |
35
|
|
|
if(isset($attributes["callback"])){ |
36
|
|
|
$callback=$attributes["callback"]; |
37
|
|
|
if(\is_callable($callback)){ |
38
|
|
|
$callback($element,$this->_modelInstance,$index); |
|
|
|
|
39
|
|
|
unset($attributes["callback"]); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
$element->fromArray($attributes); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function _addRules($element,$attributes){ |
46
|
|
|
if(isset($attributes["rules"])){ |
47
|
|
|
$rules=$attributes["rules"]; |
48
|
|
|
if(\is_array($rules)) |
49
|
|
|
$element->addRules($rules); |
50
|
|
|
else |
51
|
|
|
$element->addRule($rules); |
52
|
|
|
unset($attributes["rules"]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$identifier=null){ |
|
|
|
|
57
|
|
|
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback){ |
58
|
|
|
$caption=$this->_instanceViewer->getCaption($index); |
59
|
|
|
$name=$this->_instanceViewer->getFieldName($index); |
60
|
|
|
$element=$elementCallback($this->getIdentifier()."-".$name,$name,$value,$caption); |
|
|
|
|
61
|
|
|
if(\is_array($attributes)) |
62
|
|
|
$this->_applyAttributes($element, $attributes,$index); |
63
|
|
|
return $element; |
64
|
|
|
}); |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function fieldAsRadio($index,$attributes=NULL){ |
70
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
71
|
|
|
return new HtmlFormRadio($id,$name,$caption,$value); |
72
|
|
|
}, $index,$attributes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function fieldAsRadios($index,$elements=[],$attributes=NULL){ |
|
|
|
|
76
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption,$elements){ |
77
|
|
|
return HtmlFormFields::radios($name,$elements,$caption,$value); |
78
|
|
|
}, $index,$attributes); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function fieldAsTextarea($index,$attributes=NULL){ |
82
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
83
|
|
|
$textarea=new HtmlFormTextarea($id,$caption,$value); |
84
|
|
|
$textarea->setName($name); |
85
|
|
|
return $textarea; |
86
|
|
|
}, $index,$attributes); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function fieldAsInput($index,$attributes=NULL){ |
|
|
|
|
90
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
91
|
|
|
$input= new HtmlFormInput($id,$caption,"text",$value); |
92
|
|
|
$input->setName($name); |
93
|
|
|
return $input; |
94
|
|
|
}, $index,$attributes); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function fieldAsCheckbox($index,$attributes=NULL){ |
98
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
99
|
|
|
return new HtmlFormCheckbox($id,$caption,$value); |
100
|
|
|
}, $index,$attributes); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){ |
|
|
|
|
104
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements,$multiple){ |
105
|
|
|
$dd=new HtmlFormDropdown($id,$elements,$caption,$value,$multiple); |
106
|
|
|
$dd->setName($name); |
107
|
|
|
return $dd; |
108
|
|
|
}, $index,$attributes); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function fieldAsMessage($index,$attributes=NULL){ |
112
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
113
|
|
|
$mess= new HtmlMessage($id,$value); |
114
|
|
|
$mess->addHeader($caption); |
115
|
|
|
return $mess; |
116
|
|
|
}, $index,$attributes,"message"); |
117
|
|
|
} |
118
|
|
|
} |