Passed
Push — main ( fac3c3...502827 )
by Stefan
02:24
created

FormInput::buildSelectButton()   C

Complexity

Conditions 12
Paths 15

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 54
rs 6.9666
c 0
b 0
f 0
cc 12
nc 15
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
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
     * Build the HTML-notation for the input element.
92
     * {@inheritDoc}
93
     * @see \SKien\Formgenerator\FormElement::getHTML()
94
     */
95
    public function getHTML() : string
96
    {
97
        $this->processFlags();
98
        $this->setSize();
99
        $strHTML = $this->buildContainerDiv();
100
        
101
        $this->strID = $this->strID ?: $this->strName;
102
        
103
        $strHTML .= '<input';
104
        $strHTML .= ' type="' . $this->strType . '"';
105
        $strHTML .= ' name="' . $this->strName . '"';
106
        $strHTML .= $this->buildClass();
107
        $strHTML .= $this->buildID();
108
        $strHTML .= $this->buildStyle();
109
        $strHTML .= $this->buildTabindex();
110
        $strHTML .= $this->buildValue();
111
        $strHTML .= $this->buildAttributes();
112
        $strHTML .= '>';
113
        
114
        $strHTML .= $this->buildSelectButton();
115
        $strHTML .= $this->buildSuffix();
116
        
117
        $strHTML .= '</div>' . PHP_EOL;
118
        
119
        return $strHTML;
120
    }
121
    
122
    /**
123
     * Set the tab index of the element.
124
     * Method is called from the PageGenerator after an element is added to the form.
125
     * @param int $iTabindex
126
     * @return int the number of indexes, the element needs
127
     */
128
    public function setTabindex(int $iTabindex) : int
129
    {
130
        if ($this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)) {
131
            return 0;
132
        }
133
        $this->iTabindex = $iTabindex;
134
        return 1;
135
    }
136
    
137
    /**
138
     * Process the current flags before the HTML is generated.
139
     */
140
    protected function processFlags() : void
141
    {
142
        $this->setTypeFromFlags();
143
        
144
        if ($this->oFlags->isSet(FormFlags::MANDATORY)) {
145
            $this->addAttribute('required');
146
        }
147
        if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
148
            $this->addAttribute('readonly');
149
        } else if ($this->oFlags->isSet(FormFlags::DISABLED)) {
150
            $this->addAttribute('disabled');
151
        }
152
    }
153
154
    /**
155
     * Set the type depending on some flags
156
     */
157
    protected function setTypeFromFlags() : void
158
    {
159
        if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
160
            $this->strType = 'hidden';
161
        }
162
        if ($this->oFlags->isSet(FormFlags::PASSWORD)) {
163
            $this->strType = 'password';
164
        }
165
        if ($this->oFlags->isSet(FormFlags::FILE)) {
166
            $this->strType = 'file';
167
            if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
168
                $this->addStyle('visibility', 'hidden');
169
            }
170
        }
171
    }
172
    
173
    /**
174
     * Set the size of the element.
175
     * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a
176
     * string a width information including dimension (px, em, ...) is assumed.
177
     * 
178
     */
179
    protected function setSize() : void
180
    {
181
        if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
182
            $this->size = '';
183
        }
184
        if ((is_numeric($this->size)) && ($this->size > 0)) {
185
            $this->addAttribute('size', (string)$this->size);
186
        } else if (!empty($this->size)) {
187
            // size given as string including dimension
188
            $this->addStyle('width', $this->size);
189
        }
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     * @see \SKien\Formgenerator\FormElement::buildClass()
195
     */
196
    protected function buildClass() : string
197
    {
198
        if (!empty($this->strClass)) {
199
            $this->strClass .= ' ';
200
        }
201
        $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK';
202
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
203
            $this->strClass .= '_R';
204
        }
205
        return parent::buildClass();
206
    }
207
    
208
    /**
209
     * Build the markup for a suffix succeeding the input element.
210
     * @return string
211
     */
212
    protected function buildSuffix() : string
213
    {
214
        $strHTML = '';
215
        if (!empty($this->strSuffix)) {
216
            if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
217
                $strHTML .= '&nbsp;<span class="readonly">' . $this->strSuffix . '</span>';
218
            } else {
219
                $strHTML .= '&nbsp;' . $this->strSuffix;
220
            }
221
        }
222
        return $strHTML;
223
    }
224
    
225
    /**
226
     * Build the markup for the select button(s).
227
     * If input  is set to readonly, an additional 'delete' button is appended.
228
     * @param string $strCssClass
229
     * @return string
230
     */
231
    protected function buildSelectButton(string $strCssClass='picker') : string
232
    {
233
        $wButtonFlags = $this->oFlags->getButtonFlags();
234
        if ($wButtonFlags === 0) {
235
            return '';
236
        }
237
        
238
        $strImg = '';
239
        $strTitle = '';
240
        $strOnClick = '';
241
        
242
        // only one of the button flags is allowed - so we can use switch-case!
243
        switch ($wButtonFlags) {
244
            case FormFlags::ADD_DTU:
245
                $strUsername = $this->oFG->getData()->getValue('username');
246
                [$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_DTU);
247
                $strOnClick = "onInsertDateTimeUser('" . $this->strName . "', '" . $strUsername . "')";
248
                // $strID = $this->strName . 'DTU';
249
                break;
250
            case FormFlags::ADD_DATE_PICKER:
251
                [$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_DATE_PICKER);
252
                $strOnClick = "onDatePicker('" . $this->strName . "')";
253
                // $strID = $this->strName . 'DP';
254
                break;
255
            case FormFlags::ADD_TIME_PICKER:
256
                [$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_TIME_PICKER);
257
                $strOnClick = "onTimePicker('" . $this->strName . "')";
258
                // $strID = $this->strName . 'TP';
259
                break;
260
            case FormFlags::BROWSE_SERVER:
261
                if ($this->isFilemanagerConnected()) {
262
                    [$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_BROWSE);
263
                    $strOnClick = "browseServer('" . $this->strName . "','','" . $this->strExpandFolder . "')";
264
                    // $strID = $this->strName . 'BS';
265
                }
266
                break;
267
            case FormFlags::ADD_SELBTN:
268
                [$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_SEARCH);
269
                $strOnClick = "onSelect('" . $this->strName . "')";
270
                // $strID = $this->strName . 'SB';
271
                $strImg = $this->strSelectImg ?: $strImg;
272
                $strTitle = $this->strSelectImgTitle ?: $strTitle;
273
                break;
274
            default:
275
                trigger_error('Only one of the button-flags can be set!', E_USER_ERROR);
276
        }
277
        $strHTML = $this->buildSelectImage($strImg, $strTitle, $strOnClick, $strCssClass);
278
        if (!empty($strHTML) && $this->oFlags->isSet(FormFlags::READ_ONLY)) {
279
            [$strImg, $strTitle] = $this->oFG->getStdImage(FormImage::IMG_DELETE);
280
            $strOnClick = "resetInput('" . $this->strName . "')";
281
            // $strID = $this->strName . 'Del';
282
            $strHTML .= $this->buildSelectImage($strImg, $strTitle, $strOnClick, $strCssClass);
283
        }
284
        return $strHTML;
285
    }
286
    
287
    /**
288
     * Build the markup for a selectimage.
289
     * @param string $strImg
290
     * @param string $strTitle
291
     * @param string $strOnClick
292
     * @param string $strCssClass
293
     * @return string
294
     */
295
    protected function buildSelectImage(string $strImg, string $strTitle, string $strOnClick, string $strCssClass) : string
296
    {
297
        $strHTML = '';
298
        if (!empty($strImg)) {
299
            $strHTML = '<img class="' . $strCssClass . '" src="' . $strImg . '" alt="[?]"';
300
            if (!empty($strTitle)) {
301
                $strHTML .= ' title="' . $strTitle . '"';
302
            }
303
            if (!empty($strOnClick)) {
304
                $strHTML .= ' onclick="' . $strOnClick . '"';
305
            }
306
            $strHTML .= '>';
307
        }
308
        return $strHTML;
309
    }
310
    
311
    /**
312
     * Check, if filemanager is connected.
313
     * Triggers warning, if no filmanager is connected.
314
     * @return bool
315
     */
316
    protected function isFilemanagerConnected() : bool
317
    {
318
        $bIsConnected = $this->oFG->getConfig()->getBool('Filemanager.Connect');
319
        if (!$bIsConnected) {
320
            trigger_error('There is no filemanager connected', E_USER_WARNING );
321
        }
322
        return $bIsConnected;
323
    }
324
}
325