Passed
Push — main ( 8fe4a9...23f062 )
by Stefan
03:16
created

FormInput::buildSuffix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
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 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 (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();
41
        $this->strName = $strName;
42
        $this->size = $size;
43
        $this->iTab = -1;
44
        $this->strValidate = 'aEdit';
45
        $this->wFlags = $wFlags;
46
        if (strlen($this->strType) == 0) {
47
            $this->strType = 'text';
48
        }
49
        if (($wFlags & self::HIDDEN) != 0) {
50
            $this->strType = 'hidden';
51
        }
52
        $this->addFlags($wFlags);
53
        $this->strSelectImg = '';
54
        $this->strSelectImgTitle = '';
55
        $this->strBrowseServer = '';
56
        $this->strSuffix = '';
57
        if (($wFlags & self::ADD_EUR) != 0) {
58
            $this->strSuffix = 'EUR';
59
        }
60
    }
61
    
62
    /**
63
     * Add flags to existing element. 
64
     * Maybe overloaded in derived class(es) 
65
     * @param int $wFlags
66
     * 
67
     * {@inheritDoc}
68
     * @see \SKien\Formgenerator\FormElement::addFlags()
69
     */
70
    public function addFlags($wFlags) : void
71
    {
72
        $this->wFlags |= $wFlags;
73
74
        $this->strClass = 'inputOK';
75
        if (($this->wFlags & self::MANDATORY) != 0) {
76
            $this->strClass = 'inputMand';
77
            $this->addAttribute('required');
78
        }
79
        if (($this->wFlags & self::READ_ONLY) != 0) {
80
            $this->addAttribute('readonly');
81
        } else if (($this->wFlags & self::DISABLED) != 0) {
82
            $this->addAttribute('disabled');
83
        }
84
        
85
        if (($this->wFlags & self::ALIGN_RIGHT) != 0) {
86
            $this->strClass .= '_R';
87
        }
88
        if (($this->wFlags & self::ADD_COLOR_PICKER) != 0) {
89
            $this->strClass .= ' jscolor {hash:true}';
90
        }
91
        if (($this->wFlags & self::PASSWORD) != 0) {
92
            $this->strType = 'password';
93
        }
94
        if (($this->wFlags & self::FILE) != 0) {
95
            $this->strType = 'file';
96
            if (($this->wFlags & self::HIDDEN) != 0) {
97
                $this->addStyle('visibility', 'hidden');
98
            }
99
        }
100
    }
101
    
102
    /**
103
     * set image and title for select-button (leave strImg blank for default)
104
     * @param string $strImg
105
     * @param string $strTitle (default = '')
106
     */
107
    public function setSelectImg(string $strImg, string $strTitle = '') : void
108
    {
109
        $this->strSelectImg = $strImg;
110
        $this->strSelectImgTitle = $strTitle;
111
    }
112
    
113
    /**
114
     * @param string $strBrowseServer
115
     */
116
    public function setBrowseServer(string $strBrowseServer) : void
117
    {
118
        $this->strBrowseServer = $strBrowseServer;
119
    }
120
    
121
    /**
122
     * @param string $strSuffix
123
     */
124
    public function setSuffix(string $strSuffix) : void
125
    {
126
        $this->strSuffix = $strSuffix;
127
    }
128
    
129
    /**
130
     * Build the HTML-notation for the input element.
131
     * {@inheritDoc}
132
     * @see \SKien\Formgenerator\FormElement::getHTML()
133
     */
134
    public function getHTML() : string
135
    {
136
        $this->setSize();
137
        $strHTML  = $this->buildContainerDiv();
138
        
139
        $this->strID = $this->strID ?: $this->strName;
140
        
141
        $strHTML .= '<input';
142
        $strHTML .= ' type="' . $this->strType . '"';
143
        $strHTML .= ' name="' . $this->strName . '"';
144
        $strHTML .= $this->buildClass();
145
        $strHTML .= $this->buildID();
146
        $strHTML .= $this->buildStyle();
147
        $strHTML .= $this->buildAttributes();
148
        $strHTML .= $this->buildTab();
149
        $strHTML .= $this->buildValue();
150
        $strHTML .= '>';
151
        
152
        // some additional elements
153
        if (($this->wFlags & self::ADD_DTU) != 0) {
154
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DTU) . '"  alt="[X]"';
155
            $strHTML .= ' id="' . $this->strName . 'DTU"';
156
            $strHTML .= ' title="aktuelle Datum Uhrzeit / Benutzername eintragen"';
157
            $strHTML .= ' onclick="OnInsertDateTimeUser(' . "'" . $this->strName . "'" . ');">';
158
        } else if (($this->wFlags & self::ADD_DATE_PICKER) != 0) {
159
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DATE_PICKER) . '" alt="[X]"';
160
            $strHTML .= ' id="' . $this->strName . 'DP"';
161
            $strHTML .= ' title="Datum auswählen"';
162
            $strHTML .= ' onclick="OnDatePicker(' . "'" . $this->strName . "'" . ');">';
163
        } else if (($this->wFlags & self::ADD_TIME_PICKER) != 0) {
164
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_TIME_PICKER) . '"  alt="[X]"';
165
            $strHTML .= ' id="' . $this->strName . 'TP"';
166
            $strHTML .= ' title="Uhrzeit auswählen"';
167
            $strHTML .= ' onclick="OnTimePicker(' . "'" . $this->strName . "'" . ');">';
168
        }
169
        $strHTML .= $this->buildSelectImage();
170
        $strHTML .= $this->buildSuffix();
171
        
172
        $strHTML .= '</div>' . PHP_EOL;
173
        
174
        return $strHTML;
175
    }
176
    
177
    /**
178
     * Input elements don't need tab index if hidden, read-only or disabled
179
     * {@inheritDoc}
180
     * @see \SKien\Formgenerator\FormElement::hasTab()
181
     */
182
    public function hasTab() : bool
183
    {
184
        return (($this->wFlags & (self::HIDDEN | self::READ_ONLY | self::DISABLED)) == 0);
185
    }
186
    
187
    /**
188
     * Set the size of the element.
189
     * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a
190
     * string a width information including dimension (px, em, ...) is assumed.
191
     * 
192
     */
193
    protected function setSize() : void
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
     * Build the markup for a suffix succeeding the input element.
205
     * @return string
206
     */
207
    protected function buildSuffix() : string
208
    {
209
        $strHTML = '';
210
        if (!empty($this->strSuffix)) {
211
            if (($this->wFlags & self::READ_ONLY) != 0) {
212
                $strHTML .= '&nbsp;<span class="readonly">' . $this->strSuffix . '</span>';
213
            } else {
214
                $strHTML .= '&nbsp;' . $this->strSuffix;
215
            }
216
        }
217
        return $strHTML;
218
    }
219
    
220
    /**
221
     * Build the markup for a select button.
222
     * @param string $strClass
223
     * @return string
224
     */
225
    protected function buildSelectImage(string $strClass = 'picker') : string
226
    {
227
        $strHTML = '';
228
        if (($this->wFlags & self::ADD_SELBTN) != 0) {
229
            $strImg = $this->strSelectImg;
230
            $strTitle = $this->strSelectImgTitle;
231
            if (empty($strImg)) {
232
                $strImg = $this->getStdImage(self::IMG_SEARCH);
233
            }
234
            $strHTML .= '<img class="' . $strClass . '" src="' . $strImg . '" alt="Auswahl"';
235
            if (!empty($strTitle)) {
236
                $strHTML .= ' title="' . $strTitle . '"';
237
            }
238
            if (!empty($this->strBrowseServer)) {
239
                $strHTML .= " onclick=\"BrowseServer('" . $this->strName . "','','" . $this->strBrowseServer . "');\">";
240
                if (($this->wFlags & self::READ_ONLY) != 0) {
241
                    $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DELETE) . '" alt="L&ouml;schen" title="L&ouml;schen" ';
242
                    $strHTML .= " onclick=\"ResetInput('" . $this->strName . "');\">";
243
                }
244
            } else {
245
                $strHTML .= ' onclick="OnSelect(' . "'" . $this->strName . "'" . ');">';
246
            }
247
        }
248
        return $strHTML;
249
    }
250
}
251