12
12
13
13
/**
14
14
* DataForm widget for editing model objects
15
- * @version 1.0
15
+ * @version 1.0.2
16
16
* @author jc
17
17
* @since 2.2
18
18
* @property FormInstanceViewer $_instanceViewer
@@ -26,24 +26,24 @@ class DataForm extends Widget {
26
26
public function __construct ($ identifier , $ modelInstance =NULL ) {
27
27
parent ::__construct ($ identifier , null ,$ modelInstance );
28
28
$ this ->_form =new HtmlForm ($ identifier );
29
- $ this ->_init (new FormInstanceViewer ($ identifier ), " form " , $ this ->_form , true );
29
+ $ this ->_init (new FormInstanceViewer ($ identifier ), ' form ' , $ this ->_form , true );
30
30
}
31
31
32
- protected function _getFieldIdentifier ($ prefix ,$ name ="" ){
32
+ protected function _getFieldIdentifier ($ prefix ,$ name ='' ){
33
33
return $ this ->identifier ."- {$ name }- " .$ this ->_instanceViewer ->getIdentifier ();
34
34
}
35
35
36
36
public function compile (JsUtils $ js =NULL ,&$ view =NULL ){
37
37
if (!$ this ->_generated ){
38
38
$ this ->_instanceViewer ->setInstance ($ this ->_modelInstance );
39
39
40
- $ form =$ this ->content [" form " ];
40
+ $ form =$ this ->content [' form ' ];
41
41
$ this ->_generateContent ($ form );
42
42
43
43
if (isset ($ this ->_toolbar )){
44
44
$ this ->_setToolbarPosition ($ form );
45
45
}
46
- $ this ->content =JArray::sortAssociative ($ this ->content , [PositionInTable::BEFORETABLE ," form " ,PositionInTable::AFTERTABLE ]);
46
+ $ this ->content =JArray::sortAssociative ($ this ->content , [PositionInTable::BEFORETABLE ,' form ' ,PositionInTable::AFTERTABLE ]);
47
47
if ($ this ->_inverted ){
48
48
$ this ->content ['form ' ]->setInverted (true );
49
49
}
@@ -61,44 +61,46 @@ protected function _generateContent($form){
61
61
$ separators =$ this ->_instanceViewer ->getSeparators ();
62
62
$ headers =$ this ->_instanceViewer ->getHeaders ();
63
63
$ wrappers =$ this ->_instanceViewer ->getWrappers ();
64
+ $ names =$ this ->_instanceViewer ->getProperties ();
64
65
\sort ($ separators );
65
- $ size =\sizeof ($ separators );
66
+ $ size =\count ($ separators );
66
67
$ nb =0 ;
67
68
if ($ size ===1 ){
68
69
$ i =-1 ;
69
- foreach ($ values as $ v ){
70
- $ this ->_generateFields ($ form , [$ v ], $ headers , $ i , $ wrappers ,$ nb ++);
70
+ foreach ($ values as $ k => $ v ){
71
+ $ this ->_generateFields ($ form , [$ v ], $ headers , $ i , $ wrappers ,$ nb ++, $ names [ $ k ]?? '' );
71
72
$ i ++;
72
73
}
73
74
}else {
74
75
$ separators []=$ count ;
75
76
for ($ i =0 ;$ i <$ size ;$ i ++){
76
77
$ fields =\array_slice ($ values , $ separators [$ i ]+1 ,$ separators [$ i +1 ]-$ separators [$ i ]);
77
- $ this ->_generateFields ($ form , $ fields , $ headers , $ separators [$ i ], $ wrappers ,$ nb ++);
78
+ $ this ->_generateFields ($ form , $ fields , $ headers , $ separators [$ i ], $ wrappers ,$ nb ++, $ names [ $ i ]?? '' );
78
79
}
79
80
}
80
81
if ($ this ->_hasRules && !$ this ->getForm ()->hasValidationParams ()){
81
- $ this ->setValidationParams ([" inline " =>true ]);
82
+ $ this ->setValidationParams ([' inline ' =>true ]);
82
83
}
83
84
}
84
85
85
- protected function _generateFields ($ form ,$ values ,$ headers ,$ sepFirst ,$ wrappers ,$ nb ){
86
+ protected function _generateFields ($ form , $ values , $ headers , $ sepFirst , $ wrappers , $ nb, $ name ){
86
87
$ wrapper =null ;
87
88
if (isset ($ headers [$ sepFirst +1 ]))
88
89
$ form ->addHeader ($ headers [$ sepFirst +1 ],4 ,true );
89
90
if (isset ($ wrappers [$ sepFirst +1 ])){
90
91
$ wrapper =$ wrappers [$ sepFirst +1 ];
91
92
}
92
- if (\sizeof ($ values )===1 ){
93
+ $ count =\count ($ values );
94
+ if ($ count ===1 ){
93
95
$ added =$ form ->addField ($ values [0 ]);
94
- }elseif (\sizeof ( $ values ) >1 ){
96
+ }elseif ($ count >1 ){
95
97
$ added =$ form ->addFields ($ values );
96
98
}else
97
99
return ;
98
100
if (isset ($ wrapper )){
99
101
$ added ->wrap ($ wrapper [0 ],$ wrapper [1 ]);
100
102
}
101
- $ this ->execHook (" onGenerateFields " ,$ added ,$ nb );
103
+ $ this ->execHook (' onGenerateFields ' ,$ added ,$ nb, $ name );
102
104
}
103
105
104
106
/**
@@ -107,14 +109,14 @@ protected function _generateFields($form,$values,$headers,$sepFirst,$wrappers,$n
107
109
* @param callable $callback the fonction to call when a field is generated
108
110
*/
109
111
public function onGenerateField ($ callback ){
110
- $ this ->addHook (" onGenerateFields " ,$ callback );
112
+ $ this ->addHook (' onGenerateFields ' ,$ callback );
111
113
}
112
114
113
115
/**
114
116
* @return HtmlForm
115
117
*/
116
118
public function getForm (){
117
- return $ this ->content [" form " ];
119
+ return $ this ->content [' form ' ];
118
120
}
119
121
120
122
public function addSeparatorAfter ($ fieldNum ){
@@ -135,7 +137,7 @@ public function setSeparators($separators) {
135
137
public function fieldAsReset ($ index ,$ cssStyle =NULL ,$ attributes =NULL ){
136
138
return $ this ->_fieldAs (function ($ id ,$ name ,$ value ) use ($ cssStyle ){
137
139
$ button =new HtmlButton ($ id ,$ value ,$ cssStyle );
138
- $ button ->setProperty (" type " , " reset " );
140
+ $ button ->setProperty (' type ' , ' reset ' );
139
141
return $ button ;
140
142
}, $ index ,$ attributes );
141
143
}
@@ -146,7 +148,7 @@ public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
146
148
* @return HtmlForm
147
149
*/
148
150
public function getHtmlComponent () {
149
- return $ this ->content [" form " ];
151
+ return $ this ->content [' form ' ];
150
152
}
151
153
/**
152
154
* {@inheritdoc}
0 commit comments