1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\widgets\base; |
3
|
|
|
use Ajax\service\JString; |
4
|
|
|
use Ajax\semantic\html\elements\HtmlImage; |
5
|
|
|
use Ajax\semantic\html\base\constants\Size; |
6
|
|
|
use Ajax\semantic\html\elements\HtmlLabel; |
7
|
|
|
use Ajax\semantic\html\modules\HtmlProgress; |
8
|
|
|
use Ajax\semantic\html\modules\HtmlRating; |
9
|
|
|
use Ajax\semantic\html\elements\HtmlHeader; |
10
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormCheckbox; |
11
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormInput; |
12
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormDropdown; |
13
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormTextarea; |
14
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormFields; |
15
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
16
|
|
|
use Ajax\semantic\html\elements\HtmlButton; |
17
|
|
|
use Ajax\service\JArray; |
18
|
|
|
use Ajax\semantic\html\elements\html5\HtmlLink; |
19
|
|
|
use Ajax\semantic\html\elements\HtmlFlag; |
20
|
|
|
use Ajax\common\html\BaseHtml; |
21
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormField; |
22
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormRadio; |
23
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* trait used in Widget |
27
|
|
|
* @author jc |
28
|
|
|
* @property InstanceViewer $_instanceViewer |
29
|
|
|
* @property boolean $_edition |
30
|
|
|
* @property mixed _modelInstance |
31
|
|
|
*/ |
32
|
|
|
trait FieldAsTrait{ |
33
|
|
|
|
34
|
|
|
abstract protected function _getFieldIdentifier($prefix,$name=""); |
35
|
|
|
abstract public function setValueFunction($index,$callback); |
36
|
|
|
abstract protected function _getFieldName($index); |
37
|
|
|
abstract protected function _getFieldCaption($index); |
38
|
|
|
abstract protected function _buttonAsSubmit(BaseHtml &$button,$event,$url,$responseElement=NULL,$parameters=NULL); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param HtmlFormField $element |
42
|
|
|
* @param array $attributes |
43
|
|
|
*/ |
44
|
|
|
protected function _applyAttributes(BaseHtml $element,&$attributes,$index){ |
45
|
|
|
if(isset($attributes["jsCallback"])){ |
46
|
|
|
$callback=$attributes["jsCallback"]; |
47
|
|
|
if(\is_callable($callback)){ |
48
|
|
|
$callback($element,$this->_modelInstance,$index); |
49
|
|
|
unset($attributes["jsCallback"]); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
unset($attributes["rules"]); |
53
|
|
|
unset($attributes["ajax"]); |
54
|
|
|
unset($attributes["visibleHover"]); |
55
|
|
|
$element->fromArray($attributes); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function _getLabelField($caption,$icon=NULL){ |
59
|
|
|
$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon); |
60
|
|
|
return $label; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
protected function _addRules(HtmlFormField $element,&$attributes){ |
65
|
|
|
if(isset($attributes["rules"])){ |
66
|
|
|
$rules=$attributes["rules"]; |
67
|
|
|
if(\is_array($rules)){ |
68
|
|
|
$element->addRules($rules); |
69
|
|
|
} |
70
|
|
|
else{ |
71
|
|
|
$element->addRule($rules); |
72
|
|
|
} |
73
|
|
|
unset($attributes["rules"]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function _prepareFormFields(HtmlFormField &$field,$name,&$attributes){ |
78
|
|
|
$field->setName($name); |
79
|
|
|
$this->_addRules($field, $attributes); |
80
|
|
|
return $field; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null){ |
84
|
|
|
$this->setValueFunction($index,function($value,$instance,$index) use (&$attributes,$elementCallback,$prefix){ |
85
|
|
|
$caption=$this->_getFieldCaption($index); |
86
|
|
|
$name=$this->_getFieldName($index); |
87
|
|
|
$id=$this->_getFieldIdentifier($prefix,$name); |
88
|
|
|
if(isset($attributes["name"])){ |
89
|
|
|
$name=$attributes["name"]; |
90
|
|
|
unset($attributes["name"]); |
91
|
|
|
} |
92
|
|
|
$element=$elementCallback($id,$name,$value,$caption); |
93
|
|
|
if(\is_array($attributes)){ |
94
|
|
|
$this->_applyAttributes($element, $attributes,$index); |
95
|
|
|
} |
96
|
|
|
$element->setDisabled(!$this->_edition); |
97
|
|
|
return $element; |
98
|
|
|
}); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
View Code Duplication |
public function fieldAsProgress($index,$label=NULL, $attributes=array()){ |
|
|
|
|
104
|
|
|
$this->setValueFunction($index,function($value) use($label,$attributes){ |
105
|
|
|
$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes); |
106
|
|
|
return $pb; |
107
|
|
|
}); |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function fieldAsRating($index,$max=5, $icon=""){ |
|
|
|
|
112
|
|
|
$this->setValueFunction($index,function($value) use($max,$icon){ |
113
|
|
|
$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon); |
114
|
|
|
return $rating; |
115
|
|
|
}); |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function fieldAsLabel($index,$icon=NULL,$attributes=NULL){ |
120
|
|
|
return $this->_fieldAs(function($id,$name,$value) use($icon){ |
121
|
|
|
$lbl=new HtmlLabel($id,$value); |
122
|
|
|
if(isset($icon)) |
123
|
|
|
$lbl->addIcon($icon); |
124
|
|
|
return $lbl; |
125
|
|
|
}, $index,$attributes,"label"); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){ |
129
|
|
|
return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){ |
130
|
|
|
$header=new HtmlHeader($id,$niveau,$value); |
131
|
|
|
if(isset($icon)) |
132
|
|
|
$header->asIcon($icon, $value); |
133
|
|
|
return $header; |
134
|
|
|
}, $index,$attributes,"header"); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
public function fieldAsImage($index,$size=Size::MINI,$circular=false){ |
139
|
|
|
$this->setValueFunction($index,function($img) use($size,$circular){ |
140
|
|
|
$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular(); |
141
|
|
|
return $image; |
142
|
|
|
}); |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function fieldAsFlag($index){ |
147
|
|
|
$this->setValueFunction($index,function($flag){ |
148
|
|
|
$flag=new HtmlFlag($this->_getFieldIdentifier("flag"),$flag); |
149
|
|
|
return $flag; |
150
|
|
|
}); |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function fieldAsAvatar($index,$attributes=NULL){ |
155
|
|
|
return $this->_fieldAs(function($id,$name,$value){ |
156
|
|
|
$img=new HtmlImage($id,$value); |
157
|
|
|
$img->asAvatar(); |
158
|
|
|
return $img; |
159
|
|
|
}, $index,$attributes,"avatar"); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function fieldAsRadio($index,$attributes=NULL){ |
163
|
|
|
return $this->_fieldAs(function($id,$name,$value) use($attributes){ |
164
|
|
|
$input= new HtmlFormRadio($id,$name,$value,$value); |
165
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
166
|
|
|
}, $index,$attributes,"radio"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function fieldAsRadios($index,$elements=[],$attributes=NULL){ |
170
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){ |
171
|
|
|
return HtmlFormFields::radios($name,$elements,$caption,$value); |
172
|
|
|
}, $index,$attributes,"radios"); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function fieldAsInput($index,$attributes=NULL){ |
176
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){ |
177
|
|
|
$input= new HtmlFormInput($id,$caption,"text",$value); |
178
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
179
|
|
|
}, $index,$attributes,"input"); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function fieldAsTextarea($index,$attributes=NULL){ |
183
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){ |
184
|
|
|
$textarea=new HtmlFormTextarea($id,$caption,$value); |
185
|
|
|
return $this->_prepareFormFields($textarea, $name, $attributes); |
186
|
|
|
}, $index,$attributes,"textarea"); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function fieldAsElement($index,$tagName="div",$baseClass="",$attributes=NULL){ |
190
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes,$tagName,$baseClass){ |
191
|
|
|
$div=new HtmlSemDoubleElement($id,$tagName,$baseClass); |
192
|
|
|
$div->setContent(\htmlentities($value)); |
193
|
|
|
$textarea=new HtmlFormField("field-".$id, $div,$caption); |
194
|
|
|
return $this->_prepareFormFields($textarea, $name, $attributes); |
195
|
|
|
}, $index,$attributes,"element"); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
public function fieldAsHidden($index,$attributes=NULL){ |
200
|
|
|
if(!\is_array($attributes)){ |
201
|
|
|
$attributes=[]; |
202
|
|
|
} |
203
|
|
|
$attributes["inputType"]="hidden"; |
204
|
|
|
return $this->fieldAsInput($index,$attributes); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function fieldAsCheckbox($index,$attributes=NULL){ |
208
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use($attributes){ |
209
|
|
|
if($caption===null || $caption==="") |
210
|
|
|
$caption=""; |
211
|
|
|
$input=new HtmlFormCheckbox($id,$caption,$this->_instanceViewer->getIdentifier()); |
212
|
|
|
$input->setChecked(JString::isBooleanTrue($value)); |
213
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
214
|
|
|
}, $index,$attributes,"ck"); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){ |
218
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use($elements,$multiple,$attributes){ |
219
|
|
|
$dd=new HtmlFormDropdown($id,$elements,$caption,$value); |
220
|
|
|
$dd->asSelect($name,$multiple); |
221
|
|
|
return $this->_prepareFormFields($dd, $name, $attributes); |
222
|
|
|
}, $index,$attributes,"dd"); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function fieldAsMessage($index,$attributes=NULL){ |
226
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
227
|
|
|
$mess= new HtmlMessage("message-".$id,$caption); |
228
|
|
|
$mess->addHeader($value); |
229
|
|
|
return $mess; |
230
|
|
|
}, $index,$attributes,"message"); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function fieldAsLink($index,$attributes=NULL){ |
234
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption){ |
235
|
|
|
$lnk= new HtmlLink("message-".$id,"#",$caption); |
236
|
|
|
return $lnk; |
237
|
|
|
}, $index,$attributes,"link"); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/**Change fields type |
241
|
|
|
* @param array $types an array or associative array $type=>$attributes |
242
|
|
|
*/ |
243
|
|
|
public function fieldsAs(array $types){ |
244
|
|
|
$i=0; |
245
|
|
|
if(JArray::isAssociative($types)){ |
246
|
|
|
foreach ($types as $type=>$attributes){ |
247
|
|
|
if(\is_int($type)) |
248
|
|
|
$this->fieldAs($i++,$attributes,[]); |
249
|
|
|
else{ |
250
|
|
|
$type=preg_replace('/\d/', '', $type ); |
251
|
|
|
$this->fieldAs($i++,$type,$attributes); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
}else{ |
255
|
|
|
foreach ($types as $type){ |
256
|
|
|
$this->fieldAs($i++,$type); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function fieldAs($index,$type,$attributes=NULL){ |
262
|
|
|
$method="fieldAs".\ucfirst($type); |
263
|
|
|
if(\method_exists($this, $method)){ |
264
|
|
|
if(!\is_array($attributes)){ |
265
|
|
|
$attributes=[$index]; |
266
|
|
|
}else{ |
267
|
|
|
\array_unshift($attributes, $index); |
268
|
|
|
} |
269
|
|
|
\call_user_func_array([$this,$method], $attributes); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
274
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle,$attributes){ |
275
|
|
|
$button=new HtmlButton($id,$caption,$cssStyle); |
276
|
|
|
$this->_buttonAsSubmit($button,"click",$url,$responseElement,@$attributes["ajax"]); |
277
|
|
|
return $button; |
278
|
|
|
}, $index,$attributes,"submit"); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function fieldAsButton($index,$cssStyle=NULL,$attributes=NULL){ |
282
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){ |
|
|
|
|
283
|
|
|
$button=new HtmlButton($id,$value,$cssStyle); |
284
|
|
|
return $button; |
285
|
|
|
}, $index,$attributes,"button"); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
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.