Skip to content

Commit f3b1f4e

Browse files
committed
Update DataForm.php
1 parent 7087233 commit f3b1f4e

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/**
1414
* DataForm widget for editing model objects
15-
* @version 1.0
15+
* @version 1.0.2
1616
* @author jc
1717
* @since 2.2
1818
* @property FormInstanceViewer $_instanceViewer
@@ -26,24 +26,24 @@ class DataForm extends Widget {
2626
public function __construct($identifier, $modelInstance=NULL) {
2727
parent::__construct($identifier, null,$modelInstance);
2828
$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);
3030
}
3131

32-
protected function _getFieldIdentifier($prefix,$name=""){
32+
protected function _getFieldIdentifier($prefix,$name=''){
3333
return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier();
3434
}
3535

3636
public function compile(JsUtils $js=NULL,&$view=NULL){
3737
if(!$this->_generated){
3838
$this->_instanceViewer->setInstance($this->_modelInstance);
3939

40-
$form=$this->content["form"];
40+
$form=$this->content['form'];
4141
$this->_generateContent($form);
4242

4343
if(isset($this->_toolbar)){
4444
$this->_setToolbarPosition($form);
4545
}
46-
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]);
46+
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,'form',PositionInTable::AFTERTABLE]);
4747
if($this->_inverted){
4848
$this->content['form']->setInverted(true);
4949
}
@@ -61,44 +61,46 @@ protected function _generateContent($form){
6161
$separators=$this->_instanceViewer->getSeparators();
6262
$headers=$this->_instanceViewer->getHeaders();
6363
$wrappers=$this->_instanceViewer->getWrappers();
64+
$names=$this->_instanceViewer->getProperties();
6465
\sort($separators);
65-
$size=\sizeof($separators);
66+
$size=\count($separators);
6667
$nb=0;
6768
if($size===1){
6869
$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]??'');
7172
$i++;
7273
}
7374
}else{
7475
$separators[]=$count;
7576
for($i=0;$i<$size;$i++){
7677
$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]??'');
7879
}
7980
}
8081
if($this->_hasRules && !$this->getForm()->hasValidationParams()){
81-
$this->setValidationParams(["inline"=>true]);
82+
$this->setValidationParams(['inline'=>true]);
8283
}
8384
}
8485

85-
protected function _generateFields($form,$values,$headers,$sepFirst,$wrappers,$nb){
86+
protected function _generateFields($form, $values, $headers, $sepFirst, $wrappers, $nb, $name){
8687
$wrapper=null;
8788
if(isset($headers[$sepFirst+1]))
8889
$form->addHeader($headers[$sepFirst+1],4,true);
8990
if(isset($wrappers[$sepFirst+1])){
9091
$wrapper=$wrappers[$sepFirst+1];
9192
}
92-
if(\sizeof($values)===1){
93+
$count=\count($values);
94+
if($count===1){
9395
$added=$form->addField($values[0]);
94-
}elseif(\sizeof($values)>1){
96+
}elseif($count>1){
9597
$added=$form->addFields($values);
9698
}else
9799
return;
98100
if(isset($wrapper)){
99101
$added->wrap($wrapper[0],$wrapper[1]);
100102
}
101-
$this->execHook("onGenerateFields",$added,$nb);
103+
$this->execHook('onGenerateFields',$added,$nb,$name);
102104
}
103105

104106
/**
@@ -107,14 +109,14 @@ protected function _generateFields($form,$values,$headers,$sepFirst,$wrappers,$n
107109
* @param callable $callback the fonction to call when a field is generated
108110
*/
109111
public function onGenerateField($callback){
110-
$this->addHook("onGenerateFields",$callback);
112+
$this->addHook('onGenerateFields',$callback);
111113
}
112114

113115
/**
114116
* @return HtmlForm
115117
*/
116118
public function getForm(){
117-
return $this->content["form"];
119+
return $this->content['form'];
118120
}
119121

120122
public function addSeparatorAfter($fieldNum){
@@ -135,7 +137,7 @@ public function setSeparators($separators) {
135137
public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
136138
return $this->_fieldAs(function($id,$name,$value) use ($cssStyle){
137139
$button=new HtmlButton($id,$value,$cssStyle);
138-
$button->setProperty("type", "reset");
140+
$button->setProperty('type', 'reset');
139141
return $button;
140142
}, $index,$attributes);
141143
}
@@ -146,7 +148,7 @@ public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
146148
* @return HtmlForm
147149
*/
148150
public function getHtmlComponent() {
149-
return $this->content["form"];
151+
return $this->content['form'];
150152
}
151153
/**
152154
* {@inheritdoc}

0 commit comments

Comments
 (0)