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\elements\HtmlButton; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* DataForm widget for editing model objects |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @author jc |
16
|
|
|
* @since 2.2 |
17
|
|
|
* @property FormInstanceViewer $_instanceViewer |
18
|
|
|
*/ |
19
|
|
|
class DataForm extends Widget { |
20
|
|
|
|
21
|
|
|
public function __construct($identifier, $modelInstance=NULL) { |
22
|
|
|
parent::__construct($identifier, null,$modelInstance); |
23
|
|
|
$this->_form=new HtmlForm($identifier); |
24
|
|
|
$this->_init(new FormInstanceViewer($identifier), "form", $this->_form, true); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function _getFieldIdentifier($prefix,$name=""){ |
28
|
|
|
return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function compile(JsUtils $js=NULL,&$view=NULL){ |
|
|
|
|
32
|
|
|
if(!$this->_generated){ |
33
|
|
|
$this->_instanceViewer->setInstance($this->_modelInstance); |
34
|
|
|
|
35
|
|
|
$form=$this->content["form"]; |
36
|
|
|
$this->_generateContent($form); |
37
|
|
|
|
38
|
|
|
if(isset($this->_toolbar)){ |
39
|
|
|
$this->_setToolbarPosition($form); |
40
|
|
|
} |
41
|
|
|
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]); |
42
|
|
|
$this->_generated=true; |
43
|
|
|
} |
44
|
|
|
return parent::compile($js,$view); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param HtmlForm $form |
49
|
|
|
*/ |
50
|
|
|
protected function _generateContent($form){ |
51
|
|
|
$values= $this->_instanceViewer->getValues(); |
52
|
|
|
$count=$this->_instanceViewer->count(); |
53
|
|
|
$separators=$this->_instanceViewer->getSeparators(); |
54
|
|
|
$headers=$this->_instanceViewer->getHeaders(); |
55
|
|
|
$wrappers=$this->_instanceViewer->getWrappers(); |
56
|
|
|
\sort($separators); |
57
|
|
|
$size=\sizeof($separators); |
58
|
|
|
if($size===1){ |
59
|
|
|
foreach ($values as $v){ |
60
|
|
|
$form->addField($v); |
61
|
|
|
} |
62
|
|
|
}else{ |
63
|
|
|
$separators[]=$count; |
64
|
|
|
for($i=0;$i<$size;$i++){ |
65
|
|
|
$this->_generateFields($form, $values, $headers, $separators[$i], $separators[$i+1], $wrappers); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function _generateFields($form,$values,$headers,$sepFirst,$sepLast,$wrappers){ |
71
|
|
|
$wrapper=null; |
72
|
|
|
$fields=\array_slice($values, $sepFirst+1,$sepLast-$sepFirst); |
73
|
|
|
if(isset($headers[$sepFirst+1])) |
74
|
|
|
$form->addHeader($headers[$sepFirst+1],4,true); |
75
|
|
|
if(isset($wrappers[$sepFirst+1])){ |
76
|
|
|
$wrapper=$wrappers[$sepFirst+1]; |
77
|
|
|
} |
78
|
|
|
//TODO check why $fields is empty |
79
|
|
|
if(\sizeof($fields)===1){ |
80
|
|
|
$added=$form->addField($fields[0]); |
81
|
|
|
}elseif(\sizeof($fields)>1){ |
82
|
|
|
$added=$form->addFields($fields); |
83
|
|
|
} |
84
|
|
|
if(isset($wrapper)) |
85
|
|
|
$added->wrap($wrapper[0],$wrapper[1]); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
* @see \Ajax\common\Widget::getForm() |
91
|
|
|
*/ |
92
|
|
|
public function getForm(){ |
93
|
|
|
return $this->content["form"]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function addSeparatorAfter($fieldNum){ |
97
|
|
|
$fieldNum=$this->_getIndex($fieldNum); |
98
|
|
|
$this->_instanceViewer->addSeparatorAfter($fieldNum); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getSeparators() { |
103
|
|
|
return $this->_instanceViewer->getSeparators(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function setSeparators($separators) { |
107
|
|
|
$this->_instanceViewer->setSeparators($separators); |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){ |
112
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){ |
|
|
|
|
113
|
|
|
$button=new HtmlButton($id,$value,$cssStyle); |
114
|
|
|
$button->setProperty("type", "reset"); |
115
|
|
|
return $button; |
116
|
|
|
}, $index,$attributes); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
* @see \Ajax\common\Widget::getHtmlComponent() |
122
|
|
|
* @return HtmlForm |
123
|
|
|
*/ |
124
|
|
|
public function getHtmlComponent() { |
125
|
|
|
return $this->content["form"]; |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
* @see \Ajax\common\Widget::_setToolbarPosition() |
130
|
|
|
*/ |
131
|
|
|
protected function _setToolbarPosition($table, $captions=NULL) { |
132
|
|
|
$this->content[$this->_toolbarPosition]=$this->_toolbar; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function addDividerBefore($index,$title){ |
136
|
|
|
$index=$this->_getIndex($index); |
137
|
|
|
$this->_instanceViewer->addHeaderDividerBefore($index, $title); |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function addWrapper($index,$contentBefore,$contentAfter=null){ |
142
|
|
|
$index=$this->_getIndex($index); |
143
|
|
|
$this->_instanceViewer->addWrapper($index, $contentBefore,$contentAfter); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function run(JsUtils $js){ |
148
|
|
|
return parent::run($js); |
149
|
|
|
} |
150
|
|
|
} |
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.