Passed
Push — main ( 0c6882...1f9a40 )
by Stefan
03:05
created

FormInput::onParentSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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