1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\widgets\dataform; |
4
|
|
|
|
5
|
|
|
use Ajax\common\Widget; |
6
|
|
|
use Ajax\semantic\html\collections\form\HtmlForm; |
7
|
|
|
use Ajax\semantic\widgets\datatable\PositionInTable; |
8
|
|
|
use Ajax\service\JArray; |
9
|
|
|
use Ajax\JsUtils; |
10
|
|
|
use Ajax\semantic\html\collections\form\traits\FormTrait; |
11
|
|
|
use Ajax\semantic\html\elements\HtmlButton; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* DataForm widget for editing model objects |
15
|
|
|
* @version 1.0 |
16
|
|
|
* @author jc |
17
|
|
|
* @since 2.2 |
18
|
|
|
*/ |
19
|
|
|
class DataForm extends Widget { |
20
|
|
|
use FormFieldAsTrait,FormTrait; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function __construct($identifier, $modelInstance=NULL) { |
|
|
|
|
23
|
|
|
parent::__construct($identifier, null,$modelInstance); |
24
|
|
|
$this->_instanceViewer=new FormInstanceViewer(); |
|
|
|
|
25
|
|
|
$this->content=["form"=>new HtmlForm($identifier)]; |
26
|
|
|
$this->_toolbarPosition=PositionInTable::BEFORETABLE; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function compile(JsUtils $js=NULL,&$view=NULL){ |
|
|
|
|
30
|
|
|
$this->_instanceViewer->setInstance($this->_modelInstance); |
31
|
|
|
|
32
|
|
|
$form=$this->content["form"]; |
33
|
|
|
$this->_generateContent($form); |
34
|
|
|
|
35
|
|
|
if(isset($this->_toolbar)){ |
36
|
|
|
$this->_setToolbarPosition($form); |
37
|
|
|
} |
38
|
|
|
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]); |
39
|
|
|
return parent::compile($js,$view); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param HtmlForm $form |
44
|
|
|
*/ |
45
|
|
|
protected function _generateContent($form){ |
46
|
|
|
$values= $this->_instanceViewer->getValues(); |
47
|
|
|
$count=$this->_instanceViewer->count(); |
48
|
|
|
$separators=$this->_instanceViewer->getSeparators(); |
49
|
|
|
$size=\sizeof($separators); |
50
|
|
|
if($size===1){ |
51
|
|
|
foreach ($values as $v){ |
52
|
|
|
$form->addField($v); |
53
|
|
|
} |
54
|
|
|
}else{ |
55
|
|
|
$separators[]=$count; |
56
|
|
|
for($i=0;$i<$size-1;$i++){ |
57
|
|
|
$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]); |
58
|
|
|
if(\sizeof($fields)===1){ |
59
|
|
|
$form->addField($fields[0]); |
60
|
|
|
}else |
61
|
|
|
$form->addFields($fields); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return HtmlForm |
68
|
|
|
*/ |
69
|
|
|
protected function getForm(){ |
70
|
|
|
return $this->content["form"]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function addSeparatorAfter($fieldNum){ |
74
|
|
|
$this->_instanceViewer->addSeparatorAfter($fieldNum); |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getSeparators() { |
79
|
|
|
return $this->_instanceViewer->getSeparators(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setSeparators($separators) { |
83
|
|
|
$this->_instanceViewer->setSeparators($separators); |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){ |
88
|
|
|
$button=new HtmlButton($identifier,$value,$cssStyle); |
89
|
|
|
$this->_buttonAsSubmit($button,"click",$url,$responseElement); |
90
|
|
|
return $this->addInToolbar($button); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
94
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle){ |
|
|
|
|
95
|
|
|
$button=new HtmlButton($id,$value,$cssStyle); |
96
|
|
|
$this->_buttonAsSubmit($button,"click",$url,$responseElement); |
97
|
|
|
return $button; |
98
|
|
|
}, $index,$attributes); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){ |
102
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){ |
|
|
|
|
103
|
|
|
$button=new HtmlButton($id,$value,$cssStyle); |
104
|
|
|
$button->setProperty("type", "reset"); |
105
|
|
|
return $button; |
106
|
|
|
}, $index,$attributes); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
* @see \Ajax\common\Widget::getHtmlComponent() |
112
|
|
|
* @return HtmlForm |
113
|
|
|
*/ |
114
|
|
|
public function getHtmlComponent() { |
115
|
|
|
return $this->content["form"]; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
* @see \Ajax\common\Widget::_setToolbarPosition() |
120
|
|
|
*/ |
121
|
|
|
protected function _setToolbarPosition($table, $captions=NULL) { |
122
|
|
|
$this->content[$this->_toolbarPosition]=$this->_toolbar; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setValidationParams(array $_validationParams) { |
126
|
|
|
$this->getForm()->setValidationParams($_validationParams); |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.