1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\widgets\base; |
3
|
|
|
|
4
|
|
|
use Ajax\service\JString; |
5
|
|
|
use Ajax\semantic\html\elements\HtmlImage; |
6
|
|
|
use Ajax\semantic\html\base\constants\Size; |
7
|
|
|
use Ajax\semantic\html\elements\HtmlLabel; |
8
|
|
|
use Ajax\semantic\html\modules\HtmlProgress; |
9
|
|
|
use Ajax\semantic\html\modules\HtmlRating; |
10
|
|
|
use Ajax\semantic\html\elements\HtmlHeader; |
11
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormCheckbox; |
12
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormInput; |
13
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormDropdown; |
14
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormTextarea; |
15
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormFields; |
16
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
17
|
|
|
use Ajax\semantic\html\elements\HtmlButton; |
18
|
|
|
use Ajax\service\JArray; |
19
|
|
|
use Ajax\semantic\html\elements\html5\HtmlLink; |
20
|
|
|
use Ajax\semantic\html\elements\HtmlFlag; |
21
|
|
|
use Ajax\common\html\BaseHtml; |
22
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormField; |
23
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormRadio; |
24
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
25
|
|
|
use Ajax\semantic\html\elements\HtmlIcon; |
26
|
|
|
use Ajax\semantic\html\elements\HtmlList; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* trait used in Widget |
30
|
|
|
* |
31
|
|
|
* @author jc |
32
|
|
|
* @property InstanceViewer $_instanceViewer |
33
|
|
|
* @property boolean $_edition |
34
|
|
|
* @property mixed $_modelInstance |
35
|
|
|
* @property boolean $_hasRules |
36
|
|
|
*/ |
37
|
|
|
trait FieldAsTrait { |
38
|
|
|
|
39
|
|
|
abstract protected function _getFieldIdentifier($prefix, $name = ""); |
40
|
|
|
|
41
|
|
|
abstract public function setValueFunction($index, $callback); |
42
|
|
|
|
43
|
|
|
abstract protected function _getFieldName($index); |
44
|
|
|
|
45
|
|
|
abstract protected function _getFieldCaption($index); |
46
|
|
|
|
47
|
|
|
abstract protected function _buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement = NULL, $parameters = NULL); |
48
|
|
|
|
49
|
|
|
private $_speProperties; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @param HtmlFormField $element |
54
|
|
|
* @param array $attributes |
55
|
|
|
*/ |
56
|
|
|
protected function _applyAttributes(BaseHtml $element, &$attributes, $index, $instance = null) { |
57
|
|
|
if (isset($attributes["jsCallback"])) { |
58
|
|
|
$callback = $attributes["jsCallback"]; |
59
|
|
|
if (\is_callable($callback)) { |
60
|
|
|
$callback($element, $instance, $index, InstanceViewer::$index); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
unset($attributes["rules"]); |
64
|
|
|
unset($attributes["ajax"]); |
65
|
|
|
unset($attributes["visibleHover"]); |
66
|
|
|
$element->fromArray($attributes); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function _getLabelField($caption, $icon = NULL) { |
70
|
|
|
$label = new HtmlLabel($this->_getFieldIdentifier("lbl"), $caption, $icon); |
71
|
|
|
return $label; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function _addRules(HtmlFormField $element, &$attributes) { |
75
|
|
|
if (isset($attributes["rules"])) { |
76
|
|
|
$this->_hasRules = true; |
77
|
|
|
$rules = $attributes["rules"]; |
78
|
|
|
if (\is_array($rules)) { |
79
|
|
|
$element->addRules($rules); |
80
|
|
|
} else { |
81
|
|
|
$element->addRule($rules); |
82
|
|
|
} |
83
|
|
|
unset($attributes["rules"]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function _prepareFormFields(HtmlFormField &$field, $name, &$attributes) { |
88
|
|
|
$field->setName($name); |
89
|
|
|
$this->_addRules($field, $attributes); |
90
|
|
|
return $field; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function _fieldAs($elementCallback, &$index, $attributes = NULL, $prefix = null) { |
94
|
|
|
$this->setValueFunction($index, function ($value, $instance, $index, $rowIndex) use (&$attributes, $elementCallback, $prefix) { |
95
|
|
|
$caption = $this->_getFieldCaption($index); |
96
|
|
|
$name = $this->_getFieldName($index); |
97
|
|
|
$id = $this->_getFieldIdentifier($prefix, $name); |
98
|
|
|
if (isset($attributes["name"])) { |
99
|
|
|
$name = $attributes["name"]; |
100
|
|
|
unset($attributes["name"]); |
101
|
|
|
} |
102
|
|
|
$element = $elementCallback($id, $name, $value, $caption); |
103
|
|
|
if (isset($this->_speProperties[$index])) { |
104
|
|
|
$attributes ??= []; |
105
|
|
|
$attributes += $this->_speProperties[$index]; |
106
|
|
|
} |
107
|
|
|
if (\is_array($attributes)) { |
108
|
|
|
$this->_applyAttributes($element, $attributes, $index, $instance); |
109
|
|
|
} |
110
|
|
|
$element->setDisabled(! $this->_edition); |
111
|
|
|
return $element; |
112
|
|
|
}); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Defines the values for the fields for a property (or html attribute). |
118
|
|
|
* |
119
|
|
|
* @param int|string $property |
120
|
|
|
* the property to update |
121
|
|
|
* @param array $indexValues |
122
|
|
|
* array of field=>value |
123
|
|
|
*/ |
124
|
|
|
public function setPropertyValues($property, $indexValues) { |
125
|
|
|
foreach ($indexValues as $index => $value) { |
126
|
|
|
$ind = $this->_getIndex($index); |
|
|
|
|
127
|
|
|
$this->_speProperties[$ind][$property] = $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function fieldAsProgress($index, $label = NULL, $attributes = array()) { |
132
|
|
|
$this->setValueFunction($index, function ($value) use ($label, $attributes) { |
133
|
|
|
$pb = new HtmlProgress($this->_getFieldIdentifier("pb"), $value, $label, $attributes); |
134
|
|
|
return $pb; |
135
|
|
|
}); |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function fieldAsRating($index, $max = 5, $icon = "") { |
140
|
|
|
$this->setValueFunction($index, function ($value) use ($max, $icon) { |
141
|
|
|
$rating = new HtmlRating($this->_getFieldIdentifier("rat"), $value, $max, $icon); |
142
|
|
|
return $rating; |
143
|
|
|
}); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function fieldAsLabel($index, $icon = NULL, $attributes = NULL) { |
148
|
|
|
return $this->_fieldAs(function ($id, $name, $value) use ($icon) { |
149
|
|
|
$lbl = new HtmlLabel($id, $value); |
150
|
|
|
if (isset($icon)) |
151
|
|
|
$lbl->addIcon($icon); |
152
|
|
|
return $lbl; |
153
|
|
|
}, $index, $attributes, "label"); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function fieldAsHeader($index, $niveau = 1, $icon = NULL, $attributes = NULL) { |
157
|
|
|
return $this->_fieldAs(function ($id, $name, $value) use ($niveau, $icon) { |
158
|
|
|
$header = new HtmlHeader($id, $niveau, $value); |
159
|
|
|
if (isset($icon)) |
160
|
|
|
$header->asIcon($icon, $value); |
161
|
|
|
return $header; |
162
|
|
|
}, $index, $attributes, "header"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function fieldAsImage($index, $size = Size::MINI, $circular = false) { |
166
|
|
|
$this->setValueFunction($index, function ($img) use ($size, $circular) { |
167
|
|
|
$image = new HtmlImage($this->_getFieldIdentifier("image"), $img); |
168
|
|
|
$image->setSize($size); |
169
|
|
|
if ($circular) |
170
|
|
|
$image->setCircular(); |
171
|
|
|
return $image; |
172
|
|
|
}); |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function fieldAsFlag($index) { |
177
|
|
|
$this->setValueFunction($index, function ($flag) { |
178
|
|
|
$flag = new HtmlFlag($this->_getFieldIdentifier("flag"), $flag); |
179
|
|
|
return $flag; |
180
|
|
|
}); |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function fieldAsIcon($index) { |
185
|
|
|
$this->setValueFunction($index, function ($icon) { |
186
|
|
|
$icon = new HtmlIcon($this->_getFieldIdentifier("icon"), $icon); |
187
|
|
|
return $icon; |
188
|
|
|
}); |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function fieldAsAvatar($index, $attributes = NULL) { |
193
|
|
|
return $this->_fieldAs(function ($id, $name, $value) { |
194
|
|
|
$img = new HtmlImage($id, $value); |
195
|
|
|
$img->asAvatar(); |
196
|
|
|
return $img; |
197
|
|
|
}, $index, $attributes, "avatar"); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function fieldAsRadio($index, $attributes = NULL) { |
201
|
|
|
return $this->_fieldAs(function ($id, $name, $value) use ($attributes) { |
202
|
|
|
$input = new HtmlFormRadio($id, $name, $value, $value); |
203
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
204
|
|
|
}, $index, $attributes, "radio"); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function fieldAsRadios($index, $elements = [], $attributes = NULL) { |
208
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements) { |
209
|
|
|
return HtmlFormFields::radios($name, $elements, $caption, $value); |
210
|
|
|
}, $index, $attributes, "radios"); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function fieldAsList($index, $classNames = "", $attributes = NULL) { |
214
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($classNames) { |
215
|
|
|
$result = new HtmlList($name, $value); |
216
|
|
|
$result->addClass($classNames); |
217
|
|
|
return $result; |
218
|
|
|
}, $index, $attributes, "list"); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function fieldAsInput($index, $attributes = NULL) { |
222
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
223
|
|
|
$input = new HtmlFormInput($id, $caption, "text", $value); |
224
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
225
|
|
|
}, $index, $attributes, "input"); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function fieldAsLabeledInput($index, $attributes = NULL) { |
229
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
230
|
|
|
$input = new HtmlFormInput($id, '', 'text', $value, $caption); |
231
|
|
|
$required = ''; |
232
|
|
|
if (isset($attributes['rules'])) { |
233
|
|
|
$rules = json_encode($attributes['rules']); |
234
|
|
|
if (strpos($rules, 'empty') !== false) { |
235
|
|
|
$required = 'required'; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
$input->getField() |
239
|
|
|
->labeled($caption) |
240
|
|
|
->setTagName('label') |
241
|
|
|
->addClass($required); |
242
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
243
|
|
|
}, $index, $attributes, 'input'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function fieldAsDataList($index, ?array $items = [], $attributes = NULL) { |
247
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $items) { |
248
|
|
|
$input = new HtmlFormInput($id, $caption, "text", $value); |
249
|
|
|
$input->getField() |
250
|
|
|
->addDataList($items); |
251
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
252
|
|
|
}, $index, $attributes, "input"); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function fieldAsFile($index, $attributes = NULL) { |
256
|
|
|
if (isset($this->_form)) { |
257
|
|
|
$this->_form->setProperty('enctype', 'multipart/form-data'); |
258
|
|
|
} |
259
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
260
|
|
|
$input = new HtmlFormInput($id, $caption); |
261
|
|
|
$input->asFile(); |
262
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
263
|
|
|
}, $index, $attributes, "input"); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function fieldAsTextarea($index, $attributes = NULL) { |
267
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
268
|
|
|
$textarea = new HtmlFormTextarea($id, $caption, $value); |
269
|
|
|
return $this->_prepareFormFields($textarea, $name, $attributes); |
270
|
|
|
}, $index, $attributes, "textarea"); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function fieldAsElement($index, $tagName = "div", $baseClass = "", $attributes = NULL) { |
274
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) { |
275
|
|
|
$div = new HtmlSemDoubleElement($id, $tagName, $baseClass); |
276
|
|
|
$div->setContent(\htmlentities($value)); |
277
|
|
|
$textarea = new HtmlFormField("field-" . $id, $div, $caption); |
278
|
|
|
return $this->_prepareFormFields($textarea, $name, $attributes); |
279
|
|
|
}, $index, $attributes, "element"); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function fieldAsHidden($index, $attributes = NULL) { |
283
|
|
|
if (! \is_array($attributes)) { |
284
|
|
|
$attributes = []; |
285
|
|
|
} |
286
|
|
|
$attributes["inputType"] = "hidden"; |
287
|
|
|
$attributes["style"] = "display:none;"; |
288
|
|
|
return $this->fieldAsInput($index, $attributes); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function fieldAsCheckbox($index, $attributes = NULL) { |
292
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
293
|
|
|
if ($caption === null || $caption === "") |
294
|
|
|
$caption = ""; |
295
|
|
|
$input = new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier()); |
296
|
|
|
$input->setChecked(JString::isBooleanTrue($value)); |
297
|
|
|
return $this->_prepareFormFields($input, $name, $attributes); |
298
|
|
|
}, $index, $attributes, "ck"); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function fieldAsDropDown($index, $elements = [], $multiple = false, $attributes = NULL) { |
302
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements, $multiple, $attributes) { |
303
|
|
|
$dd = new HtmlFormDropdown($id, $elements, $caption, $value ?? ''); |
304
|
|
|
$dd->asSelect($name, $multiple); |
305
|
|
|
return $this->_prepareFormFields($dd, $name, $attributes); |
306
|
|
|
}, $index, $attributes, "dd"); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function fieldAsMessage($index, $attributes = NULL) { |
310
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) { |
311
|
|
|
$mess = new HtmlMessage("message-" . $id, $caption); |
312
|
|
|
$mess->addHeader($value); |
313
|
|
|
return $mess; |
314
|
|
|
}, $index, $attributes, "message"); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function fieldAsLink($index, $attributes = NULL) { |
318
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) { |
319
|
|
|
$lnk = new HtmlLink("message-" . $id, "#", $caption); |
320
|
|
|
return $lnk; |
321
|
|
|
}, $index, $attributes, "link"); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Change fields type |
326
|
|
|
* |
327
|
|
|
* @param array $types |
328
|
|
|
* an array or associative array $type=>$attributes |
329
|
|
|
*/ |
330
|
|
|
public function fieldsAs(array $types) { |
331
|
|
|
$i = 0; |
332
|
|
|
if (JArray::isAssociative($types)) { |
333
|
|
|
foreach ($types as $type => $attributes) { |
334
|
|
|
if (\is_int($type)) |
335
|
|
|
$this->fieldAs($i ++, $attributes, []); |
336
|
|
|
else { |
337
|
|
|
$type = preg_replace('/\d/', '', $type); |
338
|
|
|
$this->fieldAs($i ++, $type, $attributes); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
} else { |
342
|
|
|
foreach ($types as $type) { |
343
|
|
|
$this->fieldAs($i ++, $type); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function fieldAs($index, $type, $attributes = NULL) { |
349
|
|
|
$method = "fieldAs" . \ucfirst($type); |
350
|
|
|
if (\method_exists($this, $method)) { |
351
|
|
|
if (! \is_array($attributes)) { |
352
|
|
|
$attributes = [ |
353
|
|
|
$index |
354
|
|
|
]; |
355
|
|
|
} else { |
356
|
|
|
\array_unshift($attributes, $index); |
357
|
|
|
} |
358
|
|
|
\call_user_func_array([ |
359
|
|
|
$this, |
360
|
|
|
$method |
361
|
|
|
], $attributes); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function fieldAsSubmit($index, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $attributes = NULL) { |
366
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) { |
367
|
|
|
$button = new HtmlButton($id, $caption, $cssStyle); |
368
|
|
|
$this->_buttonAsSubmit($button, "click", $url, $responseElement, @$attributes["ajax"]); |
369
|
|
|
return $button; |
370
|
|
|
}, $index, $attributes, "submit"); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) { |
374
|
|
|
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($cssStyle) { |
375
|
|
|
$button = new HtmlButton($id, $value, $cssStyle); |
376
|
|
|
return $button; |
377
|
|
|
}, $index, $attributes, "button"); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|