|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Base-class for all elements intended to get user input. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Formgenerator |
|
10
|
|
|
* @author Stefanius <[email protected]> |
|
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
12
|
|
|
*/ |
|
13
|
|
|
class FormInput extends FormElement |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string value input type */ |
|
16
|
|
|
protected string $strType = ''; |
|
17
|
|
|
/** @var int|string size as number or as string including dimension ('%', 'px', 'em') */ |
|
18
|
|
|
protected $size = 0; |
|
19
|
|
|
/** @var string image displayed, if selectbutton is enabled */ |
|
20
|
|
|
protected string $strSelectImg = ''; |
|
21
|
|
|
/** @var string tooltip for selectbutton */ |
|
22
|
|
|
protected string $strSelectImgTitle = ''; |
|
23
|
|
|
/** @var string folder to expand when call the filemanager */ |
|
24
|
|
|
protected string $strExpandFolder = ''; |
|
25
|
|
|
/** @var string suffix directly after the element */ |
|
26
|
|
|
protected string $strSuffix = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $strName Name (also used as ID, if not set separate) |
|
30
|
|
|
* @param int|string $size number set the size-attribute, a string is used for the width attribute |
|
31
|
|
|
* @param int $wFlags |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(string $strName, $size, int $wFlags = 0, int $iMaxLength = 0) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($wFlags); |
|
36
|
|
|
$this->strName = $strName; |
|
37
|
|
|
$this->size = $size; |
|
38
|
|
|
$this->strType = 'text'; |
|
39
|
|
|
$this->strSelectImg = ''; |
|
40
|
|
|
$this->strSelectImgTitle = ''; |
|
41
|
|
|
$this->strSuffix = ''; |
|
42
|
|
|
|
|
43
|
|
|
$this->addFlags($wFlags); |
|
44
|
|
|
if ($iMaxLength > 0) { |
|
45
|
|
|
$this->addAttribute('maxlength', (string)$iMaxLength); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set the maxlength attribute of the element. |
|
51
|
|
|
* @param int $iMaxLength |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setMaxLength(int $iMaxLength) : void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->addAttribute('maxlength', (string)$iMaxLength); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set placeholder to display on empty input element. |
|
60
|
|
|
* @param string $strPlaceholder |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setPlaceholder(string $strPlaceholder) : void |
|
63
|
|
|
{ |
|
64
|
|
|
if (strlen($strPlaceholder) > 0) { |
|
65
|
|
|
$this->addAttribute('placeholder', $strPlaceholder); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* set image and title for select-button (leave strImg blank for default 'search') |
|
71
|
|
|
* @param string $strImg |
|
72
|
|
|
* @param string $strTitle (default = '') |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setSelectImg(string $strImg, string $strTitle = '') : void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->strSelectImg = $strImg; |
|
77
|
|
|
$this->strSelectImgTitle = $strTitle; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set the folder to expand when call the filemanager. |
|
82
|
|
|
* @param string $strExpandFolder |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setExpandFolder(string $strExpandFolder) : void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->strExpandFolder = $strExpandFolder; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $strSuffix |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setSuffix(string $strSuffix) : void |
|
93
|
|
|
{ |
|
94
|
|
|
$this->strSuffix = $strSuffix; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritDoc} |
|
99
|
|
|
* @see \SKien\Formgenerator\FormElement::onParentSet() |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function onParentSet() : void |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->oFlags->isSet(FormFlags::BROWSE_SERVER)) { |
|
104
|
|
|
$this->oFG->addConfigForJS('RichFilemanager', $this->oFG->getConfig()->getArray('RichFilemanager')); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Build the HTML-notation for the input element. |
|
110
|
|
|
* {@inheritDoc} |
|
111
|
|
|
* @see \SKien\Formgenerator\FormElement::getHTML() |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getHTML() : string |
|
114
|
|
|
{ |
|
115
|
|
|
$this->processFlags(); |
|
116
|
|
|
$this->setSize(); |
|
117
|
|
|
$strHTML = $this->buildContainerDiv(); |
|
118
|
|
|
|
|
119
|
|
|
$this->strID = $this->strID ?: $this->strName; |
|
120
|
|
|
|
|
121
|
|
|
$strHTML .= '<input'; |
|
122
|
|
|
$strHTML .= ' type="' . $this->strType . '"'; |
|
123
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
|
124
|
|
|
$strHTML .= $this->buildClass(); |
|
125
|
|
|
$strHTML .= $this->buildID(); |
|
126
|
|
|
$strHTML .= $this->buildStyle(); |
|
127
|
|
|
$strHTML .= $this->buildTabindex(); |
|
128
|
|
|
$strHTML .= $this->buildValue(); |
|
129
|
|
|
$strHTML .= $this->buildAttributes(); |
|
130
|
|
|
$strHTML .= $this->buildListLink(); |
|
131
|
|
|
$strHTML .= '>'; |
|
132
|
|
|
|
|
133
|
|
|
$strHTML .= $this->buildSelectButton(); |
|
134
|
|
|
$strHTML .= $this->buildSuffix(); |
|
135
|
|
|
$strHTML .= $this->buildDatalist(); |
|
136
|
|
|
|
|
137
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
|
138
|
|
|
|
|
139
|
|
|
return $strHTML; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set the tab index of the element. |
|
144
|
|
|
* Method is called from the PageGenerator after an element is added to the form. |
|
145
|
|
|
* @param int $iTabindex |
|
146
|
|
|
* @return int the number of indexes, the element needs |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setTabindex(int $iTabindex) : int |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
|
151
|
|
|
return 0; |
|
152
|
|
|
} |
|
153
|
|
|
$this->iTabindex = $iTabindex; |
|
154
|
|
|
return 1; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Process the current flags before the HTML is generated. |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function processFlags() : void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->setTypeFromFlags(); |
|
163
|
|
|
|
|
164
|
|
|
if ($this->oFlags->isSet(FormFlags::MANDATORY)) { |
|
165
|
|
|
$this->addAttribute('required'); |
|
166
|
|
|
} |
|
167
|
|
|
if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
|
168
|
|
|
$this->addAttribute('readonly'); |
|
169
|
|
|
} else if ($this->oFlags->isSet(FormFlags::DISABLED)) { |
|
170
|
|
|
$this->addAttribute('disabled'); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Set the type depending on some flags |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function setTypeFromFlags() : void |
|
178
|
|
|
{ |
|
179
|
|
|
if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
|
180
|
|
|
$this->strType = 'hidden'; |
|
181
|
|
|
} |
|
182
|
|
|
if ($this->oFlags->isSet(FormFlags::PASSWORD)) { |
|
183
|
|
|
$this->strType = 'password'; |
|
184
|
|
|
} |
|
185
|
|
|
if ($this->oFlags->isSet(FormFlags::FILE)) { |
|
186
|
|
|
$this->strType = 'file'; |
|
187
|
|
|
if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
|
188
|
|
|
$this->addStyle('visibility', 'hidden'); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set the size of the element. |
|
195
|
|
|
* If property $size contains numeric value, the HTML attrib 'size' is set, in case of a |
|
196
|
|
|
* string a width information including dimension (px, em, ...) is assumed. |
|
197
|
|
|
* |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function setSize() : void |
|
200
|
|
|
{ |
|
201
|
|
|
if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
|
202
|
|
|
$this->size = ''; |
|
203
|
|
|
} |
|
204
|
|
|
if ((is_numeric($this->size)) && ($this->size > 0)) { |
|
205
|
|
|
$this->addAttribute('size', (string)$this->size); |
|
206
|
|
|
} else if (!empty($this->size)) { |
|
207
|
|
|
// size given as string including dimension |
|
208
|
|
|
$this->addStyle('width', $this->size); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* {@inheritDoc} |
|
214
|
|
|
* @see \SKien\Formgenerator\FormElement::buildClass() |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function buildClass() : string |
|
217
|
|
|
{ |
|
218
|
|
|
if (!empty($this->strClass)) { |
|
219
|
|
|
$this->strClass .= ' '; |
|
220
|
|
|
} |
|
221
|
|
|
$this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK'; |
|
222
|
|
|
if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
|
223
|
|
|
$this->strClass .= '_R'; |
|
224
|
|
|
} |
|
225
|
|
|
return parent::buildClass(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Build the markup for a suffix succeeding the input element. |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function buildSuffix() : string |
|
233
|
|
|
{ |
|
234
|
|
|
$strHTML = ''; |
|
235
|
|
|
if (!empty($this->strSuffix)) { |
|
236
|
|
|
if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
|
237
|
|
|
$strHTML .= ' <span class="readonly">' . $this->strSuffix . '</span>'; |
|
238
|
|
|
} else { |
|
239
|
|
|
$strHTML .= ' ' . $this->strSuffix; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
return $strHTML; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Build attrib for associated datalist. |
|
247
|
|
|
* If the dataprovider contains a datalist in the selectoptions with the same name |
|
248
|
|
|
* as the element, we add the attrib to conect to this list. |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function buildListLink() : string |
|
252
|
|
|
{ |
|
253
|
|
|
$strLink = ''; |
|
254
|
|
|
$aOptions = $this->oFG->getData()->getSelectOptions($this->strName); |
|
255
|
|
|
if (count($aOptions) > 0) { |
|
256
|
|
|
$strLink = ' list="list' . $this->strName . '"'; |
|
257
|
|
|
} |
|
258
|
|
|
return $strLink; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Build the markup for associated datalist. |
|
263
|
|
|
* If the dataprovider contains a datalist in the selectoptions with the same name |
|
264
|
|
|
* as the element, we build this datalist. |
|
265
|
|
|
* @return string |
|
266
|
|
|
*/ |
|
267
|
|
|
protected function buildDatalist() : string |
|
268
|
|
|
{ |
|
269
|
|
|
$strHTML = ''; |
|
270
|
|
|
$aOptions = $this->oFG->getData()->getSelectOptions($this->strName); |
|
271
|
|
|
if (count($aOptions) > 0) { |
|
272
|
|
|
$strHTML .= '<datalist id="list' . $this->strName . '">' . PHP_EOL; |
|
273
|
|
|
foreach ($aOptions as $strValue) { |
|
274
|
|
|
$strHTML .= ' '; |
|
275
|
|
|
$strHTML .= '<option '; |
|
276
|
|
|
$strHTML .= 'value="' . $strValue . '">' . PHP_EOL; |
|
277
|
|
|
} |
|
278
|
|
|
$strHTML .= '</datalist>' . PHP_EOL; |
|
279
|
|
|
} |
|
280
|
|
|
return $strHTML; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Build the markup for the select button(s). |
|
285
|
|
|
* If input is set to readonly, an additional 'delete' button is appended. |
|
286
|
|
|
* @param string $strCssClass |
|
287
|
|
|
* @return string |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function buildSelectButton(string $strCssClass = 'picker') : string |
|
290
|
|
|
{ |
|
291
|
|
|
$wButtonFlags = $this->oFlags->getButtonFlags(); |
|
292
|
|
|
if ($wButtonFlags === 0 || $this->oFlags->isSet(FormFlags::HIDDEN)) { |
|
293
|
|
|
return ''; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$strImg = ''; |
|
297
|
|
|
$strTitle = ''; |
|
298
|
|
|
$strOnClick = ''; |
|
299
|
|
|
$strID = ''; |
|
300
|
|
|
|
|
301
|
|
|
// only one of the button flags is allowed - so we can use switch-case! |
|
302
|
|
|
switch ($wButtonFlags) { |
|
303
|
|
|
case FormFlags::ADD_DTU: |
|
304
|
|
|
$strUsername = $this->oFG->getData()->getValue('username'); |
|
305
|
|
|
[$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_DTU); |
|
306
|
|
|
$strOnClick = "onInsertDateTimeUser('" . $this->strName . "', '" . $strUsername . "')"; |
|
307
|
|
|
$strID = $this->strName . 'DTU'; |
|
308
|
|
|
break; |
|
309
|
|
|
case FormFlags::ADD_DATE_PICKER: |
|
310
|
|
|
[$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_DATE_PICKER); |
|
311
|
|
|
break; |
|
312
|
|
|
case FormFlags::ADD_TIME_PICKER: |
|
313
|
|
|
[$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_TIME_PICKER); |
|
314
|
|
|
break; |
|
315
|
|
|
case FormFlags::BROWSE_SERVER: |
|
316
|
|
|
[$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_BROWSE); |
|
317
|
|
|
$strOnClick = "browseServer('" . $this->strName . "','','" . $this->strExpandFolder . "')"; |
|
318
|
|
|
$strID = $this->strName . 'BS'; |
|
319
|
|
|
break; |
|
320
|
|
|
case FormFlags::ADD_SELBTN: |
|
321
|
|
|
[$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_SEARCH); |
|
322
|
|
|
$strOnClick = "onSelect('" . $this->strName . "')"; |
|
323
|
|
|
$strID = $this->strName . 'SB'; |
|
324
|
|
|
$strImg = $this->strSelectImg ?: $strImg; |
|
325
|
|
|
$strTitle = $this->strSelectImgTitle ?: $strTitle; |
|
326
|
|
|
break; |
|
327
|
|
|
default: |
|
328
|
|
|
trigger_error('Only one of the button-flags can be set!', E_USER_ERROR); |
|
329
|
|
|
} |
|
330
|
|
|
$strHTML = $this->buildSelectImage($strImg, $strTitle, $strOnClick, $strID, $strCssClass); |
|
331
|
|
|
if (!empty($strHTML) && $this->oFlags->isSet(FormFlags::READ_ONLY)) { |
|
332
|
|
|
[$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_DELETE); |
|
333
|
|
|
$strOnClick = "resetElement('" . $this->strName . "')"; |
|
334
|
|
|
$strID = $this->strName . 'Del'; |
|
335
|
|
|
$strHTML .= $this->buildSelectImage($strImg, $strTitle, $strOnClick, $strID, $strCssClass); |
|
336
|
|
|
} |
|
337
|
|
|
return $strHTML; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Build the markup for a selectimage. |
|
342
|
|
|
* @param string $strImg |
|
343
|
|
|
* @param string $strTitle |
|
344
|
|
|
* @param string $strOnClick |
|
345
|
|
|
* @param string $strID |
|
346
|
|
|
* @param string $strCssClass |
|
347
|
|
|
* @return string |
|
348
|
|
|
*/ |
|
349
|
|
|
protected function buildSelectImage(string $strImg, string $strTitle, string $strOnClick, string $strID, string $strCssClass) : string |
|
350
|
|
|
{ |
|
351
|
|
|
$strHTML = ''; |
|
352
|
|
|
if (!empty($strImg)) { |
|
353
|
|
|
$strHTML = '<img class="' . $strCssClass . '" src="' . $strImg . '" alt="[?]"'; |
|
354
|
|
|
if (!empty($strID)) { |
|
355
|
|
|
$strHTML .= ' id="' . $strID . '"'; |
|
356
|
|
|
} |
|
357
|
|
|
if (!empty($strTitle)) { |
|
358
|
|
|
$strHTML .= ' title="' . $strTitle . '"'; |
|
359
|
|
|
} |
|
360
|
|
|
if (!empty($strOnClick)) { |
|
361
|
|
|
$strHTML .= ' onclick="' . $strOnClick . '"'; |
|
362
|
|
|
} |
|
363
|
|
|
$strHTML .= '>'; |
|
364
|
|
|
} |
|
365
|
|
|
return $strHTML; |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
|