Skip to content

Commit 701cf43

Browse files
committed
asForm use case
1 parent 2bc7fe5 commit 701cf43

File tree

8 files changed

+111
-134
lines changed

8 files changed

+111
-134
lines changed

Ajax/common/Widget.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
use Ajax\semantic\html\modules\HtmlDropdown;
1313
use Ajax\service\JArray;
1414
use Ajax\service\Javascript;
15+
use Ajax\semantic\html\collections\form\HtmlForm;
16+
use Ajax\JsUtils;
17+
use Ajax\semantic\html\collections\form\HtmlFormField;
18+
use Ajax\semantic\html\collections\form\traits\FormTrait;
1519

1620
abstract class Widget extends HtmlDoubleElement {
17-
use FieldAsTrait;
21+
use FieldAsTrait,FormTrait;
1822

1923
/**
2024
* @var string classname
@@ -34,8 +38,16 @@ abstract class Widget extends HtmlDoubleElement {
3438
*/
3539
protected $_toolbarPosition;
3640

41+
/**
42+
* @var boolean
43+
*/
3744
protected $_edition;
3845

46+
/**
47+
* @var HtmlForm
48+
*/
49+
protected $_form;
50+
3951

4052
public function __construct($identifier,$model,$modelInstance=NULL) {
4153
parent::__construct($identifier);
@@ -207,7 +219,7 @@ public function addDropdownInToolbar($value,$items,$callback=NULL){
207219
* @return \Ajax\common\html\HtmlDoubleElement
208220
*/
209221
public function addButtonInToolbar($caption,$callback=NULL){
210-
$bt=new HtmlButton("",$caption);
222+
$bt=new HtmlButton("bt-".$caption,$caption);
211223
return $this->addInToolbar($bt,$callback);
212224
}
213225

@@ -235,6 +247,12 @@ public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labe
235247
return $this->addInToolbar($bt);
236248
}
237249

250+
public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
251+
$button=new HtmlButton($identifier,$value,$cssStyle);
252+
$this->_buttonAsSubmit($button,"click",$url,$responseElement);
253+
return $this->addInToolbar($button);
254+
}
255+
238256
/**
239257
* Defines a callback function to call for modifying captions
240258
* function parameters are $captions: the captions to modify and $instance: the active model instance
@@ -297,4 +315,33 @@ public function setToolbarPosition($_toolbarPosition) {
297315
return $this;
298316
}
299317

318+
public function getForm() {
319+
if(!isset($this->_form)){
320+
$this->_form=new HtmlForm("frm-".$this->identifier);
321+
$this->setEdition(true);
322+
}
323+
return $this->_form;
324+
}
325+
326+
public function run(JsUtils $js=NULL){
327+
$result=parent::run($js);
328+
if(isset($this->_form)){
329+
$this->runForm($js);
330+
}
331+
return $result;
332+
}
333+
334+
protected function runForm(JsUtils $js){
335+
$fields=$this->getContentInstances(HtmlFormField::class);
336+
foreach ($fields as $field){
337+
$this->_form->addField($field);
338+
}
339+
return $this->_form->run($js);
340+
}
341+
342+
protected function _compileForm(JsUtils $js=NULL,&$view=NULL){
343+
if(isset($this->_form)){
344+
$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."'>","</form>");
345+
}
346+
}
300347
}

Ajax/common/html/HtmlDoubleElement.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ public function wrapContent($before, $after="") {
6666
$this->wrapContentAfter=$after.$this->wrapContentAfter;
6767
return $this;
6868
}
69+
70+
public function getContentInstances($class){
71+
return $this->_getContentInstances($class,$this->content);
72+
}
73+
74+
protected function _getContentInstances($class,$content){
75+
$instances=[];
76+
if($content instanceof $class){
77+
$instances[]=$content;
78+
}elseif($content instanceof HtmlDoubleElement){
79+
$instances=\array_merge($instances,$content->getContentInstances($class));
80+
}elseif (\is_array($content)){
81+
foreach ($content as $element){
82+
$instances=\array_merge($instances,$this->_getContentInstances($class, $element));
83+
}
84+
}
85+
return $instances;
86+
}
6987
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
153153
public function run(JsUtils $js) {
154154
$compo=NULL;
155155
foreach ($this->_fields as $field){
156-
if($field instanceof HtmlFormField)
156+
if($field instanceof HtmlFormField){
157157
$compo=$this->addCompoValidation($js, $compo, $field);
158+
}
158159
}
159160
foreach ($this->content as $field){
160161
if($field instanceof HtmlFormFields){

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
1515
use Ajax\semantic\html\collections\form\HtmlFormFields;
1616
use Ajax\semantic\html\collections\HtmlMessage;
17+
use Ajax\semantic\html\elements\HtmlButton;
1718

1819
/**
1920
* @author jc
@@ -27,20 +28,21 @@ abstract protected function _getFieldIdentifier($prefix,$name="");
2728
abstract public function setValueFunction($index,$callback);
2829
abstract protected function _getFieldName($index);
2930
abstract protected function _getFieldCaption($index);
31+
abstract protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL);
3032

3133
/**
3234
* @param HtmlFormField $element
3335
* @param array $attributes
3436
*/
3537
protected function _applyAttributes($element,&$attributes,$index){
36-
$this->_addRules($element, $attributes);
3738
if(isset($attributes["callback"])){
3839
$callback=$attributes["callback"];
3940
if(\is_callable($callback)){
4041
$callback($element,$this->_modelInstance,$index);
4142
unset($attributes["callback"]);
4243
}
4344
}
45+
unset($attributes["rules"]);
4446
$element->fromArray($attributes);
4547
}
4648

@@ -55,12 +57,19 @@ protected function _addRules($element,&$attributes){
5557
if(\is_array($rules)){
5658
$element->addRules($rules);
5759
}
58-
else
60+
else{
5961
$element->addRule($rules);
60-
unset($attributes["rules"]);
62+
}
63+
unset($attributes["rules"]);
6164
}
6265
}
6366

67+
protected function _prepareFormFields(&$field,$name,&$attributes){
68+
$field->setName($name);
69+
$this->_addRules($field, $attributes);
70+
return $field;
71+
}
72+
6473
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
6574
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
6675
$caption=$this->_getFieldCaption($index);
@@ -145,10 +154,9 @@ public function fieldAsRadios($index,$elements=[],$attributes=NULL){
145154
}
146155

147156
public function fieldAsInput($index,$attributes=NULL){
148-
return $this->_fieldAs(function($id,$name,$value,$caption){
157+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
149158
$input= new HtmlFormInput($id,$caption,"text",$value);
150-
$input->setName($name);
151-
return $input;
159+
return $this->_prepareFormFields($input, $name, $attributes);
152160
}, $index,$attributes,"input");
153161
}
154162

@@ -192,4 +200,12 @@ public function fieldAsMessage($index,$attributes=NULL){
192200
return $mess;
193201
}, $index,$attributes,"message");
194202
}
203+
204+
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
205+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle){
206+
$button=new HtmlButton($id,$value,$cssStyle);
207+
$this->_buttonAsSubmit($button,"click",$url,$responseElement);
208+
return $button;
209+
}, $index,$attributes);
210+
}
195211
}

Ajax/semantic/widgets/dataelement/DataElement.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
3434
$this->_setToolbarPosition($table);
3535
}
3636
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
37+
$this->_compileForm($js,$view);
3738
return parent::compile($js,$view);
3839
}
3940

@@ -84,4 +85,8 @@ public function onNewRow($callback) {
8485
$this->content["table"]->onNewRow($callback);
8586
return $this;
8687
}
88+
89+
public function asForm(){
90+
return $this->getForm();
91+
}
8792
}

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Ajax\semantic\widgets\datatable\PositionInTable;
88
use Ajax\service\JArray;
99
use Ajax\JsUtils;
10-
use Ajax\semantic\html\collections\form\traits\FormTrait;
1110
use Ajax\semantic\html\elements\HtmlButton;
1211

1312
/**
@@ -18,11 +17,11 @@
1817
* @property FormInstanceViewer $_instanceViewer
1918
*/
2019
class DataForm extends Widget {
21-
use FormTrait;
2220

2321
public function __construct($identifier, $modelInstance=NULL) {
2422
parent::__construct($identifier, null,$modelInstance);
25-
$this->_init(new FormInstanceViewer($identifier), "form", new HtmlForm($identifier), true);
23+
$this->_form=new HtmlForm($identifier);
24+
$this->_init(new FormInstanceViewer($identifier), "form", $this->_form, true);
2625
}
2726

2827
protected function _getFieldIdentifier($prefix,$name=""){
@@ -71,9 +70,10 @@ protected function _generateContent($form){
7170
}
7271

7372
/**
74-
* @return HtmlForm
73+
* {@inheritDoc}
74+
* @see \Ajax\common\Widget::getForm()
7575
*/
76-
protected function getForm(){
76+
public function getForm(){
7777
return $this->content["form"];
7878
}
7979

@@ -91,20 +91,6 @@ public function setSeparators($separators) {
9191
return $this;
9292
}
9393

94-
public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
95-
$button=new HtmlButton($identifier,$value,$cssStyle);
96-
$this->_buttonAsSubmit($button,"click",$url,$responseElement);
97-
return $this->addInToolbar($button);
98-
}
99-
100-
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
101-
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle){
102-
$button=new HtmlButton($id,$value,$cssStyle);
103-
$this->_buttonAsSubmit($button,"click",$url,$responseElement);
104-
return $button;
105-
}, $index,$attributes);
106-
}
107-
10894
public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
10995
return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){
11096
$button=new HtmlButton($id,$value,$cssStyle);
@@ -133,4 +119,8 @@ public function setValidationParams(array $_validationParams) {
133119
$this->getForm()->setValidationParams($_validationParams);
134120
return $this;
135121
}
122+
123+
public function run(JsUtils $js=NULL){
124+
return parent::run($js);
125+
}
136126
}

Ajax/semantic/widgets/dataform/FormFieldAsTrait.php

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)