Skip to content

Commit 2bc7fe5

Browse files
committed
widget/FieldAs refactoring
1 parent 2c7b4a0 commit 2bc7fe5

File tree

7 files changed

+74
-14
lines changed

7 files changed

+74
-14
lines changed

Ajax/common/Widget.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ protected function _init($instanceViewer,$contentKey,$content,$edition){
5252
$this->_edition=$edition;
5353
}
5454

55-
protected function _getFieldIdentifier($prefix){
55+
protected function _getFieldIdentifier($prefix,$name=""){
5656
return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
5757
}
5858

59+
protected function _getFieldName($index){
60+
return $this->_instanceViewer->getFieldName($index);
61+
}
62+
63+
protected function _getFieldCaption($index){
64+
return $this->_instanceViewer->getCaption($index);
65+
}
66+
5967
abstract protected function _setToolbarPosition($table,$captions=NULL);
6068

6169
public function show($modelInstance){

Ajax/semantic/html/collections/form/HtmlForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct($identifier, $elements=array()) {
3636
$this->setProperty("name", $this->identifier);
3737
$this->_fields=array ();
3838
$this->addItems($elements);
39+
$this->_validationParams=[];
3940
}
4041

4142
protected function getForm(){

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Ajax\semantic\html\collections\form\HtmlFormInput;
1313
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
1414
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15+
use Ajax\semantic\html\collections\form\HtmlFormFields;
16+
use Ajax\semantic\html\collections\HtmlMessage;
1517

1618
/**
1719
* @author jc
@@ -21,8 +23,10 @@
2123
*/
2224
trait FieldAsTrait{
2325

24-
abstract protected function _getFieldIdentifier($prefix);
26+
abstract protected function _getFieldIdentifier($prefix,$name="");
2527
abstract public function setValueFunction($index,$callback);
28+
abstract protected function _getFieldName($index);
29+
abstract protected function _getFieldCaption($index);
2630

2731
/**
2832
* @param HtmlFormField $element
@@ -45,17 +49,31 @@ private function _getLabelField($caption,$icon=NULL){
4549
return $label;
4650
}
4751

48-
protected function _addRules($element,&$attributes){}
52+
protected function _addRules($element,&$attributes){
53+
if(isset($attributes["rules"])){
54+
$rules=$attributes["rules"];
55+
if(\is_array($rules)){
56+
$element->addRules($rules);
57+
}
58+
else
59+
$element->addRule($rules);
60+
unset($attributes["rules"]);
61+
}
62+
}
4963

5064
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
5165
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
52-
$name=$this->_instanceViewer->getFieldName($index)."[]";
66+
$caption=$this->_getFieldCaption($index);
67+
$name=$this->_getFieldName($index);
68+
$id=$this->_getFieldIdentifier($prefix,$name);
5369
if(isset($attributes["name"])){
5470
$name=$attributes["name"];
71+
unset($attributes["name"]);
5572
}
56-
$element=$elementCallback($this->_getFieldIdentifier($prefix),$name,$value,"");
57-
if(\is_array($attributes))
73+
$element=$elementCallback($id,$name,$value,$caption);
74+
if(\is_array($attributes)){
5875
$this->_applyAttributes($element, $attributes,$index);
76+
}
5977
$element->setDisabled(!$this->_edition);
6078
return $element;
6179
});
@@ -120,9 +138,15 @@ public function fieldAsRadio($index,$attributes=NULL){
120138
}, $index,$attributes,"radio");
121139
}
122140

141+
public function fieldAsRadios($index,$elements=[],$attributes=NULL){
142+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
143+
return HtmlFormFields::radios($name,$elements,$caption,$value);
144+
}, $index,$attributes,"radios");
145+
}
146+
123147
public function fieldAsInput($index,$attributes=NULL){
124-
return $this->_fieldAs(function($id,$name,$value){
125-
$input= new HtmlFormInput($id,"","text",$value);
148+
return $this->_fieldAs(function($id,$name,$value,$caption){
149+
$input= new HtmlFormInput($id,$caption,"text",$value);
126150
$input->setName($name);
127151
return $input;
128152
}, $index,$attributes,"input");
@@ -148,17 +172,24 @@ public function fieldAsCheckbox($index,$attributes=NULL){
148172
return $this->_fieldAs(function($id,$name,$value){
149173
$input=new HtmlFormCheckbox($id,NULL,$this->_instanceViewer->getIdentifier());
150174
$input->setChecked(JString::isBooleanTrue($value));
151-
$input->getField()->setProperty("name", $name);
175+
$input->setName($name);
152176
return $input;
153177
}, $index,$attributes,"ck");
154178
}
155179

156180
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
157181
return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
158-
//$dd=new HtmlDropdown($id,$value,$elements);
159182
$dd=new HtmlFormDropdown($id,$elements,NULL,$value);
160183
$dd->asSelect($name,$multiple);
161184
return $dd;
162185
}, $index,$attributes,"dd");
163186
}
187+
188+
public function fieldAsMessage($index,$attributes=NULL){
189+
return $this->_fieldAs(function($id,$name,$value,$caption){
190+
$mess= new HtmlMessage("message-".$id,$value);
191+
$mess->addHeader($caption);
192+
return $mess;
193+
}, $index,$attributes,"message");
194+
}
164195
}

Ajax/semantic/widgets/dataelement/DataElement.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ protected function _generateContent($table){
4949
}
5050
}
5151

52+
protected function _getFieldName($index){
53+
return $this->_instanceViewer->getFieldName($index);
54+
}
55+
56+
protected function _getFieldCaption($index){
57+
return null;
58+
}
59+
5260
/**
5361
* {@inheritDoc}
5462
* @see \Ajax\common\Widget::getHtmlComponent()

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
* @property FormInstanceViewer $_instanceViewer
1919
*/
2020
class DataForm extends Widget {
21-
use FormFieldAsTrait,FormTrait;
21+
use FormTrait;
2222

2323
public function __construct($identifier, $modelInstance=NULL) {
2424
parent::__construct($identifier, null,$modelInstance);
2525
$this->_init(new FormInstanceViewer($identifier), "form", new HtmlForm($identifier), true);
2626
}
2727

28+
protected function _getFieldIdentifier($prefix,$name=""){
29+
return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier();
30+
}
31+
2832
public function compile(JsUtils $js=NULL,&$view=NULL){
2933
$this->_instanceViewer->setInstance($this->_modelInstance);
3034

@@ -59,7 +63,7 @@ protected function _generateContent($form){
5963
if(\sizeof($fields)===1){
6064
$form->addField($fields[0]);
6165
}elseif(\sizeof($fields)>1){
62-
$form->addFields($fields,"grouped");
66+
$form->addFields($fields);
6367
$i+=\sizeof($fields)-1;
6468
}
6569
}

Ajax/semantic/widgets/dataform/FormFieldAsTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function _addRules($element,&$attributes){
3939
}
4040
}
4141

42-
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$identifier=null){
42+
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
4343
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback){
4444
$caption=$this->_instanceViewer->getCaption($index);
4545
$name=$this->_instanceViewer->getFieldName($index);
@@ -60,7 +60,7 @@ public function fieldAsRadio($index,$attributes=NULL){
6060
}
6161

6262
public function fieldAsRadios($index,$elements=[],$attributes=NULL){
63-
return $this->_fieldAs(function($id,$name,$value,$caption,$elements){
63+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
6464
return HtmlFormFields::radios($name,$elements,$caption,$value);
6565
}, $index,$attributes);
6666
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ private function _generatePagination($table){
126126
$menu->postOnClick($this->_urls,"{'p':$(this).attr('data-page')}","#".$this->identifier." tbody",["preventDefault"=>false,"jqueryDone"=>"replaceWith"]);
127127
}
128128

129+
protected function _getFieldName($index){
130+
return parent::_getFieldName($index)."[]";
131+
}
132+
133+
protected function _getFieldCaption($index){
134+
return null;
135+
}
136+
129137
protected function _setToolbarPosition($table,$captions=NULL){
130138
switch ($this->_toolbarPosition){
131139
case PositionInTable::BEFORETABLE:

0 commit comments

Comments
 (0)