Passed
Push — main ( dedf73...0dcdcf )
by Stefan
01:14
created

FormInput::getHTML()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 45
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 35
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 45
rs 8.7377
1
<?php
2
namespace SKien\Formgenerator;
3
4
/**
5
 * Base-class for all elements intended to get user input.
6
 *
7
 * #### History
8
 * - *2020-05-12*   initial version
9
 * - *2021-01-07*   PHP 7.4
10
 *
11
 * @package Formgenerator
12
 * @version 1.1.0
13
 * @author Stefanius <[email protected]>
14
 * @copyright MIT License - see the LICENSE file for details
15
 */
16
class FormInput extends FormElement
17
{
18
    /** @var string value of the element     */
19
    protected string $strValue;
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 additional info for BrowseServer - Button     */
29
    protected string $strBrowseServer;
30
    /** @var string suffix directly after the element     */
31
    protected string $strSuffix;
32
    
33
    /**
34
     * @param string $strName   Name and ID
35
     * @param string $strValue  
36
     * @param int|string $size number set the size-attribute, a string is used for the width attribute
37
     * @param int $wFlags       
38
     */
39
    public function __construct(string $strName, string $strValue, $size, int $wFlags = 0) 
40
    {
41
        parent::__construct();
42
        $this->strName = $strName;
43
        $this->strValue = $strValue;
44
        if (($wFlags & self::TRIM) != 0) {
45
            $this->strValue = trim($strValue);
46
        }
47
        $this->size = $size;
48
        $this->iTab = -1;
49
        $this->strValidate = 'aEdit';
50
        $this->wFlags = $wFlags;
51
        if (strlen($this->strType) == 0) {
52
            $this->strType = 'text';
53
        }
54
        if (($wFlags & self::HIDDEN) != 0) {
55
            $this->strType = 'hidden';
56
        }
57
        $this->addFlags($wFlags);
58
        $this->strSelectImg = '';
59
        $this->strSelectImgTitle = '';
60
        $this->strBrowseServer = '';
61
        $this->strSuffix = '';
62
        if (($wFlags & self::ADD_EUR) != 0) {
63
            $this->strSuffix = 'EUR';
64
        }
65
    }
66
    
67
    /**
68
     * Aadd flags too existing element. 
69
     * Maybe overloaded in derived class(es) 
70
     * @param int $wFlags
71
     * 
72
     * {@inheritDoc}
73
     * @see \SKien\Formgenerator\FormElement::addFlags()
74
     */
75
    public function addFlags($wFlags) : void
76
    {
77
        $this->wFlags |= $wFlags;
78
79
        $this->strClass = 'inputOK';
80
        if (($this->wFlags & self::MANDATORY) != 0) {
81
            $this->strClass = 'inputMand';
82
            $this->addAttribute('required');
83
        }
84
        if (($this->wFlags & self::READ_ONLY) != 0) {
85
            $this->addAttribute('readonly');
86
        } else if (($this->wFlags & self::DISABLED) != 0) {
87
            $this->addAttribute('disabled');
88
        }
89
        
90
        if (($this->wFlags & self::ALIGN_RIGHT) != 0) {
91
            $this->strClass .= '_R';
92
        }
93
        if (($this->wFlags & self::ADD_COLOR_PICKER) != 0) {
94
            $this->strClass .= ' jscolor {hash:true}';
95
        }
96
        if (($this->wFlags & self::PASSWORD) != 0) {
97
            $this->strType = 'password';
98
        }
99
        if (($this->wFlags & self::FILE) != 0) {
100
            $this->strType = 'file';
101
            if (($this->wFlags & self::HIDDEN) != 0) {
102
                $this->addStyle('visibility', 'hidden');
103
            }
104
        }
105
    }
106
    
107
    /**
108
     * Input elements don't need tab position if hidden, read-only or disabled
109
     * {@inheritDoc}
110
     * @see \SKien\Formgenerator\FormElement::hasTab()
111
     */
112
    public function hasTab() : bool
113
    {
114
        return (($this->wFlags & (self::HIDDEN | self::READ_ONLY | self::DISABLED)) == 0);
115
    }
116
    
117
    /**
118
     * Set the size of the element.
119
     * TODO: explain int|string usage
120
     */
121
    protected function setSize() : void
122
    {
123
        if (is_numeric($this->size)) {
124
            if ($this->size > 0) {
125
                if ($this->strType == 'number') {
126
                    // HTML5 number input doesn't support size attribute :-(
127
                    // TODO: move to FormInt where strType == 'number'
128
                    $this->addStyle('width', $this->size . 'em');
129
                } else {
130
                    $this->addAttribute('size', (string)$this->size);
131
                }
132
            }
133
        } else if (!empty($this->size)) {
134
            // size given as string including dimension
135
            $this->addStyle('width', $this->size);
136
        }
137
        
138
    }
139
    
140
    /**
141
     * Build the markup for the value attribute.
142
     * @param mixed $value
143
     * @return string
144
     */
145
    protected function buildValue($value) : string 
146
    {
147
        $strHTML = '';
148
        if (($this->wFlags & self::NO_ZERO) == 0 || ($value != 0 && $value != '0')) { 
149
            $strHTML = ' value="' . str_replace('"', '&quot;', $value) . '"';
150
        }
151
        return $strHTML;
152
    }
153
    
154
    /**
155
     * Build the markup for a select button.
156
     * @param string $strClass
157
     * @return string
158
     */
159
    protected function buildSelectImage(string $strClass = 'picker') : string
160
    {
161
        $strHTML = '';
162
        if (($this->wFlags & self::ADD_SELBTN) != 0) {
163
            $strImg = $this->strSelectImg;
164
            $strTitle = $this->strSelectImgTitle;
165
            if (empty($strImg) && $this->oFG !== null) {
166
                $strImg = $this->oFG->getImagePath() . '16x16/search.png';
167
            }
168
            $strHTML .= '<img class="' . $strClass . '" src="' . $strImg . '" alt="Auswahl"';
169
            if (!empty($strTitle)) {
170
                $strHTML .= ' title="' . $strTitle . '"';
171
            }
172
            if (!empty($this->strBrowseServer)) {
173
                $strHTML .= " onclick=\"BrowseServer('" . $this->strName . "','','" . $this->strBrowseServer . "');\">";
174
                if (($this->wFlags & self::READ_ONLY) != 0) {
175
                    $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/admin_delete.png" alt="L&ouml;schen" title="L&ouml;schen" ';
0 ignored issues
show
Bug introduced by
The method getImagePath() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
                    $strHTML .= '<img class="picker" src="' . $this->oFG->/** @scrutinizer ignore-call */ getImagePath() . '16x16/admin_delete.png" alt="L&ouml;schen" title="L&ouml;schen" ';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
                    $strHTML .= " onclick=\"ResetInput('" . $this->strName . "');\">";
177
                }
178
            } else {
179
                $strHTML .= ' onclick="OnSelect(' . "'" . $this->strName . "'" . ');">';
180
            }
181
        }
182
        return $strHTML;
183
    }
184
    
185
    /**
186
     * Build the HTML-notation for the input element.
187
     * {@inheritDoc}
188
     * @see \SKien\Formgenerator\FormElement::getHTML()
189
     */
190
    public function getHTML() : string 
191
    {
192
        $this->setSize();
193
        $strHTML  = $this->buildContainerDiv();
194
        
195
        $strHTML .= '<input';
196
        $strHTML .= ' type="' . $this->strType . '"';
197
        $strHTML .= ' class="' . $this->strClass . '"';
198
        $strHTML .= ' name="' . $this->strName . '"';
199
        $strHTML .= ' id="' . $this->strName . '"';
200
        $strHTML .= $this->buildStyle();
201
        $strHTML .= $this->buildAttributes();
202
        $strHTML .= $this->buildTab($this->iTab);
203
        $strHTML .= $this->buildValue($this->strValue);
204
        $strHTML .= '>';
205
        
206
        // some additional elements
207
        if (($this->wFlags & self::ADD_DTU) != 0) {
208
            $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/admin_dtu.png"  alt="[X]"';
209
            $strHTML .= ' id="' . $this->strName . 'DTU"';
210
            $strHTML .= ' title="aktuelle Datum Uhrzeit / Benutzername eintragen"';
211
            $strHTML .= ' onclick="OnInsertDateTimeUser(' . "'" . $this->strName . "'" . ');">';
212
        } else if (($this->wFlags & self::ADD_DATE_PICKER) != 0) {
213
            $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/datepicker.png" alt="[X]"';
214
            $strHTML .= ' id="' . $this->strName . 'DP"';
215
            $strHTML .= ' title="Datum auswählen"';
216
            $strHTML .= ' onclick="OnDatePicker(' . "'" . $this->strName . "'" . ');">';
217
        } else if (($this->wFlags & self::ADD_TIME_PICKER) != 0) {
218
            $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/timepicker.png"  alt="[X]"';
219
            $strHTML .= ' id="' . $this->strName . 'TP"';
220
            $strHTML .= ' title="Uhrzeit auswählen"';
221
            $strHTML .= ' onclick="OnTimePicker(' . "'" . $this->strName . "'" . ');">';
222
        }
223
        $strHTML .= $this->buildSelectImage();
224
        if (!empty($this->strSuffix)) {
225
            if (($this->wFlags & self::READ_ONLY) != 0) {
226
                $strHTML .= '&nbsp;<span class="readonly">' . $this->strSuffix . '</span>';
227
            } else {
228
                $strHTML .= '&nbsp;' . $this->strSuffix;
229
            }
230
        }
231
        
232
        $strHTML .= '</div>' . PHP_EOL;
233
        
234
        return $strHTML;
235
    }
236
    
237
    /**
238
     * set image and title for select-button (leave strImg blank for default)
239
     * default image is 16x16/search.png
240
     * @param string $strImg
241
     * @param string $strTitle (default = '')
242
     */
243
    public function setSelectImg(string $strImg, string $strTitle='') : void 
244
    {
245
        $this->strSelectImg = $strImg;
246
        $this->strSelectImgTitle = $strTitle;
247
    }
248
    
249
    /**
250
     * @param string $strBrowseServer
251
     */
252
    public function setBrowseServer(string $strBrowseServer) : void 
253
    {
254
        $this->strBrowseServer = $strBrowseServer;
255
    }
256
257
    /**
258
     * @param string $strSuffix
259
     */
260
    public function setSuffix(string $strSuffix) : void 
261
    {
262
        $this->strSuffix = $strSuffix;
263
    }
264
}
265