99use Ajax \semantic \html \base \constants \Size ;
1010use Ajax \semantic \html \elements \HtmlLabel ;
1111use Ajax \semantic \html \modules \HtmlProgress ;
12+ use Ajax \semantic \html \modules \HtmlRating ;
13+ use Ajax \semantic \html \base \HtmlSemDoubleElement ;
1214
1315/**
1416 * @author jc
1719
1820trait FieldAsTrait{
1921
20- protected abstract function _getFieldIdentifier ($ prefix );
21- public abstract function setValueFunction ($ index ,$ callback );
22+ abstract protected function _getFieldIdentifier ($ prefix );
23+ abstract public function setValueFunction ($ index ,$ callback );
2224
2325 private function _getLabelField ($ caption ,$ icon =NULL ){
2426 $ label =new HtmlLabel ($ this ->_getFieldIdentifier ("lbl " ),$ caption ,$ icon );
2527 return $ label ;
2628 }
2729
30+ /**
31+ * @param HtmlSemDoubleElement $element
32+ * @param array $attributes
33+ */
34+ protected function _applyAttributes ($ element ,&$ attributes ,$ index ){
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+
2845
2946 public function fieldAsProgress ($ index ,$ label =NULL , $ attributes =array ()){
3047 $ this ->setValueFunction ($ index ,function ($ value ) use ($ label ,$ attributes ){
@@ -34,6 +51,14 @@ public function fieldAsProgress($index,$label=NULL, $attributes=array()){
3451 return $ this ;
3552 }
3653
54+ public function fieldAsRating ($ index ,$ max =5 , $ icon ="" ){
55+ $ this ->setValueFunction ($ index ,function ($ value ) use ($ max ,$ icon ){
56+ $ rating =new HtmlRating ($ this ->_getFieldIdentifier ("rat " ),$ value ,$ max ,$ icon );
57+ return $ rating ;
58+ });
59+ return $ this ;
60+ }
61+
3762 public function fieldAsLabel ($ index ,$ icon =NULL ){
3863 $ this ->setValueFunction ($ index ,function ($ caption ) use ($ icon ){
3964 $ lbl =$ this ->_getLabelField ($ caption ,$ icon );
@@ -55,49 +80,54 @@ public function fieldAsAvatar($index){
5580 return $ this ;
5681 }
5782
58- public function fieldAsRadio ($ index ,$ name =NULL ){
59- $ this ->setValueFunction ($ index ,function ($ value )use ($ index ,$ name ){
60- if (isset ($ name )===false ){
61- $ name =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
83+
84+ public function fieldAsRadio ($ index ,$ attributes =NULL ){
85+ $ this ->setValueFunction ($ index ,function ($ value )use ($ index ,$ attributes ){
86+ if (isset ($ attributes ["name " ])===false ){
87+ $ attributes ["name " ]=$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
6288 }
63- $ radio =new HtmlRadio ($ this ->_getFieldIdentifier ("radio " ),$ name ,$ value ,$ value );
89+ $ radio =new HtmlRadio ($ this ->_getFieldIdentifier ("radio " ),$ attributes ["name " ],$ value ,$ value );
90+ $ this ->_applyAttributes ($ radio , $ attributes , $ index );
6491 return $ radio ;
6592 });
6693 return $ this ;
6794 }
6895
69- public function fieldAsInput ($ index ,$ name =NULL , $ type = " text " , $ placeholder = "" ){
70- $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ name , $ type , $ placeholder ){
71- $ input =new HtmlInput ($ this ->_getFieldIdentifier ("input " ),$ type ,$ value, $ placeholder );
72- if (isset ($ name )===false ){
73- $ name =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
96+ public function fieldAsInput ($ index ,$ attributes =NULL ){
97+ $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ attributes ){
98+ $ input =new HtmlInput ($ this ->_getFieldIdentifier ("input " )," text " ,$ value );
99+ if (isset ($ attributes [ " name " ] )===false ){
100+ $ attributes [ " name " ] =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
74101 }
75- $ input ->getField ()->setProperty ("name " , $ name );
102+ $ input ->getField ()->setProperty ("name " , $ attributes ["name " ]);
103+ $ this ->_applyAttributes ($ input , $ attributes , $ index );
76104 return $ input ;
77105 });
78106 return $ this ;
79107 }
80108
81- public function fieldAsCheckbox ($ index ,$ name =NULL ){
82- $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ name ){
109+ public function fieldAsCheckbox ($ index ,$ attributes =NULL ){
110+ $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ attributes ){
83111 $ checkbox =new HtmlCheckbox ($ this ->_getFieldIdentifier ("ck " ),"" ,$ value );
84112 $ checkbox ->setChecked (JString::isBooleanTrue ($ value ));
85- if (isset ($ name )===false ){
86- $ name =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
113+ if (isset ($ attributes [ " name " ] )===false ){
114+ $ attributes [ " name " ] =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
87115 }
88- $ checkbox ->getField ()->setProperty ("name " , $ name );
116+ $ checkbox ->getField ()->setProperty ("name " , $ attributes ["name " ]);
117+ $ this ->_applyAttributes ($ checkbox , $ attributes , $ index );
89118 return $ checkbox ;
90119 });
91120 return $ this ;
92121 }
93122
94- public function fieldAsDropDown ($ index ,$ elements =[],$ multiple =false ,$ name =NULL ){
95- $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ elements ,$ multiple ,$ name ){
123+ public function fieldAsDropDown ($ index ,$ elements =[],$ multiple =false ,$ attributes =NULL ){
124+ $ this ->setValueFunction ($ index ,function ($ value ) use ($ index ,$ elements ,$ multiple ,$ attributes ){
96125 $ dd =new HtmlDropdown ($ this ->_getFieldIdentifier ("dd " ),$ value ,$ elements );
97- if (isset ($ name )===false ){
98- $ name =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
126+ if (isset ($ attributes [ " name " ] )===false ){
127+ $ attributes [ " name " ] =$ this ->_instanceViewer ->getCaption ($ index )."[] " ;
99128 }
100- $ dd ->asSelect ($ name ,$ multiple );
129+ $ dd ->asSelect ($ attributes ["name " ],$ multiple );
130+ $ this ->_applyAttributes ($ dd , $ attributes , $ index );
101131 return $ dd ;
102132 });
103133 return $ this ;
0 commit comments